/Users/deen/code/yugabyte-db/src/yb/util/stol_utils-test.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/util/stol_utils.h" |
15 | | #include "yb/util/test_macros.h" |
16 | | #include "yb/util/test_util.h" |
17 | | |
18 | | namespace yb { |
19 | | namespace util { |
20 | | |
21 | | class StolUtilsTest : public YBTest { |
22 | | protected: |
23 | 4 | void TestStoi(const std::string& str, const int32_t int_val) { |
24 | 4 | auto decoded_int = CheckedStoi(str); |
25 | 4 | ASSERT_OK(decoded_int); |
26 | 4 | ASSERT_EQ(int_val, *decoded_int); |
27 | 4 | } |
28 | | |
29 | 4 | void TestStoll(const std::string& str, const int64_t int_val) { |
30 | 4 | auto decoded_int = CheckedStoll(str); |
31 | 4 | ASSERT_OK(decoded_int); |
32 | 4 | ASSERT_EQ(int_val, *decoded_int); |
33 | 4 | } |
34 | | |
35 | 5 | void TestStold(const std::string& str, const long double double_val) { |
36 | 5 | auto decoded_double = CheckedStold(str); |
37 | 5 | ASSERT_OK(decoded_double); |
38 | 5 | ASSERT_DOUBLE_EQ(double_val, *decoded_double); |
39 | 5 | } |
40 | | }; |
41 | | |
42 | 1 | TEST_F(StolUtilsTest, TestCheckedStoild) { |
43 | 1 | TestStoi("123", 123); |
44 | 1 | TestStoi("-123", -123); |
45 | 1 | TestStoi("2147483647", 2147483647); |
46 | 1 | TestStoi("-2147483648", -2147483648); |
47 | 1 | ASSERT_NOK(CheckedStoi("abcd")); |
48 | 1 | ASSERT_NOK(CheckedStoi(" 123")); |
49 | 1 | ASSERT_NOK(CheckedStoi("123abc")); |
50 | 1 | ASSERT_NOK(CheckedStoi("123.1")); |
51 | 1 | ASSERT_NOK(CheckedStoi("123456789011")); |
52 | 1 | ASSERT_NOK(CheckedStoi("123-abc")); |
53 | 1 | ASSERT_NOK(CheckedStoi("123 123")); |
54 | 1 | ASSERT_NOK(CheckedStoi("2147483648")); |
55 | 1 | ASSERT_NOK(CheckedStoi("-2147483649")); |
56 | 1 | ASSERT_NOK(CheckedStoi("")); |
57 | 1 | ASSERT_NOK(CheckedStoi("123 ")); |
58 | | |
59 | 1 | TestStoll("123", 123); |
60 | 1 | TestStoll("-123", -123); |
61 | 1 | TestStoll("9223372036854775807", 9223372036854775807LL); |
62 | 1 | TestStoll("-9223372036854775808", -9223372036854775808ULL); |
63 | 1 | ASSERT_NOK(CheckedStoll("abcd")); |
64 | 1 | ASSERT_NOK(CheckedStoll(" 123")); |
65 | 1 | ASSERT_NOK(CheckedStoll("123abc")); |
66 | 1 | ASSERT_NOK(CheckedStoll("-9223372036854775809")); |
67 | 1 | ASSERT_NOK(CheckedStoll("9223372036854775808")); |
68 | 1 | ASSERT_NOK(CheckedStoll("123.1")); |
69 | 1 | ASSERT_NOK(CheckedStoll("123456789123456789123456789")); |
70 | 1 | ASSERT_NOK(CheckedStoll("123 123")); |
71 | 1 | ASSERT_NOK(CheckedStoll("")); |
72 | | |
73 | 1 | TestStold("123", 123); |
74 | 1 | TestStold("-123", -123); |
75 | 1 | TestStold("123.1", 123.1); |
76 | 1 | TestStold("1.7e308", 1.7e308); |
77 | 1 | TestStold("-1.7e308", -1.7e308); |
78 | 1 | ASSERT_NOK(CheckedStold("abcd")); |
79 | 1 | ASSERT_NOK(CheckedStold(" 123")); |
80 | 1 | ASSERT_NOK(CheckedStold("123abc")); |
81 | 1 | ASSERT_NOK(CheckedStold("123 123")); |
82 | 1 | ASSERT_NOK(CheckedStold("9223372036854775808e9223372036854775808")); |
83 | 1 | ASSERT_NOK(CheckedStold("-9223372036854775808.8e9223372036854775808")); |
84 | 1 | ASSERT_NOK(CheckedStold("")); |
85 | 1 | } |
86 | | |
87 | | } // namespace util |
88 | | } // namespace yb |