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