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/optional.ipp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc #include <iostream>
32*11be35a1SLionel Sambuc #include <sstream>
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <atf-c++.hpp>
35*11be35a1SLionel Sambuc
36*11be35a1SLionel Sambuc using utils::none;
37*11be35a1SLionel Sambuc using utils::optional;
38*11be35a1SLionel Sambuc
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc namespace {
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc /// Fake class to capture calls to the new and delete operators.
44*11be35a1SLionel Sambuc class test_alloc {
45*11be35a1SLionel Sambuc public:
46*11be35a1SLionel Sambuc /// Value to disambiguate objects after construction.
47*11be35a1SLionel Sambuc int value;
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc /// Balance of alive instances of this class in dynamic memory.
50*11be35a1SLionel Sambuc static size_t instances;
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc /// Constructs a new optional object.
53*11be35a1SLionel Sambuc ///
54*11be35a1SLionel Sambuc /// \param value_ The value to store in this object for disambiguation.
test_alloc(int value_)55*11be35a1SLionel Sambuc test_alloc(int value_) : value(value_)
56*11be35a1SLionel Sambuc {
57*11be35a1SLionel Sambuc }
58*11be35a1SLionel Sambuc
59*11be35a1SLionel Sambuc /// Allocates a new object and records its existence.
60*11be35a1SLionel Sambuc ///
61*11be35a1SLionel Sambuc /// \param size The amount of memory to allocate.
62*11be35a1SLionel Sambuc ///
63*11be35a1SLionel Sambuc /// \return A pointer to the allocated memory.
64*11be35a1SLionel Sambuc ///
65*11be35a1SLionel Sambuc /// \throw std::bad_alloc If the memory allocation fails.
66*11be35a1SLionel Sambuc void*
operator new(size_t size)67*11be35a1SLionel Sambuc operator new(size_t size)
68*11be35a1SLionel Sambuc {
69*11be35a1SLionel Sambuc instances++;
70*11be35a1SLionel Sambuc std::cout << "test_alloc::operator new called\n";
71*11be35a1SLionel Sambuc return ::operator new(size);
72*11be35a1SLionel Sambuc }
73*11be35a1SLionel Sambuc
74*11be35a1SLionel Sambuc /// Deallocates an existing object and unrecords its existence.
75*11be35a1SLionel Sambuc ///
76*11be35a1SLionel Sambuc /// \param mem The pointer to the memory to deallocate.
77*11be35a1SLionel Sambuc void
operator delete(void * mem)78*11be35a1SLionel Sambuc operator delete(void* mem)
79*11be35a1SLionel Sambuc {
80*11be35a1SLionel Sambuc instances--;
81*11be35a1SLionel Sambuc std::cout << "test_alloc::operator delete called\n";
82*11be35a1SLionel Sambuc ::operator delete(mem);
83*11be35a1SLionel Sambuc }
84*11be35a1SLionel Sambuc };
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc size_t test_alloc::instances = 0;
88*11be35a1SLionel Sambuc
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc /// Constructs and returns an optional object.
91*11be35a1SLionel Sambuc ///
92*11be35a1SLionel Sambuc /// This is used by tests to validate that returning an object from within a
93*11be35a1SLionel Sambuc /// function works (i.e. the necessary constructors are available).
94*11be35a1SLionel Sambuc ///
95*11be35a1SLionel Sambuc /// \tparam Type The type of the object included in the optional wrapper.
96*11be35a1SLionel Sambuc /// \param value The value to put inside the optional wrapper.
97*11be35a1SLionel Sambuc ///
98*11be35a1SLionel Sambuc /// \return The constructed optional object.
99*11be35a1SLionel Sambuc template< typename Type >
100*11be35a1SLionel Sambuc optional< Type >
return_optional(const Type & value)101*11be35a1SLionel Sambuc return_optional(const Type& value)
102*11be35a1SLionel Sambuc {
103*11be35a1SLionel Sambuc return optional< Type >(value);
104*11be35a1SLionel Sambuc }
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc
107*11be35a1SLionel Sambuc } // anonymous namespace
108*11be35a1SLionel Sambuc
109*11be35a1SLionel Sambuc
110*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ctors__native_type);
ATF_TEST_CASE_BODY(ctors__native_type)111*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ctors__native_type)
112*11be35a1SLionel Sambuc {
113*11be35a1SLionel Sambuc const optional< int > no_args;
114*11be35a1SLionel Sambuc ATF_REQUIRE(!no_args);
115*11be35a1SLionel Sambuc
116*11be35a1SLionel Sambuc const optional< int > with_none(none);
117*11be35a1SLionel Sambuc ATF_REQUIRE(!with_none);
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc const optional< int > with_arg(3);
120*11be35a1SLionel Sambuc ATF_REQUIRE(with_arg);
121*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, with_arg.get());
122*11be35a1SLionel Sambuc
123*11be35a1SLionel Sambuc const optional< int > copy_none(with_none);
124*11be35a1SLionel Sambuc ATF_REQUIRE(!copy_none);
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc const optional< int > copy_arg(with_arg);
127*11be35a1SLionel Sambuc ATF_REQUIRE(copy_arg);
128*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, copy_arg.get());
129*11be35a1SLionel Sambuc }
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(ctors__complex_type);
ATF_TEST_CASE_BODY(ctors__complex_type)133*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(ctors__complex_type)
134*11be35a1SLionel Sambuc {
135*11be35a1SLionel Sambuc const optional< std::string > no_args;
136*11be35a1SLionel Sambuc ATF_REQUIRE(!no_args);
137*11be35a1SLionel Sambuc
138*11be35a1SLionel Sambuc const optional< std::string > with_none(none);
139*11be35a1SLionel Sambuc ATF_REQUIRE(!with_none);
140*11be35a1SLionel Sambuc
141*11be35a1SLionel Sambuc const optional< std::string > with_arg("foo");
142*11be35a1SLionel Sambuc ATF_REQUIRE(with_arg);
143*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("foo", with_arg.get());
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc const optional< std::string > copy_none(with_none);
146*11be35a1SLionel Sambuc ATF_REQUIRE(!copy_none);
147*11be35a1SLionel Sambuc
148*11be35a1SLionel Sambuc const optional< std::string > copy_arg(with_arg);
149*11be35a1SLionel Sambuc ATF_REQUIRE(copy_arg);
150*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("foo", copy_arg.get());
151*11be35a1SLionel Sambuc }
152*11be35a1SLionel Sambuc
153*11be35a1SLionel Sambuc
154*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(assign);
ATF_TEST_CASE_BODY(assign)155*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(assign)
156*11be35a1SLionel Sambuc {
157*11be35a1SLionel Sambuc optional< int > from_default;
158*11be35a1SLionel Sambuc from_default = optional< int >();
159*11be35a1SLionel Sambuc ATF_REQUIRE(!from_default);
160*11be35a1SLionel Sambuc
161*11be35a1SLionel Sambuc optional< int > from_none(3);
162*11be35a1SLionel Sambuc from_none = none;
163*11be35a1SLionel Sambuc ATF_REQUIRE(!from_none);
164*11be35a1SLionel Sambuc
165*11be35a1SLionel Sambuc optional< int > from_int;
166*11be35a1SLionel Sambuc from_int = 6;
167*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(6, from_int.get());
168*11be35a1SLionel Sambuc }
169*11be35a1SLionel Sambuc
170*11be35a1SLionel Sambuc
171*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(return);
ATF_TEST_CASE_BODY(return)172*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(return)
173*11be35a1SLionel Sambuc {
174*11be35a1SLionel Sambuc optional< long > from_return(return_optional< long >(123));
175*11be35a1SLionel Sambuc ATF_REQUIRE(from_return);
176*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(123, from_return.get());
177*11be35a1SLionel Sambuc }
178*11be35a1SLionel Sambuc
179*11be35a1SLionel Sambuc
180*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(memory);
ATF_TEST_CASE_BODY(memory)181*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(memory)
182*11be35a1SLionel Sambuc {
183*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, test_alloc::instances);
184*11be35a1SLionel Sambuc {
185*11be35a1SLionel Sambuc optional< test_alloc > optional1(test_alloc(3));
186*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, test_alloc::instances);
187*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, optional1.get().value);
188*11be35a1SLionel Sambuc
189*11be35a1SLionel Sambuc {
190*11be35a1SLionel Sambuc optional< test_alloc > optional2(optional1);
191*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(2, test_alloc::instances);
192*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, optional2.get().value);
193*11be35a1SLionel Sambuc
194*11be35a1SLionel Sambuc optional2 = 5;
195*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(2, test_alloc::instances);
196*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(5, optional2.get().value);
197*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, optional1.get().value);
198*11be35a1SLionel Sambuc }
199*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, test_alloc::instances);
200*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, optional1.get().value);
201*11be35a1SLionel Sambuc }
202*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, test_alloc::instances);
203*11be35a1SLionel Sambuc }
204*11be35a1SLionel Sambuc
205*11be35a1SLionel Sambuc
206*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_default);
ATF_TEST_CASE_BODY(get_default)207*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_default)
208*11be35a1SLionel Sambuc {
209*11be35a1SLionel Sambuc const std::string def_value = "hello";
210*11be35a1SLionel Sambuc optional< std::string > optional;
211*11be35a1SLionel Sambuc ATF_REQUIRE(&def_value == &optional.get_default(def_value));
212*11be35a1SLionel Sambuc optional = "bye";
213*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("bye", optional.get_default(def_value));
214*11be35a1SLionel Sambuc }
215*11be35a1SLionel Sambuc
216*11be35a1SLionel Sambuc
217*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(make_optional);
ATF_TEST_CASE_BODY(make_optional)218*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(make_optional)
219*11be35a1SLionel Sambuc {
220*11be35a1SLionel Sambuc optional< int > opt = utils::make_optional(576);
221*11be35a1SLionel Sambuc ATF_REQUIRE(opt);
222*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(576, opt.get());
223*11be35a1SLionel Sambuc }
224*11be35a1SLionel Sambuc
225*11be35a1SLionel Sambuc
226*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(operators_eq_and_ne);
ATF_TEST_CASE_BODY(operators_eq_and_ne)227*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(operators_eq_and_ne)
228*11be35a1SLionel Sambuc {
229*11be35a1SLionel Sambuc optional< int > opt1, opt2;
230*11be35a1SLionel Sambuc
231*11be35a1SLionel Sambuc opt1 = none; opt2 = none;
232*11be35a1SLionel Sambuc ATF_REQUIRE( opt1 == opt2);
233*11be35a1SLionel Sambuc ATF_REQUIRE(!(opt1 != opt2));
234*11be35a1SLionel Sambuc
235*11be35a1SLionel Sambuc opt1 = utils::make_optional(5); opt2 = none;
236*11be35a1SLionel Sambuc ATF_REQUIRE(!(opt1 == opt2));
237*11be35a1SLionel Sambuc ATF_REQUIRE( opt1 != opt2);
238*11be35a1SLionel Sambuc
239*11be35a1SLionel Sambuc opt1 = none; opt2 = utils::make_optional(5);
240*11be35a1SLionel Sambuc ATF_REQUIRE(!(opt1 == opt2));
241*11be35a1SLionel Sambuc ATF_REQUIRE( opt1 != opt2);
242*11be35a1SLionel Sambuc
243*11be35a1SLionel Sambuc opt1 = utils::make_optional(5); opt2 = utils::make_optional(5);
244*11be35a1SLionel Sambuc ATF_REQUIRE( opt1 == opt2);
245*11be35a1SLionel Sambuc ATF_REQUIRE(!(opt1 != opt2));
246*11be35a1SLionel Sambuc
247*11be35a1SLionel Sambuc opt1 = utils::make_optional(6); opt2 = utils::make_optional(5);
248*11be35a1SLionel Sambuc ATF_REQUIRE(!(opt1 == opt2));
249*11be35a1SLionel Sambuc ATF_REQUIRE( opt1 != opt2);
250*11be35a1SLionel Sambuc }
251*11be35a1SLionel Sambuc
252*11be35a1SLionel Sambuc
253*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(output);
ATF_TEST_CASE_BODY(output)254*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(output)
255*11be35a1SLionel Sambuc {
256*11be35a1SLionel Sambuc {
257*11be35a1SLionel Sambuc std::ostringstream str;
258*11be35a1SLionel Sambuc str << optional< int >(none);
259*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("none", str.str());
260*11be35a1SLionel Sambuc }
261*11be35a1SLionel Sambuc {
262*11be35a1SLionel Sambuc std::ostringstream str;
263*11be35a1SLionel Sambuc str << optional< int >(5);
264*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("5", str.str());
265*11be35a1SLionel Sambuc }
266*11be35a1SLionel Sambuc {
267*11be35a1SLionel Sambuc std::ostringstream str;
268*11be35a1SLionel Sambuc str << optional< std::string >("this is a text");
269*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("this is a text", str.str());
270*11be35a1SLionel Sambuc }
271*11be35a1SLionel Sambuc }
272*11be35a1SLionel Sambuc
273*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)274*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
275*11be35a1SLionel Sambuc {
276*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ctors__native_type);
277*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, ctors__complex_type);
278*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, assign);
279*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, return);
280*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, memory);
281*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, get_default);
282*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, make_optional);
283*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, operators_eq_and_ne);
284*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, output);
285*11be35a1SLionel Sambuc }
286