1 /* Unit tests for unique-ptr.h.
2 Copyright (C) 2017-2018 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. */
36 stats () : ctor_count (0), dtor_count (0) {}
42 /* A class that uses "stats" to track its ctor and dtor invocations. */
47 foo (stats
&s
) : m_s (s
) { ++m_s
.ctor_count
; }
48 ~foo () { ++m_s
.dtor_count
; }
50 int example_method () const { return 42; }
54 foo
& operator= (const foo
&);
60 /* A struct for testing unique_ptr<T[]>. */
62 struct has_default_ctor
64 has_default_ctor () : m_field (42) {}
68 /* A dummy struct for testing unique_xmalloc_ptr. */
75 } // anonymous namespace
77 /* Verify that the default ctor inits ptrs to NULL. */
82 gnu::unique_ptr
<void *> p
;
85 gnu::unique_xmalloc_ptr
<void *> q
;
89 /* Verify that deletion happens when a unique_ptr goes out of scope. */
92 test_implicit_deletion ()
95 ASSERT_EQ (0, s
.ctor_count
);
96 ASSERT_EQ (0, s
.dtor_count
);
99 gnu::unique_ptr
<foo
> f (new foo (s
));
101 ASSERT_EQ (1, s
.ctor_count
);
102 ASSERT_EQ (0, s
.dtor_count
);
105 /* Verify that the foo was implicitly deleted. */
106 ASSERT_EQ (1, s
.ctor_count
);
107 ASSERT_EQ (1, s
.dtor_count
);
110 /* Verify that we can assign to a NULL unique_ptr. */
113 test_overwrite_of_null ()
116 ASSERT_EQ (0, s
.ctor_count
);
117 ASSERT_EQ (0, s
.dtor_count
);
120 gnu::unique_ptr
<foo
> f
;
122 ASSERT_EQ (0, s
.ctor_count
);
123 ASSERT_EQ (0, s
.dtor_count
);
125 /* Overwrite with a non-NULL value. */
126 f
= gnu::unique_ptr
<foo
> (new foo (s
));
127 ASSERT_EQ (1, s
.ctor_count
);
128 ASSERT_EQ (0, s
.dtor_count
);
131 /* Verify that the foo is implicitly deleted. */
132 ASSERT_EQ (1, s
.ctor_count
);
133 ASSERT_EQ (1, s
.dtor_count
);
136 /* Verify that we can assign to a non-NULL unique_ptr. */
139 test_overwrite_of_non_null ()
142 ASSERT_EQ (0, s
.ctor_count
);
143 ASSERT_EQ (0, s
.dtor_count
);
146 gnu::unique_ptr
<foo
> f (new foo (s
));
148 ASSERT_EQ (1, s
.ctor_count
);
149 ASSERT_EQ (0, s
.dtor_count
);
151 /* Overwrite with a different value. */
152 f
= gnu::unique_ptr
<foo
> (new foo (s
));
153 ASSERT_EQ (2, s
.ctor_count
);
154 ASSERT_EQ (1, s
.dtor_count
);
157 /* Verify that the 2nd foo was implicitly deleted. */
158 ASSERT_EQ (2, s
.ctor_count
);
159 ASSERT_EQ (2, s
.dtor_count
);
162 /* Verify that unique_ptr's overloaded ops work. */
165 test_overloaded_ops ()
168 gnu::unique_ptr
<foo
> f (new foo (s
));
169 ASSERT_EQ (42, f
->example_method ());
170 ASSERT_EQ (42, (*f
).example_method ());
172 ASSERT_NE (NULL
, f
.get ());
174 gnu::unique_ptr
<foo
> g (new foo (s
));
178 /* Verify that the gnu::unique_ptr specialization for T[] works. */
184 gnu::unique_ptr
<has_default_ctor
[]> p (new has_default_ctor
[num
]);
185 ASSERT_NE (NULL
, p
.get ());
186 /* Verify that operator[] works, and that the default ctor was called
188 for (int i
= 0; i
< num
; i
++)
189 ASSERT_EQ (42, p
[i
].m_field
);
192 /* Verify that gnu::unique_xmalloc_ptr works. */
197 gnu::unique_xmalloc_ptr
<dummy
> p (XNEW (dummy
));
198 ASSERT_NE (NULL
, p
.get ());
201 /* Verify the gnu::unique_xmalloc_ptr specialization for T[]. */
204 test_xmalloc_array ()
207 gnu::unique_xmalloc_ptr
<dummy
[]> p (XNEWVEC (dummy
, num
));
208 ASSERT_NE (NULL
, p
.get ());
210 /* Verify that operator[] works. */
211 for (int i
= 0; i
< num
; i
++)
213 for (int i
= 0; i
< num
; i
++)
214 ASSERT_EQ (42, p
[i
].field
);
217 /* Run all of the selftests within this file. */
220 unique_ptr_tests_cc_tests ()
223 test_implicit_deletion ();
224 test_overwrite_of_null ();
225 test_overwrite_of_non_null ();
226 test_overloaded_ops ();
229 test_xmalloc_array ();
232 } // namespace selftest
234 #endif /* #if CHECKING_P */