OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/base/string_to_number.h" |
| 12 |
| 13 #include <string> |
| 14 #include <type_traits> |
| 15 #include <limits> |
| 16 |
| 17 #include "webrtc/base/gunit.h" |
| 18 |
| 19 namespace rtc { |
| 20 |
| 21 namespace { |
| 22 // clang-format off |
| 23 using IntegerTypes = |
| 24 ::testing::Types<signed char, unsigned char, |
| 25 short, unsigned short, |
| 26 int, unsigned int, |
| 27 long, unsigned long, |
| 28 long long, unsigned long long, |
| 29 int8_t, uint8_t, |
| 30 int16_t, uint16_t, |
| 31 int32_t, uint32_t, |
| 32 int64_t, uint64_t>; |
| 33 // clang-format on |
| 34 |
| 35 template <typename T> |
| 36 class BasicNumberTest : public ::testing::Test {}; |
| 37 |
| 38 TYPED_TEST_CASE_P(BasicNumberTest); |
| 39 |
| 40 TYPED_TEST_P(BasicNumberTest, TestValidNumbers) { |
| 41 using T = TypeParam; |
| 42 constexpr T min_value = std::numeric_limits<T>::lowest(); |
| 43 constexpr T max_value = std::numeric_limits<T>::max(); |
| 44 const std::string min_string = std::to_string(min_value); |
| 45 const std::string max_string = std::to_string(max_value); |
| 46 EXPECT_EQ(min_value, StringToNumber<T>(min_string)); |
| 47 EXPECT_EQ(min_value, StringToNumber<T>(min_string.c_str())); |
| 48 EXPECT_EQ(max_value, StringToNumber<T>(max_string)); |
| 49 EXPECT_EQ(max_value, StringToNumber<T>(max_string.c_str())); |
| 50 EXPECT_EQ(0, StringToNumber<T>("0")); |
| 51 EXPECT_EQ(0, StringToNumber<T>("-0")); |
| 52 EXPECT_EQ(0, StringToNumber<T>(std::string("-0000000000000"))); |
| 53 } |
| 54 |
| 55 TYPED_TEST_P(BasicNumberTest, TestInvalidNumbers) { |
| 56 using T = TypeParam; |
| 57 // Value ranges aren't strictly enforced in this test, since that would either |
| 58 // require doctoring specific strings for each data type, which is a hassle |
| 59 // across platforms, or to be able to do addition of values larger than the |
| 60 // largest type, which is another hassle. |
| 61 constexpr T min_value = std::numeric_limits<T>::lowest(); |
| 62 constexpr T max_value = std::numeric_limits<T>::max(); |
| 63 // If the type supports negative values, make the large negative value |
| 64 // approximately ten times larger. If the type is unsigned, just use -2. |
| 65 const std::string too_low_string = |
| 66 (min_value == 0) ? "-2" : (std::to_string(min_value) + "1"); |
| 67 // Make the large value approximately ten times larger than the maximum. |
| 68 const std::string too_large_string = std::to_string(max_value) + "1"; |
| 69 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_low_string)); |
| 70 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_low_string.c_str())); |
| 71 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_large_string)); |
| 72 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_large_string.c_str())); |
| 73 } |
| 74 |
| 75 TYPED_TEST_P(BasicNumberTest, TestInvalidInputs) { |
| 76 using T = TypeParam; |
| 77 const char kInvalidCharArray[] = "Invalid string containing 47"; |
| 78 const char kPlusMinusCharArray[] = "+-100"; |
| 79 const char kNumberFollowedByCruft[] = "640x480"; |
| 80 const rtc::Optional<T> kEmptyOptional; |
| 81 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kInvalidCharArray)); |
| 82 EXPECT_EQ(rtc::Optional<T>(), |
| 83 StringToNumber<T>(std::string(kInvalidCharArray))); |
| 84 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kPlusMinusCharArray)); |
| 85 EXPECT_EQ(rtc::Optional<T>(), |
| 86 StringToNumber<T>(std::string(kPlusMinusCharArray))); |
| 87 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kNumberFollowedByCruft)); |
| 88 EXPECT_EQ(rtc::Optional<T>(), |
| 89 StringToNumber<T>(std::string(kNumberFollowedByCruft))); |
| 90 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(" 5")); |
| 91 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(" - 5")); |
| 92 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>("- 5")); |
| 93 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(" -5")); |
| 94 } |
| 95 |
| 96 REGISTER_TYPED_TEST_CASE_P(BasicNumberTest, |
| 97 TestValidNumbers, |
| 98 TestInvalidNumbers, |
| 99 TestInvalidInputs); |
| 100 |
| 101 } // namespace |
| 102 |
| 103 INSTANTIATE_TYPED_TEST_CASE_P(StringToNumberTest_Integers, |
| 104 BasicNumberTest, |
| 105 IntegerTypes); |
| 106 |
| 107 TEST(StringToNumberTest, TestSpecificValues) { |
| 108 EXPECT_EQ(rtc::Optional<uint8_t>(), StringToNumber<uint8_t>("256")); |
| 109 EXPECT_EQ(rtc::Optional<uint8_t>(), StringToNumber<uint8_t>("-256")); |
| 110 EXPECT_EQ(rtc::Optional<int8_t>(), StringToNumber<int8_t>("256")); |
| 111 EXPECT_EQ(rtc::Optional<int8_t>(), StringToNumber<int8_t>("-256")); |
| 112 } |
| 113 |
| 114 } // namespace rtc |
OLD | NEW |