]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/atf/atf-c++/utils_test.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / atf / atf-c++ / utils_test.cpp
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.
15 //
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.
28 //
29
30 #include <iostream>
31
32 #include "atf-c/defs.h"
33
34 #include "macros.hpp"
35 #include "utils.hpp"
36
37 #include "detail/test_helpers.hpp"
38
39 // ------------------------------------------------------------------------
40 // Tests for the "auto_array" class.
41 // ------------------------------------------------------------------------
42
43 class test_array {
44 public:
45     int m_value;
46
47     static ssize_t m_nblocks;
48
49     static
50     atf::utils::auto_array< test_array >
51     do_copy(atf::utils::auto_array< test_array >& ta)
52     {
53         return atf::utils::auto_array< test_array >(ta);
54     }
55
56     void* operator new(size_t size ATF_DEFS_ATTRIBUTE_UNUSED)
57     {
58         ATF_FAIL("New called but should have been new[]");
59         return new int(5);
60     }
61
62     void* operator new[](size_t size)
63     {
64         m_nblocks++;
65         void* mem = ::operator new(size);
66         std::cout << "Allocated 'test_array' object " << mem << "\n";
67         return mem;
68     }
69
70     void operator delete(void* mem ATF_DEFS_ATTRIBUTE_UNUSED)
71     {
72         ATF_FAIL("Delete called but should have been delete[]");
73     }
74
75     void operator delete[](void* mem)
76     {
77         std::cout << "Releasing 'test_array' object " << mem << "\n";
78         if (m_nblocks == 0)
79             ATF_FAIL("Unbalanced delete[]");
80         m_nblocks--;
81         ::operator delete(mem);
82     }
83 };
84
85 ssize_t test_array::m_nblocks = 0;
86
87 ATF_TEST_CASE(auto_array_scope);
88 ATF_TEST_CASE_HEAD(auto_array_scope)
89 {
90     set_md_var("descr", "Tests the automatic scope handling in the "
91                "auto_array smart pointer class");
92 }
93 ATF_TEST_CASE_BODY(auto_array_scope)
94 {
95     using atf::utils::auto_array;
96
97     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
98     {
99         auto_array< test_array > t(new test_array[10]);
100         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
101     }
102     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
103 }
104
105 ATF_TEST_CASE(auto_array_copy);
106 ATF_TEST_CASE_HEAD(auto_array_copy)
107 {
108     set_md_var("descr", "Tests the auto_array smart pointer class' copy "
109                "constructor");
110 }
111 ATF_TEST_CASE_BODY(auto_array_copy)
112 {
113     using atf::utils::auto_array;
114
115     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
116     {
117         auto_array< test_array > t1(new test_array[10]);
118         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
119
120         {
121             auto_array< test_array > t2(t1);
122             ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
123         }
124         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
125     }
126     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
127 }
128
129 ATF_TEST_CASE(auto_array_copy_ref);
130 ATF_TEST_CASE_HEAD(auto_array_copy_ref)
131 {
132     set_md_var("descr", "Tests the auto_array smart pointer class' copy "
133                "constructor through the auxiliary auto_array_ref object");
134 }
135 ATF_TEST_CASE_BODY(auto_array_copy_ref)
136 {
137     using atf::utils::auto_array;
138
139     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
140     {
141         auto_array< test_array > t1(new test_array[10]);
142         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
143
144         {
145             auto_array< test_array > t2 = test_array::do_copy(t1);
146             ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
147         }
148         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
149     }
150     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
151 }
152
153 ATF_TEST_CASE(auto_array_get);
154 ATF_TEST_CASE_HEAD(auto_array_get)
155 {
156     set_md_var("descr", "Tests the auto_array smart pointer class' get "
157                "method");
158 }
159 ATF_TEST_CASE_BODY(auto_array_get)
160 {
161     using atf::utils::auto_array;
162
163     test_array* ta = new test_array[10];
164     auto_array< test_array > t(ta);
165     ATF_REQUIRE_EQ(t.get(), ta);
166 }
167
168 ATF_TEST_CASE(auto_array_release);
169 ATF_TEST_CASE_HEAD(auto_array_release)
170 {
171     set_md_var("descr", "Tests the auto_array smart pointer class' release "
172                "method");
173 }
174 ATF_TEST_CASE_BODY(auto_array_release)
175 {
176     using atf::utils::auto_array;
177
178     test_array* ta1 = new test_array[10];
179     {
180         auto_array< test_array > t(ta1);
181         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
182         test_array* ta2 = t.release();
183         ATF_REQUIRE_EQ(ta2, ta1);
184         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
185     }
186     ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
187     delete [] ta1;
188 }
189
190 ATF_TEST_CASE(auto_array_reset);
191 ATF_TEST_CASE_HEAD(auto_array_reset)
192 {
193     set_md_var("descr", "Tests the auto_array smart pointer class' reset "
194                "method");
195 }
196 ATF_TEST_CASE_BODY(auto_array_reset)
197 {
198     using atf::utils::auto_array;
199
200     test_array* ta1 = new test_array[10];
201     test_array* ta2 = new test_array[10];
202     ATF_REQUIRE_EQ(test_array::m_nblocks, 2);
203
204     {
205         auto_array< test_array > t(ta1);
206         ATF_REQUIRE_EQ(test_array::m_nblocks, 2);
207         t.reset(ta2);
208         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
209         t.reset();
210         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
211     }
212     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
213 }
214
215 ATF_TEST_CASE(auto_array_assign);
216 ATF_TEST_CASE_HEAD(auto_array_assign)
217 {
218     set_md_var("descr", "Tests the auto_array smart pointer class' "
219                "assignment operator");
220 }
221 ATF_TEST_CASE_BODY(auto_array_assign)
222 {
223     using atf::utils::auto_array;
224
225     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
226     {
227         auto_array< test_array > t1(new test_array[10]);
228         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
229
230         {
231             auto_array< test_array > t2;
232             t2 = t1;
233             ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
234         }
235         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
236     }
237     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
238 }
239
240 ATF_TEST_CASE(auto_array_assign_ref);
241 ATF_TEST_CASE_HEAD(auto_array_assign_ref)
242 {
243     set_md_var("descr", "Tests the auto_array smart pointer class' "
244                "assignment operator through the auxiliary auto_array_ref "
245                "object");
246 }
247 ATF_TEST_CASE_BODY(auto_array_assign_ref)
248 {
249     using atf::utils::auto_array;
250
251     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
252     {
253         auto_array< test_array > t1(new test_array[10]);
254         ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
255
256         {
257             auto_array< test_array > t2;
258             t2 = test_array::do_copy(t1);
259             ATF_REQUIRE_EQ(test_array::m_nblocks, 1);
260         }
261         ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
262     }
263     ATF_REQUIRE_EQ(test_array::m_nblocks, 0);
264 }
265
266 ATF_TEST_CASE(auto_array_access);
267 ATF_TEST_CASE_HEAD(auto_array_access)
268 {
269     set_md_var("descr", "Tests the auto_array smart pointer class' access "
270                "operator");
271 }
272 ATF_TEST_CASE_BODY(auto_array_access)
273 {
274     using atf::utils::auto_array;
275
276     auto_array< test_array > t(new test_array[10]);
277
278     for (int i = 0; i < 10; i++)
279         t[i].m_value = i * 2;
280
281     for (int i = 0; i < 10; i++)
282         ATF_REQUIRE_EQ(t[i].m_value, i * 2);
283 }
284
285 // ------------------------------------------------------------------------
286 // Tests cases for the header file.
287 // ------------------------------------------------------------------------
288
289 HEADER_TC(include, "atf-c++/utils.hpp");
290
291 // ------------------------------------------------------------------------
292 // Main.
293 // ------------------------------------------------------------------------
294
295 ATF_INIT_TEST_CASES(tcs)
296 {
297     // Add the test for the "auto_array" class.
298     ATF_ADD_TEST_CASE(tcs, auto_array_scope);
299     ATF_ADD_TEST_CASE(tcs, auto_array_copy);
300     ATF_ADD_TEST_CASE(tcs, auto_array_copy_ref);
301     ATF_ADD_TEST_CASE(tcs, auto_array_get);
302     ATF_ADD_TEST_CASE(tcs, auto_array_release);
303     ATF_ADD_TEST_CASE(tcs, auto_array_reset);
304     ATF_ADD_TEST_CASE(tcs, auto_array_assign);
305     ATF_ADD_TEST_CASE(tcs, auto_array_assign_ref);
306     ATF_ADD_TEST_CASE(tcs, auto_array_access);
307
308     // Add the test cases for the header file.
309     ATF_ADD_TEST_CASE(tcs, include);
310 }