1 /* Unit tests for unique-ptr.h.
2    Copyright (C) 2017-2021 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include "config.h"
21 #define INCLUDE_UNIQUE_PTR
22 #include "system.h"
23 #include "coretypes.h"
24 #include "selftest.h"
25 
26 #if CHECKING_P
27 
28 namespace selftest {
29 
30 namespace {
31 
32 /* A class for counting ctor and dtor invocations.  */
33 
34 class stats
35 {
36 public:
stats()37   stats () : ctor_count (0), dtor_count (0) {}
38 
39   int ctor_count;
40   int dtor_count;
41 };
42 
43 /* A class that uses "stats" to track its ctor and dtor invocations.  */
44 
45 class foo
46 {
47 public:
foo(stats & s)48   foo (stats &s) : m_s (s) { ++m_s.ctor_count; }
~foo()49   ~foo () { ++m_s.dtor_count; }
50 
example_method() const51   int example_method () const { return 42; }
52 
53 private:
54   foo (const foo&);
55   foo & operator= (const foo &);
56 
57 private:
58   stats &m_s;
59 };
60 
61 /* A struct for testing unique_ptr<T[]>.  */
62 
63 class has_default_ctor
64 {
65 public:
has_default_ctor()66   has_default_ctor () : m_field (42) {}
67   int m_field;
68 };
69 
70 /* A dummy struct for testing unique_xmalloc_ptr.  */
71 
72 struct dummy
73 {
74   int field;
75 };
76 
77 } // anonymous namespace
78 
79 /* Verify that the default ctor inits ptrs to NULL.  */
80 
81 static void
test_null_ptr()82 test_null_ptr ()
83 {
84   gnu::unique_ptr<void *> p;
85   ASSERT_EQ (NULL, p);
86 
87   gnu::unique_xmalloc_ptr<void *> q;
88   ASSERT_EQ (NULL, q);
89 }
90 
91 /* Verify that deletion happens when a unique_ptr goes out of scope.  */
92 
93 static void
test_implicit_deletion()94 test_implicit_deletion ()
95 {
96   stats s;
97   ASSERT_EQ (0, s.ctor_count);
98   ASSERT_EQ (0, s.dtor_count);
99 
100   {
101     gnu::unique_ptr<foo> f (new foo (s));
102     ASSERT_NE (NULL, f);
103     ASSERT_EQ (1, s.ctor_count);
104     ASSERT_EQ (0, s.dtor_count);
105   }
106 
107   /* Verify that the foo was implicitly deleted.  */
108   ASSERT_EQ (1, s.ctor_count);
109   ASSERT_EQ (1, s.dtor_count);
110 }
111 
112 /* Verify that we can assign to a NULL unique_ptr.  */
113 
114 static void
test_overwrite_of_null()115 test_overwrite_of_null ()
116 {
117   stats s;
118   ASSERT_EQ (0, s.ctor_count);
119   ASSERT_EQ (0, s.dtor_count);
120 
121   {
122     gnu::unique_ptr<foo> f;
123     ASSERT_EQ (NULL, f);
124     ASSERT_EQ (0, s.ctor_count);
125     ASSERT_EQ (0, s.dtor_count);
126 
127     /* Overwrite with a non-NULL value.  */
128     f = gnu::unique_ptr<foo> (new foo (s));
129     ASSERT_EQ (1, s.ctor_count);
130     ASSERT_EQ (0, s.dtor_count);
131   }
132 
133   /* Verify that the foo is implicitly deleted.  */
134   ASSERT_EQ (1, s.ctor_count);
135   ASSERT_EQ (1, s.dtor_count);
136 }
137 
138 /* Verify that we can assign to a non-NULL unique_ptr.  */
139 
140 static void
test_overwrite_of_non_null()141 test_overwrite_of_non_null ()
142 {
143   stats s;
144   ASSERT_EQ (0, s.ctor_count);
145   ASSERT_EQ (0, s.dtor_count);
146 
147   {
148     gnu::unique_ptr<foo> f (new foo (s));
149     ASSERT_NE (NULL, f);
150     ASSERT_EQ (1, s.ctor_count);
151     ASSERT_EQ (0, s.dtor_count);
152 
153     /* Overwrite with a different value.  */
154     f = gnu::unique_ptr<foo> (new foo (s));
155     ASSERT_EQ (2, s.ctor_count);
156     ASSERT_EQ (1, s.dtor_count);
157   }
158 
159   /* Verify that the 2nd foo was implicitly deleted.  */
160   ASSERT_EQ (2, s.ctor_count);
161   ASSERT_EQ (2, s.dtor_count);
162 }
163 
164 /* Verify that unique_ptr's overloaded ops work.  */
165 
166 static void
test_overloaded_ops()167 test_overloaded_ops ()
168 {
169   stats s;
170   gnu::unique_ptr<foo> f (new foo (s));
171   ASSERT_EQ (42, f->example_method ());
172   ASSERT_EQ (42, (*f).example_method ());
173   ASSERT_EQ (f, f);
174   ASSERT_NE (NULL, f.get ());
175 
176   gnu::unique_ptr<foo> g (new foo (s));
177   ASSERT_NE (f, g);
178 }
179 
180 /* Verify that the gnu::unique_ptr specialization for T[] works.  */
181 
182 static void
test_array_new()183 test_array_new ()
184 {
185   const int num = 10;
186   gnu::unique_ptr<has_default_ctor[]> p (new has_default_ctor[num]);
187   ASSERT_NE (NULL, p.get ());
188   /* Verify that operator[] works, and that the default ctor was called
189      on each element.  */
190   for (int i = 0; i < num; i++)
191     ASSERT_EQ (42, p[i].m_field);
192 }
193 
194 /* Verify that gnu::unique_xmalloc_ptr works.  */
195 
196 static void
test_xmalloc()197 test_xmalloc ()
198 {
199   gnu::unique_xmalloc_ptr<dummy> p (XNEW (dummy));
200   ASSERT_NE (NULL, p.get ());
201 }
202 
203 /* Verify the gnu::unique_xmalloc_ptr specialization for T[].  */
204 
205 static void
test_xmalloc_array()206 test_xmalloc_array ()
207 {
208   const int num = 10;
209   gnu::unique_xmalloc_ptr<dummy[]> p (XNEWVEC (dummy, num));
210   ASSERT_NE (NULL, p.get ());
211 
212   /* Verify that operator[] works.  */
213   for (int i = 0; i < num; i++)
214     p[i].field = 42;
215   for (int i = 0; i < num; i++)
216     ASSERT_EQ (42, p[i].field);
217 }
218 
219 /* Run all of the selftests within this file.  */
220 
221 void
unique_ptr_tests_cc_tests()222 unique_ptr_tests_cc_tests ()
223 {
224   test_null_ptr ();
225   test_implicit_deletion ();
226   test_overwrite_of_null ();
227   test_overwrite_of_non_null ();
228   test_overloaded_ops ();
229   test_array_new ();
230   test_xmalloc ();
231   test_xmalloc_array ();
232 }
233 
234 } // namespace selftest
235 
236 #endif /* #if CHECKING_P */
237