]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/unique-ptr-tests.cc
2019-10-18 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
[thirdparty/gcc.git] / gcc / unique-ptr-tests.cc
CommitLineData
b6b04a7b 1/* Unit tests for unique-ptr.h.
fbd26352 2 Copyright (C) 2017-2019 Free Software Foundation, Inc.
b6b04a7b 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
95f39a98 21#define INCLUDE_UNIQUE_PTR
b6b04a7b 22#include "system.h"
23#include "coretypes.h"
b6b04a7b 24#include "selftest.h"
25
26#if CHECKING_P
27
28namespace selftest {
29
30namespace {
31
32/* A class for counting ctor and dtor invocations. */
33
251317e4 34class stats
b6b04a7b 35{
251317e4 36public:
b6b04a7b 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
45class foo
46{
47public:
48 foo (stats &s) : m_s (s) { ++m_s.ctor_count; }
49 ~foo () { ++m_s.dtor_count; }
50
51 int example_method () const { return 42; }
52
53private:
54 foo (const foo&);
55 foo & operator= (const foo &);
56
57private:
58 stats &m_s;
59};
60
61/* A struct for testing unique_ptr<T[]>. */
62
251317e4 63class has_default_ctor
b6b04a7b 64{
251317e4 65public:
b6b04a7b 66 has_default_ctor () : m_field (42) {}
67 int m_field;
68};
69
70/* A dummy struct for testing unique_xmalloc_ptr. */
71
72struct dummy
73{
74 int field;
75};
76
77} // anonymous namespace
78
79/* Verify that the default ctor inits ptrs to NULL. */
80
81static void
82test_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
93static void
94test_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
114static void
115test_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
140static void
141test_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
166static void
167test_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
182static void
183test_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
196static void
197test_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
205static void
206test_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
221void
222unique_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 */