8e7b3f8e4f8f261bf92b1d54009ad1bfc31ede60
[alexxy/gromacs.git] / src / gromacs / compat / tests / pointers.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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
51 namespace gmx
52 {
53 namespace compat
54 {
55 namespace
56 {
57
58 TEST(NotNullConstruction, Works)
59 {
60     // shared_ptr<int> is nullptr assignable
61     not_null<std::shared_ptr<int>> sharedPointer(std::make_shared<int>(10));
62
63 #ifndef NDEBUG
64 /* The workaround here is needed because the intel implementation
65  * will not trigger the assert when using the pointer without
66  * a valid object. This was needed due to an internal error
67  * being triggered instead with the compiler under this condition.
68  */
69 #    if !defined(__INTEL_COMPILER) || !(__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE == 0)
70     int* nullPointer = nullptr;
71     EXPECT_DEATH_IF_SUPPORTED(not_null<int*> invalidNullPointer(nullPointer), "");
72 #    endif
73 #endif
74
75     int  value        = 20;
76     int* validPointer = &value;
77     {
78         not_null<int*> validNotNullPointer(validPointer);
79         GMX_UNUSED_VALUE(validNotNullPointer);
80     }
81     {
82         not_null<int*> validNotNullPointer = not_null<int*>(validPointer);
83         GMX_UNUSED_VALUE(validNotNullPointer);
84     }
85 }
86
87 TEST(NotNullCasting, Works)
88 {
89     struct MyBase
90     {
91     };
92     struct MyDerived : public MyBase
93     {
94     };
95     struct Unrelated
96     {
97     };
98
99     MyBase    base;
100     MyDerived derived;
101     Unrelated unrelated;
102
103     not_null<Unrelated*> u{ &unrelated };
104     (void)u;
105     not_null<MyDerived*> p{ &derived };
106     not_null<MyBase*>    q(&base);
107     // Allowed with heterogeneous copy constructor
108     q = p;
109
110     not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
111     EXPECT_EQ(reinterpret_cast<void*>(p.get()), reinterpret_cast<void*>(t.get()));
112 }
113
114 TEST(NotNullAssignment, Works)
115 {
116     int            i = 12;
117     not_null<int*> p(&i);
118     EXPECT_EQ(*p, 12);
119 }
120
121 TEST(MakeNotNull, Works)
122 {
123     {
124         int i = 42;
125
126         const not_null<int*> x = make_not_null(&i);
127         EXPECT_EQ(*x, 42);
128         not_null<int*> y = make_not_null(&i);
129         EXPECT_EQ(*y, 42);
130         not_null<const int*> z = make_not_null(&i);
131         EXPECT_EQ(*z, 42);
132     }
133
134     {
135         // TODO These should work, but the GSL version of
136         // make_not_null has an auto return type that we can't use
137         // here, so maybe the issue is there.
138         /*
139            int i = 42;
140            int* p = &i;
141
142            not_null<int *> x = make_not_null(p);
143            EXPECT_EQ(*x, 42);
144            not_null<int *> y = make_not_null(p);
145            EXPECT_EQ(*y, 42);
146            not_null<const int *> z = make_not_null(p);
147            EXPECT_EQ(*z, 42);
148          */
149     }
150
151     {
152         std::unique_ptr<int> i = std::make_unique<int>(42);
153
154         const not_null<int*> x = make_not_null(i);
155         EXPECT_EQ(*x, 42);
156         not_null<int*> y = make_not_null(i);
157         EXPECT_EQ(*y, 42);
158         not_null<const int*> z = make_not_null(i);
159         EXPECT_EQ(*z, 42);
160     }
161
162     {
163         std::unique_ptr<const int> i = std::make_unique<int>(42);
164
165         // not_null<int *> does not compile, as expected
166         not_null<const int*> z = make_not_null(i);
167         EXPECT_EQ(*z, 42);
168     }
169 }
170
171 TEST(NotNull, WorksInContainers)
172 {
173     int            i = 12;
174     not_null<int*> p(&i);
175
176     std::vector<not_null<int*>> v;
177     v.push_back(p);
178     EXPECT_EQ(*v.back(), 12);
179 }
180
181 // TODO We currently don't have infrastructure for checking that e.g.
182 // expected static assertions fire and calls to deleted functions do
183 // not compile. When we do, there are more tests that should be found
184 // here.
185
186 } // anonymous namespace
187 } // namespace compat
188 } // namespace gmx