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