2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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.
31 #include <sys/types.h>
36 #include <atf-c++.hpp>
38 #include "auto_array.hpp"
40 // ------------------------------------------------------------------------
41 // Tests for the "auto_array" class.
42 // ------------------------------------------------------------------------
48 static ssize_t m_nblocks
;
51 tools::auto_array
< test_array
>
52 do_copy(tools::auto_array
< test_array
>& ta
)
54 return tools::auto_array
< test_array
>(ta
);
57 void* operator new(size_t size
__attribute__((__unused__
)))
59 ATF_FAIL("New called but should have been new[]");
63 void* operator new[](size_t size
)
66 void* mem
= ::operator new(size
);
67 std::cout
<< "Allocated 'test_array' object " << mem
<< "\n";
71 void operator delete(void* mem
__attribute__((__unused__
)))
73 ATF_FAIL("Delete called but should have been delete[]");
76 void operator delete[](void* mem
)
78 std::cout
<< "Releasing 'test_array' object " << mem
<< "\n";
80 ATF_FAIL("Unbalanced delete[]");
82 ::operator delete(mem
);
86 ssize_t
test_array::m_nblocks
= 0;
88 ATF_TEST_CASE(auto_array_scope
);
89 ATF_TEST_CASE_HEAD(auto_array_scope
)
91 set_md_var("descr", "Tests the automatic scope handling in the "
92 "auto_array smart pointer class");
94 ATF_TEST_CASE_BODY(auto_array_scope
)
96 using tools::auto_array
;
98 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
100 auto_array
< test_array
> t(new test_array
[10]);
101 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
103 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
106 ATF_TEST_CASE(auto_array_copy
);
107 ATF_TEST_CASE_HEAD(auto_array_copy
)
109 set_md_var("descr", "Tests the auto_array smart pointer class' copy "
112 ATF_TEST_CASE_BODY(auto_array_copy
)
114 using tools::auto_array
;
116 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
118 auto_array
< test_array
> t1(new test_array
[10]);
119 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
122 auto_array
< test_array
> t2(t1
);
123 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
125 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
127 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
130 ATF_TEST_CASE(auto_array_copy_ref
);
131 ATF_TEST_CASE_HEAD(auto_array_copy_ref
)
133 set_md_var("descr", "Tests the auto_array smart pointer class' copy "
134 "constructor through the auxiliary auto_array_ref object");
136 ATF_TEST_CASE_BODY(auto_array_copy_ref
)
138 using tools::auto_array
;
140 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
142 auto_array
< test_array
> t1(new test_array
[10]);
143 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
146 auto_array
< test_array
> t2
= test_array::do_copy(t1
);
147 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
149 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
151 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
154 ATF_TEST_CASE(auto_array_get
);
155 ATF_TEST_CASE_HEAD(auto_array_get
)
157 set_md_var("descr", "Tests the auto_array smart pointer class' get "
160 ATF_TEST_CASE_BODY(auto_array_get
)
162 using tools::auto_array
;
164 test_array
* ta
= new test_array
[10];
165 auto_array
< test_array
> t(ta
);
166 ATF_REQUIRE_EQ(t
.get(), ta
);
169 ATF_TEST_CASE(auto_array_release
);
170 ATF_TEST_CASE_HEAD(auto_array_release
)
172 set_md_var("descr", "Tests the auto_array smart pointer class' release "
175 ATF_TEST_CASE_BODY(auto_array_release
)
177 using tools::auto_array
;
179 test_array
* ta1
= new test_array
[10];
181 auto_array
< test_array
> t(ta1
);
182 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
183 test_array
* ta2
= t
.release();
184 ATF_REQUIRE_EQ(ta2
, ta1
);
185 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
187 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
191 ATF_TEST_CASE(auto_array_reset
);
192 ATF_TEST_CASE_HEAD(auto_array_reset
)
194 set_md_var("descr", "Tests the auto_array smart pointer class' reset "
197 ATF_TEST_CASE_BODY(auto_array_reset
)
199 using tools::auto_array
;
201 test_array
* ta1
= new test_array
[10];
202 test_array
* ta2
= new test_array
[10];
203 ATF_REQUIRE_EQ(test_array::m_nblocks
, 2);
206 auto_array
< test_array
> t(ta1
);
207 ATF_REQUIRE_EQ(test_array::m_nblocks
, 2);
209 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
211 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
213 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
216 ATF_TEST_CASE(auto_array_assign
);
217 ATF_TEST_CASE_HEAD(auto_array_assign
)
219 set_md_var("descr", "Tests the auto_array smart pointer class' "
220 "assignment operator");
222 ATF_TEST_CASE_BODY(auto_array_assign
)
224 using tools::auto_array
;
226 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
228 auto_array
< test_array
> t1(new test_array
[10]);
229 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
232 auto_array
< test_array
> t2
;
234 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
236 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
238 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
241 ATF_TEST_CASE(auto_array_assign_ref
);
242 ATF_TEST_CASE_HEAD(auto_array_assign_ref
)
244 set_md_var("descr", "Tests the auto_array smart pointer class' "
245 "assignment operator through the auxiliary auto_array_ref "
248 ATF_TEST_CASE_BODY(auto_array_assign_ref
)
250 using tools::auto_array
;
252 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
254 auto_array
< test_array
> t1(new test_array
[10]);
255 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
258 auto_array
< test_array
> t2
;
259 t2
= test_array::do_copy(t1
);
260 ATF_REQUIRE_EQ(test_array::m_nblocks
, 1);
262 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
264 ATF_REQUIRE_EQ(test_array::m_nblocks
, 0);
267 ATF_TEST_CASE(auto_array_access
);
268 ATF_TEST_CASE_HEAD(auto_array_access
)
270 set_md_var("descr", "Tests the auto_array smart pointer class' access "
273 ATF_TEST_CASE_BODY(auto_array_access
)
275 using tools::auto_array
;
277 auto_array
< test_array
> t(new test_array
[10]);
279 for (int i
= 0; i
< 10; i
++)
280 t
[i
].m_value
= i
* 2;
282 for (int i
= 0; i
< 10; i
++)
283 ATF_REQUIRE_EQ(t
[i
].m_value
, i
* 2);
286 // ------------------------------------------------------------------------
288 // ------------------------------------------------------------------------
290 ATF_INIT_TEST_CASES(tcs
)
292 // Add the test for the "auto_array" class.
293 ATF_ADD_TEST_CASE(tcs
, auto_array_scope
);
294 ATF_ADD_TEST_CASE(tcs
, auto_array_copy
);
295 ATF_ADD_TEST_CASE(tcs
, auto_array_copy_ref
);
296 ATF_ADD_TEST_CASE(tcs
, auto_array_get
);
297 ATF_ADD_TEST_CASE(tcs
, auto_array_release
);
298 ATF_ADD_TEST_CASE(tcs
, auto_array_reset
);
299 ATF_ADD_TEST_CASE(tcs
, auto_array_assign
);
300 ATF_ADD_TEST_CASE(tcs
, auto_array_assign_ref
);
301 ATF_ADD_TEST_CASE(tcs
, auto_array_access
);