]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add tests.
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 29 Mar 2018 01:30:45 +0000 (19:30 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Thu, 29 Mar 2018 01:30:45 +0000 (19:30 -0600)
tests/base/thread_validity_13.cc [new file with mode: 0644]
tests/base/thread_validity_13.output [new file with mode: 0644]
tests/base/thread_validity_14.cc [new file with mode: 0644]
tests/base/thread_validity_14.output [new file with mode: 0644]

diff --git a/tests/base/thread_validity_13.cc b/tests/base/thread_validity_13.cc
new file mode 100644 (file)
index 0000000..6bf7c98
--- /dev/null
@@ -0,0 +1,95 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2008 - 2017 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.
+//
+// ---------------------------------------------------------------------
+
+
+// See that we can query a thread object's return value for cases
+// where these returned objects are not copyable but only
+// movable. This is relevant, for example, when calling functions that
+// return a std::unique_ptr.
+
+#include "../tests.h"
+#include <unistd.h>
+
+#include <deal.II/base/thread_management.h>
+
+
+class X
+{
+public:
+  // default constructor
+  X () :
+    value (13)
+  {}
+
+  X (int i) :
+    value (i)
+  {}
+
+  // delete the copy constructor
+  X (const X &) = delete;
+
+  // move constructor. sets the moved-from value to a recognizable
+  // value
+  X (X &&x)
+  {
+    value = x.value;
+    x.value = 0;
+  }
+
+  // same idea about copy operators
+  X &operator = (const X &) = delete;
+
+  X &operator = (X &&x)
+  {
+    value = x.value;
+    x.value = 0;
+
+    return *this;
+  }
+
+
+  int value;
+};
+
+
+
+X foo ()
+{
+  return X(42);
+}
+
+
+
+int main()
+{
+  initlog();
+
+  Threads::Thread<X> t = Threads::new_thread (&foo);
+
+  // wait for the thread to return and query its value
+  deallog << t.return_value().value
+          << std::endl;
+
+  // we can't copy the return_value() object directly, but we can move
+  // it. do so and check that we still get the correct value. then
+  // also check that the value of the original return object has been
+  // reset in the move constructor/operator
+  X x = std::move(t.return_value());
+  deallog << x.value
+          << std::endl;
+
+  deallog << t.return_value().value
+          << std::endl;
+}
diff --git a/tests/base/thread_validity_13.output b/tests/base/thread_validity_13.output
new file mode 100644 (file)
index 0000000..63953b2
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::42
+DEAL::42
+DEAL::0
diff --git a/tests/base/thread_validity_14.cc b/tests/base/thread_validity_14.cc
new file mode 100644 (file)
index 0000000..015eff2
--- /dev/null
@@ -0,0 +1,95 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2008 - 2017 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.
+//
+// ---------------------------------------------------------------------
+
+
+// See that we can query a task object's return value for cases
+// where these returned objects are not copyable but only
+// movable. This is relevant, for example, when calling functions that
+// return a std::unique_ptr.
+
+#include "../tests.h"
+#include <unistd.h>
+
+#include <deal.II/base/thread_management.h>
+
+
+class X
+{
+public:
+  X (int i) :
+    value (i)
+  {}
+
+  // default constructor
+  X () :
+    value (13)
+  {}
+
+  // delete the copy constructor
+  X (const X &) = delete;
+
+  // move constructor. sets the moved-from value to a recognizable
+  // value
+  X (X &&x)
+  {
+    value = x.value;
+    x.value = 0;
+  }
+
+  // same idea about copy operators
+  X &operator = (const X &) = delete;
+
+  X &operator = (X &&x)
+  {
+    value = x.value;
+    x.value = 0;
+
+    return *this;
+  }
+
+
+  int value;
+};
+
+
+
+X foo ()
+{
+  return X(42);
+}
+
+
+
+int main()
+{
+  initlog();
+
+  Threads::Task<X> t = Threads::new_task (&foo);
+
+  // wait for the thread to return and query its value
+  deallog << t.return_value().value
+          << std::endl;
+
+  // we can't copy the return_value() object directly, but we can move
+  // it. do so and check that we still get the correct value. then
+  // also check that the value of the original return object has been
+  // reset in the move constructor/operator
+  X x = std::move(t.return_value());
+  deallog << x.value
+          << std::endl;
+
+  deallog << t.return_value().value
+          << std::endl;
+}
diff --git a/tests/base/thread_validity_14.output b/tests/base/thread_validity_14.output
new file mode 100644 (file)
index 0000000..63953b2
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::42
+DEAL::42
+DEAL::0

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.