blob: f5e5b573f1ad29f4baa808584233af93ebf9aefa [file] [log] [blame]
ossua280f7c2017-04-06 09:02:151/*
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 04:47:3111#include "rtc_base/string_to_number.h"
ossua280f7c2017-04-06 09:02:1512
13#include <string>
14#include <type_traits>
15#include <limits>
16
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "rtc_base/gunit.h"
ossua280f7c2017-04-06 09:02:1518
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";
Oskar Sundbom9c780582017-11-27 09:28:2270 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(too_low_string));
71 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(too_low_string.c_str()));
72 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(too_large_string));
73 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(too_large_string.c_str()));
ossua280f7c2017-04-06 09:02:1574}
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";
Oskar Sundbom9c780582017-11-27 09:28:2281 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(kInvalidCharArray));
82 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(std::string(kInvalidCharArray)));
83 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(kPlusMinusCharArray));
84 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(std::string(kPlusMinusCharArray)));
85 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(kNumberFollowedByCruft));
86 EXPECT_EQ(rtc::nullopt,
ossua280f7c2017-04-06 09:02:1587 StringToNumber<T>(std::string(kNumberFollowedByCruft)));
Oskar Sundbom9c780582017-11-27 09:28:2288 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(" 5"));
89 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(" - 5"));
90 EXPECT_EQ(rtc::nullopt, StringToNumber<T>("- 5"));
91 EXPECT_EQ(rtc::nullopt, StringToNumber<T>(" -5"));
92 EXPECT_EQ(rtc::nullopt, StringToNumber<T>("5 "));
ossua280f7c2017-04-06 09:02:1593}
94
95REGISTER_TYPED_TEST_CASE_P(BasicNumberTest,
96 TestValidNumbers,
97 TestInvalidNumbers,
98 TestInvalidInputs);
99
100} // namespace
101
102INSTANTIATE_TYPED_TEST_CASE_P(StringToNumberTest_Integers,
103 BasicNumberTest,
104 IntegerTypes);
105
106TEST(StringToNumberTest, TestSpecificValues) {
Oskar Sundbom9c780582017-11-27 09:28:22107 EXPECT_EQ(rtc::nullopt, StringToNumber<uint8_t>("256"));
108 EXPECT_EQ(rtc::nullopt, StringToNumber<uint8_t>("-256"));
109 EXPECT_EQ(rtc::nullopt, StringToNumber<int8_t>("256"));
110 EXPECT_EQ(rtc::nullopt, StringToNumber<int8_t>("-256"));
ossua280f7c2017-04-06 09:02:15111}
112
113} // namespace rtc