]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2011 Dreamhost | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include "common/strtol.h" | |
16 | #include <string> | |
17 | #include <map> | |
18 | ||
19 | #include "gtest/gtest.h" | |
20 | ||
21 | static void test_strict_strtoll(const char *str, long long expected) | |
22 | { | |
23 | std::string err; | |
24 | long long val = strict_strtoll(str, 10, &err); | |
25 | if (!err.empty()) { | |
26 | ASSERT_EQ(err, ""); | |
27 | } | |
28 | else { | |
29 | ASSERT_EQ(val, expected); | |
30 | } | |
31 | } | |
32 | ||
33 | static void test_strict_strtol(const char *str, long expected) | |
34 | { | |
35 | std::string err; | |
36 | long val = strict_strtol(str, 10, &err); | |
37 | if (!err.empty()) { | |
38 | ASSERT_EQ(err, ""); | |
39 | } | |
40 | else { | |
41 | ASSERT_EQ(val, expected); | |
42 | } | |
43 | } | |
44 | ||
45 | static void test_strict_strtod(const char *str, double expected) | |
46 | { | |
47 | std::string err; | |
48 | double val = strict_strtod(str, &err); | |
49 | if (!err.empty()) { | |
50 | ASSERT_EQ(err, ""); | |
51 | } | |
52 | else { | |
53 | // when comparing floats, use a margin of error | |
54 | if ((expected - 0.001 > val) || (expected + 0.001 < val)) { | |
55 | ASSERT_EQ(val, expected); | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | static void test_strict_strtof(const char *str, float expected) | |
61 | { | |
62 | std::string err; | |
63 | float val = strict_strtof(str, &err); | |
64 | if (!err.empty()) { | |
65 | ASSERT_EQ(err, ""); | |
66 | } | |
67 | else { | |
68 | // when comparing floats, use a margin of error | |
69 | if ((expected - 0.001 > val) || (expected + 0.001 < val)) { | |
70 | ASSERT_EQ(val, expected); | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | TEST(StrToL, Simple1) { | |
76 | test_strict_strtoll("123", 123); | |
77 | test_strict_strtoll("0", 0); | |
78 | test_strict_strtoll("-123", -123); | |
79 | test_strict_strtoll("8796093022208", 8796093022208LL); | |
80 | test_strict_strtoll("-8796093022208", -8796093022208LL); | |
81 | ||
82 | test_strict_strtol("208", 208); | |
83 | test_strict_strtol("-4", -4); | |
84 | test_strict_strtol("0", 0); | |
85 | test_strict_strtol("2147483646", 2147483646); | |
86 | ||
87 | test_strict_strtof("0.05", 0.05); | |
88 | test_strict_strtof("0", 0.0); | |
89 | test_strict_strtof("-0", 0.0); | |
90 | test_strict_strtof("10000000.5", 10000000.5); | |
91 | ||
92 | test_strict_strtod("-0.2", -0.2); | |
93 | test_strict_strtod("0.1", 0.1); | |
94 | test_strict_strtod("0", 0.0); | |
95 | } | |
96 | ||
97 | static void test_strict_strtoll_err(const char *str) | |
98 | { | |
99 | std::string err; | |
100 | strict_strtoll(str, 10, &err); | |
101 | ASSERT_NE(err, ""); | |
102 | } | |
103 | ||
104 | static void test_strict_strtol_err(const char *str) | |
105 | { | |
106 | std::string err; | |
107 | strict_strtol(str, 10, &err); | |
108 | ASSERT_NE(err, ""); | |
109 | } | |
110 | ||
111 | static void test_strict_strtod_err(const char *str) | |
112 | { | |
113 | std::string err; | |
114 | strict_strtod(str, &err); | |
115 | ASSERT_NE(err, ""); | |
116 | } | |
117 | ||
118 | static void test_strict_strtof_err(const char *str) | |
119 | { | |
120 | std::string err; | |
121 | strict_strtof(str, &err); | |
122 | ASSERT_NE(err, ""); | |
123 | } | |
124 | ||
125 | TEST(StrToL, Error1) { | |
126 | test_strict_strtoll_err("604462909807314587353088"); // overflow | |
127 | test_strict_strtoll_err("aw shucks"); // invalid | |
128 | test_strict_strtoll_err("343245 aw shucks"); // invalid chars at end | |
129 | ||
130 | test_strict_strtol_err("35 aw shucks"); // invalid chars at end | |
131 | test_strict_strtol_err("--0"); | |
132 | ||
133 | test_strict_strtod_err("345345.0-"); | |
134 | test_strict_strtod_err("34.0 garbo"); | |
135 | ||
136 | test_strict_strtof_err("0.05.0"); | |
137 | } | |
138 | ||
139 | ||
140 | static void test_strict_sistrtoll(const char *str) | |
141 | { | |
142 | std::string err; | |
143 | strict_sistrtoll(str, &err); | |
144 | ASSERT_EQ(err, ""); | |
145 | } | |
146 | ||
147 | static void test_strict_sistrtoll_units(const std::string& foo, | |
148 | char u, const int m) | |
149 | { | |
150 | std::string s(foo); | |
151 | s.push_back(u); | |
152 | const char *str = s.c_str(); | |
153 | std::string err; | |
154 | uint64_t r = strict_sistrtoll(str, &err); | |
155 | ASSERT_EQ(err, ""); | |
156 | ||
157 | str = foo.c_str(); | |
158 | std::string err2; | |
159 | long long tmp = strict_strtoll(str, 10, &err2); | |
160 | ASSERT_EQ(err2, ""); | |
161 | tmp = (tmp << m); | |
162 | ASSERT_EQ(tmp, (long long)r); | |
163 | } | |
164 | ||
165 | TEST(SIStrToLL, WithUnits) { | |
166 | std::map<char,int> units; | |
167 | units['B'] = 0; | |
168 | units['K'] = 10; | |
169 | units['M'] = 20; | |
170 | units['G'] = 30; | |
171 | units['T'] = 40; | |
172 | units['P'] = 50; | |
173 | units['E'] = 60; | |
174 | ||
175 | for (std::map<char,int>::iterator p = units.begin(); | |
176 | p != units.end(); ++p) { | |
177 | // the upper bound of uint64_t is 2^64 = 4E | |
178 | test_strict_sistrtoll_units("4", p->first, p->second); | |
179 | test_strict_sistrtoll_units("1", p->first, p->second); | |
180 | test_strict_sistrtoll_units("0", p->first, p->second); | |
181 | } | |
182 | } | |
183 | ||
184 | TEST(SIStrToLL, WithoutUnits) { | |
185 | test_strict_sistrtoll("1024"); | |
186 | test_strict_sistrtoll("1152921504606846976"); | |
187 | test_strict_sistrtoll("0"); | |
188 | } | |
189 | ||
190 | static void test_strict_sistrtoll_err(const char *str) | |
191 | { | |
192 | std::string err; | |
193 | strict_sistrtoll(str, &err); | |
194 | ASSERT_NE(err, ""); | |
195 | } | |
196 | ||
197 | TEST(SIStrToLL, Error) { | |
198 | test_strict_sistrtoll_err("1024F"); | |
199 | test_strict_sistrtoll_err("QDDSA"); | |
200 | test_strict_sistrtoll_err("1b"); | |
201 | test_strict_sistrtoll_err("100k"); | |
202 | test_strict_sistrtoll_err("1000m"); | |
203 | test_strict_sistrtoll_err("1g"); | |
204 | test_strict_sistrtoll_err("20t"); | |
205 | test_strict_sistrtoll_err("100p"); | |
206 | test_strict_sistrtoll_err("1000e"); | |
207 | test_strict_sistrtoll_err("B"); | |
208 | test_strict_sistrtoll_err("M"); | |
209 | test_strict_sistrtoll_err("BM"); | |
210 | test_strict_sistrtoll_err("B0wef"); | |
211 | test_strict_sistrtoll_err("0m"); | |
212 | test_strict_sistrtoll_err("-1"); // it returns uint64_t | |
213 | test_strict_sistrtoll_err("-1K"); | |
214 | // the upper bound of uint64_t is 2^64 = 4E, so 1024E overflows | |
215 | test_strict_sistrtoll_err("1024E"); // overflows after adding the suffix | |
216 | } | |
217 | ||
218 | // since strict_sistrtoll is an alias of strict_si_cast<uint64_t>(), quite a few | |
219 | // of cases are covered by existing test cases of strict_sistrtoll already. | |
220 | TEST(StrictSICast, Error) { | |
221 | { | |
222 | std::string err; | |
223 | // the SI prefix is way too large for `int`. | |
224 | (void)strict_si_cast<int>("2E", &err); | |
225 | ASSERT_NE(err, ""); | |
226 | } | |
227 | { | |
228 | std::string err; | |
229 | (void)strict_si_cast<int>("-2E", &err); | |
230 | ASSERT_NE(err, ""); | |
231 | } | |
232 | { | |
233 | std::string err; | |
234 | (void)strict_si_cast<int>("1T", &err); | |
235 | ASSERT_NE(err, ""); | |
236 | } | |
237 | { | |
238 | std::string err; | |
239 | (void)strict_si_cast<int64_t>("2E", &err); | |
240 | ASSERT_EQ(err, ""); | |
241 | } | |
242 | { | |
243 | std::string err; | |
244 | (void)strict_si_cast<int64_t>("-2E", &err); | |
245 | ASSERT_EQ(err, ""); | |
246 | } | |
247 | { | |
248 | std::string err; | |
249 | (void)strict_si_cast<int64_t>("1T", &err); | |
250 | ASSERT_EQ(err, ""); | |
251 | } | |
252 | } | |
253 | ||
254 | /* | |
255 | * Local Variables: | |
256 | * compile-command: "cd .. ; make unittest_strtol && ./unittest_strtol" | |
257 | * End: | |
258 | */ |