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 const auto min_value = std::numeric_limits<T>::lowest(); | |
43 const auto max_value = std::numeric_limits<T>::max(); | |
kwiberg-webrtc
2017/02/18 12:28:53
constexpr T
| |
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>(std::string("-0000000000000"))); | |
52 } | |
53 | |
54 TYPED_TEST_P(BasicNumberTest, TestInvalidNumbers) { | |
55 using T = TypeParam; | |
56 // Value ranges aren't strictly enforced in this test, since that would either | |
57 // require doctoring specific strings for each data type, which is a hassle | |
58 // across platforms, or to be able to do addition of values larger than the | |
59 // largest type, which is another hassle. | |
kwiberg-webrtc
2017/02/18 12:28:53
Could you make one or a few tests for some specifi
| |
60 const auto min_value = std::numeric_limits<T>::lowest(); | |
61 const auto max_value = std::numeric_limits<T>::max(); | |
62 // If the type supports negative values, make the large negative value | |
63 // approximately ten times larger. If the type is unsigned, just use -2. | |
64 const std::string too_low_string = | |
65 (min_value == 0) ? "-2" : (std::to_string(min_value) + "1"); | |
66 // Make the large value approximately ten times larger than the maximum. | |
67 const std::string too_large_string = std::to_string(max_value) + "1"; | |
68 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_low_string)); | |
69 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_low_string.c_str())); | |
70 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_large_string)); | |
71 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_large_string.c_str())); | |
72 } | |
73 | |
74 TYPED_TEST_P(BasicNumberTest, TestInvalidInputs) { | |
75 using T = TypeParam; | |
76 const char kInvalidCharArray[] = "Invalid string containing 47"; | |
77 const char kPlusMinusCharArray[] = "+-100"; | |
78 const char kNumberFollowedByCruft[] = "640x480"; | |
79 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kInvalidCharArray)); | |
80 EXPECT_EQ(rtc::Optional<T>(), | |
81 StringToNumber<T>(std::string(kInvalidCharArray))); | |
82 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kPlusMinusCharArray)); | |
83 EXPECT_EQ(rtc::Optional<T>(), | |
84 StringToNumber<T>(std::string(kPlusMinusCharArray))); | |
85 EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kNumberFollowedByCruft)); | |
86 EXPECT_EQ(rtc::Optional<T>(), | |
87 StringToNumber<T>(std::string(kNumberFollowedByCruft))); | |
88 } | |
89 | |
90 REGISTER_TYPED_TEST_CASE_P(BasicNumberTest, | |
91 TestValidNumbers, | |
92 TestInvalidNumbers, | |
93 TestInvalidInputs); | |
94 | |
95 } // namespace | |
96 | |
97 INSTANTIATE_TYPED_TEST_CASE_P(StringToNumberTest_Integers, | |
98 BasicNumberTest, | |
99 IntegerTypes); | |
100 | |
101 } // namespace rtc | |
OLD | NEW |