From: Wolfgang Bangerth Date: Tue, 3 Apr 2018 17:15:08 +0000 (-0600) Subject: Add a test. X-Git-Tag: v9.0.0-rc1~224^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=53942f5c4e07daa6d47a8dcc274a61e8a36a36af;p=dealii.git Add a test. --- diff --git a/tests/base/dynamic_unique_cast.cc b/tests/base/dynamic_unique_cast.cc new file mode 100644 index 0000000000..0707a3b983 --- /dev/null +++ b/tests/base/dynamic_unique_cast.cc @@ -0,0 +1,82 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2018 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// test dynamic_unique_cast + +#include "../tests.h" + +#include + + +class B +{ +public: + virtual ~B() {} +}; + +class D : public B {}; + + +void test () +{ + // Create a pointer to D + std::unique_ptr d = std_cxx14::make_unique(); + + // See that we can successfully downcast to B + std::unique_ptr b = dynamic_unique_cast(std::move(d)); + + // Ownership now rests with b, but it's still a D. Verify this: + Assert (d.get() == nullptr, ExcInternalError()); + Assert (b.get() != nullptr, ExcInternalError()); + Assert (dynamic_cast(b.get()) != nullptr, ExcInternalError()); + + // Check that we can again upcast to D: + std::unique_ptr dd = dynamic_unique_cast(std::move(b)); + + // Ownership now rests with b, but it's still a D. Verify this: + Assert (b.get() == nullptr, ExcInternalError()); + Assert (dd.get() != nullptr, ExcInternalError()); + + deallog << "OK" << std::endl; +} + + +void invalid_test () +{ + // Create a pointer to B + std::unique_ptr b = std_cxx14::make_unique(); + + // Check that we can indeed not upcast to D: + try + { + std::unique_ptr dd = dynamic_unique_cast(std::move(b)); + } + catch (const std::bad_cast &) + { + deallog << "OK" << std::endl; + } +} + + + + +int main() +{ + initlog(); + + test (); + invalid_test (); +} diff --git a/tests/base/dynamic_unique_cast.output b/tests/base/dynamic_unique_cast.output new file mode 100644 index 0000000000..8b3b075900 --- /dev/null +++ b/tests/base/dynamic_unique_cast.output @@ -0,0 +1,3 @@ + +DEAL::OK +DEAL::OK