1 /*
2 * This file is part of the GROMACS molecular simulation package.
3 *
4 * Copyright (c) 2018,2019,2020, 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.
8 *
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.
13 *
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.
18 *
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.
23 *
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.
31 *
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.
34 */
35 /*! \internal \file
36 * \brief Tests for pointers.h, e.g. gmx::compat::not_null
37 *
38 * \author Mark Abraham <mark.j.abraham@gmail.com>
39 * \ingroup module_compat
40 */
41 #include "gmxpre.h"
42
43 #include "gromacs/compat/pointers.h"
44
45 #include <memory>
46 #include <vector>
47
48 #include <gtest/gtest.h>
49
50 #include "testutils/testasserts.h"
51
52 namespace gmx
53 {
54 namespace compat
55 {
56 namespace
57 {
58
TEST(NotNullConstruction,Works)59 TEST(NotNullConstruction, Works)
60 {
61 // shared_ptr<int> is nullptr assignable
62 not_null<std::shared_ptr<int>> sharedPointer(std::make_shared<int>(10));
63
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.
69 *
70 * Death tests can also not be used safely in a parallel environment.
71 */
72 # if !defined(__INTEL_COMPILER) || !(__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE == 0)
73 int* nullPointer = nullptr;
74 GMX_EXPECT_DEATH_IF_SUPPORTED(not_null<int*> invalidNullPointer(nullPointer), "");
75 # endif
76 #endif
77
78 int value = 20;
79 int* validPointer = &value;
80 {
81 not_null<int*> validNotNullPointer(validPointer);
82 GMX_UNUSED_VALUE(validNotNullPointer);
83 }
84 {
85 not_null<int*> validNotNullPointer = not_null<int*>(validPointer);
86 GMX_UNUSED_VALUE(validNotNullPointer);
87 }
88 }
89
TEST(NotNullCasting,Works)90 TEST(NotNullCasting, Works)
91 {
92 struct MyBase
93 {
94 };
95 struct MyDerived : public MyBase
96 {
97 };
98 struct Unrelated
99 {
100 };
101
102 MyBase base;
103 MyDerived derived;
104 Unrelated unrelated;
105
106 not_null<Unrelated*> u{ &unrelated };
107 (void)u;
108 not_null<MyDerived*> p{ &derived };
109 not_null<MyBase*> q(&base);
110 // Allowed with heterogeneous copy constructor
111 q = p;
112
113 not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
114 EXPECT_EQ(reinterpret_cast<void*>(p.get()), reinterpret_cast<void*>(t.get()));
115 }
116
TEST(NotNullAssignment,Works)117 TEST(NotNullAssignment, Works)
118 {
119 int i = 12;
120 not_null<int*> p(&i);
121 EXPECT_EQ(*p, 12);
122 }
123
TEST(MakeNotNull,Works)124 TEST(MakeNotNull, Works)
125 {
126 {
127 int i = 42;
128
129 const not_null<int*> x = make_not_null(&i);
130 EXPECT_EQ(*x, 42);
131 not_null<int*> y = make_not_null(&i);
132 EXPECT_EQ(*y, 42);
133 not_null<const int*> z = make_not_null(&i);
134 EXPECT_EQ(*z, 42);
135 }
136
137 {
138 // TODO These should work, but the GSL version of
139 // make_not_null has an auto return type that we can't use
140 // here, so maybe the issue is there.
141 /*
142 int i = 42;
143 int* p = &i;
144
145 not_null<int *> x = make_not_null(p);
146 EXPECT_EQ(*x, 42);
147 not_null<int *> y = make_not_null(p);
148 EXPECT_EQ(*y, 42);
149 not_null<const int *> z = make_not_null(p);
150 EXPECT_EQ(*z, 42);
151 */
152 }
153
154 {
155 std::unique_ptr<int> i = std::make_unique<int>(42);
156
157 const not_null<int*> x = make_not_null(i);
158 EXPECT_EQ(*x, 42);
159 not_null<int*> y = make_not_null(i);
160 EXPECT_EQ(*y, 42);
161 not_null<const int*> z = make_not_null(i);
162 EXPECT_EQ(*z, 42);
163 }
164
165 {
166 std::unique_ptr<const int> i = std::make_unique<int>(42);
167
168 // not_null<int *> does not compile, as expected
169 not_null<const int*> z = make_not_null(i);
170 EXPECT_EQ(*z, 42);
171 }
172 }
173
TEST(NotNull,WorksInContainers)174 TEST(NotNull, WorksInContainers)
175 {
176 int i = 12;
177 not_null<int*> p(&i);
178
179 std::vector<not_null<int*>> v;
180 v.push_back(p);
181 EXPECT_EQ(*v.back(), 12);
182 }
183
184 // TODO We currently don't have infrastructure for checking that e.g.
185 // expected static assertions fire and calls to deleted functions do
186 // not compile. When we do, there are more tests that should be found
187 // here.
188
189 } // anonymous namespace
190 } // namespace compat
191 } // namespace gmx
192