Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(424)

Side by Side Diff: webrtc/base/string_to_number_unittest.cc

Issue 2696003004: Added integer parsing functions in base/string_to_number.h (Closed)
Patch Set: Added more debug printing to invalid inputs tests. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 typedef ::testing::Types<signed char, unsigned char,
kwiberg-webrtc 2017/02/16 00:27:24 using?
ossu 2017/02/16 10:19:26 Sure!
24 short, unsigned short,
25 int, unsigned int,
26 long, unsigned long,
27 long long, unsigned long long,
28 int8_t, uint8_t,
29 int16_t, uint16_t,
30 int32_t, uint32_t,
31 int64_t, uint64_t> IntegerTypes;
32
33 typedef ::testing::Types<float, double, long double> FloatTypes;
34 // clang-format on
35
36 template <typename T>
37 class BasicNumberTest : public ::testing::Test {};
38
39 TYPED_TEST_CASE_P(BasicNumberTest);
40
41 TYPED_TEST_P(BasicNumberTest, TestValidNumbers) {
42 const auto lowest_value = std::numeric_limits<TypeParam>::lowest();
kwiberg-webrtc 2017/02/16 00:27:24 "min_value"? I don't think the lowest/min naming o
ossu 2017/02/16 10:19:26 I think it would be more confusing if min meant tw
43 const auto max_value = std::numeric_limits<TypeParam>::max();
44 const std::string lowest_string = std::to_string(lowest_value);
45 const std::string max_string = std::to_string(max_value);
46 EXPECT_EQ(lowest_value, StringToNumber<TypeParam>(lowest_string))
47 << lowest_string << " should be parsable ";
48 EXPECT_EQ(lowest_value, StringToNumber<TypeParam>(lowest_string.c_str()))
49 << lowest_string << " should be parsable ";
50 EXPECT_EQ(max_value, StringToNumber<TypeParam>(max_string))
51 << max_string << " should be parsable ";
52 EXPECT_EQ(max_value, StringToNumber<TypeParam>(max_string.c_str()))
53 << max_string << " should be parsable ";
54 EXPECT_EQ(0, StringToNumber<TypeParam>("-0"));
55 EXPECT_EQ(0, StringToNumber<TypeParam>(std::string("-0000000000000")));
kwiberg-webrtc 2017/02/16 00:27:24 Hmm. Consider "using T = TypeParam" in these metho
ossu 2017/02/16 10:19:26 Sure!
56 }
kwiberg-webrtc 2017/02/16 00:27:24 Consider testing a few specific simple cases, such
57
58 TYPED_TEST_P(BasicNumberTest, TestInvalidNumbers) {
59 // Value ranges aren't strictly enforced in this test, since that would either
60 // require doctoring specific strings for each data type, which is a hassle
61 // across platforms, or to be able to do addition of values larger than the
62 // largest type, which is another hassle.
63 const auto lowest_value = std::numeric_limits<TypeParam>::lowest();
64 const auto max_value = std::numeric_limits<TypeParam>::max();
65 // If the type supports negative values, make the large negative value
66 // approximately ten times larger. If the type is unsigned, just use -2.
kwiberg-webrtc 2017/02/16 00:27:24 Prepending a "1" doesn't make a value approximatel
ossu 2017/02/16 10:19:26 That is true. Initially I appended a "1" instead,
kwiberg-webrtc 2017/02/16 11:59:42 Oh right, I totally forgot about the floating poin
67 const std::string too_low_string =
68 (lowest_value == 0) ? "-2"
69 : ("-1" + std::to_string(lowest_value).substr(1));
70 // Make the large value approximately ten times larger than the maximum.
71 const std::string too_large_string = "1" + std::to_string(max_value);
72 EXPECT_EQ(rtc::Optional<TypeParam>(),
73 StringToNumber<TypeParam>(too_low_string))
74 << too_low_string << " should not be parsable, but parsed as "
75 << *StringToNumber<TypeParam>(too_low_string);
kwiberg-webrtc 2017/02/16 00:27:24 Do you need to repeat the parsed number? Won't EXP
ossu 2017/02/16 10:19:26 No. It can't print out things through rtc::Optiona
kwiberg-webrtc 2017/02/16 11:59:42 Yes, that sounds good.
76 EXPECT_EQ(rtc::Optional<TypeParam>(),
77 StringToNumber<TypeParam>(too_low_string.c_str()))
78 << too_low_string << " should not be parsable, but parsed as "
79 << *StringToNumber<TypeParam>(too_low_string.c_str());
80 EXPECT_EQ(rtc::Optional<TypeParam>(),
81 StringToNumber<TypeParam>(too_large_string))
82 << too_large_string << " should not be parsable, but parsed as "
83 << *StringToNumber<TypeParam>(too_large_string);
84 EXPECT_EQ(rtc::Optional<TypeParam>(),
85 StringToNumber<TypeParam>(too_large_string.c_str()))
86 << too_large_string << " should not be parsable, but parsed as "
87 << *StringToNumber<TypeParam>(too_large_string.c_str());
88 }
89
90 TYPED_TEST_P(BasicNumberTest, TestInvalidInputs) {
91 const char kInvalidCharArray[] = "Invalid string containing 47";
92 const char kPlusMinusCharArray[] = "+-100";
93 EXPECT_EQ(rtc::Optional<TypeParam>(),
94 StringToNumber<TypeParam>(kInvalidCharArray));
95 EXPECT_EQ(rtc::Optional<TypeParam>(),
96 StringToNumber<TypeParam>(std::string(kInvalidCharArray)));
97 EXPECT_EQ(rtc::Optional<TypeParam>(),
98 StringToNumber<TypeParam>(kPlusMinusCharArray));
99 EXPECT_EQ(rtc::Optional<TypeParam>(),
100 StringToNumber<TypeParam>(std::string(kPlusMinusCharArray)));
101 }
102
103 REGISTER_TYPED_TEST_CASE_P(BasicNumberTest,
104 TestValidNumbers,
105 TestInvalidNumbers,
106 TestInvalidInputs);
107
108 } // namespace
109
110 INSTANTIATE_TYPED_TEST_CASE_P(StringToNumberTest_Integers,
111 BasicNumberTest,
112 IntegerTypes);
113
114 INSTANTIATE_TYPED_TEST_CASE_P(StringToNumberTest_Floats,
115 BasicNumberTest,
116 FloatTypes);
117
118 } // namespace rtc
OLDNEW
« webrtc/base/string_to_number.h ('K') | « webrtc/base/string_to_number.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698