numbers_test_common.h
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 //
00015 // This file contains common things needed by numbers_test.cc,
00016 // numbers_legacy_test.cc and numbers_benchmark.cc.
00017 
00018 #ifndef ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
00019 #define ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
00020 
00021 #include <array>
00022 #include <cstdint>
00023 #include <limits>
00024 #include <string>
00025 
00026 namespace absl {
00027 namespace strings_internal {
00028 
00029 template <typename IntType>
00030 inline bool Itoa(IntType value, int base, std::string* destination) {
00031   destination->clear();
00032   if (base <= 1 || base > 36) {
00033     return false;
00034   }
00035 
00036   if (value == 0) {
00037     destination->push_back('0');
00038     return true;
00039   }
00040 
00041   bool negative = value < 0;
00042   while (value != 0) {
00043     const IntType next_value = value / base;
00044     // Can't use std::abs here because of problems when IntType is unsigned.
00045     int remainder = value > next_value * base ? value - next_value * base
00046                                               : next_value * base - value;
00047     char c = remainder < 10 ? '0' + remainder : 'A' + remainder - 10;
00048     destination->insert(0, 1, c);
00049     value = next_value;
00050   }
00051 
00052   if (negative) {
00053     destination->insert(0, 1, '-');
00054   }
00055   return true;
00056 }
00057 
00058 struct uint32_test_case {
00059   const char* str;
00060   bool expect_ok;
00061   int base;  // base to pass to the conversion function
00062   uint32_t expected;
00063 };
00064 
00065 inline const std::array<uint32_test_case, 27>& strtouint32_test_cases() {
00066   static const std::array<uint32_test_case, 27> test_cases{{
00067       {"0xffffffff", true, 16, (std::numeric_limits<uint32_t>::max)()},
00068       {"0x34234324", true, 16, 0x34234324},
00069       {"34234324", true, 16, 0x34234324},
00070       {"0", true, 16, 0},
00071       {" \t\n 0xffffffff", true, 16, (std::numeric_limits<uint32_t>::max)()},
00072       {" \f\v 46", true, 10, 46},  // must accept weird whitespace
00073       {" \t\n 72717222", true, 8, 072717222},
00074       {" \t\n 072717222", true, 8, 072717222},
00075       {" \t\n 072717228", false, 8, 07271722},
00076       {"0", true, 0, 0},
00077 
00078       // Base-10 version.
00079       {"34234324", true, 0, 34234324},
00080       {"4294967295", true, 0, (std::numeric_limits<uint32_t>::max)()},
00081       {"34234324 \n\t", true, 10, 34234324},
00082 
00083       // Unusual base
00084       {"0", true, 3, 0},
00085       {"2", true, 3, 2},
00086       {"11", true, 3, 4},
00087 
00088       // Invalid uints.
00089       {"", false, 0, 0},
00090       {"  ", false, 0, 0},
00091       {"abc", false, 0, 0},  // would be valid hex, but prefix is missing
00092       {"34234324a", false, 0, 34234324},
00093       {"34234.3", false, 0, 34234},
00094       {"-1", false, 0, 0},
00095       {"   -123", false, 0, 0},
00096       {" \t\n -123", false, 0, 0},
00097 
00098       // Out of bounds.
00099       {"4294967296", false, 0, (std::numeric_limits<uint32_t>::max)()},
00100       {"0x100000000", false, 0, (std::numeric_limits<uint32_t>::max)()},
00101       {nullptr, false, 0, 0},
00102   }};
00103   return test_cases;
00104 }
00105 
00106 struct uint64_test_case {
00107   const char* str;
00108   bool expect_ok;
00109   int base;
00110   uint64_t expected;
00111 };
00112 
00113 inline const std::array<uint64_test_case, 34>& strtouint64_test_cases() {
00114   static const std::array<uint64_test_case, 34> test_cases{{
00115       {"0x3423432448783446", true, 16, int64_t{0x3423432448783446}},
00116       {"3423432448783446", true, 16, int64_t{0x3423432448783446}},
00117 
00118       {"0", true, 16, 0},
00119       {"000", true, 0, 0},
00120       {"0", true, 0, 0},
00121       {" \t\n 0xffffffffffffffff", true, 16,
00122        (std::numeric_limits<uint64_t>::max)()},
00123 
00124       {"012345670123456701234", true, 8, int64_t{012345670123456701234}},
00125       {"12345670123456701234", true, 8, int64_t{012345670123456701234}},
00126 
00127       {"12845670123456701234", false, 8, 0},
00128 
00129       // Base-10 version.
00130       {"34234324487834466", true, 0, int64_t{34234324487834466}},
00131 
00132       {" \t\n 18446744073709551615", true, 0,
00133        (std::numeric_limits<uint64_t>::max)()},
00134 
00135       {"34234324487834466 \n\t ", true, 0, int64_t{34234324487834466}},
00136 
00137       {" \f\v 46", true, 10, 46},  // must accept weird whitespace
00138 
00139       // Unusual base
00140       {"0", true, 3, 0},
00141       {"2", true, 3, 2},
00142       {"11", true, 3, 4},
00143 
00144       {"0", true, 0, 0},
00145 
00146       // Invalid uints.
00147       {"", false, 0, 0},
00148       {"  ", false, 0, 0},
00149       {"abc", false, 0, 0},
00150       {"34234324487834466a", false, 0, 0},
00151       {"34234487834466.3", false, 0, 0},
00152       {"-1", false, 0, 0},
00153       {"   -123", false, 0, 0},
00154       {" \t\n -123", false, 0, 0},
00155 
00156       // Out of bounds.
00157       {"18446744073709551616", false, 10, 0},
00158       {"18446744073709551616", false, 0, 0},
00159       {"0x10000000000000000", false, 16,
00160        (std::numeric_limits<uint64_t>::max)()},
00161       {"0X10000000000000000", false, 16,
00162        (std::numeric_limits<uint64_t>::max)()},  // 0X versus 0x.
00163       {"0x10000000000000000", false, 0, (std::numeric_limits<uint64_t>::max)()},
00164       {"0X10000000000000000", false, 0,
00165        (std::numeric_limits<uint64_t>::max)()},  // 0X versus 0x.
00166 
00167       {"0x1234", true, 16, 0x1234},
00168 
00169       // Base-10 std::string version.
00170       {"1234", true, 0, 1234},
00171       {nullptr, false, 0, 0},
00172   }};
00173   return test_cases;
00174 }
00175 
00176 }  // namespace strings_internal
00177 }  // namespace absl
00178 
00179 #endif  // ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15