1a18eacbeSJulio Merino // Copyright (c) 2007 The NetBSD Foundation, Inc.
2a18eacbeSJulio Merino // All rights reserved.
3a18eacbeSJulio Merino //
4a18eacbeSJulio Merino // Redistribution and use in source and binary forms, with or without
5a18eacbeSJulio Merino // modification, are permitted provided that the following conditions
6a18eacbeSJulio Merino // are met:
7a18eacbeSJulio Merino // 1. Redistributions of source code must retain the above copyright
8a18eacbeSJulio Merino // notice, this list of conditions and the following disclaimer.
9a18eacbeSJulio Merino // 2. Redistributions in binary form must reproduce the above copyright
10a18eacbeSJulio Merino // notice, this list of conditions and the following disclaimer in the
11a18eacbeSJulio Merino // documentation and/or other materials provided with the distribution.
12a18eacbeSJulio Merino //
13a18eacbeSJulio Merino // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14a18eacbeSJulio Merino // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15a18eacbeSJulio Merino // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16a18eacbeSJulio Merino // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17a18eacbeSJulio Merino // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18a18eacbeSJulio Merino // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a18eacbeSJulio Merino // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20a18eacbeSJulio Merino // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21a18eacbeSJulio Merino // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22a18eacbeSJulio Merino // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23a18eacbeSJulio Merino // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24a18eacbeSJulio Merino // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*0677dfd1SJulio Merino
26*0677dfd1SJulio Merino #include "atf-c++/detail/auto_array.hpp"
27a18eacbeSJulio Merino
28a18eacbeSJulio Merino extern "C" {
29a18eacbeSJulio Merino #include <sys/types.h>
30a18eacbeSJulio Merino }
31a18eacbeSJulio Merino
32a18eacbeSJulio Merino #include <iostream>
33a18eacbeSJulio Merino
34*0677dfd1SJulio Merino #include <atf-c++.hpp>
35*0677dfd1SJulio Merino
36*0677dfd1SJulio Merino extern "C" {
37a18eacbeSJulio Merino #include "atf-c/defs.h"
38*0677dfd1SJulio Merino }
39a18eacbeSJulio Merino
40a18eacbeSJulio Merino // ------------------------------------------------------------------------
41a18eacbeSJulio Merino // Tests for the "auto_array" class.
42a18eacbeSJulio Merino // ------------------------------------------------------------------------
43a18eacbeSJulio Merino
44a18eacbeSJulio Merino class test_array {
45a18eacbeSJulio Merino public:
46a18eacbeSJulio Merino int m_value;
47a18eacbeSJulio Merino
48a18eacbeSJulio Merino static ssize_t m_nblocks;
49a18eacbeSJulio Merino
50a18eacbeSJulio Merino static
51a18eacbeSJulio Merino atf::auto_array< test_array >
do_copy(atf::auto_array<test_array> & ta)52a18eacbeSJulio Merino do_copy(atf::auto_array< test_array >& ta)
53a18eacbeSJulio Merino {
54a18eacbeSJulio Merino return atf::auto_array< test_array >(ta);
55a18eacbeSJulio Merino }
56a18eacbeSJulio Merino
operator new(size_t size ATF_DEFS_ATTRIBUTE_UNUSED)57a18eacbeSJulio Merino void* operator new(size_t size ATF_DEFS_ATTRIBUTE_UNUSED)
58a18eacbeSJulio Merino {
59a18eacbeSJulio Merino ATF_FAIL("New called but should have been new[]");
60a18eacbeSJulio Merino return new int(5);
61a18eacbeSJulio Merino }
62a18eacbeSJulio Merino
operator new[](size_t size)63a18eacbeSJulio Merino void* operator new[](size_t size)
64a18eacbeSJulio Merino {
65a18eacbeSJulio Merino m_nblocks++;
66a18eacbeSJulio Merino void* mem = ::operator new(size);
67a18eacbeSJulio Merino std::cout << "Allocated 'test_array' object " << mem << "\n";
68a18eacbeSJulio Merino return mem;
69a18eacbeSJulio Merino }
70a18eacbeSJulio Merino
operator delete(void * mem ATF_DEFS_ATTRIBUTE_UNUSED)71a18eacbeSJulio Merino void operator delete(void* mem ATF_DEFS_ATTRIBUTE_UNUSED)
72a18eacbeSJulio Merino {
73a18eacbeSJulio Merino ATF_FAIL("Delete called but should have been delete[]");
74a18eacbeSJulio Merino }
75a18eacbeSJulio Merino
operator delete[](void * mem)76a18eacbeSJulio Merino void operator delete[](void* mem)
77a18eacbeSJulio Merino {
78a18eacbeSJulio Merino std::cout << "Releasing 'test_array' object " << mem << "\n";
79a18eacbeSJulio Merino if (m_nblocks == 0)
80a18eacbeSJulio Merino ATF_FAIL("Unbalanced delete[]");
81a18eacbeSJulio Merino m_nblocks--;
82a18eacbeSJulio Merino ::operator delete(mem);
83a18eacbeSJulio Merino }
84a18eacbeSJulio Merino };
85a18eacbeSJulio Merino
86a18eacbeSJulio Merino ssize_t test_array::m_nblocks = 0;
87a18eacbeSJulio Merino
88a18eacbeSJulio Merino ATF_TEST_CASE(auto_array_scope);
ATF_TEST_CASE_HEAD(auto_array_scope)89a18eacbeSJulio Merino ATF_TEST_CASE_HEAD(auto_array_scope)
90a18eacbeSJulio Merino {
91a18eacbeSJulio Merino set_md_var("descr", "Tests the automatic scope handling in the "
92a18eacbeSJulio Merino "auto_array smart pointer class");
93a18eacbeSJulio Merino }
ATF_TEST_CASE_BODY(auto_array_scope)94a18eacbeSJulio Merino ATF_TEST_CASE_BODY(auto_array_scope)
95a18eacbeSJulio Merino {
96a18eacbeSJulio Merino using atf::auto_array;
97a18eacbeSJulio Merino
98a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
99a18eacbeSJulio Merino {
100a18eacbeSJulio Merino auto_array< test_array > t(new test_array[10]);
101a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
102a18eacbeSJulio Merino }
103a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
104a18eacbeSJulio Merino }
105a18eacbeSJulio Merino
106a18eacbeSJulio Merino ATF_TEST_CASE(auto_array_copy);
ATF_TEST_CASE_HEAD(auto_array_copy)107a18eacbeSJulio Merino ATF_TEST_CASE_HEAD(auto_array_copy)
108a18eacbeSJulio Merino {
109a18eacbeSJulio Merino set_md_var("descr", "Tests the auto_array smart pointer class' copy "
110a18eacbeSJulio Merino "constructor");
111a18eacbeSJulio Merino }
ATF_TEST_CASE_BODY(auto_array_copy)112a18eacbeSJulio Merino ATF_TEST_CASE_BODY(auto_array_copy)
113a18eacbeSJulio Merino {
114a18eacbeSJulio Merino using atf::auto_array;
115a18eacbeSJulio Merino
116a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
117a18eacbeSJulio Merino {
118a18eacbeSJulio Merino auto_array< test_array > t1(new test_array[10]);
119a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
120a18eacbeSJulio Merino
121a18eacbeSJulio Merino {
122a18eacbeSJulio Merino auto_array< test_array > t2(t1);
123a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
124a18eacbeSJulio Merino }
125a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
126a18eacbeSJulio Merino }
127a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
128a18eacbeSJulio Merino }
129a18eacbeSJulio Merino
130a18eacbeSJulio Merino ATF_TEST_CASE(auto_array_copy_ref);
ATF_TEST_CASE_HEAD(auto_array_copy_ref)131a18eacbeSJulio Merino ATF_TEST_CASE_HEAD(auto_array_copy_ref)
132a18eacbeSJulio Merino {
133a18eacbeSJulio Merino set_md_var("descr", "Tests the auto_array smart pointer class' copy "
134a18eacbeSJulio Merino "constructor through the auxiliary auto_array_ref object");
135a18eacbeSJulio Merino }
ATF_TEST_CASE_BODY(auto_array_copy_ref)136a18eacbeSJulio Merino ATF_TEST_CASE_BODY(auto_array_copy_ref)
137a18eacbeSJulio Merino {
138a18eacbeSJulio Merino using atf::auto_array;
139a18eacbeSJulio Merino
140a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
141a18eacbeSJulio Merino {
142a18eacbeSJulio Merino auto_array< test_array > t1(new test_array[10]);
143a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
144a18eacbeSJulio Merino
145a18eacbeSJulio Merino {
146a18eacbeSJulio Merino auto_array< test_array > t2 = test_array::do_copy(t1);
147a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
148a18eacbeSJulio Merino }
149a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
150a18eacbeSJulio Merino }
151a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
152a18eacbeSJulio Merino }
153a18eacbeSJulio Merino
154a18eacbeSJulio Merino ATF_TEST_CASE(auto_array_get);
ATF_TEST_CASE_HEAD(auto_array_get)155a18eacbeSJulio Merino ATF_TEST_CASE_HEAD(auto_array_get)
156a18eacbeSJulio Merino {
157a18eacbeSJulio Merino set_md_var("descr", "Tests the auto_array smart pointer class' get "
158a18eacbeSJulio Merino "method");
159a18eacbeSJulio Merino }
ATF_TEST_CASE_BODY(auto_array_get)160a18eacbeSJulio Merino ATF_TEST_CASE_BODY(auto_array_get)
161a18eacbeSJulio Merino {
162a18eacbeSJulio Merino using atf::auto_array;
163a18eacbeSJulio Merino
164a18eacbeSJulio Merino test_array* ta = new test_array[10];
165a18eacbeSJulio Merino auto_array< test_array > t(ta);
166a18eacbeSJulio Merino ATF_REQUIRE_EQ(t.get(), ta);
167a18eacbeSJulio Merino }
168a18eacbeSJulio Merino
169a18eacbeSJulio Merino ATF_TEST_CASE(auto_array_release);
ATF_TEST_CASE_HEAD(auto_array_release)170a18eacbeSJulio Merino ATF_TEST_CASE_HEAD(auto_array_release)
171a18eacbeSJulio Merino {
172a18eacbeSJulio Merino set_md_var("descr", "Tests the auto_array smart pointer class' release "
173a18eacbeSJulio Merino "method");
174a18eacbeSJulio Merino }
ATF_TEST_CASE_BODY(auto_array_release)175a18eacbeSJulio Merino ATF_TEST_CASE_BODY(auto_array_release)
176a18eacbeSJulio Merino {
177a18eacbeSJulio Merino using atf::auto_array;
178a18eacbeSJulio Merino
179a18eacbeSJulio Merino test_array* ta1 = new test_array[10];
180a18eacbeSJulio Merino {
181a18eacbeSJulio Merino auto_array< test_array > t(ta1);
182a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
183a18eacbeSJulio Merino test_array* ta2 = t.release();
184a18eacbeSJulio Merino ATF_REQUIRE_EQ(ta2, ta1);
185a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
186a18eacbeSJulio Merino }
187a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
188a18eacbeSJulio Merino delete [] ta1;
189a18eacbeSJulio Merino }
190a18eacbeSJulio Merino
191a18eacbeSJulio Merino ATF_TEST_CASE(auto_array_reset);
ATF_TEST_CASE_HEAD(auto_array_reset)192a18eacbeSJulio Merino ATF_TEST_CASE_HEAD(auto_array_reset)
193a18eacbeSJulio Merino {
194a18eacbeSJulio Merino set_md_var("descr", "Tests the auto_array smart pointer class' reset "
195a18eacbeSJulio Merino "method");
196a18eacbeSJulio Merino }
ATF_TEST_CASE_BODY(auto_array_reset)197a18eacbeSJulio Merino ATF_TEST_CASE_BODY(auto_array_reset)
198a18eacbeSJulio Merino {
199a18eacbeSJulio Merino using atf::auto_array;
200a18eacbeSJulio Merino
201a18eacbeSJulio Merino test_array* ta1 = new test_array[10];
202a18eacbeSJulio Merino test_array* ta2 = new test_array[10];
203a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 2);
204a18eacbeSJulio Merino
205a18eacbeSJulio Merino {
206a18eacbeSJulio Merino auto_array< test_array > t(ta1);
207a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 2);
208a18eacbeSJulio Merino t.reset(ta2);
209a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
210a18eacbeSJulio Merino t.reset();
211a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
212a18eacbeSJulio Merino }
213a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
214a18eacbeSJulio Merino }
215a18eacbeSJulio Merino
216a18eacbeSJulio Merino ATF_TEST_CASE(auto_array_assign);
ATF_TEST_CASE_HEAD(auto_array_assign)217a18eacbeSJulio Merino ATF_TEST_CASE_HEAD(auto_array_assign)
218a18eacbeSJulio Merino {
219a18eacbeSJulio Merino set_md_var("descr", "Tests the auto_array smart pointer class' "
220a18eacbeSJulio Merino "assignment operator");
221a18eacbeSJulio Merino }
ATF_TEST_CASE_BODY(auto_array_assign)222a18eacbeSJulio Merino ATF_TEST_CASE_BODY(auto_array_assign)
223a18eacbeSJulio Merino {
224a18eacbeSJulio Merino using atf::auto_array;
225a18eacbeSJulio Merino
226a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
227a18eacbeSJulio Merino {
228a18eacbeSJulio Merino auto_array< test_array > t1(new test_array[10]);
229a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
230a18eacbeSJulio Merino
231a18eacbeSJulio Merino {
232a18eacbeSJulio Merino auto_array< test_array > t2;
233a18eacbeSJulio Merino t2 = t1;
234a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
235a18eacbeSJulio Merino }
236a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
237a18eacbeSJulio Merino }
238a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
239a18eacbeSJulio Merino }
240a18eacbeSJulio Merino
241a18eacbeSJulio Merino ATF_TEST_CASE(auto_array_assign_ref);
ATF_TEST_CASE_HEAD(auto_array_assign_ref)242a18eacbeSJulio Merino ATF_TEST_CASE_HEAD(auto_array_assign_ref)
243a18eacbeSJulio Merino {
244a18eacbeSJulio Merino set_md_var("descr", "Tests the auto_array smart pointer class' "
245a18eacbeSJulio Merino "assignment operator through the auxiliary auto_array_ref "
246a18eacbeSJulio Merino "object");
247a18eacbeSJulio Merino }
ATF_TEST_CASE_BODY(auto_array_assign_ref)248a18eacbeSJulio Merino ATF_TEST_CASE_BODY(auto_array_assign_ref)
249a18eacbeSJulio Merino {
250a18eacbeSJulio Merino using atf::auto_array;
251a18eacbeSJulio Merino
252a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
253a18eacbeSJulio Merino {
254a18eacbeSJulio Merino auto_array< test_array > t1(new test_array[10]);
255a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
256a18eacbeSJulio Merino
257a18eacbeSJulio Merino {
258a18eacbeSJulio Merino auto_array< test_array > t2;
259a18eacbeSJulio Merino t2 = test_array::do_copy(t1);
260a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
261a18eacbeSJulio Merino }
262a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
263a18eacbeSJulio Merino }
264a18eacbeSJulio Merino ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
265a18eacbeSJulio Merino }
266a18eacbeSJulio Merino
267a18eacbeSJulio Merino ATF_TEST_CASE(auto_array_access);
ATF_TEST_CASE_HEAD(auto_array_access)268a18eacbeSJulio Merino ATF_TEST_CASE_HEAD(auto_array_access)
269a18eacbeSJulio Merino {
270a18eacbeSJulio Merino set_md_var("descr", "Tests the auto_array smart pointer class' access "
271a18eacbeSJulio Merino "operator");
272a18eacbeSJulio Merino }
ATF_TEST_CASE_BODY(auto_array_access)273a18eacbeSJulio Merino ATF_TEST_CASE_BODY(auto_array_access)
274a18eacbeSJulio Merino {
275a18eacbeSJulio Merino using atf::auto_array;
276a18eacbeSJulio Merino
277a18eacbeSJulio Merino auto_array< test_array > t(new test_array[10]);
278a18eacbeSJulio Merino
279a18eacbeSJulio Merino for (int i = 0; i < 10; i++)
280a18eacbeSJulio Merino t[i].m_value = i * 2;
281a18eacbeSJulio Merino
282a18eacbeSJulio Merino for (int i = 0; i < 10; i++)
283a18eacbeSJulio Merino ATF_REQUIRE_EQ(t[i].m_value, i * 2);
284a18eacbeSJulio Merino }
285a18eacbeSJulio Merino
286a18eacbeSJulio Merino // ------------------------------------------------------------------------
287a18eacbeSJulio Merino // Main.
288a18eacbeSJulio Merino // ------------------------------------------------------------------------
289a18eacbeSJulio Merino
ATF_INIT_TEST_CASES(tcs)290a18eacbeSJulio Merino ATF_INIT_TEST_CASES(tcs)
291a18eacbeSJulio Merino {
292a18eacbeSJulio Merino // Add the test for the "auto_array" class.
293a18eacbeSJulio Merino ATF_ADD_TEST_CASE(tcs, auto_array_scope);
294a18eacbeSJulio Merino ATF_ADD_TEST_CASE(tcs, auto_array_copy);
295a18eacbeSJulio Merino ATF_ADD_TEST_CASE(tcs, auto_array_copy_ref);
296a18eacbeSJulio Merino ATF_ADD_TEST_CASE(tcs, auto_array_get);
297a18eacbeSJulio Merino ATF_ADD_TEST_CASE(tcs, auto_array_release);
298a18eacbeSJulio Merino ATF_ADD_TEST_CASE(tcs, auto_array_reset);
299a18eacbeSJulio Merino ATF_ADD_TEST_CASE(tcs, auto_array_assign);
300a18eacbeSJulio Merino ATF_ADD_TEST_CASE(tcs, auto_array_assign_ref);
301a18eacbeSJulio Merino ATF_ADD_TEST_CASE(tcs, auto_array_access);
302a18eacbeSJulio Merino }
303