From 2444f570294708018276e232c95d5caf09705452 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 29 Nov 2021 15:26:45 -0700 Subject: [PATCH] Add a test. --- tests/base/aligned_vector_memory_02.cc | 109 +++++++++++++++++++++ tests/base/aligned_vector_memory_02.output | 13 +++ 2 files changed, 122 insertions(+) create mode 100644 tests/base/aligned_vector_memory_02.cc create mode 100644 tests/base/aligned_vector_memory_02.output diff --git a/tests/base/aligned_vector_memory_02.cc b/tests/base/aligned_vector_memory_02.cc new file mode 100644 index 0000000000..7a478cd9b3 --- /dev/null +++ b/tests/base/aligned_vector_memory_02.cc @@ -0,0 +1,109 @@ +// -------------------------------------------------------------------------------------------- +// +// Copyright (C) 2021 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.md at +// the top level directory of deal.II. +// +// -------------------------------------------------------------------------------------------- + + +// Check for a bug in memory management in class Table (which uses +// AlignedVector) that had crept in at some point. + +#include +#include + +#include "../tests.h" + + +int object_number = 0; +int objects_destroyed = 0; + +class C +{ +public: + C() + { + object_number = ::object_number++; + deallog << "default constructor. Object number " << object_number + << std::endl; + } + + C(const C &c) + { + object_number = ::object_number++; + deallog << "copy constructor from " << c.object_number << ". Object number " + << object_number << std::endl; + } + + C(const C &&c) + { + object_number = ::object_number++; + deallog << "move constructor from " << c.object_number << ". Object number " + << object_number << std::endl; + } + + C & + operator=(const C &c) + { + deallog << "copy operator called for " << object_number << " <- " + << c.object_number << std::endl; + return *this; + } + + C & + operator=(const C &&c) + { + deallog << "move operator called for " << object_number << " <- std::move(" + << c.object_number << ')' << std::endl; + return *this; + } + + + ~C() + { + deallog << "destructor. Object number " << object_number << std::endl; + ++objects_destroyed; + } + +private: + unsigned int object_number; +}; + + + +void +test() +{ + deallog << "---- Creating outer table" << std::endl; + Table<1, C> table({1}); + + // Copy the object, then destroy the copy again. + { + deallog << "---- Cloning outer table" << std::endl; + Table<1, C> x(table); + deallog << "---- Destroying the clone" << std::endl; + } + + deallog << "---- Destroying the source table" << std::endl; +} + + + +int +main(int argc, char **argv) +{ + initlog(); + + test(); + + deallog << "Objects created: " << object_number << std::endl; + deallog << "Objects destroyed: " << objects_destroyed << std::endl; +} diff --git a/tests/base/aligned_vector_memory_02.output b/tests/base/aligned_vector_memory_02.output new file mode 100644 index 0000000000..81cb061837 --- /dev/null +++ b/tests/base/aligned_vector_memory_02.output @@ -0,0 +1,13 @@ + +DEAL::---- Creating outer table +DEAL::default constructor. Object number 0 +DEAL::---- Cloning outer table +DEAL::default constructor. Object number 1 +DEAL::destructor. Object number 1 +DEAL::copy constructor from 0. Object number 2 +DEAL::---- Destroying the clone +DEAL::destructor. Object number 2 +DEAL::---- Destroying the source table +DEAL::destructor. Object number 0 +DEAL::Objects created: 3 +DEAL::Objects destroyed: 3 -- 2.39.5