xref: /freebsd/contrib/kyua/utils/format/containers_test.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis // Copyright 2014 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis #include "utils/format/containers.ipp"
30*b0d29bc4SBrooks Davis 
31*b0d29bc4SBrooks Davis #include <memory>
32*b0d29bc4SBrooks Davis #include <ostream>
33*b0d29bc4SBrooks Davis #include <set>
34*b0d29bc4SBrooks Davis #include <string>
35*b0d29bc4SBrooks Davis #include <utility>
36*b0d29bc4SBrooks Davis #include <vector>
37*b0d29bc4SBrooks Davis 
38*b0d29bc4SBrooks Davis #include <atf-c++.hpp>
39*b0d29bc4SBrooks Davis 
40*b0d29bc4SBrooks Davis 
41*b0d29bc4SBrooks Davis 
42*b0d29bc4SBrooks Davis namespace {
43*b0d29bc4SBrooks Davis 
44*b0d29bc4SBrooks Davis 
45*b0d29bc4SBrooks Davis /// Formats a value and compares it to an expected string.
46*b0d29bc4SBrooks Davis ///
47*b0d29bc4SBrooks Davis /// \tparam T The type of the value to format.
48*b0d29bc4SBrooks Davis /// \param expected Expected formatted text.
49*b0d29bc4SBrooks Davis /// \param actual The value to format.
50*b0d29bc4SBrooks Davis ///
51*b0d29bc4SBrooks Davis /// \post Fails the test case if the formatted actual value does not match
52*b0d29bc4SBrooks Davis /// the provided expected string.
53*b0d29bc4SBrooks Davis template< typename T >
54*b0d29bc4SBrooks Davis static void
do_check(const char * expected,const T & actual)55*b0d29bc4SBrooks Davis do_check(const char* expected, const T& actual)
56*b0d29bc4SBrooks Davis {
57*b0d29bc4SBrooks Davis     std::ostringstream str;
58*b0d29bc4SBrooks Davis     str << actual;
59*b0d29bc4SBrooks Davis     ATF_REQUIRE_EQ(expected, str.str());
60*b0d29bc4SBrooks Davis }
61*b0d29bc4SBrooks Davis 
62*b0d29bc4SBrooks Davis 
63*b0d29bc4SBrooks Davis }  // anonymous namespace
64*b0d29bc4SBrooks Davis 
65*b0d29bc4SBrooks Davis 
66*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(std_map__empty);
ATF_TEST_CASE_BODY(std_map__empty)67*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(std_map__empty)
68*b0d29bc4SBrooks Davis {
69*b0d29bc4SBrooks Davis     do_check("map()", std::map< char, char >());
70*b0d29bc4SBrooks Davis     do_check("map()", std::map< int, long >());
71*b0d29bc4SBrooks Davis }
72*b0d29bc4SBrooks Davis 
73*b0d29bc4SBrooks Davis 
74*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(std_map__some);
ATF_TEST_CASE_BODY(std_map__some)75*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(std_map__some)
76*b0d29bc4SBrooks Davis {
77*b0d29bc4SBrooks Davis     {
78*b0d29bc4SBrooks Davis         std::map< char, int > v;
79*b0d29bc4SBrooks Davis         v['b'] = 123;
80*b0d29bc4SBrooks Davis         v['z'] = 321;
81*b0d29bc4SBrooks Davis         do_check("map(b=123, z=321)", v);
82*b0d29bc4SBrooks Davis     }
83*b0d29bc4SBrooks Davis 
84*b0d29bc4SBrooks Davis     {
85*b0d29bc4SBrooks Davis         std::map< int, std::string > v;
86*b0d29bc4SBrooks Davis         v[5] = "first";
87*b0d29bc4SBrooks Davis         v[2] = "second";
88*b0d29bc4SBrooks Davis         v[8] = "third";
89*b0d29bc4SBrooks Davis         do_check("map(2=second, 5=first, 8=third)", v);
90*b0d29bc4SBrooks Davis     }
91*b0d29bc4SBrooks Davis }
92*b0d29bc4SBrooks Davis 
93*b0d29bc4SBrooks Davis 
94*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(std_pair);
ATF_TEST_CASE_BODY(std_pair)95*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(std_pair)
96*b0d29bc4SBrooks Davis {
97*b0d29bc4SBrooks Davis     do_check("pair(5, b)", std::pair< int, char >(5, 'b'));
98*b0d29bc4SBrooks Davis     do_check("pair(foo bar, baz)",
99*b0d29bc4SBrooks Davis              std::pair< std::string, std::string >("foo bar", "baz"));
100*b0d29bc4SBrooks Davis }
101*b0d29bc4SBrooks Davis 
102*b0d29bc4SBrooks Davis 
103*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(std_shared_ptr__null);
ATF_TEST_CASE_BODY(std_shared_ptr__null)104*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(std_shared_ptr__null)
105*b0d29bc4SBrooks Davis {
106*b0d29bc4SBrooks Davis     do_check("<NULL>", std::shared_ptr< char >());
107*b0d29bc4SBrooks Davis     do_check("<NULL>", std::shared_ptr< int >());
108*b0d29bc4SBrooks Davis }
109*b0d29bc4SBrooks Davis 
110*b0d29bc4SBrooks Davis 
111*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(std_shared_ptr__not_null);
ATF_TEST_CASE_BODY(std_shared_ptr__not_null)112*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(std_shared_ptr__not_null)
113*b0d29bc4SBrooks Davis {
114*b0d29bc4SBrooks Davis     do_check("f", std::shared_ptr< char >(new char('f')));
115*b0d29bc4SBrooks Davis     do_check("8", std::shared_ptr< int >(new int(8)));
116*b0d29bc4SBrooks Davis }
117*b0d29bc4SBrooks Davis 
118*b0d29bc4SBrooks Davis 
119*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(std_set__empty);
ATF_TEST_CASE_BODY(std_set__empty)120*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(std_set__empty)
121*b0d29bc4SBrooks Davis {
122*b0d29bc4SBrooks Davis     do_check("set()", std::set< char >());
123*b0d29bc4SBrooks Davis     do_check("set()", std::set< int >());
124*b0d29bc4SBrooks Davis }
125*b0d29bc4SBrooks Davis 
126*b0d29bc4SBrooks Davis 
127*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(std_set__some);
ATF_TEST_CASE_BODY(std_set__some)128*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(std_set__some)
129*b0d29bc4SBrooks Davis {
130*b0d29bc4SBrooks Davis     {
131*b0d29bc4SBrooks Davis         std::set< char > v;
132*b0d29bc4SBrooks Davis         v.insert('b');
133*b0d29bc4SBrooks Davis         v.insert('z');
134*b0d29bc4SBrooks Davis         do_check("set(b, z)", v);
135*b0d29bc4SBrooks Davis     }
136*b0d29bc4SBrooks Davis 
137*b0d29bc4SBrooks Davis     {
138*b0d29bc4SBrooks Davis         std::set< int > v;
139*b0d29bc4SBrooks Davis         v.insert(5);
140*b0d29bc4SBrooks Davis         v.insert(2);
141*b0d29bc4SBrooks Davis         v.insert(8);
142*b0d29bc4SBrooks Davis         do_check("set(2, 5, 8)", v);
143*b0d29bc4SBrooks Davis     }
144*b0d29bc4SBrooks Davis }
145*b0d29bc4SBrooks Davis 
146*b0d29bc4SBrooks Davis 
147*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(std_vector__empty);
ATF_TEST_CASE_BODY(std_vector__empty)148*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(std_vector__empty)
149*b0d29bc4SBrooks Davis {
150*b0d29bc4SBrooks Davis     do_check("[]", std::vector< char >());
151*b0d29bc4SBrooks Davis     do_check("[]", std::vector< int >());
152*b0d29bc4SBrooks Davis }
153*b0d29bc4SBrooks Davis 
154*b0d29bc4SBrooks Davis 
155*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(std_vector__some);
ATF_TEST_CASE_BODY(std_vector__some)156*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(std_vector__some)
157*b0d29bc4SBrooks Davis {
158*b0d29bc4SBrooks Davis     {
159*b0d29bc4SBrooks Davis         std::vector< char > v;
160*b0d29bc4SBrooks Davis         v.push_back('b');
161*b0d29bc4SBrooks Davis         v.push_back('z');
162*b0d29bc4SBrooks Davis         do_check("[b, z]", v);
163*b0d29bc4SBrooks Davis     }
164*b0d29bc4SBrooks Davis 
165*b0d29bc4SBrooks Davis     {
166*b0d29bc4SBrooks Davis         std::vector< int > v;
167*b0d29bc4SBrooks Davis         v.push_back(5);
168*b0d29bc4SBrooks Davis         v.push_back(2);
169*b0d29bc4SBrooks Davis         v.push_back(8);
170*b0d29bc4SBrooks Davis         do_check("[5, 2, 8]", v);
171*b0d29bc4SBrooks Davis     }
172*b0d29bc4SBrooks Davis }
173*b0d29bc4SBrooks Davis 
174*b0d29bc4SBrooks Davis 
ATF_INIT_TEST_CASES(tcs)175*b0d29bc4SBrooks Davis ATF_INIT_TEST_CASES(tcs)
176*b0d29bc4SBrooks Davis {
177*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, std_map__empty);
178*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, std_map__some);
179*b0d29bc4SBrooks Davis 
180*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, std_pair);
181*b0d29bc4SBrooks Davis 
182*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, std_shared_ptr__null);
183*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, std_shared_ptr__not_null);
184*b0d29bc4SBrooks Davis 
185*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, std_set__empty);
186*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, std_set__some);
187*b0d29bc4SBrooks Davis 
188*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, std_vector__empty);
189*b0d29bc4SBrooks Davis     ATF_ADD_TEST_CASE(tcs, std_vector__some);
190*b0d29bc4SBrooks Davis }
191