1*11be35a1SLionel Sambuc // Copyright 2012 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/units.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc #include <sstream>
32*11be35a1SLionel Sambuc #include <stdexcept>
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <atf-c++.hpp>
35*11be35a1SLionel Sambuc
36*11be35a1SLionel Sambuc namespace units = utils::units;
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc
39*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__tb);
ATF_TEST_CASE_BODY(bytes__format__tb)40*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__format__tb)
41*11be35a1SLionel Sambuc {
42*11be35a1SLionel Sambuc using units::TB;
43*11be35a1SLionel Sambuc using units::GB;
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2.00T", units::bytes(2 * TB).format());
46*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("45.12T", units::bytes(45 * TB + 120 * GB).format());
47*11be35a1SLionel Sambuc }
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__gb);
ATF_TEST_CASE_BODY(bytes__format__gb)51*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__format__gb)
52*11be35a1SLionel Sambuc {
53*11be35a1SLionel Sambuc using units::GB;
54*11be35a1SLionel Sambuc using units::MB;
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("5.00G", units::bytes(5 * GB).format());
57*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("745.96G", units::bytes(745 * GB + 980 * MB).format());
58*11be35a1SLionel Sambuc }
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc
61*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__mb);
ATF_TEST_CASE_BODY(bytes__format__mb)62*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__format__mb)
63*11be35a1SLionel Sambuc {
64*11be35a1SLionel Sambuc using units::MB;
65*11be35a1SLionel Sambuc using units::KB;
66*11be35a1SLionel Sambuc
67*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("1.00M", units::bytes(1 * MB).format());
68*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("1023.50M", units::bytes(1023 * MB + 512 * KB).format());
69*11be35a1SLionel Sambuc }
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc
72*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__kb);
ATF_TEST_CASE_BODY(bytes__format__kb)73*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__format__kb)
74*11be35a1SLionel Sambuc {
75*11be35a1SLionel Sambuc using units::KB;
76*11be35a1SLionel Sambuc
77*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("3.00K", units::bytes(3 * KB).format());
78*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("1.33K", units::bytes(1 * KB + 340).format());
79*11be35a1SLionel Sambuc }
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc
82*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__b);
ATF_TEST_CASE_BODY(bytes__format__b)83*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__format__b)
84*11be35a1SLionel Sambuc {
85*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("0", units::bytes().format());
86*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("0", units::bytes(0).format());
87*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("1023", units::bytes(1023).format());
88*11be35a1SLionel Sambuc }
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__tb);
ATF_TEST_CASE_BODY(bytes__parse__tb)92*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__parse__tb)
93*11be35a1SLionel Sambuc {
94*11be35a1SLionel Sambuc using units::TB;
95*11be35a1SLionel Sambuc using units::GB;
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, units::bytes::parse("0T"));
98*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(TB), units::bytes::parse("1T"));
99*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(TB), units::bytes::parse("1t"));
100*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(13567973486755LL, units::bytes::parse("12.340000T"));
101*11be35a1SLionel Sambuc }
102*11be35a1SLionel Sambuc
103*11be35a1SLionel Sambuc
104*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__gb);
ATF_TEST_CASE_BODY(bytes__parse__gb)105*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__parse__gb)
106*11be35a1SLionel Sambuc {
107*11be35a1SLionel Sambuc using units::GB;
108*11be35a1SLionel Sambuc using units::MB;
109*11be35a1SLionel Sambuc
110*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, units::bytes::parse("0G"));
111*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(GB), units::bytes::parse("1G"));
112*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(GB), units::bytes::parse("1g"));
113*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(13249974108LL, units::bytes::parse("12.340G"));
114*11be35a1SLionel Sambuc }
115*11be35a1SLionel Sambuc
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__mb);
ATF_TEST_CASE_BODY(bytes__parse__mb)118*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__parse__mb)
119*11be35a1SLionel Sambuc {
120*11be35a1SLionel Sambuc using units::MB;
121*11be35a1SLionel Sambuc using units::KB;
122*11be35a1SLionel Sambuc
123*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, units::bytes::parse("0M"));
124*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(MB), units::bytes::parse("1M"));
125*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(MB), units::bytes::parse("1m"));
126*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(12939427, units::bytes::parse("12.34000M"));
127*11be35a1SLionel Sambuc }
128*11be35a1SLionel Sambuc
129*11be35a1SLionel Sambuc
130*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__kb);
ATF_TEST_CASE_BODY(bytes__parse__kb)131*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__parse__kb)
132*11be35a1SLionel Sambuc {
133*11be35a1SLionel Sambuc using units::KB;
134*11be35a1SLionel Sambuc
135*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, units::bytes::parse("0K"));
136*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(KB), units::bytes::parse("1K"));
137*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(KB), units::bytes::parse("1k"));
138*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(12636, units::bytes::parse("12.34K"));
139*11be35a1SLionel Sambuc }
140*11be35a1SLionel Sambuc
141*11be35a1SLionel Sambuc
142*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__b);
ATF_TEST_CASE_BODY(bytes__parse__b)143*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__parse__b)
144*11be35a1SLionel Sambuc {
145*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, units::bytes::parse("0"));
146*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(89, units::bytes::parse("89"));
147*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1234, units::bytes::parse("1234"));
148*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1234567890, units::bytes::parse("1234567890"));
149*11be35a1SLionel Sambuc }
150*11be35a1SLionel Sambuc
151*11be35a1SLionel Sambuc
152*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__error);
ATF_TEST_CASE_BODY(bytes__parse__error)153*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__parse__error)
154*11be35a1SLionel Sambuc {
155*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "empty", units::bytes::parse(""));
156*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "empty", units::bytes::parse("k"));
157*11be35a1SLionel Sambuc
158*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "Invalid.*'.'",
159*11be35a1SLionel Sambuc units::bytes::parse("."));
160*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "Invalid.*'3.'",
161*11be35a1SLionel Sambuc units::bytes::parse("3."));
162*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "Invalid.*'.3'",
163*11be35a1SLionel Sambuc units::bytes::parse(".3"));
164*11be35a1SLionel Sambuc
165*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "Invalid.*' t'",
166*11be35a1SLionel Sambuc units::bytes::parse(" t"));
167*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "Invalid.*'.t'",
168*11be35a1SLionel Sambuc units::bytes::parse(".t"));
169*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "Invalid.*'12 t'",
170*11be35a1SLionel Sambuc units::bytes::parse("12 t"));
171*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "Invalid.*'12.t'",
172*11be35a1SLionel Sambuc units::bytes::parse("12.t"));
173*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "Invalid.*'.12t'",
174*11be35a1SLionel Sambuc units::bytes::parse(".12t"));
175*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(std::runtime_error, "Invalid.*'abt'",
176*11be35a1SLionel Sambuc units::bytes::parse("abt"));
177*11be35a1SLionel Sambuc }
178*11be35a1SLionel Sambuc
179*11be35a1SLionel Sambuc
180*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__istream__one_word);
ATF_TEST_CASE_BODY(bytes__istream__one_word)181*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__istream__one_word)
182*11be35a1SLionel Sambuc {
183*11be35a1SLionel Sambuc std::istringstream input("12M");
184*11be35a1SLionel Sambuc
185*11be35a1SLionel Sambuc units::bytes bytes;
186*11be35a1SLionel Sambuc input >> bytes;
187*11be35a1SLionel Sambuc ATF_REQUIRE(input.eof());
188*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(12 * units::MB), bytes);
189*11be35a1SLionel Sambuc }
190*11be35a1SLionel Sambuc
191*11be35a1SLionel Sambuc
192*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__istream__many_words);
ATF_TEST_CASE_BODY(bytes__istream__many_words)193*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__istream__many_words)
194*11be35a1SLionel Sambuc {
195*11be35a1SLionel Sambuc std::istringstream input("12M more");
196*11be35a1SLionel Sambuc
197*11be35a1SLionel Sambuc units::bytes bytes;
198*11be35a1SLionel Sambuc input >> bytes;
199*11be35a1SLionel Sambuc ATF_REQUIRE(input.good());
200*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(12 * units::MB), bytes);
201*11be35a1SLionel Sambuc
202*11be35a1SLionel Sambuc std::string word;
203*11be35a1SLionel Sambuc input >> word;
204*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("more", word);
205*11be35a1SLionel Sambuc }
206*11be35a1SLionel Sambuc
207*11be35a1SLionel Sambuc
208*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__istream__error);
ATF_TEST_CASE_BODY(bytes__istream__error)209*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__istream__error)
210*11be35a1SLionel Sambuc {
211*11be35a1SLionel Sambuc std::istringstream input("12.M more");
212*11be35a1SLionel Sambuc
213*11be35a1SLionel Sambuc units::bytes bytes(123456789);
214*11be35a1SLionel Sambuc input >> bytes;
215*11be35a1SLionel Sambuc ATF_REQUIRE(input.bad());
216*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(units::bytes(123456789), bytes);
217*11be35a1SLionel Sambuc }
218*11be35a1SLionel Sambuc
219*11be35a1SLionel Sambuc
220*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bytes__ostream);
ATF_TEST_CASE_BODY(bytes__ostream)221*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bytes__ostream)
222*11be35a1SLionel Sambuc {
223*11be35a1SLionel Sambuc std::ostringstream output;
224*11be35a1SLionel Sambuc output << "foo " << units::bytes(5 * units::KB) << " bar";
225*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("foo 5.00K bar", output.str());
226*11be35a1SLionel Sambuc }
227*11be35a1SLionel Sambuc
228*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)229*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
230*11be35a1SLionel Sambuc {
231*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__format__tb);
232*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__format__gb);
233*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__format__mb);
234*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__format__kb);
235*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__format__b);
236*11be35a1SLionel Sambuc
237*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__parse__tb);
238*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__parse__gb);
239*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__parse__mb);
240*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__parse__kb);
241*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__parse__b);
242*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__parse__error);
243*11be35a1SLionel Sambuc
244*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__istream__one_word);
245*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__istream__many_words);
246*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__istream__error);
247*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bytes__ostream);
248*11be35a1SLionel Sambuc }
249