• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
15 // This file contains common things needed by numbers_test.cc,
16 // numbers_legacy_test.cc and numbers_benchmark.cc.
17 
18 #ifndef ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
19 #define ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
20 
21 #include <array>
22 #include <cstdint>
23 #include <limits>
24 #include <string>
25 
26 #include "absl/base/config.h"
27 
28 namespace absl {
29 ABSL_NAMESPACE_BEGIN
30 namespace strings_internal {
31 
32 template <typename IntType>
Itoa(IntType value,int base,std::string * destination)33 inline bool Itoa(IntType value, int base, std::string* destination) {
34   destination->clear();
35   if (base <= 1 || base > 36) {
36     return false;
37   }
38 
39   if (value == 0) {
40     destination->push_back('0');
41     return true;
42   }
43 
44   bool negative = value < 0;
45   while (value != 0) {
46     const IntType next_value = value / base;
47     // Can't use std::abs here because of problems when IntType is unsigned.
48     int remainder =
49         static_cast<int>(value > next_value * base ? value - next_value * base
50                                                    : next_value * base - value);
51     char c = remainder < 10 ? '0' + remainder : 'A' + remainder - 10;
52     destination->insert(0, 1, c);
53     value = next_value;
54   }
55 
56   if (negative) {
57     destination->insert(0, 1, '-');
58   }
59   return true;
60 }
61 
62 struct uint32_test_case {
63   const char* str;
64   bool expect_ok;
65   int base;  // base to pass to the conversion function
66   uint32_t expected;
67 };
68 
strtouint32_test_cases()69 inline const std::array<uint32_test_case, 27>& strtouint32_test_cases() {
70   static const std::array<uint32_test_case, 27> test_cases{{
71       {"0xffffffff", true, 16, (std::numeric_limits<uint32_t>::max)()},
72       {"0x34234324", true, 16, 0x34234324},
73       {"34234324", true, 16, 0x34234324},
74       {"0", true, 16, 0},
75       {" \t\n 0xffffffff", true, 16, (std::numeric_limits<uint32_t>::max)()},
76       {" \f\v 46", true, 10, 46},  // must accept weird whitespace
77       {" \t\n 72717222", true, 8, 072717222},
78       {" \t\n 072717222", true, 8, 072717222},
79       {" \t\n 072717228", false, 8, 07271722},
80       {"0", true, 0, 0},
81 
82       // Base-10 version.
83       {"34234324", true, 0, 34234324},
84       {"4294967295", true, 0, (std::numeric_limits<uint32_t>::max)()},
85       {"34234324 \n\t", true, 10, 34234324},
86 
87       // Unusual base
88       {"0", true, 3, 0},
89       {"2", true, 3, 2},
90       {"11", true, 3, 4},
91 
92       // Invalid uints.
93       {"", false, 0, 0},
94       {"  ", false, 0, 0},
95       {"abc", false, 0, 0},  // would be valid hex, but prefix is missing
96       {"34234324a", false, 0, 34234324},
97       {"34234.3", false, 0, 34234},
98       {"-1", false, 0, 0},
99       {"   -123", false, 0, 0},
100       {" \t\n -123", false, 0, 0},
101 
102       // Out of bounds.
103       {"4294967296", false, 0, (std::numeric_limits<uint32_t>::max)()},
104       {"0x100000000", false, 0, (std::numeric_limits<uint32_t>::max)()},
105       {nullptr, false, 0, 0},
106   }};
107   return test_cases;
108 }
109 
110 struct uint64_test_case {
111   const char* str;
112   bool expect_ok;
113   int base;
114   uint64_t expected;
115 };
116 
strtouint64_test_cases()117 inline const std::array<uint64_test_case, 34>& strtouint64_test_cases() {
118   static const std::array<uint64_test_case, 34> test_cases{{
119       {"0x3423432448783446", true, 16, int64_t{0x3423432448783446}},
120       {"3423432448783446", true, 16, int64_t{0x3423432448783446}},
121 
122       {"0", true, 16, 0},
123       {"000", true, 0, 0},
124       {"0", true, 0, 0},
125       {" \t\n 0xffffffffffffffff", true, 16,
126        (std::numeric_limits<uint64_t>::max)()},
127 
128       {"012345670123456701234", true, 8, int64_t{012345670123456701234}},
129       {"12345670123456701234", true, 8, int64_t{012345670123456701234}},
130 
131       {"12845670123456701234", false, 8, 0},
132 
133       // Base-10 version.
134       {"34234324487834466", true, 0, int64_t{34234324487834466}},
135 
136       {" \t\n 18446744073709551615", true, 0,
137        (std::numeric_limits<uint64_t>::max)()},
138 
139       {"34234324487834466 \n\t ", true, 0, int64_t{34234324487834466}},
140 
141       {" \f\v 46", true, 10, 46},  // must accept weird whitespace
142 
143       // Unusual base
144       {"0", true, 3, 0},
145       {"2", true, 3, 2},
146       {"11", true, 3, 4},
147 
148       {"0", true, 0, 0},
149 
150       // Invalid uints.
151       {"", false, 0, 0},
152       {"  ", false, 0, 0},
153       {"abc", false, 0, 0},
154       {"34234324487834466a", false, 0, 0},
155       {"34234487834466.3", false, 0, 0},
156       {"-1", false, 0, 0},
157       {"   -123", false, 0, 0},
158       {" \t\n -123", false, 0, 0},
159 
160       // Out of bounds.
161       {"18446744073709551616", false, 10, 0},
162       {"18446744073709551616", false, 0, 0},
163       {"0x10000000000000000", false, 16,
164        (std::numeric_limits<uint64_t>::max)()},
165       {"0X10000000000000000", false, 16,
166        (std::numeric_limits<uint64_t>::max)()},  // 0X versus 0x.
167       {"0x10000000000000000", false, 0, (std::numeric_limits<uint64_t>::max)()},
168       {"0X10000000000000000", false, 0,
169        (std::numeric_limits<uint64_t>::max)()},  // 0X versus 0x.
170 
171       {"0x1234", true, 16, 0x1234},
172 
173       // Base-10 std::string version.
174       {"1234", true, 0, 1234},
175       {nullptr, false, 0, 0},
176   }};
177   return test_cases;
178 }
179 
180 }  // namespace strings_internal
181 ABSL_NAMESPACE_END
182 }  // namespace absl
183 
184 #endif  // ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
185