xref: /minix3/external/bsd/kyua-cli/dist/utils/auto_array_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "utils/auto_array.ipp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <sys/types.h>
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <iostream>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include <atf-c++.hpp>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include "utils/defs.hpp"
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc using utils::auto_array;
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc namespace {
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc /// Mock class to capture calls to the new and delete operators.
48*11be35a1SLionel Sambuc class test_array {
49*11be35a1SLionel Sambuc public:
50*11be35a1SLionel Sambuc     /// User-settable cookie to disambiguate instances of this class.
51*11be35a1SLionel Sambuc     int m_value;
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc     /// The current balance of existing test_array instances.
54*11be35a1SLionel Sambuc     static ssize_t m_nblocks;
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc     /// Captures invalid calls to new on an array.
57*11be35a1SLionel Sambuc     ///
58*11be35a1SLionel Sambuc     /// \param unused_size The amount of memory to allocate, in bytes.
59*11be35a1SLionel Sambuc     ///
60*11be35a1SLionel Sambuc     /// \return Nothing; this always fails the test case.
61*11be35a1SLionel Sambuc     void*
operator new(const size_t UTILS_UNUSED_PARAM (size))62*11be35a1SLionel Sambuc     operator new(const size_t UTILS_UNUSED_PARAM(size))
63*11be35a1SLionel Sambuc     {
64*11be35a1SLionel Sambuc         ATF_FAIL("New called but should have been new[]");
65*11be35a1SLionel Sambuc         return new int(5);
66*11be35a1SLionel Sambuc     }
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc     /// Obtains memory for a new instance and increments m_nblocks.
69*11be35a1SLionel Sambuc     ///
70*11be35a1SLionel Sambuc     /// \param size The amount of memory to allocate, in bytes.
71*11be35a1SLionel Sambuc     ///
72*11be35a1SLionel Sambuc     /// \return A pointer to the allocated memory.
73*11be35a1SLionel Sambuc     ///
74*11be35a1SLionel Sambuc     /// \throw std::bad_alloc If the memory cannot be allocated.
75*11be35a1SLionel Sambuc     void*
operator new[](const size_t size)76*11be35a1SLionel Sambuc     operator new[](const size_t size)
77*11be35a1SLionel Sambuc     {
78*11be35a1SLionel Sambuc         void* mem = ::operator new(size);
79*11be35a1SLionel Sambuc         m_nblocks++;
80*11be35a1SLionel Sambuc         std::cout << "Allocated 'test_array' object " << mem << "\n";
81*11be35a1SLionel Sambuc         return mem;
82*11be35a1SLionel Sambuc     }
83*11be35a1SLionel Sambuc 
84*11be35a1SLionel Sambuc     /// Captures invalid calls to delete on an array.
85*11be35a1SLionel Sambuc     ///
86*11be35a1SLionel Sambuc     /// \param unused_mem The pointer to the memory to be deleted.
87*11be35a1SLionel Sambuc     ///
88*11be35a1SLionel Sambuc     /// \return Nothing; this always fails the test case.
89*11be35a1SLionel Sambuc     void
operator delete(void * UTILS_UNUSED_PARAM (mem))90*11be35a1SLionel Sambuc     operator delete(void* UTILS_UNUSED_PARAM(mem))
91*11be35a1SLionel Sambuc     {
92*11be35a1SLionel Sambuc         ATF_FAIL("Delete called but should have been delete[]");
93*11be35a1SLionel Sambuc     }
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc     /// Deletes a previously allocated array and decrements m_nblocks.
96*11be35a1SLionel Sambuc     ///
97*11be35a1SLionel Sambuc     /// \param mem The pointer to the memory to be deleted.
98*11be35a1SLionel Sambuc     void
operator delete[](void * mem)99*11be35a1SLionel Sambuc     operator delete[](void* mem)
100*11be35a1SLionel Sambuc     {
101*11be35a1SLionel Sambuc         std::cout << "Releasing 'test_array' object " << mem << "\n";
102*11be35a1SLionel Sambuc         if (m_nblocks == 0)
103*11be35a1SLionel Sambuc             ATF_FAIL("Unbalanced delete[]");
104*11be35a1SLionel Sambuc         m_nblocks--;
105*11be35a1SLionel Sambuc         ::operator delete(mem);
106*11be35a1SLionel Sambuc     }
107*11be35a1SLionel Sambuc };
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc 
110*11be35a1SLionel Sambuc ssize_t test_array::m_nblocks = 0;
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc }  // anonymous namespace
114*11be35a1SLionel Sambuc 
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc ATF_TEST_CASE(scope);
ATF_TEST_CASE_HEAD(scope)117*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(scope)
118*11be35a1SLionel Sambuc {
119*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the automatic scope handling in the "
120*11be35a1SLionel Sambuc                "auto_array smart pointer class");
121*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(scope)122*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(scope)
123*11be35a1SLionel Sambuc {
124*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
125*11be35a1SLionel Sambuc     {
126*11be35a1SLionel Sambuc         auto_array< test_array > t(new test_array[10]);
127*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
128*11be35a1SLionel Sambuc     }
129*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
130*11be35a1SLionel Sambuc }
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc 
133*11be35a1SLionel Sambuc ATF_TEST_CASE(copy);
ATF_TEST_CASE_HEAD(copy)134*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(copy)
135*11be35a1SLionel Sambuc {
136*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the auto_array smart pointer class' copy "
137*11be35a1SLionel Sambuc                "constructor");
138*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(copy)139*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(copy)
140*11be35a1SLionel Sambuc {
141*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
142*11be35a1SLionel Sambuc     {
143*11be35a1SLionel Sambuc         auto_array< test_array > t1(new test_array[10]);
144*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc         {
147*11be35a1SLionel Sambuc             auto_array< test_array > t2(t1);
148*11be35a1SLionel Sambuc             ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
149*11be35a1SLionel Sambuc         }
150*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
151*11be35a1SLionel Sambuc     }
152*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
153*11be35a1SLionel Sambuc }
154*11be35a1SLionel Sambuc 
155*11be35a1SLionel Sambuc 
156*11be35a1SLionel Sambuc ATF_TEST_CASE(copy_ref);
ATF_TEST_CASE_HEAD(copy_ref)157*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(copy_ref)
158*11be35a1SLionel Sambuc {
159*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the auto_array smart pointer class' copy "
160*11be35a1SLionel Sambuc                "constructor through the auxiliary ref object");
161*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(copy_ref)162*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(copy_ref)
163*11be35a1SLionel Sambuc {
164*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
165*11be35a1SLionel Sambuc     {
166*11be35a1SLionel Sambuc         auto_array< test_array > t1(new test_array[10]);
167*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc         {
170*11be35a1SLionel Sambuc             auto_array< test_array > t2 = t1;
171*11be35a1SLionel Sambuc             ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
172*11be35a1SLionel Sambuc         }
173*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
174*11be35a1SLionel Sambuc     }
175*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
176*11be35a1SLionel Sambuc }
177*11be35a1SLionel Sambuc 
178*11be35a1SLionel Sambuc 
179*11be35a1SLionel Sambuc ATF_TEST_CASE(get);
ATF_TEST_CASE_HEAD(get)180*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(get)
181*11be35a1SLionel Sambuc {
182*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the auto_array smart pointer class' get "
183*11be35a1SLionel Sambuc                "method");
184*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(get)185*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get)
186*11be35a1SLionel Sambuc {
187*11be35a1SLionel Sambuc     test_array* ta = new test_array[10];
188*11be35a1SLionel Sambuc     auto_array< test_array > t(ta);
189*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(t.get(), ta);
190*11be35a1SLionel Sambuc }
191*11be35a1SLionel Sambuc 
192*11be35a1SLionel Sambuc 
193*11be35a1SLionel Sambuc ATF_TEST_CASE(release);
ATF_TEST_CASE_HEAD(release)194*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(release)
195*11be35a1SLionel Sambuc {
196*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the auto_array smart pointer class' release "
197*11be35a1SLionel Sambuc                "method");
198*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(release)199*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(release)
200*11be35a1SLionel Sambuc {
201*11be35a1SLionel Sambuc     test_array* ta1 = new test_array[10];
202*11be35a1SLionel Sambuc     {
203*11be35a1SLionel Sambuc         auto_array< test_array > t(ta1);
204*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
205*11be35a1SLionel Sambuc         test_array* ta2 = t.release();
206*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(ta2, ta1);
207*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
208*11be35a1SLionel Sambuc     }
209*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
210*11be35a1SLionel Sambuc     delete [] ta1;
211*11be35a1SLionel Sambuc }
212*11be35a1SLionel Sambuc 
213*11be35a1SLionel Sambuc 
214*11be35a1SLionel Sambuc ATF_TEST_CASE(reset);
ATF_TEST_CASE_HEAD(reset)215*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(reset)
216*11be35a1SLionel Sambuc {
217*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the auto_array smart pointer class' reset "
218*11be35a1SLionel Sambuc                "method");
219*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(reset)220*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(reset)
221*11be35a1SLionel Sambuc {
222*11be35a1SLionel Sambuc     test_array* ta1 = new test_array[10];
223*11be35a1SLionel Sambuc     test_array* ta2 = new test_array[10];
224*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 2);
225*11be35a1SLionel Sambuc 
226*11be35a1SLionel Sambuc     {
227*11be35a1SLionel Sambuc         auto_array< test_array > t(ta1);
228*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 2);
229*11be35a1SLionel Sambuc         t.reset(ta2);
230*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
231*11be35a1SLionel Sambuc         t.reset();
232*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
233*11be35a1SLionel Sambuc     }
234*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
235*11be35a1SLionel Sambuc }
236*11be35a1SLionel Sambuc 
237*11be35a1SLionel Sambuc 
238*11be35a1SLionel Sambuc ATF_TEST_CASE(assign);
ATF_TEST_CASE_HEAD(assign)239*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(assign)
240*11be35a1SLionel Sambuc {
241*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the auto_array smart pointer class' "
242*11be35a1SLionel Sambuc                "assignment operator");
243*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(assign)244*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(assign)
245*11be35a1SLionel Sambuc {
246*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
247*11be35a1SLionel Sambuc     {
248*11be35a1SLionel Sambuc         auto_array< test_array > t1(new test_array[10]);
249*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
250*11be35a1SLionel Sambuc 
251*11be35a1SLionel Sambuc         {
252*11be35a1SLionel Sambuc             auto_array< test_array > t2;
253*11be35a1SLionel Sambuc             t2 = t1;
254*11be35a1SLionel Sambuc             ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
255*11be35a1SLionel Sambuc         }
256*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
257*11be35a1SLionel Sambuc     }
258*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
259*11be35a1SLionel Sambuc }
260*11be35a1SLionel Sambuc 
261*11be35a1SLionel Sambuc 
262*11be35a1SLionel Sambuc ATF_TEST_CASE(assign_ref);
ATF_TEST_CASE_HEAD(assign_ref)263*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(assign_ref)
264*11be35a1SLionel Sambuc {
265*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the auto_array smart pointer class' "
266*11be35a1SLionel Sambuc                "assignment operator through the auxiliary ref "
267*11be35a1SLionel Sambuc                "object");
268*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(assign_ref)269*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(assign_ref)
270*11be35a1SLionel Sambuc {
271*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
272*11be35a1SLionel Sambuc     {
273*11be35a1SLionel Sambuc         auto_array< test_array > t1(new test_array[10]);
274*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
275*11be35a1SLionel Sambuc 
276*11be35a1SLionel Sambuc         {
277*11be35a1SLionel Sambuc             auto_array< test_array > t2;
278*11be35a1SLionel Sambuc             t2 = t1;
279*11be35a1SLionel Sambuc             ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
280*11be35a1SLionel Sambuc         }
281*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
282*11be35a1SLionel Sambuc     }
283*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
284*11be35a1SLionel Sambuc }
285*11be35a1SLionel Sambuc 
286*11be35a1SLionel Sambuc 
287*11be35a1SLionel Sambuc ATF_TEST_CASE(access);
ATF_TEST_CASE_HEAD(access)288*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(access)
289*11be35a1SLionel Sambuc {
290*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the auto_array smart pointer class' access "
291*11be35a1SLionel Sambuc                "operator");
292*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(access)293*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(access)
294*11be35a1SLionel Sambuc {
295*11be35a1SLionel Sambuc     auto_array< test_array > t(new test_array[10]);
296*11be35a1SLionel Sambuc 
297*11be35a1SLionel Sambuc     for (int i = 0; i < 10; i++)
298*11be35a1SLionel Sambuc         t[i].m_value = i * 2;
299*11be35a1SLionel Sambuc 
300*11be35a1SLionel Sambuc     for (int i = 0; i < 10; i++)
301*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(t[i].m_value, i * 2);
302*11be35a1SLionel Sambuc }
303*11be35a1SLionel Sambuc 
304*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)305*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
306*11be35a1SLionel Sambuc {
307*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, scope);
308*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, copy);
309*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, copy_ref);
310*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get);
311*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, release);
312*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, reset);
313*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, assign);
314*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, assign_ref);
315*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, access);
316*11be35a1SLionel Sambuc }
317