]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/strtol.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 Dreamhost
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.
15 #include "common/strtol.h"
19 #include "gtest/gtest.h"
21 static void test_strict_strtoll(const char *str
, long long expected
)
24 long long val
= strict_strtoll(str
, 10, &err
);
29 ASSERT_EQ(val
, expected
);
33 static void test_strict_strtol(const char *str
, long expected
)
36 long val
= strict_strtol(str
, 10, &err
);
41 ASSERT_EQ(val
, expected
);
45 static void test_strict_strtod(const char *str
, double expected
)
48 double val
= strict_strtod(str
, &err
);
53 // when comparing floats, use a margin of error
54 if ((expected
- 0.001 > val
) || (expected
+ 0.001 < val
)) {
55 ASSERT_EQ(val
, expected
);
60 static void test_strict_strtof(const char *str
, float expected
)
63 float val
= strict_strtof(str
, &err
);
68 // when comparing floats, use a margin of error
69 if ((expected
- 0.001 > val
) || (expected
+ 0.001 < val
)) {
70 ASSERT_EQ(val
, expected
);
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);
82 test_strict_strtol("208", 208);
83 test_strict_strtol("-4", -4);
84 test_strict_strtol("0", 0);
85 test_strict_strtol("2147483646", 2147483646);
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);
92 test_strict_strtod("-0.2", -0.2);
93 test_strict_strtod("0.1", 0.1);
94 test_strict_strtod("0", 0.0);
97 static void test_strict_strtoll_err(const char *str
)
100 strict_strtoll(str
, 10, &err
);
104 static void test_strict_strtol_err(const char *str
)
107 strict_strtol(str
, 10, &err
);
111 static void test_strict_strtod_err(const char *str
)
114 strict_strtod(str
, &err
);
118 static void test_strict_strtof_err(const char *str
)
121 strict_strtof(str
, &err
);
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
130 test_strict_strtol_err("35 aw shucks"); // invalid chars at end
131 test_strict_strtol_err("--0");
133 test_strict_strtod_err("345345.0-");
134 test_strict_strtod_err("34.0 garbo");
136 test_strict_strtof_err("0.05.0");
140 static void test_strict_sistrtoll(const char *str
)
143 strict_sistrtoll(str
, &err
);
147 static void test_strict_sistrtoll_units(const std::string
& foo
,
152 const char *str
= s
.c_str();
154 uint64_t r
= strict_sistrtoll(str
, &err
);
159 long long tmp
= strict_strtoll(str
, 10, &err2
);
162 ASSERT_EQ(tmp
, (long long)r
);
165 TEST(SIStrToLL
, WithUnits
) {
166 std::map
<char,int> units
;
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
);
184 TEST(SIStrToLL
, WithoutUnits
) {
185 test_strict_sistrtoll("1024");
186 test_strict_sistrtoll("1152921504606846976");
187 test_strict_sistrtoll("0");
190 static void test_strict_sistrtoll_err(const char *str
)
193 strict_sistrtoll(str
, &err
);
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
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
) {
223 // the SI prefix is way too large for `int`.
224 (void)strict_si_cast
<int>("2E", &err
);
229 (void)strict_si_cast
<int>("-2E", &err
);
234 (void)strict_si_cast
<int>("1T", &err
);
239 (void)strict_si_cast
<int64_t>("2E", &err
);
244 (void)strict_si_cast
<int64_t>("-2E", &err
);
249 (void)strict_si_cast
<int64_t>("1T", &err
);
256 * compile-command: "cd .. ; make unittest_strtol && ./unittest_strtol"