Remove unused function generate_excls and make clean_excls static
[gromacs.git] / src / gromacs / compat / tests / pointers.cpp
blob28b79586625cf74605836ac449d0cd02a37ccdeb
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \internal \file
36 * \brief Tests for pointers.h, e.g. gmx::compat::not_null
38 * \author Mark Abraham <mark.j.abraham@gmail.com>
39 * \ingroup module_compat
41 #include "gmxpre.h"
43 #include "gromacs/compat/pointers.h"
45 #include <memory>
46 #include <vector>
48 #include <gtest/gtest.h>
51 namespace gmx
53 namespace compat
55 namespace
58 TEST(NotNullConstruction, Works)
60 // shared_ptr<int> is nullptr assignable
61 not_null < std::shared_ptr < int>> sharedPointer(
62 std::make_shared<int>(10));
64 #ifndef NDEBUG
65 /* The workaround here is needed because the intel implementation
66 * will not trigger the assert when using the pointer without
67 * a valid object. This was needed due to an internal error
68 * being triggered instead with the compiler under this condition.
70 #if !defined(__INTEL_COMPILER) || !(__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE == 0)
71 int *nullPointer = nullptr;
72 EXPECT_DEATH_IF_SUPPORTED(not_null<int *> invalidNullPointer(nullPointer), "");
73 #endif
74 #endif
76 int value = 20;
77 int *validPointer = &value;
79 not_null<int *> validNotNullPointer(validPointer);
80 GMX_UNUSED_VALUE(validNotNullPointer);
83 not_null<int *> validNotNullPointer = not_null<int *>(validPointer);
84 GMX_UNUSED_VALUE(validNotNullPointer);
88 TEST(NotNullCasting, Works)
90 struct MyBase
93 struct MyDerived : public MyBase
96 struct Unrelated
100 MyBase base;
101 MyDerived derived;
102 Unrelated unrelated;
104 not_null<Unrelated*> u {
105 &unrelated
107 (void) u;
108 not_null<MyDerived*> p {
109 &derived
111 not_null<MyBase*> q(&base);
112 // Allowed with heterogeneous copy constructor
113 q = p;
115 not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
116 EXPECT_EQ(reinterpret_cast<void*>(p.get()), reinterpret_cast<void*>(t.get()));
119 TEST(NotNullAssignment, Works)
121 int i = 12;
122 not_null<int*> p(&i);
123 EXPECT_EQ(*p, 12);
126 TEST(MakeNotNull, Works)
129 int i = 42;
131 const not_null<int *> x = make_not_null(&i);
132 EXPECT_EQ(*x, 42);
133 not_null<int *> y = make_not_null(&i);
134 EXPECT_EQ(*y, 42);
135 not_null<const int *> z = make_not_null(&i);
136 EXPECT_EQ(*z, 42);
140 // TODO These should work, but the GSL version of
141 // make_not_null has an auto return type that we can't use
142 // here, so maybe the issue is there.
144 int i = 42;
145 int* p = &i;
147 not_null<int *> x = make_not_null(p);
148 EXPECT_EQ(*x, 42);
149 not_null<int *> y = make_not_null(p);
150 EXPECT_EQ(*y, 42);
151 not_null<const int *> z = make_not_null(p);
152 EXPECT_EQ(*z, 42);
157 std::unique_ptr<int> i = std::make_unique<int>(42);
159 const not_null<int *> x = make_not_null(i);
160 EXPECT_EQ(*x, 42);
161 not_null<int *> y = make_not_null(i);
162 EXPECT_EQ(*y, 42);
163 not_null<const int *> z = make_not_null(i);
164 EXPECT_EQ(*z, 42);
168 std::unique_ptr<const int> i = std::make_unique<int>(42);
170 // not_null<int *> does not compile, as expected
171 not_null<const int *> z = make_not_null(i);
172 EXPECT_EQ(*z, 42);
177 TEST(NotNull, WorksInContainers)
179 int i = 12;
180 not_null<int*> p(&i);
182 std::vector < not_null < int*>> v;
183 v.push_back(p);
184 EXPECT_EQ(*v.back(), 12);
187 // TODO We currently don't have infrastructure for checking that e.g.
188 // expected static assertions fire and calls to deleted functions do
189 // not compile. When we do, there are more tests that should be found
190 // here.
192 } // anonymous namespace
193 } // namespace compat
194 } // namespace gmx