]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add a test for the fast-path pack()/unpack() functions. 5810/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Fri, 26 Jan 2018 17:40:08 +0000 (10:40 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Fri, 26 Jan 2018 17:40:08 +0000 (10:40 -0700)
tests/base/utilities_pack_unpack_03.cc [new file with mode: 0644]
tests/base/utilities_pack_unpack_03.output [new file with mode: 0644]

diff --git a/tests/base/utilities_pack_unpack_03.cc b/tests/base/utilities_pack_unpack_03.cc
new file mode 100644 (file)
index 0000000..be7162c
--- /dev/null
@@ -0,0 +1,77 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 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.
+//
+// ---------------------------------------------------------------------
+
+
+// test Utilities::pack/unpack on some types. this test checks that
+// for trivially-copyable (small) types, packing is just a memcpy
+// operation
+
+#include "../tests.h"
+
+#include <deal.II/base/utilities.h>
+#include <deal.II/base/point.h>
+#include <boost/serialization/utility.hpp>
+
+#include <tuple>
+#include <cstring>
+
+struct X
+{
+  int i;
+  int k;
+  double d;
+
+  bool operator == (const X &x) const
+  {
+    return i==x.i && k==x.k && d==x.d;
+  }
+
+  template <class Archive>
+  void serialize(Archive &ar, const unsigned int)
+  {
+    ar   &i &k &d;
+  }
+};
+
+
+
+template <typename T>
+void check (const T &object)
+{
+  const std::vector<char> buffer = Utilities::pack (object);
+  if (!(buffer.size() == sizeof(object))
+      ||
+      !(std::memcmp(buffer.data(), &object, buffer.size()) == 0)
+      ||
+      !(Utilities::unpack<T>(buffer) == object))
+    deallog << "Fail!" << std::endl;
+}
+
+
+void test()
+{
+  check (std::make_pair(1, 3.14));
+  check (X { 1, 2, 3.1415926 });
+  check (std::tuple<int,double,char> {1,1,1});
+
+  deallog << "OK!" << std::endl;
+}
+
+int main()
+{
+  initlog();
+
+  test();
+}
diff --git a/tests/base/utilities_pack_unpack_03.output b/tests/base/utilities_pack_unpack_03.output
new file mode 100644 (file)
index 0000000..5cfb783
--- /dev/null
@@ -0,0 +1,2 @@
+
+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.