blob: 5a6a23709f8f7ab6ca2d033d25c0f9d58ea2ed78 [file] [log] [blame]
ossua280f7c2017-04-06 02:02:15 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/string_to_number.h"
ossua280f7c2017-04-06 02:02:15 -070012
13#include <string>
14#include <type_traits>
15#include <limits>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/gunit.h"
ossua280f7c2017-04-06 02:02:15 -070018
19namespace rtc {
20
21namespace {
22// clang-format off
23using IntegerTypes =
24 ::testing::Types<char,
25 signed char, unsigned char, // NOLINT(runtime/int)
26 short, unsigned short, // NOLINT(runtime/int)
27 int, unsigned int, // NOLINT(runtime/int)
28 long, unsigned long, // NOLINT(runtime/int)
29 long long, unsigned long long, // NOLINT(runtime/int)
30 int8_t, uint8_t,
31 int16_t, uint16_t,
32 int32_t, uint32_t,
33 int64_t, uint64_t>;
34// clang-format on
35
36template <typename T>
37class BasicNumberTest : public ::testing::Test {};
38
39TYPED_TEST_CASE_P(BasicNumberTest);
40
41TYPED_TEST_P(BasicNumberTest, TestValidNumbers) {
42 using T = TypeParam;
43 constexpr T min_value = std::numeric_limits<T>::lowest();
44 constexpr T max_value = std::numeric_limits<T>::max();
45 const std::string min_string = std::to_string(min_value);
46 const std::string max_string = std::to_string(max_value);
47 EXPECT_EQ(min_value, StringToNumber<T>(min_string));
48 EXPECT_EQ(min_value, StringToNumber<T>(min_string.c_str()));
49 EXPECT_EQ(max_value, StringToNumber<T>(max_string));
50 EXPECT_EQ(max_value, StringToNumber<T>(max_string.c_str()));
51 EXPECT_EQ(0, StringToNumber<T>("0"));
52 EXPECT_EQ(0, StringToNumber<T>("-0"));
53 EXPECT_EQ(0, StringToNumber<T>(std::string("-0000000000000")));
54}
55
56TYPED_TEST_P(BasicNumberTest, TestInvalidNumbers) {
57 using T = TypeParam;
58 // Value ranges aren't strictly enforced in this test, since that would either
59 // require doctoring specific strings for each data type, which is a hassle
60 // across platforms, or to be able to do addition of values larger than the
61 // largest type, which is another hassle.
62 constexpr T min_value = std::numeric_limits<T>::lowest();
63 constexpr T max_value = std::numeric_limits<T>::max();
64 // If the type supports negative values, make the large negative value
65 // approximately ten times larger. If the type is unsigned, just use -2.
66 const std::string too_low_string =
67 (min_value == 0) ? "-2" : (std::to_string(min_value) + "1");
68 // Make the large value approximately ten times larger than the maximum.
69 const std::string too_large_string = std::to_string(max_value) + "1";
70 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_low_string));
71 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_low_string.c_str()));
72 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_large_string));
73 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_large_string.c_str()));
74}
75
76TYPED_TEST_P(BasicNumberTest, TestInvalidInputs) {
77 using T = TypeParam;
78 const char kInvalidCharArray[] = "Invalid string containing 47";
79 const char kPlusMinusCharArray[] = "+-100";
80 const char kNumberFollowedByCruft[] = "640x480";
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 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>("5 "));
95}
96
97REGISTER_TYPED_TEST_CASE_P(BasicNumberTest,
98 TestValidNumbers,
99 TestInvalidNumbers,
100 TestInvalidInputs);
101
102} // namespace
103
104INSTANTIATE_TYPED_TEST_CASE_P(StringToNumberTest_Integers,
105 BasicNumberTest,
106 IntegerTypes);
107
108TEST(StringToNumberTest, TestSpecificValues) {
109 EXPECT_EQ(rtc::Optional<uint8_t>(), StringToNumber<uint8_t>("256"));
110 EXPECT_EQ(rtc::Optional<uint8_t>(), StringToNumber<uint8_t>("-256"));
111 EXPECT_EQ(rtc::Optional<int8_t>(), StringToNumber<int8_t>("256"));
112 EXPECT_EQ(rtc::Optional<int8_t>(), StringToNumber<int8_t>("-256"));
113}
114
115} // namespace rtc