From f8ca6434261f08de83ad8c60c8fa0eba7f3bfda7 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 26 Jan 2018 10:40:08 -0700 Subject: [PATCH] Add a test for the fast-path pack()/unpack() functions. --- tests/base/utilities_pack_unpack_03.cc | 77 ++++++++++++++++++++++ tests/base/utilities_pack_unpack_03.output | 2 + 2 files changed, 79 insertions(+) create mode 100644 tests/base/utilities_pack_unpack_03.cc create mode 100644 tests/base/utilities_pack_unpack_03.output diff --git a/tests/base/utilities_pack_unpack_03.cc b/tests/base/utilities_pack_unpack_03.cc new file mode 100644 index 0000000000..be7162ce2b --- /dev/null +++ b/tests/base/utilities_pack_unpack_03.cc @@ -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 +#include +#include + +#include +#include + +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 + void serialize(Archive &ar, const unsigned int) + { + ar &i &k &d; + } +}; + + + +template +void check (const T &object) +{ + const std::vector buffer = Utilities::pack (object); + if (!(buffer.size() == sizeof(object)) + || + !(std::memcmp(buffer.data(), &object, buffer.size()) == 0) + || + !(Utilities::unpack(buffer) == object)) + deallog << "Fail!" << std::endl; +} + + +void test() +{ + check (std::make_pair(1, 3.14)); + check (X { 1, 2, 3.1415926 }); + check (std::tuple {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 index 0000000000..5cfb783b8f --- /dev/null +++ b/tests/base/utilities_pack_unpack_03.output @@ -0,0 +1,2 @@ + +DEAL::OK! -- 2.39.5