]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add a test.
authorWolfgang Bangerth <bangerth@colostate.edu>
Tue, 3 Apr 2018 17:15:08 +0000 (11:15 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Wed, 4 Apr 2018 14:28:04 +0000 (08:28 -0600)
tests/base/dynamic_unique_cast.cc [new file with mode: 0644]
tests/base/dynamic_unique_cast.output [new file with mode: 0644]

diff --git a/tests/base/dynamic_unique_cast.cc b/tests/base/dynamic_unique_cast.cc
new file mode 100644 (file)
index 0000000..0707a3b
--- /dev/null
@@ -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 <deal.II/base/std_cxx11/unique_ptr.h>
+
+
+class B
+{
+public:
+  virtual ~B() {}
+};
+
+class D : public B {};
+
+
+void test ()
+{
+  // Create a pointer to D
+  std::unique_ptr<D> d = std_cxx14::make_unique<D>();
+
+  // See that we can successfully downcast to B
+  std::unique_ptr<B> b = dynamic_unique_cast<B>(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<D *>(b.get()) != nullptr, ExcInternalError());
+
+  // Check that we can again upcast to D:
+  std::unique_ptr<D> dd = dynamic_unique_cast<D>(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> b = std_cxx14::make_unique<B>();
+
+  // Check that we can indeed not upcast to D:
+  try
+    {
+      std::unique_ptr<D> dd = dynamic_unique_cast<D>(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 (file)
index 0000000..8b3b075
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL::OK
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.