numbers_test_common.h
Go to the documentation of this file.
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 namespace absl {
27 namespace strings_internal {
28 
29 template <typename IntType>
30 inline bool Itoa(IntType value, int base, std::string* destination) {
31  destination->clear();
32  if (base <= 1 || base > 36) {
33  return false;
34  }
35 
36  if (value == 0) {
37  destination->push_back('0');
38  return true;
39  }
40 
41  bool negative = value < 0;
42  while (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);
49  value = next_value;
50  }
51 
52  if (negative) {
53  destination->insert(0, 1, '-');
54  }
55  return true;
56 }
57 
59  const char* str;
60  bool expect_ok;
61  int base; // base to pass to the conversion function
62  uint32_t expected;
63 };
64 
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},
70  {"0", true, 16, 0},
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},
76  {"0", true, 0, 0},
77 
78  // Base-10 version.
79  {"34234324", true, 0, 34234324},
80  {"4294967295", true, 0, (std::numeric_limits<uint32_t>::max)()},
81  {"34234324 \n\t", true, 10, 34234324},
82 
83  // Unusual base
84  {"0", true, 3, 0},
85  {"2", true, 3, 2},
86  {"11", true, 3, 4},
87 
88  // Invalid uints.
89  {"", false, 0, 0},
90  {" ", false, 0, 0},
91  {"abc", false, 0, 0}, // would be valid hex, but prefix is missing
92  {"34234324a", false, 0, 34234324},
93  {"34234.3", false, 0, 34234},
94  {"-1", false, 0, 0},
95  {" -123", false, 0, 0},
96  {" \t\n -123", false, 0, 0},
97 
98  // Out of bounds.
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},
102  }};
103  return test_cases;
104 }
105 
107  const char* str;
108  bool expect_ok;
109  int base;
110  uint64_t expected;
111 };
112 
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}},
117 
118  {"0", true, 16, 0},
119  {"000", true, 0, 0},
120  {"0", true, 0, 0},
121  {" \t\n 0xffffffffffffffff", true, 16,
122  (std::numeric_limits<uint64_t>::max)()},
123 
124  {"012345670123456701234", true, 8, int64_t{012345670123456701234}},
125  {"12345670123456701234", true, 8, int64_t{012345670123456701234}},
126 
127  {"12845670123456701234", false, 8, 0},
128 
129  // Base-10 version.
130  {"34234324487834466", true, 0, int64_t{34234324487834466}},
131 
132  {" \t\n 18446744073709551615", true, 0,
133  (std::numeric_limits<uint64_t>::max)()},
134 
135  {"34234324487834466 \n\t ", true, 0, int64_t{34234324487834466}},
136 
137  {" \f\v 46", true, 10, 46}, // must accept weird whitespace
138 
139  // Unusual base
140  {"0", true, 3, 0},
141  {"2", true, 3, 2},
142  {"11", true, 3, 4},
143 
144  {"0", true, 0, 0},
145 
146  // Invalid uints.
147  {"", false, 0, 0},
148  {" ", false, 0, 0},
149  {"abc", false, 0, 0},
150  {"34234324487834466a", false, 0, 0},
151  {"34234487834466.3", false, 0, 0},
152  {"-1", false, 0, 0},
153  {" -123", false, 0, 0},
154  {" \t\n -123", false, 0, 0},
155 
156  // Out of bounds.
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.
166 
167  {"0x1234", true, 16, 0x1234},
168 
169  // Base-10 std::string version.
170  {"1234", true, 0, 1234},
171  {nullptr, false, 0, 0},
172  }};
173  return test_cases;
174 }
175 
176 } // namespace strings_internal
177 } // namespace absl
178 
179 #endif // ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
bool Itoa(IntType value, int base, std::string *destination)
Definition: algorithm.h:29
const std::array< uint64_test_case, 34 > & strtouint64_test_cases()
size_t value
const std::array< uint32_test_case, 27 > & strtouint32_test_cases()


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:36