1 // Copyright 2017 The Abseil Authors.
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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.
15 // This file contains common things needed by numbers_test.cc,
16 // numbers_legacy_test.cc and numbers_benchmark.cc.
18 #ifndef ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
19 #define ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
27 namespace strings_internal {
29 template <typename IntType>
30 inline bool Itoa(IntType value, int base, std::string* destination) {
32 if (base <= 1 || base > 36) {
37 destination->push_back('0');
41 bool negative = value < 0;
43 const IntType next_value = value / base;
44 // Can't use std::abs here because of problems when IntType is unsigned.
45 int remainder = value > next_value * base ? value - next_value * base
46 : next_value * base - value;
47 char c = remainder < 10 ? '0' + remainder : 'A' + remainder - 10;
48 destination->insert(0, 1, c);
53 destination->insert(0, 1, '-');
58 struct uint32_test_case {
61 int base; // base to pass to the conversion function
65 inline const std::array<uint32_test_case, 27>& strtouint32_test_cases() {
66 static const std::array<uint32_test_case, 27> test_cases{{
67 {"0xffffffff", true, 16, (std::numeric_limits<uint32_t>::max)()},
68 {"0x34234324", true, 16, 0x34234324},
69 {"34234324", true, 16, 0x34234324},
71 {" \t\n 0xffffffff", true, 16, (std::numeric_limits<uint32_t>::max)()},
72 {" \f\v 46", true, 10, 46}, // must accept weird whitespace
73 {" \t\n 72717222", true, 8, 072717222},
74 {" \t\n 072717222", true, 8, 072717222},
75 {" \t\n 072717228", false, 8, 07271722},
79 {"34234324", true, 0, 34234324},
80 {"4294967295", true, 0, (std::numeric_limits<uint32_t>::max)()},
81 {"34234324 \n\t", true, 10, 34234324},
91 {"abc", false, 0, 0}, // would be valid hex, but prefix is missing
92 {"34234324a", false, 0, 34234324},
93 {"34234.3", false, 0, 34234},
95 {" -123", false, 0, 0},
96 {" \t\n -123", false, 0, 0},
99 {"4294967296", false, 0, (std::numeric_limits<uint32_t>::max)()},
100 {"0x100000000", false, 0, (std::numeric_limits<uint32_t>::max)()},
101 {nullptr, false, 0, 0},
106 struct uint64_test_case {
113 inline const std::array<uint64_test_case, 34>& strtouint64_test_cases() {
114 static const std::array<uint64_test_case, 34> test_cases{{
115 {"0x3423432448783446", true, 16, int64_t{0x3423432448783446}},
116 {"3423432448783446", true, 16, int64_t{0x3423432448783446}},
121 {" \t\n 0xffffffffffffffff", true, 16,
122 (std::numeric_limits<uint64_t>::max)()},
124 {"012345670123456701234", true, 8, int64_t{012345670123456701234}},
125 {"12345670123456701234", true, 8, int64_t{012345670123456701234}},
127 {"12845670123456701234", false, 8, 0},
130 {"34234324487834466", true, 0, int64_t{34234324487834466}},
132 {" \t\n 18446744073709551615", true, 0,
133 (std::numeric_limits<uint64_t>::max)()},
135 {"34234324487834466 \n\t ", true, 0, int64_t{34234324487834466}},
137 {" \f\v 46", true, 10, 46}, // must accept weird whitespace
149 {"abc", false, 0, 0},
150 {"34234324487834466a", false, 0, 0},
151 {"34234487834466.3", false, 0, 0},
153 {" -123", false, 0, 0},
154 {" \t\n -123", false, 0, 0},
157 {"18446744073709551616", false, 10, 0},
158 {"18446744073709551616", false, 0, 0},
159 {"0x10000000000000000", false, 16,
160 (std::numeric_limits<uint64_t>::max)()},
161 {"0X10000000000000000", false, 16,
162 (std::numeric_limits<uint64_t>::max)()}, // 0X versus 0x.
163 {"0x10000000000000000", false, 0, (std::numeric_limits<uint64_t>::max)()},
164 {"0X10000000000000000", false, 0,
165 (std::numeric_limits<uint64_t>::max)()}, // 0X versus 0x.
167 {"0x1234", true, 16, 0x1234},
169 // Base-10 std::string version.
170 {"1234", true, 0, 1234},
171 {nullptr, false, 0, 0},
176 } // namespace strings_internal
179 #endif // ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_