Revise -mdisable-fpregs option and add new -msoft-mult option
[official-gcc.git] / gcc / unique-ptr-tests.cc
blob975dd57398e3887ff74fb32ed04327fb69d2a648
1 /* Unit tests for unique-ptr.h.
2 Copyright (C) 2017-2021 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
9 version.
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.
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/>. */
20 #include "config.h"
21 #define INCLUDE_UNIQUE_PTR
22 #include "system.h"
23 #include "coretypes.h"
24 #include "selftest.h"
26 #if CHECKING_P
28 namespace selftest {
30 namespace {
32 /* A class for counting ctor and dtor invocations. */
34 class stats
36 public:
37 stats () : ctor_count (0), dtor_count (0) {}
39 int ctor_count;
40 int dtor_count;
43 /* A class that uses "stats" to track its ctor and dtor invocations. */
45 class foo
47 public:
48 foo (stats &s) : m_s (s) { ++m_s.ctor_count; }
49 ~foo () { ++m_s.dtor_count; }
51 int example_method () const { return 42; }
53 private:
54 foo (const foo&);
55 foo & operator= (const foo &);
57 private:
58 stats &m_s;
61 /* A struct for testing unique_ptr<T[]>. */
63 class has_default_ctor
65 public:
66 has_default_ctor () : m_field (42) {}
67 int m_field;
70 /* A dummy struct for testing unique_xmalloc_ptr. */
72 struct dummy
74 int field;
77 } // anonymous namespace
79 /* Verify that the default ctor inits ptrs to NULL. */
81 static void
82 test_null_ptr ()
84 gnu::unique_ptr<void *> p;
85 ASSERT_EQ (NULL, p);
87 gnu::unique_xmalloc_ptr<void *> q;
88 ASSERT_EQ (NULL, q);
91 /* Verify that deletion happens when a unique_ptr goes out of scope. */
93 static void
94 test_implicit_deletion ()
96 stats s;
97 ASSERT_EQ (0, s.ctor_count);
98 ASSERT_EQ (0, s.dtor_count);
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);
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. */
114 static void
115 test_overwrite_of_null ()
117 stats s;
118 ASSERT_EQ (0, s.ctor_count);
119 ASSERT_EQ (0, s.dtor_count);
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);
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. */
140 static void
141 test_overwrite_of_non_null ()
143 stats s;
144 ASSERT_EQ (0, s.ctor_count);
145 ASSERT_EQ (0, s.dtor_count);
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);
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. */
166 static void
167 test_overloaded_ops ()
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 ());
176 gnu::unique_ptr<foo> g (new foo (s));
177 ASSERT_NE (f, g);
180 /* Verify that the gnu::unique_ptr specialization for T[] works. */
182 static void
183 test_array_new ()
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);
194 /* Verify that gnu::unique_xmalloc_ptr works. */
196 static void
197 test_xmalloc ()
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[]. */
205 static void
206 test_xmalloc_array ()
208 const int num = 10;
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++)
214 p[i].field = 42;
215 for (int i = 0; i < num; i++)
216 ASSERT_EQ (42, p[i].field);
219 /* Run all of the selftests within this file. */
221 void
222 unique_ptr_tests_cc_tests ()
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 ();
234 } // namespace selftest
236 #endif /* #if CHECKING_P */