numbers_test.cc 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // This file tests string processing functions related to numeric values.
  15. #include "absl/strings/numbers.h"
  16. #include <sys/types.h>
  17. #include <cfenv> // NOLINT(build/c++11)
  18. #include <cinttypes>
  19. #include <climits>
  20. #include <cmath>
  21. #include <cstddef>
  22. #include <cstdint>
  23. #include <cstdio>
  24. #include <cstdlib>
  25. #include <cstring>
  26. #include <limits>
  27. #include <numeric>
  28. #include <random>
  29. #include <set>
  30. #include <string>
  31. #include <vector>
  32. #include "gmock/gmock.h"
  33. #include "gtest/gtest.h"
  34. #include "absl/base/internal/raw_logging.h"
  35. #include "absl/random/distributions.h"
  36. #include "absl/random/random.h"
  37. #include "absl/strings/internal/numbers_test_common.h"
  38. #include "absl/strings/internal/ostringstream.h"
  39. #include "absl/strings/internal/pow10_helper.h"
  40. #include "absl/strings/str_cat.h"
  41. namespace {
  42. using absl::SimpleAtoi;
  43. using absl::SimpleHexAtoi;
  44. using absl::numbers_internal::kSixDigitsToBufferSize;
  45. using absl::numbers_internal::safe_strto32_base;
  46. using absl::numbers_internal::safe_strto64_base;
  47. using absl::numbers_internal::safe_strtou32_base;
  48. using absl::numbers_internal::safe_strtou64_base;
  49. using absl::numbers_internal::SixDigitsToBuffer;
  50. using absl::strings_internal::Itoa;
  51. using absl::strings_internal::strtouint32_test_cases;
  52. using absl::strings_internal::strtouint64_test_cases;
  53. using testing::Eq;
  54. using testing::MatchesRegex;
  55. // Number of floats to test with.
  56. // 5,000,000 is a reasonable default for a test that only takes a few seconds.
  57. // 1,000,000,000+ triggers checking for all possible mantissa values for
  58. // double-precision tests. 2,000,000,000+ triggers checking for every possible
  59. // single-precision float.
  60. const int kFloatNumCases = 5000000;
  61. // This is a slow, brute-force routine to compute the exact base-10
  62. // representation of a double-precision floating-point number. It
  63. // is useful for debugging only.
  64. std::string PerfectDtoa(double d) {
  65. if (d == 0) return "0";
  66. if (d < 0) return "-" + PerfectDtoa(-d);
  67. // Basic theory: decompose d into mantissa and exp, where
  68. // d = mantissa * 2^exp, and exp is as close to zero as possible.
  69. int64_t mantissa, exp = 0;
  70. while (d >= 1ULL << 63) ++exp, d *= 0.5;
  71. while ((mantissa = d) != d) --exp, d *= 2.0;
  72. // Then convert mantissa to ASCII, and either double it (if
  73. // exp > 0) or halve it (if exp < 0) repeatedly. "halve it"
  74. // in this case means multiplying it by five and dividing by 10.
  75. constexpr int maxlen = 1100; // worst case is actually 1030 or so.
  76. char buf[maxlen + 5];
  77. for (int64_t num = mantissa, pos = maxlen; --pos >= 0;) {
  78. buf[pos] = '0' + (num % 10);
  79. num /= 10;
  80. }
  81. char* begin = &buf[0];
  82. char* end = buf + maxlen;
  83. for (int i = 0; i != exp; i += (exp > 0) ? 1 : -1) {
  84. int carry = 0;
  85. for (char* p = end; --p != begin;) {
  86. int dig = *p - '0';
  87. dig = dig * (exp > 0 ? 2 : 5) + carry;
  88. carry = dig / 10;
  89. dig %= 10;
  90. *p = '0' + dig;
  91. }
  92. }
  93. if (exp < 0) {
  94. // "dividing by 10" above means we have to add the decimal point.
  95. memmove(end + 1 + exp, end + exp, 1 - exp);
  96. end[exp] = '.';
  97. ++end;
  98. }
  99. while (*begin == '0' && begin[1] != '.') ++begin;
  100. return {begin, end};
  101. }
  102. TEST(ToString, PerfectDtoa) {
  103. EXPECT_THAT(PerfectDtoa(1), Eq("1"));
  104. EXPECT_THAT(PerfectDtoa(0.1),
  105. Eq("0.1000000000000000055511151231257827021181583404541015625"));
  106. EXPECT_THAT(PerfectDtoa(1e24), Eq("999999999999999983222784"));
  107. EXPECT_THAT(PerfectDtoa(5e-324), MatchesRegex("0.0000.*625"));
  108. for (int i = 0; i < 100; ++i) {
  109. for (double multiplier :
  110. {1e-300, 1e-200, 1e-100, 0.1, 1.0, 10.0, 1e100, 1e300}) {
  111. double d = multiplier * i;
  112. std::string s = PerfectDtoa(d);
  113. EXPECT_DOUBLE_EQ(d, strtod(s.c_str(), nullptr));
  114. }
  115. }
  116. }
  117. template <typename integer>
  118. struct MyInteger {
  119. integer i;
  120. explicit constexpr MyInteger(integer i) : i(i) {}
  121. constexpr operator integer() const { return i; }
  122. constexpr MyInteger operator+(MyInteger other) const { return i + other.i; }
  123. constexpr MyInteger operator-(MyInteger other) const { return i - other.i; }
  124. constexpr MyInteger operator*(MyInteger other) const { return i * other.i; }
  125. constexpr MyInteger operator/(MyInteger other) const { return i / other.i; }
  126. constexpr bool operator<(MyInteger other) const { return i < other.i; }
  127. constexpr bool operator<=(MyInteger other) const { return i <= other.i; }
  128. constexpr bool operator==(MyInteger other) const { return i == other.i; }
  129. constexpr bool operator>=(MyInteger other) const { return i >= other.i; }
  130. constexpr bool operator>(MyInteger other) const { return i > other.i; }
  131. constexpr bool operator!=(MyInteger other) const { return i != other.i; }
  132. integer as_integer() const { return i; }
  133. };
  134. typedef MyInteger<int64_t> MyInt64;
  135. typedef MyInteger<uint64_t> MyUInt64;
  136. void CheckInt32(int32_t x) {
  137. char buffer[absl::numbers_internal::kFastToBufferSize];
  138. char* actual = absl::numbers_internal::FastIntToBuffer(x, buffer);
  139. std::string expected = std::to_string(x);
  140. EXPECT_EQ(expected, std::string(buffer, actual)) << " Input " << x;
  141. char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, buffer);
  142. EXPECT_EQ(expected, std::string(buffer, generic_actual)) << " Input " << x;
  143. }
  144. void CheckInt64(int64_t x) {
  145. char buffer[absl::numbers_internal::kFastToBufferSize + 3];
  146. buffer[0] = '*';
  147. buffer[23] = '*';
  148. buffer[24] = '*';
  149. char* actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]);
  150. std::string expected = std::to_string(x);
  151. EXPECT_EQ(expected, std::string(&buffer[1], actual)) << " Input " << x;
  152. EXPECT_EQ(buffer[0], '*');
  153. EXPECT_EQ(buffer[23], '*');
  154. EXPECT_EQ(buffer[24], '*');
  155. char* my_actual =
  156. absl::numbers_internal::FastIntToBuffer(MyInt64(x), &buffer[1]);
  157. EXPECT_EQ(expected, std::string(&buffer[1], my_actual)) << " Input " << x;
  158. }
  159. void CheckUInt32(uint32_t x) {
  160. char buffer[absl::numbers_internal::kFastToBufferSize];
  161. char* actual = absl::numbers_internal::FastIntToBuffer(x, buffer);
  162. std::string expected = std::to_string(x);
  163. EXPECT_EQ(expected, std::string(buffer, actual)) << " Input " << x;
  164. char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, buffer);
  165. EXPECT_EQ(expected, std::string(buffer, generic_actual)) << " Input " << x;
  166. }
  167. void CheckUInt64(uint64_t x) {
  168. char buffer[absl::numbers_internal::kFastToBufferSize + 1];
  169. char* actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]);
  170. std::string expected = std::to_string(x);
  171. EXPECT_EQ(expected, std::string(&buffer[1], actual)) << " Input " << x;
  172. char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]);
  173. EXPECT_EQ(expected, std::string(&buffer[1], generic_actual))
  174. << " Input " << x;
  175. char* my_actual =
  176. absl::numbers_internal::FastIntToBuffer(MyUInt64(x), &buffer[1]);
  177. EXPECT_EQ(expected, std::string(&buffer[1], my_actual)) << " Input " << x;
  178. }
  179. void CheckHex64(uint64_t v) {
  180. char expected[16 + 1];
  181. std::string actual = absl::StrCat(absl::Hex(v, absl::kZeroPad16));
  182. snprintf(expected, sizeof(expected), "%016" PRIx64, static_cast<uint64_t>(v));
  183. EXPECT_EQ(expected, actual) << " Input " << v;
  184. actual = absl::StrCat(absl::Hex(v, absl::kSpacePad16));
  185. snprintf(expected, sizeof(expected), "%16" PRIx64, static_cast<uint64_t>(v));
  186. EXPECT_EQ(expected, actual) << " Input " << v;
  187. }
  188. TEST(Numbers, TestFastPrints) {
  189. for (int i = -100; i <= 100; i++) {
  190. CheckInt32(i);
  191. CheckInt64(i);
  192. }
  193. for (int i = 0; i <= 100; i++) {
  194. CheckUInt32(i);
  195. CheckUInt64(i);
  196. }
  197. // Test min int to make sure that works
  198. CheckInt32(INT_MIN);
  199. CheckInt32(INT_MAX);
  200. CheckInt64(LONG_MIN);
  201. CheckInt64(uint64_t{1000000000});
  202. CheckInt64(uint64_t{9999999999});
  203. CheckInt64(uint64_t{100000000000000});
  204. CheckInt64(uint64_t{999999999999999});
  205. CheckInt64(uint64_t{1000000000000000000});
  206. CheckInt64(uint64_t{1199999999999999999});
  207. CheckInt64(int64_t{-700000000000000000});
  208. CheckInt64(LONG_MAX);
  209. CheckUInt32(std::numeric_limits<uint32_t>::max());
  210. CheckUInt64(uint64_t{1000000000});
  211. CheckUInt64(uint64_t{9999999999});
  212. CheckUInt64(uint64_t{100000000000000});
  213. CheckUInt64(uint64_t{999999999999999});
  214. CheckUInt64(uint64_t{1000000000000000000});
  215. CheckUInt64(uint64_t{1199999999999999999});
  216. CheckUInt64(std::numeric_limits<uint64_t>::max());
  217. for (int i = 0; i < 10000; i++) {
  218. CheckHex64(i);
  219. }
  220. CheckHex64(uint64_t{0x123456789abcdef0});
  221. }
  222. template <typename int_type, typename in_val_type>
  223. void VerifySimpleAtoiGood(in_val_type in_value, int_type exp_value) {
  224. std::string s;
  225. // (u)int128 can be streamed but not StrCat'd.
  226. absl::strings_internal::OStringStream(&s) << in_value;
  227. int_type x = static_cast<int_type>(~exp_value);
  228. EXPECT_TRUE(SimpleAtoi(s, &x))
  229. << "in_value=" << in_value << " s=" << s << " x=" << x;
  230. EXPECT_EQ(exp_value, x);
  231. x = static_cast<int_type>(~exp_value);
  232. EXPECT_TRUE(SimpleAtoi(s.c_str(), &x));
  233. EXPECT_EQ(exp_value, x);
  234. }
  235. template <typename int_type, typename in_val_type>
  236. void VerifySimpleAtoiBad(in_val_type in_value) {
  237. std::string s;
  238. // (u)int128 can be streamed but not StrCat'd.
  239. absl::strings_internal::OStringStream(&s) << in_value;
  240. int_type x;
  241. EXPECT_FALSE(SimpleAtoi(s, &x));
  242. EXPECT_FALSE(SimpleAtoi(s.c_str(), &x));
  243. }
  244. TEST(NumbersTest, Atoi) {
  245. // SimpleAtoi(absl::string_view, int32_t)
  246. VerifySimpleAtoiGood<int32_t>(0, 0);
  247. VerifySimpleAtoiGood<int32_t>(42, 42);
  248. VerifySimpleAtoiGood<int32_t>(-42, -42);
  249. VerifySimpleAtoiGood<int32_t>(std::numeric_limits<int32_t>::min(),
  250. std::numeric_limits<int32_t>::min());
  251. VerifySimpleAtoiGood<int32_t>(std::numeric_limits<int32_t>::max(),
  252. std::numeric_limits<int32_t>::max());
  253. // SimpleAtoi(absl::string_view, uint32_t)
  254. VerifySimpleAtoiGood<uint32_t>(0, 0);
  255. VerifySimpleAtoiGood<uint32_t>(42, 42);
  256. VerifySimpleAtoiBad<uint32_t>(-42);
  257. VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<int32_t>::min());
  258. VerifySimpleAtoiGood<uint32_t>(std::numeric_limits<int32_t>::max(),
  259. std::numeric_limits<int32_t>::max());
  260. VerifySimpleAtoiGood<uint32_t>(std::numeric_limits<uint32_t>::max(),
  261. std::numeric_limits<uint32_t>::max());
  262. VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<int64_t>::min());
  263. VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<int64_t>::max());
  264. VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<uint64_t>::max());
  265. // SimpleAtoi(absl::string_view, int64_t)
  266. VerifySimpleAtoiGood<int64_t>(0, 0);
  267. VerifySimpleAtoiGood<int64_t>(42, 42);
  268. VerifySimpleAtoiGood<int64_t>(-42, -42);
  269. VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int32_t>::min(),
  270. std::numeric_limits<int32_t>::min());
  271. VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int32_t>::max(),
  272. std::numeric_limits<int32_t>::max());
  273. VerifySimpleAtoiGood<int64_t>(std::numeric_limits<uint32_t>::max(),
  274. std::numeric_limits<uint32_t>::max());
  275. VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int64_t>::min(),
  276. std::numeric_limits<int64_t>::min());
  277. VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int64_t>::max(),
  278. std::numeric_limits<int64_t>::max());
  279. VerifySimpleAtoiBad<int64_t>(std::numeric_limits<uint64_t>::max());
  280. // SimpleAtoi(absl::string_view, uint64_t)
  281. VerifySimpleAtoiGood<uint64_t>(0, 0);
  282. VerifySimpleAtoiGood<uint64_t>(42, 42);
  283. VerifySimpleAtoiBad<uint64_t>(-42);
  284. VerifySimpleAtoiBad<uint64_t>(std::numeric_limits<int32_t>::min());
  285. VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<int32_t>::max(),
  286. std::numeric_limits<int32_t>::max());
  287. VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<uint32_t>::max(),
  288. std::numeric_limits<uint32_t>::max());
  289. VerifySimpleAtoiBad<uint64_t>(std::numeric_limits<int64_t>::min());
  290. VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<int64_t>::max(),
  291. std::numeric_limits<int64_t>::max());
  292. VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<uint64_t>::max(),
  293. std::numeric_limits<uint64_t>::max());
  294. // SimpleAtoi(absl::string_view, absl::uint128)
  295. VerifySimpleAtoiGood<absl::uint128>(0, 0);
  296. VerifySimpleAtoiGood<absl::uint128>(42, 42);
  297. VerifySimpleAtoiBad<absl::uint128>(-42);
  298. VerifySimpleAtoiBad<absl::uint128>(std::numeric_limits<int32_t>::min());
  299. VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<int32_t>::max(),
  300. std::numeric_limits<int32_t>::max());
  301. VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<uint32_t>::max(),
  302. std::numeric_limits<uint32_t>::max());
  303. VerifySimpleAtoiBad<absl::uint128>(std::numeric_limits<int64_t>::min());
  304. VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<int64_t>::max(),
  305. std::numeric_limits<int64_t>::max());
  306. VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<uint64_t>::max(),
  307. std::numeric_limits<uint64_t>::max());
  308. VerifySimpleAtoiGood<absl::uint128>(
  309. std::numeric_limits<absl::uint128>::max(),
  310. std::numeric_limits<absl::uint128>::max());
  311. // SimpleAtoi(absl::string_view, absl::int128)
  312. VerifySimpleAtoiGood<absl::int128>(0, 0);
  313. VerifySimpleAtoiGood<absl::int128>(42, 42);
  314. VerifySimpleAtoiGood<absl::int128>(-42, -42);
  315. VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int32_t>::min(),
  316. std::numeric_limits<int32_t>::min());
  317. VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int32_t>::max(),
  318. std::numeric_limits<int32_t>::max());
  319. VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<uint32_t>::max(),
  320. std::numeric_limits<uint32_t>::max());
  321. VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int64_t>::min(),
  322. std::numeric_limits<int64_t>::min());
  323. VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int64_t>::max(),
  324. std::numeric_limits<int64_t>::max());
  325. VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<uint64_t>::max(),
  326. std::numeric_limits<uint64_t>::max());
  327. VerifySimpleAtoiGood<absl::int128>(
  328. std::numeric_limits<absl::int128>::min(),
  329. std::numeric_limits<absl::int128>::min());
  330. VerifySimpleAtoiGood<absl::int128>(
  331. std::numeric_limits<absl::int128>::max(),
  332. std::numeric_limits<absl::int128>::max());
  333. VerifySimpleAtoiBad<absl::int128>(std::numeric_limits<absl::uint128>::max());
  334. // Some other types
  335. VerifySimpleAtoiGood<int>(-42, -42);
  336. VerifySimpleAtoiGood<int32_t>(-42, -42);
  337. VerifySimpleAtoiGood<uint32_t>(42, 42);
  338. VerifySimpleAtoiGood<unsigned int>(42, 42);
  339. VerifySimpleAtoiGood<int64_t>(-42, -42);
  340. VerifySimpleAtoiGood<long>(-42, -42); // NOLINT: runtime-int
  341. VerifySimpleAtoiGood<uint64_t>(42, 42);
  342. VerifySimpleAtoiGood<size_t>(42, 42);
  343. VerifySimpleAtoiGood<std::string::size_type>(42, 42);
  344. }
  345. TEST(NumbersTest, Atod) {
  346. double d;
  347. EXPECT_TRUE(absl::SimpleAtod("nan", &d));
  348. EXPECT_TRUE(std::isnan(d));
  349. }
  350. TEST(NumbersTest, Prefixes) {
  351. double d;
  352. EXPECT_FALSE(absl::SimpleAtod("++1", &d));
  353. EXPECT_FALSE(absl::SimpleAtod("+-1", &d));
  354. EXPECT_FALSE(absl::SimpleAtod("-+1", &d));
  355. EXPECT_FALSE(absl::SimpleAtod("--1", &d));
  356. EXPECT_TRUE(absl::SimpleAtod("-1", &d));
  357. EXPECT_EQ(d, -1.);
  358. EXPECT_TRUE(absl::SimpleAtod("+1", &d));
  359. EXPECT_EQ(d, +1.);
  360. float f;
  361. EXPECT_FALSE(absl::SimpleAtof("++1", &f));
  362. EXPECT_FALSE(absl::SimpleAtof("+-1", &f));
  363. EXPECT_FALSE(absl::SimpleAtof("-+1", &f));
  364. EXPECT_FALSE(absl::SimpleAtof("--1", &f));
  365. EXPECT_TRUE(absl::SimpleAtof("-1", &f));
  366. EXPECT_EQ(f, -1.f);
  367. EXPECT_TRUE(absl::SimpleAtof("+1", &f));
  368. EXPECT_EQ(f, +1.f);
  369. }
  370. TEST(NumbersTest, Atoenum) {
  371. enum E01 {
  372. E01_zero = 0,
  373. E01_one = 1,
  374. };
  375. VerifySimpleAtoiGood<E01>(E01_zero, E01_zero);
  376. VerifySimpleAtoiGood<E01>(E01_one, E01_one);
  377. enum E_101 {
  378. E_101_minusone = -1,
  379. E_101_zero = 0,
  380. E_101_one = 1,
  381. };
  382. VerifySimpleAtoiGood<E_101>(E_101_minusone, E_101_minusone);
  383. VerifySimpleAtoiGood<E_101>(E_101_zero, E_101_zero);
  384. VerifySimpleAtoiGood<E_101>(E_101_one, E_101_one);
  385. enum E_bigint {
  386. E_bigint_zero = 0,
  387. E_bigint_one = 1,
  388. E_bigint_max31 = static_cast<int32_t>(0x7FFFFFFF),
  389. };
  390. VerifySimpleAtoiGood<E_bigint>(E_bigint_zero, E_bigint_zero);
  391. VerifySimpleAtoiGood<E_bigint>(E_bigint_one, E_bigint_one);
  392. VerifySimpleAtoiGood<E_bigint>(E_bigint_max31, E_bigint_max31);
  393. enum E_fullint {
  394. E_fullint_zero = 0,
  395. E_fullint_one = 1,
  396. E_fullint_max31 = static_cast<int32_t>(0x7FFFFFFF),
  397. E_fullint_min32 = INT32_MIN,
  398. };
  399. VerifySimpleAtoiGood<E_fullint>(E_fullint_zero, E_fullint_zero);
  400. VerifySimpleAtoiGood<E_fullint>(E_fullint_one, E_fullint_one);
  401. VerifySimpleAtoiGood<E_fullint>(E_fullint_max31, E_fullint_max31);
  402. VerifySimpleAtoiGood<E_fullint>(E_fullint_min32, E_fullint_min32);
  403. enum E_biguint {
  404. E_biguint_zero = 0,
  405. E_biguint_one = 1,
  406. E_biguint_max31 = static_cast<uint32_t>(0x7FFFFFFF),
  407. E_biguint_max32 = static_cast<uint32_t>(0xFFFFFFFF),
  408. };
  409. VerifySimpleAtoiGood<E_biguint>(E_biguint_zero, E_biguint_zero);
  410. VerifySimpleAtoiGood<E_biguint>(E_biguint_one, E_biguint_one);
  411. VerifySimpleAtoiGood<E_biguint>(E_biguint_max31, E_biguint_max31);
  412. VerifySimpleAtoiGood<E_biguint>(E_biguint_max32, E_biguint_max32);
  413. }
  414. template <typename int_type, typename in_val_type>
  415. void VerifySimpleHexAtoiGood(in_val_type in_value, int_type exp_value) {
  416. std::string s;
  417. // uint128 can be streamed but not StrCat'd
  418. absl::strings_internal::OStringStream strm(&s);
  419. if (in_value >= 0) {
  420. strm << std::hex << in_value;
  421. } else {
  422. // Inefficient for small integers, but works with all integral types.
  423. strm << "-" << std::hex << -absl::uint128(in_value);
  424. }
  425. int_type x = static_cast<int_type>(~exp_value);
  426. EXPECT_TRUE(SimpleHexAtoi(s, &x))
  427. << "in_value=" << std::hex << in_value << " s=" << s << " x=" << x;
  428. EXPECT_EQ(exp_value, x);
  429. x = static_cast<int_type>(~exp_value);
  430. EXPECT_TRUE(SimpleHexAtoi(
  431. s.c_str(), &x)); // NOLINT: readability-redundant-string-conversions
  432. EXPECT_EQ(exp_value, x);
  433. }
  434. template <typename int_type, typename in_val_type>
  435. void VerifySimpleHexAtoiBad(in_val_type in_value) {
  436. std::string s;
  437. // uint128 can be streamed but not StrCat'd
  438. absl::strings_internal::OStringStream strm(&s);
  439. if (in_value >= 0) {
  440. strm << std::hex << in_value;
  441. } else {
  442. // Inefficient for small integers, but works with all integral types.
  443. strm << "-" << std::hex << -absl::uint128(in_value);
  444. }
  445. int_type x;
  446. EXPECT_FALSE(SimpleHexAtoi(s, &x));
  447. EXPECT_FALSE(SimpleHexAtoi(
  448. s.c_str(), &x)); // NOLINT: readability-redundant-string-conversions
  449. }
  450. TEST(NumbersTest, HexAtoi) {
  451. // SimpleHexAtoi(absl::string_view, int32_t)
  452. VerifySimpleHexAtoiGood<int32_t>(0, 0);
  453. VerifySimpleHexAtoiGood<int32_t>(0x42, 0x42);
  454. VerifySimpleHexAtoiGood<int32_t>(-0x42, -0x42);
  455. VerifySimpleHexAtoiGood<int32_t>(std::numeric_limits<int32_t>::min(),
  456. std::numeric_limits<int32_t>::min());
  457. VerifySimpleHexAtoiGood<int32_t>(std::numeric_limits<int32_t>::max(),
  458. std::numeric_limits<int32_t>::max());
  459. // SimpleHexAtoi(absl::string_view, uint32_t)
  460. VerifySimpleHexAtoiGood<uint32_t>(0, 0);
  461. VerifySimpleHexAtoiGood<uint32_t>(0x42, 0x42);
  462. VerifySimpleHexAtoiBad<uint32_t>(-0x42);
  463. VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<int32_t>::min());
  464. VerifySimpleHexAtoiGood<uint32_t>(std::numeric_limits<int32_t>::max(),
  465. std::numeric_limits<int32_t>::max());
  466. VerifySimpleHexAtoiGood<uint32_t>(std::numeric_limits<uint32_t>::max(),
  467. std::numeric_limits<uint32_t>::max());
  468. VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<int64_t>::min());
  469. VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<int64_t>::max());
  470. VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<uint64_t>::max());
  471. // SimpleHexAtoi(absl::string_view, int64_t)
  472. VerifySimpleHexAtoiGood<int64_t>(0, 0);
  473. VerifySimpleHexAtoiGood<int64_t>(0x42, 0x42);
  474. VerifySimpleHexAtoiGood<int64_t>(-0x42, -0x42);
  475. VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int32_t>::min(),
  476. std::numeric_limits<int32_t>::min());
  477. VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int32_t>::max(),
  478. std::numeric_limits<int32_t>::max());
  479. VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<uint32_t>::max(),
  480. std::numeric_limits<uint32_t>::max());
  481. VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int64_t>::min(),
  482. std::numeric_limits<int64_t>::min());
  483. VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int64_t>::max(),
  484. std::numeric_limits<int64_t>::max());
  485. VerifySimpleHexAtoiBad<int64_t>(std::numeric_limits<uint64_t>::max());
  486. // SimpleHexAtoi(absl::string_view, uint64_t)
  487. VerifySimpleHexAtoiGood<uint64_t>(0, 0);
  488. VerifySimpleHexAtoiGood<uint64_t>(0x42, 0x42);
  489. VerifySimpleHexAtoiBad<uint64_t>(-0x42);
  490. VerifySimpleHexAtoiBad<uint64_t>(std::numeric_limits<int32_t>::min());
  491. VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<int32_t>::max(),
  492. std::numeric_limits<int32_t>::max());
  493. VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<uint32_t>::max(),
  494. std::numeric_limits<uint32_t>::max());
  495. VerifySimpleHexAtoiBad<uint64_t>(std::numeric_limits<int64_t>::min());
  496. VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<int64_t>::max(),
  497. std::numeric_limits<int64_t>::max());
  498. VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<uint64_t>::max(),
  499. std::numeric_limits<uint64_t>::max());
  500. // SimpleHexAtoi(absl::string_view, absl::uint128)
  501. VerifySimpleHexAtoiGood<absl::uint128>(0, 0);
  502. VerifySimpleHexAtoiGood<absl::uint128>(0x42, 0x42);
  503. VerifySimpleHexAtoiBad<absl::uint128>(-0x42);
  504. VerifySimpleHexAtoiBad<absl::uint128>(std::numeric_limits<int32_t>::min());
  505. VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<int32_t>::max(),
  506. std::numeric_limits<int32_t>::max());
  507. VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<uint32_t>::max(),
  508. std::numeric_limits<uint32_t>::max());
  509. VerifySimpleHexAtoiBad<absl::uint128>(std::numeric_limits<int64_t>::min());
  510. VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<int64_t>::max(),
  511. std::numeric_limits<int64_t>::max());
  512. VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<uint64_t>::max(),
  513. std::numeric_limits<uint64_t>::max());
  514. VerifySimpleHexAtoiGood<absl::uint128>(
  515. std::numeric_limits<absl::uint128>::max(),
  516. std::numeric_limits<absl::uint128>::max());
  517. // Some other types
  518. VerifySimpleHexAtoiGood<int>(-0x42, -0x42);
  519. VerifySimpleHexAtoiGood<int32_t>(-0x42, -0x42);
  520. VerifySimpleHexAtoiGood<uint32_t>(0x42, 0x42);
  521. VerifySimpleHexAtoiGood<unsigned int>(0x42, 0x42);
  522. VerifySimpleHexAtoiGood<int64_t>(-0x42, -0x42);
  523. VerifySimpleHexAtoiGood<long>(-0x42, -0x42); // NOLINT: runtime-int
  524. VerifySimpleHexAtoiGood<uint64_t>(0x42, 0x42);
  525. VerifySimpleHexAtoiGood<size_t>(0x42, 0x42);
  526. VerifySimpleHexAtoiGood<std::string::size_type>(0x42, 0x42);
  527. // Number prefix
  528. int32_t value;
  529. EXPECT_TRUE(safe_strto32_base("0x34234324", &value, 16));
  530. EXPECT_EQ(0x34234324, value);
  531. EXPECT_TRUE(safe_strto32_base("0X34234324", &value, 16));
  532. EXPECT_EQ(0x34234324, value);
  533. // ASCII whitespace
  534. EXPECT_TRUE(safe_strto32_base(" \t\n 34234324", &value, 16));
  535. EXPECT_EQ(0x34234324, value);
  536. EXPECT_TRUE(safe_strto32_base("34234324 \t\n ", &value, 16));
  537. EXPECT_EQ(0x34234324, value);
  538. }
  539. TEST(stringtest, safe_strto32_base) {
  540. int32_t value;
  541. EXPECT_TRUE(safe_strto32_base("0x34234324", &value, 16));
  542. EXPECT_EQ(0x34234324, value);
  543. EXPECT_TRUE(safe_strto32_base("0X34234324", &value, 16));
  544. EXPECT_EQ(0x34234324, value);
  545. EXPECT_TRUE(safe_strto32_base("34234324", &value, 16));
  546. EXPECT_EQ(0x34234324, value);
  547. EXPECT_TRUE(safe_strto32_base("0", &value, 16));
  548. EXPECT_EQ(0, value);
  549. EXPECT_TRUE(safe_strto32_base(" \t\n -0x34234324", &value, 16));
  550. EXPECT_EQ(-0x34234324, value);
  551. EXPECT_TRUE(safe_strto32_base(" \t\n -34234324", &value, 16));
  552. EXPECT_EQ(-0x34234324, value);
  553. EXPECT_TRUE(safe_strto32_base("7654321", &value, 8));
  554. EXPECT_EQ(07654321, value);
  555. EXPECT_TRUE(safe_strto32_base("-01234", &value, 8));
  556. EXPECT_EQ(-01234, value);
  557. EXPECT_FALSE(safe_strto32_base("1834", &value, 8));
  558. // Autodetect base.
  559. EXPECT_TRUE(safe_strto32_base("0", &value, 0));
  560. EXPECT_EQ(0, value);
  561. EXPECT_TRUE(safe_strto32_base("077", &value, 0));
  562. EXPECT_EQ(077, value); // Octal interpretation
  563. // Leading zero indicates octal, but then followed by invalid digit.
  564. EXPECT_FALSE(safe_strto32_base("088", &value, 0));
  565. // Leading 0x indicated hex, but then followed by invalid digit.
  566. EXPECT_FALSE(safe_strto32_base("0xG", &value, 0));
  567. // Base-10 version.
  568. EXPECT_TRUE(safe_strto32_base("34234324", &value, 10));
  569. EXPECT_EQ(34234324, value);
  570. EXPECT_TRUE(safe_strto32_base("0", &value, 10));
  571. EXPECT_EQ(0, value);
  572. EXPECT_TRUE(safe_strto32_base(" \t\n -34234324", &value, 10));
  573. EXPECT_EQ(-34234324, value);
  574. EXPECT_TRUE(safe_strto32_base("34234324 \n\t ", &value, 10));
  575. EXPECT_EQ(34234324, value);
  576. // Invalid ints.
  577. EXPECT_FALSE(safe_strto32_base("", &value, 10));
  578. EXPECT_FALSE(safe_strto32_base(" ", &value, 10));
  579. EXPECT_FALSE(safe_strto32_base("abc", &value, 10));
  580. EXPECT_FALSE(safe_strto32_base("34234324a", &value, 10));
  581. EXPECT_FALSE(safe_strto32_base("34234.3", &value, 10));
  582. // Out of bounds.
  583. EXPECT_FALSE(safe_strto32_base("2147483648", &value, 10));
  584. EXPECT_FALSE(safe_strto32_base("-2147483649", &value, 10));
  585. // String version.
  586. EXPECT_TRUE(safe_strto32_base(std::string("0x1234"), &value, 16));
  587. EXPECT_EQ(0x1234, value);
  588. // Base-10 string version.
  589. EXPECT_TRUE(safe_strto32_base("1234", &value, 10));
  590. EXPECT_EQ(1234, value);
  591. }
  592. TEST(stringtest, safe_strto32_range) {
  593. // These tests verify underflow/overflow behaviour.
  594. int32_t value;
  595. EXPECT_FALSE(safe_strto32_base("2147483648", &value, 10));
  596. EXPECT_EQ(std::numeric_limits<int32_t>::max(), value);
  597. EXPECT_TRUE(safe_strto32_base("-2147483648", &value, 10));
  598. EXPECT_EQ(std::numeric_limits<int32_t>::min(), value);
  599. EXPECT_FALSE(safe_strto32_base("-2147483649", &value, 10));
  600. EXPECT_EQ(std::numeric_limits<int32_t>::min(), value);
  601. }
  602. TEST(stringtest, safe_strto64_range) {
  603. // These tests verify underflow/overflow behaviour.
  604. int64_t value;
  605. EXPECT_FALSE(safe_strto64_base("9223372036854775808", &value, 10));
  606. EXPECT_EQ(std::numeric_limits<int64_t>::max(), value);
  607. EXPECT_TRUE(safe_strto64_base("-9223372036854775808", &value, 10));
  608. EXPECT_EQ(std::numeric_limits<int64_t>::min(), value);
  609. EXPECT_FALSE(safe_strto64_base("-9223372036854775809", &value, 10));
  610. EXPECT_EQ(std::numeric_limits<int64_t>::min(), value);
  611. }
  612. TEST(stringtest, safe_strto32_leading_substring) {
  613. // These tests verify this comment in numbers.h:
  614. // On error, returns false, and sets *value to: [...]
  615. // conversion of leading substring if available ("123@@@" -> 123)
  616. // 0 if no leading substring available
  617. int32_t value;
  618. EXPECT_FALSE(safe_strto32_base("04069@@@", &value, 10));
  619. EXPECT_EQ(4069, value);
  620. EXPECT_FALSE(safe_strto32_base("04069@@@", &value, 8));
  621. EXPECT_EQ(0406, value);
  622. EXPECT_FALSE(safe_strto32_base("04069balloons", &value, 10));
  623. EXPECT_EQ(4069, value);
  624. EXPECT_FALSE(safe_strto32_base("04069balloons", &value, 16));
  625. EXPECT_EQ(0x4069ba, value);
  626. EXPECT_FALSE(safe_strto32_base("@@@", &value, 10));
  627. EXPECT_EQ(0, value); // there was no leading substring
  628. }
  629. TEST(stringtest, safe_strto64_leading_substring) {
  630. // These tests verify this comment in numbers.h:
  631. // On error, returns false, and sets *value to: [...]
  632. // conversion of leading substring if available ("123@@@" -> 123)
  633. // 0 if no leading substring available
  634. int64_t value;
  635. EXPECT_FALSE(safe_strto64_base("04069@@@", &value, 10));
  636. EXPECT_EQ(4069, value);
  637. EXPECT_FALSE(safe_strto64_base("04069@@@", &value, 8));
  638. EXPECT_EQ(0406, value);
  639. EXPECT_FALSE(safe_strto64_base("04069balloons", &value, 10));
  640. EXPECT_EQ(4069, value);
  641. EXPECT_FALSE(safe_strto64_base("04069balloons", &value, 16));
  642. EXPECT_EQ(0x4069ba, value);
  643. EXPECT_FALSE(safe_strto64_base("@@@", &value, 10));
  644. EXPECT_EQ(0, value); // there was no leading substring
  645. }
  646. TEST(stringtest, safe_strto64_base) {
  647. int64_t value;
  648. EXPECT_TRUE(safe_strto64_base("0x3423432448783446", &value, 16));
  649. EXPECT_EQ(int64_t{0x3423432448783446}, value);
  650. EXPECT_TRUE(safe_strto64_base("3423432448783446", &value, 16));
  651. EXPECT_EQ(int64_t{0x3423432448783446}, value);
  652. EXPECT_TRUE(safe_strto64_base("0", &value, 16));
  653. EXPECT_EQ(0, value);
  654. EXPECT_TRUE(safe_strto64_base(" \t\n -0x3423432448783446", &value, 16));
  655. EXPECT_EQ(int64_t{-0x3423432448783446}, value);
  656. EXPECT_TRUE(safe_strto64_base(" \t\n -3423432448783446", &value, 16));
  657. EXPECT_EQ(int64_t{-0x3423432448783446}, value);
  658. EXPECT_TRUE(safe_strto64_base("123456701234567012", &value, 8));
  659. EXPECT_EQ(int64_t{0123456701234567012}, value);
  660. EXPECT_TRUE(safe_strto64_base("-017777777777777", &value, 8));
  661. EXPECT_EQ(int64_t{-017777777777777}, value);
  662. EXPECT_FALSE(safe_strto64_base("19777777777777", &value, 8));
  663. // Autodetect base.
  664. EXPECT_TRUE(safe_strto64_base("0", &value, 0));
  665. EXPECT_EQ(0, value);
  666. EXPECT_TRUE(safe_strto64_base("077", &value, 0));
  667. EXPECT_EQ(077, value); // Octal interpretation
  668. // Leading zero indicates octal, but then followed by invalid digit.
  669. EXPECT_FALSE(safe_strto64_base("088", &value, 0));
  670. // Leading 0x indicated hex, but then followed by invalid digit.
  671. EXPECT_FALSE(safe_strto64_base("0xG", &value, 0));
  672. // Base-10 version.
  673. EXPECT_TRUE(safe_strto64_base("34234324487834466", &value, 10));
  674. EXPECT_EQ(int64_t{34234324487834466}, value);
  675. EXPECT_TRUE(safe_strto64_base("0", &value, 10));
  676. EXPECT_EQ(0, value);
  677. EXPECT_TRUE(safe_strto64_base(" \t\n -34234324487834466", &value, 10));
  678. EXPECT_EQ(int64_t{-34234324487834466}, value);
  679. EXPECT_TRUE(safe_strto64_base("34234324487834466 \n\t ", &value, 10));
  680. EXPECT_EQ(int64_t{34234324487834466}, value);
  681. // Invalid ints.
  682. EXPECT_FALSE(safe_strto64_base("", &value, 10));
  683. EXPECT_FALSE(safe_strto64_base(" ", &value, 10));
  684. EXPECT_FALSE(safe_strto64_base("abc", &value, 10));
  685. EXPECT_FALSE(safe_strto64_base("34234324487834466a", &value, 10));
  686. EXPECT_FALSE(safe_strto64_base("34234487834466.3", &value, 10));
  687. // Out of bounds.
  688. EXPECT_FALSE(safe_strto64_base("9223372036854775808", &value, 10));
  689. EXPECT_FALSE(safe_strto64_base("-9223372036854775809", &value, 10));
  690. // String version.
  691. EXPECT_TRUE(safe_strto64_base(std::string("0x1234"), &value, 16));
  692. EXPECT_EQ(0x1234, value);
  693. // Base-10 string version.
  694. EXPECT_TRUE(safe_strto64_base("1234", &value, 10));
  695. EXPECT_EQ(1234, value);
  696. }
  697. const size_t kNumRandomTests = 10000;
  698. template <typename IntType>
  699. void test_random_integer_parse_base(bool (*parse_func)(absl::string_view,
  700. IntType* value,
  701. int base)) {
  702. using RandomEngine = std::minstd_rand0;
  703. std::random_device rd;
  704. RandomEngine rng(rd());
  705. std::uniform_int_distribution<IntType> random_int(
  706. std::numeric_limits<IntType>::min());
  707. std::uniform_int_distribution<int> random_base(2, 35);
  708. for (size_t i = 0; i < kNumRandomTests; i++) {
  709. IntType value = random_int(rng);
  710. int base = random_base(rng);
  711. std::string str_value;
  712. EXPECT_TRUE(Itoa<IntType>(value, base, &str_value));
  713. IntType parsed_value;
  714. // Test successful parse
  715. EXPECT_TRUE(parse_func(str_value, &parsed_value, base));
  716. EXPECT_EQ(parsed_value, value);
  717. // Test overflow
  718. EXPECT_FALSE(
  719. parse_func(absl::StrCat(std::numeric_limits<IntType>::max(), value),
  720. &parsed_value, base));
  721. // Test underflow
  722. if (std::numeric_limits<IntType>::min() < 0) {
  723. EXPECT_FALSE(
  724. parse_func(absl::StrCat(std::numeric_limits<IntType>::min(), value),
  725. &parsed_value, base));
  726. } else {
  727. EXPECT_FALSE(parse_func(absl::StrCat("-", value), &parsed_value, base));
  728. }
  729. }
  730. }
  731. TEST(stringtest, safe_strto32_random) {
  732. test_random_integer_parse_base<int32_t>(&safe_strto32_base);
  733. }
  734. TEST(stringtest, safe_strto64_random) {
  735. test_random_integer_parse_base<int64_t>(&safe_strto64_base);
  736. }
  737. TEST(stringtest, safe_strtou32_random) {
  738. test_random_integer_parse_base<uint32_t>(&safe_strtou32_base);
  739. }
  740. TEST(stringtest, safe_strtou64_random) {
  741. test_random_integer_parse_base<uint64_t>(&safe_strtou64_base);
  742. }
  743. TEST(stringtest, safe_strtou128_random) {
  744. // random number generators don't work for uint128, and
  745. // uint128 can be streamed but not StrCat'd, so this code must be custom
  746. // implemented for uint128, but is generally the same as what's above.
  747. // test_random_integer_parse_base<absl::uint128>(
  748. // &absl::numbers_internal::safe_strtou128_base);
  749. using RandomEngine = std::minstd_rand0;
  750. using IntType = absl::uint128;
  751. constexpr auto parse_func = &absl::numbers_internal::safe_strtou128_base;
  752. std::random_device rd;
  753. RandomEngine rng(rd());
  754. std::uniform_int_distribution<uint64_t> random_uint64(
  755. std::numeric_limits<uint64_t>::min());
  756. std::uniform_int_distribution<int> random_base(2, 35);
  757. for (size_t i = 0; i < kNumRandomTests; i++) {
  758. IntType value = random_uint64(rng);
  759. value = (value << 64) + random_uint64(rng);
  760. int base = random_base(rng);
  761. std::string str_value;
  762. EXPECT_TRUE(Itoa<IntType>(value, base, &str_value));
  763. IntType parsed_value;
  764. // Test successful parse
  765. EXPECT_TRUE(parse_func(str_value, &parsed_value, base));
  766. EXPECT_EQ(parsed_value, value);
  767. // Test overflow
  768. std::string s;
  769. absl::strings_internal::OStringStream(&s)
  770. << std::numeric_limits<IntType>::max() << value;
  771. EXPECT_FALSE(parse_func(s, &parsed_value, base));
  772. // Test underflow
  773. s.clear();
  774. absl::strings_internal::OStringStream(&s) << "-" << value;
  775. EXPECT_FALSE(parse_func(s, &parsed_value, base));
  776. }
  777. }
  778. TEST(stringtest, safe_strto128_random) {
  779. // random number generators don't work for int128, and
  780. // int128 can be streamed but not StrCat'd, so this code must be custom
  781. // implemented for int128, but is generally the same as what's above.
  782. // test_random_integer_parse_base<absl::int128>(
  783. // &absl::numbers_internal::safe_strto128_base);
  784. using RandomEngine = std::minstd_rand0;
  785. using IntType = absl::int128;
  786. constexpr auto parse_func = &absl::numbers_internal::safe_strto128_base;
  787. std::random_device rd;
  788. RandomEngine rng(rd());
  789. std::uniform_int_distribution<int64_t> random_int64(
  790. std::numeric_limits<int64_t>::min());
  791. std::uniform_int_distribution<uint64_t> random_uint64(
  792. std::numeric_limits<uint64_t>::min());
  793. std::uniform_int_distribution<int> random_base(2, 35);
  794. for (size_t i = 0; i < kNumRandomTests; ++i) {
  795. int64_t high = random_int64(rng);
  796. uint64_t low = random_uint64(rng);
  797. IntType value = absl::MakeInt128(high, low);
  798. int base = random_base(rng);
  799. std::string str_value;
  800. EXPECT_TRUE(Itoa<IntType>(value, base, &str_value));
  801. IntType parsed_value;
  802. // Test successful parse
  803. EXPECT_TRUE(parse_func(str_value, &parsed_value, base));
  804. EXPECT_EQ(parsed_value, value);
  805. // Test overflow
  806. std::string s;
  807. absl::strings_internal::OStringStream(&s)
  808. << std::numeric_limits<IntType>::max() << value;
  809. EXPECT_FALSE(parse_func(s, &parsed_value, base));
  810. // Test underflow
  811. s.clear();
  812. absl::strings_internal::OStringStream(&s)
  813. << std::numeric_limits<IntType>::min() << value;
  814. EXPECT_FALSE(parse_func(s, &parsed_value, base));
  815. }
  816. }
  817. TEST(stringtest, safe_strtou32_base) {
  818. for (int i = 0; strtouint32_test_cases()[i].str != nullptr; ++i) {
  819. const auto& e = strtouint32_test_cases()[i];
  820. uint32_t value;
  821. EXPECT_EQ(e.expect_ok, safe_strtou32_base(e.str, &value, e.base))
  822. << "str=\"" << e.str << "\" base=" << e.base;
  823. if (e.expect_ok) {
  824. EXPECT_EQ(e.expected, value) << "i=" << i << " str=\"" << e.str
  825. << "\" base=" << e.base;
  826. }
  827. }
  828. }
  829. TEST(stringtest, safe_strtou32_base_length_delimited) {
  830. for (int i = 0; strtouint32_test_cases()[i].str != nullptr; ++i) {
  831. const auto& e = strtouint32_test_cases()[i];
  832. std::string tmp(e.str);
  833. tmp.append("12"); // Adds garbage at the end.
  834. uint32_t value;
  835. EXPECT_EQ(e.expect_ok,
  836. safe_strtou32_base(absl::string_view(tmp.data(), strlen(e.str)),
  837. &value, e.base))
  838. << "str=\"" << e.str << "\" base=" << e.base;
  839. if (e.expect_ok) {
  840. EXPECT_EQ(e.expected, value) << "i=" << i << " str=" << e.str
  841. << " base=" << e.base;
  842. }
  843. }
  844. }
  845. TEST(stringtest, safe_strtou64_base) {
  846. for (int i = 0; strtouint64_test_cases()[i].str != nullptr; ++i) {
  847. const auto& e = strtouint64_test_cases()[i];
  848. uint64_t value;
  849. EXPECT_EQ(e.expect_ok, safe_strtou64_base(e.str, &value, e.base))
  850. << "str=\"" << e.str << "\" base=" << e.base;
  851. if (e.expect_ok) {
  852. EXPECT_EQ(e.expected, value) << "str=" << e.str << " base=" << e.base;
  853. }
  854. }
  855. }
  856. TEST(stringtest, safe_strtou64_base_length_delimited) {
  857. for (int i = 0; strtouint64_test_cases()[i].str != nullptr; ++i) {
  858. const auto& e = strtouint64_test_cases()[i];
  859. std::string tmp(e.str);
  860. tmp.append("12"); // Adds garbage at the end.
  861. uint64_t value;
  862. EXPECT_EQ(e.expect_ok,
  863. safe_strtou64_base(absl::string_view(tmp.data(), strlen(e.str)),
  864. &value, e.base))
  865. << "str=\"" << e.str << "\" base=" << e.base;
  866. if (e.expect_ok) {
  867. EXPECT_EQ(e.expected, value) << "str=\"" << e.str << "\" base=" << e.base;
  868. }
  869. }
  870. }
  871. // feenableexcept() and fedisableexcept() are extensions supported by some libc
  872. // implementations.
  873. #if defined(__GLIBC__) || defined(__BIONIC__)
  874. #define ABSL_HAVE_FEENABLEEXCEPT 1
  875. #define ABSL_HAVE_FEDISABLEEXCEPT 1
  876. #endif
  877. class SimpleDtoaTest : public testing::Test {
  878. protected:
  879. void SetUp() override {
  880. // Store the current floating point env & clear away any pending exceptions.
  881. feholdexcept(&fp_env_);
  882. #ifdef ABSL_HAVE_FEENABLEEXCEPT
  883. // Turn on floating point exceptions.
  884. feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
  885. #endif
  886. }
  887. void TearDown() override {
  888. // Restore the floating point environment to the original state.
  889. // In theory fedisableexcept is unnecessary; fesetenv will also do it.
  890. // In practice, our toolchains have subtle bugs.
  891. #ifdef ABSL_HAVE_FEDISABLEEXCEPT
  892. fedisableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
  893. #endif
  894. fesetenv(&fp_env_);
  895. }
  896. std::string ToNineDigits(double value) {
  897. char buffer[16]; // more than enough for %.9g
  898. snprintf(buffer, sizeof(buffer), "%.9g", value);
  899. return buffer;
  900. }
  901. fenv_t fp_env_;
  902. };
  903. // Run the given runnable functor for "cases" test cases, chosen over the
  904. // available range of float. pi and e and 1/e are seeded, and then all
  905. // available integer powers of 2 and 10 are multiplied against them. In
  906. // addition to trying all those values, we try the next higher and next lower
  907. // float, and then we add additional test cases evenly distributed between them.
  908. // Each test case is passed to runnable as both a positive and negative value.
  909. template <typename R>
  910. void ExhaustiveFloat(uint32_t cases, R&& runnable) {
  911. runnable(0.0f);
  912. runnable(-0.0f);
  913. if (cases >= 2e9) { // more than 2 billion? Might as well run them all.
  914. for (float f = 0; f < std::numeric_limits<float>::max(); ) {
  915. f = nextafterf(f, std::numeric_limits<float>::max());
  916. runnable(-f);
  917. runnable(f);
  918. }
  919. return;
  920. }
  921. std::set<float> floats = {3.4028234e38f};
  922. for (float f : {1.0, 3.14159265, 2.718281828, 1 / 2.718281828}) {
  923. for (float testf = f; testf != 0; testf *= 0.1f) floats.insert(testf);
  924. for (float testf = f; testf != 0; testf *= 0.5f) floats.insert(testf);
  925. for (float testf = f; testf < 3e38f / 2; testf *= 2.0f)
  926. floats.insert(testf);
  927. for (float testf = f; testf < 3e38f / 10; testf *= 10) floats.insert(testf);
  928. }
  929. float last = *floats.begin();
  930. runnable(last);
  931. runnable(-last);
  932. int iters_per_float = cases / floats.size();
  933. if (iters_per_float == 0) iters_per_float = 1;
  934. for (float f : floats) {
  935. if (f == last) continue;
  936. float testf = std::nextafter(last, std::numeric_limits<float>::max());
  937. runnable(testf);
  938. runnable(-testf);
  939. last = testf;
  940. if (f == last) continue;
  941. double step = (double{f} - last) / iters_per_float;
  942. for (double d = last + step; d < f; d += step) {
  943. testf = d;
  944. if (testf != last) {
  945. runnable(testf);
  946. runnable(-testf);
  947. last = testf;
  948. }
  949. }
  950. testf = std::nextafter(f, 0.0f);
  951. if (testf > last) {
  952. runnable(testf);
  953. runnable(-testf);
  954. last = testf;
  955. }
  956. if (f != last) {
  957. runnable(f);
  958. runnable(-f);
  959. last = f;
  960. }
  961. }
  962. }
  963. TEST_F(SimpleDtoaTest, ExhaustiveDoubleToSixDigits) {
  964. uint64_t test_count = 0;
  965. std::vector<double> mismatches;
  966. auto checker = [&](double d) {
  967. if (d != d) return; // rule out NaNs
  968. ++test_count;
  969. char sixdigitsbuf[kSixDigitsToBufferSize] = {0};
  970. SixDigitsToBuffer(d, sixdigitsbuf);
  971. char snprintfbuf[kSixDigitsToBufferSize] = {0};
  972. snprintf(snprintfbuf, kSixDigitsToBufferSize, "%g", d);
  973. if (strcmp(sixdigitsbuf, snprintfbuf) != 0) {
  974. mismatches.push_back(d);
  975. if (mismatches.size() < 10) {
  976. ABSL_RAW_LOG(ERROR, "%s",
  977. absl::StrCat("Six-digit failure with double. ", "d=", d,
  978. "=", d, " sixdigits=", sixdigitsbuf,
  979. " printf(%g)=", snprintfbuf)
  980. .c_str());
  981. }
  982. }
  983. };
  984. // Some quick sanity checks...
  985. checker(5e-324);
  986. checker(1e-308);
  987. checker(1.0);
  988. checker(1.000005);
  989. checker(1.7976931348623157e308);
  990. checker(0.00390625);
  991. #ifndef _MSC_VER
  992. // on MSVC, snprintf() rounds it to 0.00195313. SixDigitsToBuffer() rounds it
  993. // to 0.00195312 (round half to even).
  994. checker(0.001953125);
  995. #endif
  996. checker(0.005859375);
  997. // Some cases where the rounding is very very close
  998. checker(1.089095e-15);
  999. checker(3.274195e-55);
  1000. checker(6.534355e-146);
  1001. checker(2.920845e+234);
  1002. if (mismatches.empty()) {
  1003. test_count = 0;
  1004. ExhaustiveFloat(kFloatNumCases, checker);
  1005. test_count = 0;
  1006. std::vector<int> digit_testcases{
  1007. 100000, 100001, 100002, 100005, 100010, 100020, 100050, 100100, // misc
  1008. 195312, 195313, // 1.953125 is a case where we round down, just barely.
  1009. 200000, 500000, 800000, // misc mid-range cases
  1010. 585937, 585938, // 5.859375 is a case where we round up, just barely.
  1011. 900000, 990000, 999000, 999900, 999990, 999996, 999997, 999998, 999999};
  1012. if (kFloatNumCases >= 1e9) {
  1013. // If at least 1 billion test cases were requested, user wants an
  1014. // exhaustive test. So let's test all mantissas, too.
  1015. constexpr int min_mantissa = 100000, max_mantissa = 999999;
  1016. digit_testcases.resize(max_mantissa - min_mantissa + 1);
  1017. std::iota(digit_testcases.begin(), digit_testcases.end(), min_mantissa);
  1018. }
  1019. for (int exponent = -324; exponent <= 308; ++exponent) {
  1020. double powten = absl::strings_internal::Pow10(exponent);
  1021. if (powten == 0) powten = 5e-324;
  1022. if (kFloatNumCases >= 1e9) {
  1023. // The exhaustive test takes a very long time, so log progress.
  1024. char buf[kSixDigitsToBufferSize];
  1025. ABSL_RAW_LOG(
  1026. INFO, "%s",
  1027. absl::StrCat("Exp ", exponent, " powten=", powten, "(", powten,
  1028. ") (",
  1029. std::string(buf, SixDigitsToBuffer(powten, buf)), ")")
  1030. .c_str());
  1031. }
  1032. for (int digits : digit_testcases) {
  1033. if (exponent == 308 && digits >= 179769) break; // don't overflow!
  1034. double digiform = (digits + 0.5) * 0.00001;
  1035. double testval = digiform * powten;
  1036. double pretestval = nextafter(testval, 0);
  1037. double posttestval = nextafter(testval, 1.7976931348623157e308);
  1038. checker(testval);
  1039. checker(pretestval);
  1040. checker(posttestval);
  1041. }
  1042. }
  1043. } else {
  1044. EXPECT_EQ(mismatches.size(), 0);
  1045. for (size_t i = 0; i < mismatches.size(); ++i) {
  1046. if (i > 100) i = mismatches.size() - 1;
  1047. double d = mismatches[i];
  1048. char sixdigitsbuf[kSixDigitsToBufferSize] = {0};
  1049. SixDigitsToBuffer(d, sixdigitsbuf);
  1050. char snprintfbuf[kSixDigitsToBufferSize] = {0};
  1051. snprintf(snprintfbuf, kSixDigitsToBufferSize, "%g", d);
  1052. double before = nextafter(d, 0.0);
  1053. double after = nextafter(d, 1.7976931348623157e308);
  1054. char b1[32], b2[kSixDigitsToBufferSize];
  1055. ABSL_RAW_LOG(
  1056. ERROR, "%s",
  1057. absl::StrCat(
  1058. "Mismatch #", i, " d=", d, " (", ToNineDigits(d), ")",
  1059. " sixdigits='", sixdigitsbuf, "'", " snprintf='", snprintfbuf,
  1060. "'", " Before.=", PerfectDtoa(before), " ",
  1061. (SixDigitsToBuffer(before, b2), b2),
  1062. " vs snprintf=", (snprintf(b1, sizeof(b1), "%g", before), b1),
  1063. " Perfect=", PerfectDtoa(d), " ", (SixDigitsToBuffer(d, b2), b2),
  1064. " vs snprintf=", (snprintf(b1, sizeof(b1), "%g", d), b1),
  1065. " After.=.", PerfectDtoa(after), " ",
  1066. (SixDigitsToBuffer(after, b2), b2),
  1067. " vs snprintf=", (snprintf(b1, sizeof(b1), "%g", after), b1))
  1068. .c_str());
  1069. }
  1070. }
  1071. }
  1072. TEST(StrToInt32, Partial) {
  1073. struct Int32TestLine {
  1074. std::string input;
  1075. bool status;
  1076. int32_t value;
  1077. };
  1078. const int32_t int32_min = std::numeric_limits<int32_t>::min();
  1079. const int32_t int32_max = std::numeric_limits<int32_t>::max();
  1080. Int32TestLine int32_test_line[] = {
  1081. {"", false, 0},
  1082. {" ", false, 0},
  1083. {"-", false, 0},
  1084. {"123@@@", false, 123},
  1085. {absl::StrCat(int32_min, int32_max), false, int32_min},
  1086. {absl::StrCat(int32_max, int32_max), false, int32_max},
  1087. };
  1088. for (const Int32TestLine& test_line : int32_test_line) {
  1089. int32_t value = -2;
  1090. bool status = safe_strto32_base(test_line.input, &value, 10);
  1091. EXPECT_EQ(test_line.status, status) << test_line.input;
  1092. EXPECT_EQ(test_line.value, value) << test_line.input;
  1093. value = -2;
  1094. status = safe_strto32_base(test_line.input, &value, 10);
  1095. EXPECT_EQ(test_line.status, status) << test_line.input;
  1096. EXPECT_EQ(test_line.value, value) << test_line.input;
  1097. value = -2;
  1098. status = safe_strto32_base(absl::string_view(test_line.input), &value, 10);
  1099. EXPECT_EQ(test_line.status, status) << test_line.input;
  1100. EXPECT_EQ(test_line.value, value) << test_line.input;
  1101. }
  1102. }
  1103. TEST(StrToUint32, Partial) {
  1104. struct Uint32TestLine {
  1105. std::string input;
  1106. bool status;
  1107. uint32_t value;
  1108. };
  1109. const uint32_t uint32_max = std::numeric_limits<uint32_t>::max();
  1110. Uint32TestLine uint32_test_line[] = {
  1111. {"", false, 0},
  1112. {" ", false, 0},
  1113. {"-", false, 0},
  1114. {"123@@@", false, 123},
  1115. {absl::StrCat(uint32_max, uint32_max), false, uint32_max},
  1116. };
  1117. for (const Uint32TestLine& test_line : uint32_test_line) {
  1118. uint32_t value = 2;
  1119. bool status = safe_strtou32_base(test_line.input, &value, 10);
  1120. EXPECT_EQ(test_line.status, status) << test_line.input;
  1121. EXPECT_EQ(test_line.value, value) << test_line.input;
  1122. value = 2;
  1123. status = safe_strtou32_base(test_line.input, &value, 10);
  1124. EXPECT_EQ(test_line.status, status) << test_line.input;
  1125. EXPECT_EQ(test_line.value, value) << test_line.input;
  1126. value = 2;
  1127. status = safe_strtou32_base(absl::string_view(test_line.input), &value, 10);
  1128. EXPECT_EQ(test_line.status, status) << test_line.input;
  1129. EXPECT_EQ(test_line.value, value) << test_line.input;
  1130. }
  1131. }
  1132. TEST(StrToInt64, Partial) {
  1133. struct Int64TestLine {
  1134. std::string input;
  1135. bool status;
  1136. int64_t value;
  1137. };
  1138. const int64_t int64_min = std::numeric_limits<int64_t>::min();
  1139. const int64_t int64_max = std::numeric_limits<int64_t>::max();
  1140. Int64TestLine int64_test_line[] = {
  1141. {"", false, 0},
  1142. {" ", false, 0},
  1143. {"-", false, 0},
  1144. {"123@@@", false, 123},
  1145. {absl::StrCat(int64_min, int64_max), false, int64_min},
  1146. {absl::StrCat(int64_max, int64_max), false, int64_max},
  1147. };
  1148. for (const Int64TestLine& test_line : int64_test_line) {
  1149. int64_t value = -2;
  1150. bool status = safe_strto64_base(test_line.input, &value, 10);
  1151. EXPECT_EQ(test_line.status, status) << test_line.input;
  1152. EXPECT_EQ(test_line.value, value) << test_line.input;
  1153. value = -2;
  1154. status = safe_strto64_base(test_line.input, &value, 10);
  1155. EXPECT_EQ(test_line.status, status) << test_line.input;
  1156. EXPECT_EQ(test_line.value, value) << test_line.input;
  1157. value = -2;
  1158. status = safe_strto64_base(absl::string_view(test_line.input), &value, 10);
  1159. EXPECT_EQ(test_line.status, status) << test_line.input;
  1160. EXPECT_EQ(test_line.value, value) << test_line.input;
  1161. }
  1162. }
  1163. TEST(StrToUint64, Partial) {
  1164. struct Uint64TestLine {
  1165. std::string input;
  1166. bool status;
  1167. uint64_t value;
  1168. };
  1169. const uint64_t uint64_max = std::numeric_limits<uint64_t>::max();
  1170. Uint64TestLine uint64_test_line[] = {
  1171. {"", false, 0},
  1172. {" ", false, 0},
  1173. {"-", false, 0},
  1174. {"123@@@", false, 123},
  1175. {absl::StrCat(uint64_max, uint64_max), false, uint64_max},
  1176. };
  1177. for (const Uint64TestLine& test_line : uint64_test_line) {
  1178. uint64_t value = 2;
  1179. bool status = safe_strtou64_base(test_line.input, &value, 10);
  1180. EXPECT_EQ(test_line.status, status) << test_line.input;
  1181. EXPECT_EQ(test_line.value, value) << test_line.input;
  1182. value = 2;
  1183. status = safe_strtou64_base(test_line.input, &value, 10);
  1184. EXPECT_EQ(test_line.status, status) << test_line.input;
  1185. EXPECT_EQ(test_line.value, value) << test_line.input;
  1186. value = 2;
  1187. status = safe_strtou64_base(absl::string_view(test_line.input), &value, 10);
  1188. EXPECT_EQ(test_line.status, status) << test_line.input;
  1189. EXPECT_EQ(test_line.value, value) << test_line.input;
  1190. }
  1191. }
  1192. TEST(StrToInt32Base, PrefixOnly) {
  1193. struct Int32TestLine {
  1194. std::string input;
  1195. bool status;
  1196. int32_t value;
  1197. };
  1198. Int32TestLine int32_test_line[] = {
  1199. { "", false, 0 },
  1200. { "-", false, 0 },
  1201. { "-0", true, 0 },
  1202. { "0", true, 0 },
  1203. { "0x", false, 0 },
  1204. { "-0x", false, 0 },
  1205. };
  1206. const int base_array[] = { 0, 2, 8, 10, 16 };
  1207. for (const Int32TestLine& line : int32_test_line) {
  1208. for (const int base : base_array) {
  1209. int32_t value = 2;
  1210. bool status = safe_strto32_base(line.input.c_str(), &value, base);
  1211. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1212. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1213. value = 2;
  1214. status = safe_strto32_base(line.input, &value, base);
  1215. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1216. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1217. value = 2;
  1218. status = safe_strto32_base(absl::string_view(line.input), &value, base);
  1219. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1220. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1221. }
  1222. }
  1223. }
  1224. TEST(StrToUint32Base, PrefixOnly) {
  1225. struct Uint32TestLine {
  1226. std::string input;
  1227. bool status;
  1228. uint32_t value;
  1229. };
  1230. Uint32TestLine uint32_test_line[] = {
  1231. { "", false, 0 },
  1232. { "0", true, 0 },
  1233. { "0x", false, 0 },
  1234. };
  1235. const int base_array[] = { 0, 2, 8, 10, 16 };
  1236. for (const Uint32TestLine& line : uint32_test_line) {
  1237. for (const int base : base_array) {
  1238. uint32_t value = 2;
  1239. bool status = safe_strtou32_base(line.input.c_str(), &value, base);
  1240. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1241. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1242. value = 2;
  1243. status = safe_strtou32_base(line.input, &value, base);
  1244. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1245. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1246. value = 2;
  1247. status = safe_strtou32_base(absl::string_view(line.input), &value, base);
  1248. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1249. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1250. }
  1251. }
  1252. }
  1253. TEST(StrToInt64Base, PrefixOnly) {
  1254. struct Int64TestLine {
  1255. std::string input;
  1256. bool status;
  1257. int64_t value;
  1258. };
  1259. Int64TestLine int64_test_line[] = {
  1260. { "", false, 0 },
  1261. { "-", false, 0 },
  1262. { "-0", true, 0 },
  1263. { "0", true, 0 },
  1264. { "0x", false, 0 },
  1265. { "-0x", false, 0 },
  1266. };
  1267. const int base_array[] = { 0, 2, 8, 10, 16 };
  1268. for (const Int64TestLine& line : int64_test_line) {
  1269. for (const int base : base_array) {
  1270. int64_t value = 2;
  1271. bool status = safe_strto64_base(line.input.c_str(), &value, base);
  1272. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1273. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1274. value = 2;
  1275. status = safe_strto64_base(line.input, &value, base);
  1276. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1277. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1278. value = 2;
  1279. status = safe_strto64_base(absl::string_view(line.input), &value, base);
  1280. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1281. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1282. }
  1283. }
  1284. }
  1285. TEST(StrToUint64Base, PrefixOnly) {
  1286. struct Uint64TestLine {
  1287. std::string input;
  1288. bool status;
  1289. uint64_t value;
  1290. };
  1291. Uint64TestLine uint64_test_line[] = {
  1292. { "", false, 0 },
  1293. { "0", true, 0 },
  1294. { "0x", false, 0 },
  1295. };
  1296. const int base_array[] = { 0, 2, 8, 10, 16 };
  1297. for (const Uint64TestLine& line : uint64_test_line) {
  1298. for (const int base : base_array) {
  1299. uint64_t value = 2;
  1300. bool status = safe_strtou64_base(line.input.c_str(), &value, base);
  1301. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1302. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1303. value = 2;
  1304. status = safe_strtou64_base(line.input, &value, base);
  1305. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1306. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1307. value = 2;
  1308. status = safe_strtou64_base(absl::string_view(line.input), &value, base);
  1309. EXPECT_EQ(line.status, status) << line.input << " " << base;
  1310. EXPECT_EQ(line.value, value) << line.input << " " << base;
  1311. }
  1312. }
  1313. }
  1314. void TestFastHexToBufferZeroPad16(uint64_t v) {
  1315. char buf[16];
  1316. auto digits = absl::numbers_internal::FastHexToBufferZeroPad16(v, buf);
  1317. absl::string_view res(buf, 16);
  1318. char buf2[17];
  1319. snprintf(buf2, sizeof(buf2), "%016" PRIx64, v);
  1320. EXPECT_EQ(res, buf2) << v;
  1321. size_t expected_digits = snprintf(buf2, sizeof(buf2), "%" PRIx64, v);
  1322. EXPECT_EQ(digits, expected_digits) << v;
  1323. }
  1324. TEST(FastHexToBufferZeroPad16, Smoke) {
  1325. TestFastHexToBufferZeroPad16(std::numeric_limits<uint64_t>::min());
  1326. TestFastHexToBufferZeroPad16(std::numeric_limits<uint64_t>::max());
  1327. TestFastHexToBufferZeroPad16(std::numeric_limits<int64_t>::min());
  1328. TestFastHexToBufferZeroPad16(std::numeric_limits<int64_t>::max());
  1329. absl::BitGen rng;
  1330. for (int i = 0; i < 100000; ++i) {
  1331. TestFastHexToBufferZeroPad16(
  1332. absl::LogUniform(rng, std::numeric_limits<uint64_t>::min(),
  1333. std::numeric_limits<uint64_t>::max()));
  1334. }
  1335. }
  1336. } // namespace