1 // Copyright 2012 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "utils/units.hpp"
34 #include <atf-c++.hpp>
36 namespace units
= utils::units
;
39 ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__tb
);
40 ATF_TEST_CASE_BODY(bytes__format__tb
)
45 ATF_REQUIRE_EQ("2.00T", units::bytes(2 * TB
).format());
46 ATF_REQUIRE_EQ("45.12T", units::bytes(45 * TB
+ 120 * GB
).format());
50 ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__gb
);
51 ATF_TEST_CASE_BODY(bytes__format__gb
)
56 ATF_REQUIRE_EQ("5.00G", units::bytes(5 * GB
).format());
57 ATF_REQUIRE_EQ("745.96G", units::bytes(745 * GB
+ 980 * MB
).format());
61 ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__mb
);
62 ATF_TEST_CASE_BODY(bytes__format__mb
)
67 ATF_REQUIRE_EQ("1.00M", units::bytes(1 * MB
).format());
68 ATF_REQUIRE_EQ("1023.50M", units::bytes(1023 * MB
+ 512 * KB
).format());
72 ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__kb
);
73 ATF_TEST_CASE_BODY(bytes__format__kb
)
77 ATF_REQUIRE_EQ("3.00K", units::bytes(3 * KB
).format());
78 ATF_REQUIRE_EQ("1.33K", units::bytes(1 * KB
+ 340).format());
82 ATF_TEST_CASE_WITHOUT_HEAD(bytes__format__b
);
83 ATF_TEST_CASE_BODY(bytes__format__b
)
85 ATF_REQUIRE_EQ("0", units::bytes().format());
86 ATF_REQUIRE_EQ("0", units::bytes(0).format());
87 ATF_REQUIRE_EQ("1023", units::bytes(1023).format());
91 ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__tb
);
92 ATF_TEST_CASE_BODY(bytes__parse__tb
)
97 ATF_REQUIRE_EQ(0, units::bytes::parse("0T"));
98 ATF_REQUIRE_EQ(units::bytes(TB
), units::bytes::parse("1T"));
99 ATF_REQUIRE_EQ(units::bytes(TB
), units::bytes::parse("1t"));
100 ATF_REQUIRE_EQ(13567973486755LL, units::bytes::parse("12.340000T"));
104 ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__gb
);
105 ATF_TEST_CASE_BODY(bytes__parse__gb
)
110 ATF_REQUIRE_EQ(0, units::bytes::parse("0G"));
111 ATF_REQUIRE_EQ(units::bytes(GB
), units::bytes::parse("1G"));
112 ATF_REQUIRE_EQ(units::bytes(GB
), units::bytes::parse("1g"));
113 ATF_REQUIRE_EQ(13249974108LL, units::bytes::parse("12.340G"));
117 ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__mb
);
118 ATF_TEST_CASE_BODY(bytes__parse__mb
)
123 ATF_REQUIRE_EQ(0, units::bytes::parse("0M"));
124 ATF_REQUIRE_EQ(units::bytes(MB
), units::bytes::parse("1M"));
125 ATF_REQUIRE_EQ(units::bytes(MB
), units::bytes::parse("1m"));
126 ATF_REQUIRE_EQ(12939427, units::bytes::parse("12.34000M"));
130 ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__kb
);
131 ATF_TEST_CASE_BODY(bytes__parse__kb
)
135 ATF_REQUIRE_EQ(0, units::bytes::parse("0K"));
136 ATF_REQUIRE_EQ(units::bytes(KB
), units::bytes::parse("1K"));
137 ATF_REQUIRE_EQ(units::bytes(KB
), units::bytes::parse("1k"));
138 ATF_REQUIRE_EQ(12636, units::bytes::parse("12.34K"));
142 ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__b
);
143 ATF_TEST_CASE_BODY(bytes__parse__b
)
145 ATF_REQUIRE_EQ(0, units::bytes::parse("0"));
146 ATF_REQUIRE_EQ(89, units::bytes::parse("89"));
147 ATF_REQUIRE_EQ(1234, units::bytes::parse("1234"));
148 ATF_REQUIRE_EQ(1234567890, units::bytes::parse("1234567890"));
152 ATF_TEST_CASE_WITHOUT_HEAD(bytes__parse__error
);
153 ATF_TEST_CASE_BODY(bytes__parse__error
)
155 ATF_REQUIRE_THROW_RE(std::runtime_error
, "empty", units::bytes::parse(""));
156 ATF_REQUIRE_THROW_RE(std::runtime_error
, "empty", units::bytes::parse("k"));
158 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Invalid.*'.'",
159 units::bytes::parse("."));
160 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Invalid.*'3.'",
161 units::bytes::parse("3."));
162 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Invalid.*'.3'",
163 units::bytes::parse(".3"));
165 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Invalid.*' t'",
166 units::bytes::parse(" t"));
167 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Invalid.*'.t'",
168 units::bytes::parse(".t"));
169 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Invalid.*'12 t'",
170 units::bytes::parse("12 t"));
171 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Invalid.*'12.t'",
172 units::bytes::parse("12.t"));
173 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Invalid.*'.12t'",
174 units::bytes::parse(".12t"));
175 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Invalid.*'abt'",
176 units::bytes::parse("abt"));
180 ATF_TEST_CASE_WITHOUT_HEAD(bytes__istream__one_word
);
181 ATF_TEST_CASE_BODY(bytes__istream__one_word
)
183 std::istringstream
input("12M");
187 ATF_REQUIRE(input
.eof());
188 ATF_REQUIRE_EQ(units::bytes(12 * units::MB
), bytes
);
192 ATF_TEST_CASE_WITHOUT_HEAD(bytes__istream__many_words
);
193 ATF_TEST_CASE_BODY(bytes__istream__many_words
)
195 std::istringstream
input("12M more");
199 ATF_REQUIRE(input
.good());
200 ATF_REQUIRE_EQ(units::bytes(12 * units::MB
), bytes
);
204 ATF_REQUIRE_EQ("more", word
);
208 ATF_TEST_CASE_WITHOUT_HEAD(bytes__istream__error
);
209 ATF_TEST_CASE_BODY(bytes__istream__error
)
211 std::istringstream
input("12.M more");
213 units::bytes
bytes(123456789);
215 ATF_REQUIRE(input
.bad());
216 ATF_REQUIRE_EQ(units::bytes(123456789), bytes
);
220 ATF_TEST_CASE_WITHOUT_HEAD(bytes__ostream
);
221 ATF_TEST_CASE_BODY(bytes__ostream
)
223 std::ostringstream output
;
224 output
<< "foo " << units::bytes(5 * units::KB
) << " bar";
225 ATF_REQUIRE_EQ("foo 5.00K bar", output
.str());
229 ATF_INIT_TEST_CASES(tcs
)
231 ATF_ADD_TEST_CASE(tcs
, bytes__format__tb
);
232 ATF_ADD_TEST_CASE(tcs
, bytes__format__gb
);
233 ATF_ADD_TEST_CASE(tcs
, bytes__format__mb
);
234 ATF_ADD_TEST_CASE(tcs
, bytes__format__kb
);
235 ATF_ADD_TEST_CASE(tcs
, bytes__format__b
);
237 ATF_ADD_TEST_CASE(tcs
, bytes__parse__tb
);
238 ATF_ADD_TEST_CASE(tcs
, bytes__parse__gb
);
239 ATF_ADD_TEST_CASE(tcs
, bytes__parse__mb
);
240 ATF_ADD_TEST_CASE(tcs
, bytes__parse__kb
);
241 ATF_ADD_TEST_CASE(tcs
, bytes__parse__b
);
242 ATF_ADD_TEST_CASE(tcs
, bytes__parse__error
);
244 ATF_ADD_TEST_CASE(tcs
, bytes__istream__one_word
);
245 ATF_ADD_TEST_CASE(tcs
, bytes__istream__many_words
);
246 ATF_ADD_TEST_CASE(tcs
, bytes__istream__error
);
247 ATF_ADD_TEST_CASE(tcs
, bytes__ostream
);