tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / atf / dist / atf-c++ / detail / auto_array_test.cpp
blobdcfe41551ce4ee0754846f7fc43f11fa2e7dded0
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 extern "C" {
31 #include <sys/types.h>
34 #include <iostream>
36 #include "atf-c/defs.h"
38 #include "../macros.hpp"
40 #include "auto_array.hpp"
42 // ------------------------------------------------------------------------
43 // Tests for the "auto_array" class.
44 // ------------------------------------------------------------------------
46 class test_array {
47 public:
48 int m_value;
50 static ssize_t m_nblocks;
52 static
53 atf::auto_array< test_array >
54 do_copy(atf::auto_array< test_array >& ta)
56 return atf::auto_array< test_array >(ta);
59 void* operator new(size_t size ATF_DEFS_ATTRIBUTE_UNUSED)
61 ATF_FAIL("New called but should have been new[]");
62 return new int(5);
65 void* operator new[](size_t size)
67 m_nblocks++;
68 void* mem = ::operator new(size);
69 std::cout << "Allocated 'test_array' object " << mem << "\n";
70 return mem;
73 void operator delete(void* mem ATF_DEFS_ATTRIBUTE_UNUSED)
75 ATF_FAIL("Delete called but should have been delete[]");
78 void operator delete[](void* mem)
80 std::cout << "Releasing 'test_array' object " << mem << "\n";
81 if (m_nblocks == 0)
82 ATF_FAIL("Unbalanced delete[]");
83 m_nblocks--;
84 ::operator delete(mem);
88 ssize_t test_array::m_nblocks = 0;
90 ATF_TEST_CASE(auto_array_scope);
91 ATF_TEST_CASE_HEAD(auto_array_scope)
93 set_md_var("descr", "Tests the automatic scope handling in the "
94 "auto_array smart pointer class");
96 ATF_TEST_CASE_BODY(auto_array_scope)
98 using atf::auto_array;
100 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
102 auto_array< test_array > t(new test_array[10]);
103 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
105 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
108 ATF_TEST_CASE(auto_array_copy);
109 ATF_TEST_CASE_HEAD(auto_array_copy)
111 set_md_var("descr", "Tests the auto_array smart pointer class' copy "
112 "constructor");
114 ATF_TEST_CASE_BODY(auto_array_copy)
116 using atf::auto_array;
118 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
120 auto_array< test_array > t1(new test_array[10]);
121 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
124 auto_array< test_array > t2(t1);
125 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
127 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
129 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
132 ATF_TEST_CASE(auto_array_copy_ref);
133 ATF_TEST_CASE_HEAD(auto_array_copy_ref)
135 set_md_var("descr", "Tests the auto_array smart pointer class' copy "
136 "constructor through the auxiliary auto_array_ref object");
138 ATF_TEST_CASE_BODY(auto_array_copy_ref)
140 using atf::auto_array;
142 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
144 auto_array< test_array > t1(new test_array[10]);
145 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
148 auto_array< test_array > t2 = test_array::do_copy(t1);
149 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
151 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
153 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
156 ATF_TEST_CASE(auto_array_get);
157 ATF_TEST_CASE_HEAD(auto_array_get)
159 set_md_var("descr", "Tests the auto_array smart pointer class' get "
160 "method");
162 ATF_TEST_CASE_BODY(auto_array_get)
164 using atf::auto_array;
166 test_array* ta = new test_array[10];
167 auto_array< test_array > t(ta);
168 ATF_REQUIRE_EQ(t.get(), ta);
171 ATF_TEST_CASE(auto_array_release);
172 ATF_TEST_CASE_HEAD(auto_array_release)
174 set_md_var("descr", "Tests the auto_array smart pointer class' release "
175 "method");
177 ATF_TEST_CASE_BODY(auto_array_release)
179 using atf::auto_array;
181 test_array* ta1 = new test_array[10];
183 auto_array< test_array > t(ta1);
184 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
185 test_array* ta2 = t.release();
186 ATF_REQUIRE_EQ(ta2, ta1);
187 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
189 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
190 delete [] ta1;
193 ATF_TEST_CASE(auto_array_reset);
194 ATF_TEST_CASE_HEAD(auto_array_reset)
196 set_md_var("descr", "Tests the auto_array smart pointer class' reset "
197 "method");
199 ATF_TEST_CASE_BODY(auto_array_reset)
201 using atf::auto_array;
203 test_array* ta1 = new test_array[10];
204 test_array* ta2 = new test_array[10];
205 ATF_REQUIRE_EQ(test_array::m_nblocks, 2);
208 auto_array< test_array > t(ta1);
209 ATF_REQUIRE_EQ(test_array::m_nblocks, 2);
210 t.reset(ta2);
211 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
212 t.reset();
213 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
215 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
218 ATF_TEST_CASE(auto_array_assign);
219 ATF_TEST_CASE_HEAD(auto_array_assign)
221 set_md_var("descr", "Tests the auto_array smart pointer class' "
222 "assignment operator");
224 ATF_TEST_CASE_BODY(auto_array_assign)
226 using atf::auto_array;
228 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
230 auto_array< test_array > t1(new test_array[10]);
231 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
234 auto_array< test_array > t2;
235 t2 = t1;
236 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
238 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
240 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
243 ATF_TEST_CASE(auto_array_assign_ref);
244 ATF_TEST_CASE_HEAD(auto_array_assign_ref)
246 set_md_var("descr", "Tests the auto_array smart pointer class' "
247 "assignment operator through the auxiliary auto_array_ref "
248 "object");
250 ATF_TEST_CASE_BODY(auto_array_assign_ref)
252 using atf::auto_array;
254 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
256 auto_array< test_array > t1(new test_array[10]);
257 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
260 auto_array< test_array > t2;
261 t2 = test_array::do_copy(t1);
262 ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
264 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
266 ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
269 ATF_TEST_CASE(auto_array_access);
270 ATF_TEST_CASE_HEAD(auto_array_access)
272 set_md_var("descr", "Tests the auto_array smart pointer class' access "
273 "operator");
275 ATF_TEST_CASE_BODY(auto_array_access)
277 using atf::auto_array;
279 auto_array< test_array > t(new test_array[10]);
281 for (int i = 0; i < 10; i++)
282 t[i].m_value = i * 2;
284 for (int i = 0; i < 10; i++)
285 ATF_REQUIRE_EQ(t[i].m_value, i * 2);
288 // ------------------------------------------------------------------------
289 // Main.
290 // ------------------------------------------------------------------------
292 ATF_INIT_TEST_CASES(tcs)
294 // Add the test for the "auto_array" class.
295 ATF_ADD_TEST_CASE(tcs, auto_array_scope);
296 ATF_ADD_TEST_CASE(tcs, auto_array_copy);
297 ATF_ADD_TEST_CASE(tcs, auto_array_copy_ref);
298 ATF_ADD_TEST_CASE(tcs, auto_array_get);
299 ATF_ADD_TEST_CASE(tcs, auto_array_release);
300 ATF_ADD_TEST_CASE(tcs, auto_array_reset);
301 ATF_ADD_TEST_CASE(tcs, auto_array_assign);
302 ATF_ADD_TEST_CASE(tcs, auto_array_assign_ref);
303 ATF_ADD_TEST_CASE(tcs, auto_array_access);