1 /* Unit tests for unique-ptr.h.
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
21 #define INCLUDE_UNIQUE_PTR
23 #include "coretypes.h"
32 /* A class for counting ctor and dtor invocations. */
37 stats () : ctor_count (0), dtor_count (0) {}
43 /* A class that uses "stats" to track its ctor and dtor invocations. */
48 foo (stats
&s
) : m_s (s
) { ++m_s
.ctor_count
; }
49 ~foo () { ++m_s
.dtor_count
; }
51 int example_method () const { return 42; }
55 foo
& operator= (const foo
&);
61 /* A struct for testing unique_ptr<T[]>. */
63 class has_default_ctor
66 has_default_ctor () : m_field (42) {}
70 /* A dummy struct for testing unique_xmalloc_ptr. */
77 } // anonymous namespace
79 /* Verify that the default ctor inits ptrs to NULL. */
84 gnu::unique_ptr
<void *> p
;
87 gnu::unique_xmalloc_ptr
<void *> q
;
91 /* Verify that deletion happens when a unique_ptr goes out of scope. */
94 test_implicit_deletion ()
97 ASSERT_EQ (0, s
.ctor_count
);
98 ASSERT_EQ (0, s
.dtor_count
);
101 gnu::unique_ptr
<foo
> f (new foo (s
));
103 ASSERT_EQ (1, s
.ctor_count
);
104 ASSERT_EQ (0, s
.dtor_count
);
107 /* Verify that the foo was implicitly deleted. */
108 ASSERT_EQ (1, s
.ctor_count
);
109 ASSERT_EQ (1, s
.dtor_count
);
112 /* Verify that we can assign to a NULL unique_ptr. */
115 test_overwrite_of_null ()
118 ASSERT_EQ (0, s
.ctor_count
);
119 ASSERT_EQ (0, s
.dtor_count
);
122 gnu::unique_ptr
<foo
> f
;
124 ASSERT_EQ (0, s
.ctor_count
);
125 ASSERT_EQ (0, s
.dtor_count
);
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
);
133 /* Verify that the foo is implicitly deleted. */
134 ASSERT_EQ (1, s
.ctor_count
);
135 ASSERT_EQ (1, s
.dtor_count
);
138 /* Verify that we can assign to a non-NULL unique_ptr. */
141 test_overwrite_of_non_null ()
144 ASSERT_EQ (0, s
.ctor_count
);
145 ASSERT_EQ (0, s
.dtor_count
);
148 gnu::unique_ptr
<foo
> f (new foo (s
));
150 ASSERT_EQ (1, s
.ctor_count
);
151 ASSERT_EQ (0, s
.dtor_count
);
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
);
159 /* Verify that the 2nd foo was implicitly deleted. */
160 ASSERT_EQ (2, s
.ctor_count
);
161 ASSERT_EQ (2, s
.dtor_count
);
164 /* Verify that unique_ptr's overloaded ops work. */
167 test_overloaded_ops ()
170 gnu::unique_ptr
<foo
> f (new foo (s
));
171 ASSERT_EQ (42, f
->example_method ());
172 ASSERT_EQ (42, (*f
).example_method ());
174 ASSERT_NE (NULL
, f
.get ());
176 gnu::unique_ptr
<foo
> g (new foo (s
));
180 /* Verify that the gnu::unique_ptr specialization for T[] works. */
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
190 for (int i
= 0; i
< num
; i
++)
191 ASSERT_EQ (42, p
[i
].m_field
);
194 /* Verify that gnu::unique_xmalloc_ptr works. */
199 gnu::unique_xmalloc_ptr
<dummy
> p (XNEW (dummy
));
200 ASSERT_NE (NULL
, p
.get ());
203 /* Verify the gnu::unique_xmalloc_ptr specialization for T[]. */
206 test_xmalloc_array ()
209 gnu::unique_xmalloc_ptr
<dummy
[]> p (XNEWVEC (dummy
, num
));
210 ASSERT_NE (NULL
, p
.get ());
212 /* Verify that operator[] works. */
213 for (int i
= 0; i
< num
; i
++)
215 for (int i
= 0; i
< num
; i
++)
216 ASSERT_EQ (42, p
[i
].field
);
219 /* Run all of the selftests within this file. */
222 unique_ptr_tests_cc_tests ()
225 test_implicit_deletion ();
226 test_overwrite_of_null ();
227 test_overwrite_of_non_null ();
228 test_overloaded_ops ();
231 test_xmalloc_array ();
234 } // namespace selftest
236 #endif /* #if CHECKING_P */