1 // Copyright 2005 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // This tests to make sure numbers are parsed from strings
6 // correctly.
7 // Todo: Expand the test to validate strings parsed to the other types
8 // supported by RE2::Arg class
9
10 #include <stdint.h>
11 #include <string.h>
12
13 #include "util/test.h"
14 #include "util/logging.h"
15 #include "re2/re2.h"
16
17 namespace re2 {
18
19 struct SuccessTable {
20 const char * value_string;
21 int64_t value;
22 bool success[6];
23 };
24
25 // Test boundary cases for different integral sizes.
26 // Specifically I want to make sure that values outside the boundries
27 // of an integral type will fail and that negative numbers will fail
28 // for unsigned types. The following table contains the boundaries for
29 // the various integral types and has entries for whether or not each
30 // type can contain the given value.
31 const SuccessTable kSuccessTable[] = {
32 // string integer value i16 u16 i32 u32 i64 u64
33 // 0 to 2^7-1
34 { "0", 0, { true, true, true, true, true, true }},
35 { "127", 127, { true, true, true, true, true, true }},
36
37 // -1 to -2^7
38 { "-1", -1, { true, false, true, false, true, false }},
39 { "-128", -128, { true, false, true, false, true, false }},
40
41 // 2^7 to 2^8-1
42 { "128", 128, { true, true, true, true, true, true }},
43 { "255", 255, { true, true, true, true, true, true }},
44
45 // 2^8 to 2^15-1
46 { "256", 256, { true, true, true, true, true, true }},
47 { "32767", 32767, { true, true, true, true, true, true }},
48
49 // -2^7-1 to -2^15
50 { "-129", -129, { true, false, true, false, true, false }},
51 { "-32768", -32768, { true, false, true, false, true, false }},
52
53 // 2^15 to 2^16-1
54 { "32768", 32768, { false, true, true, true, true, true }},
55 { "65535", 65535, { false, true, true, true, true, true }},
56
57 // 2^16 to 2^31-1
58 { "65536", 65536, { false, false, true, true, true, true }},
59 { "2147483647", 2147483647, { false, false, true, true, true, true }},
60
61 // -2^15-1 to -2^31
62 { "-32769", -32769, { false, false, true, false, true, false }},
63 { "-2147483648", static_cast<int64_t>(0xFFFFFFFF80000000LL),
64 { false, false, true, false, true, false }},
65
66 // 2^31 to 2^32-1
67 { "2147483648", 2147483648U, { false, false, false, true, true, true }},
68 { "4294967295", 4294967295U, { false, false, false, true, true, true }},
69
70 // 2^32 to 2^63-1
71 { "4294967296", 4294967296LL, { false, false, false, false, true, true }},
72 { "9223372036854775807",
73 9223372036854775807LL, { false, false, false, false, true, true }},
74
75 // -2^31-1 to -2^63
76 { "-2147483649", -2147483649LL, { false, false, false, false, true, false }},
77 { "-9223372036854775808", static_cast<int64_t>(0x8000000000000000LL),
78 { false, false, false, false, true, false }},
79
80 // 2^63 to 2^64-1
81 { "9223372036854775808", static_cast<int64_t>(9223372036854775808ULL),
82 { false, false, false, false, false, true }},
83 { "18446744073709551615", static_cast<int64_t>(18446744073709551615ULL),
84 { false, false, false, false, false, true }},
85
86 // >= 2^64
87 { "18446744073709551616", 0, { false, false, false, false, false, false }},
88 };
89
90 const int kNumStrings = arraysize(kSuccessTable);
91
92 // It's ugly to use a macro, but we apparently can't use the EXPECT_EQ
93 // macro outside of a TEST block and this seems to be the only way to
94 // avoid code duplication. I can also pull off a couple nice tricks
95 // using concatenation for the type I'm checking against.
96 #define PARSE_FOR_TYPE(type, column) { \
97 type r; \
98 for (int i = 0; i < kNumStrings; ++i) { \
99 RE2::Arg arg(&r); \
100 const char* const p = kSuccessTable[i].value_string; \
101 bool retval = arg.Parse(p, strlen(p)); \
102 bool success = kSuccessTable[i].success[column]; \
103 EXPECT_EQ(retval, success) \
104 << "Parsing '" << p << "' for type " #type " should return " \
105 << success; \
106 if (success) { \
107 EXPECT_EQ(r, (type)kSuccessTable[i].value); \
108 } \
109 } \
110 }
111
TEST(RE2ArgTest,Int16Test)112 TEST(RE2ArgTest, Int16Test) {
113 PARSE_FOR_TYPE(int16_t, 0);
114 }
115
TEST(RE2ArgTest,Uint16Test)116 TEST(RE2ArgTest, Uint16Test) {
117 PARSE_FOR_TYPE(uint16_t, 1);
118 }
119
TEST(RE2ArgTest,Int32Test)120 TEST(RE2ArgTest, Int32Test) {
121 PARSE_FOR_TYPE(int32_t, 2);
122 }
123
TEST(RE2ArgTest,Uint32Test)124 TEST(RE2ArgTest, Uint32Test) {
125 PARSE_FOR_TYPE(uint32_t, 3);
126 }
127
TEST(RE2ArgTest,Int64Test)128 TEST(RE2ArgTest, Int64Test) {
129 PARSE_FOR_TYPE(int64_t, 4);
130 }
131
TEST(RE2ArgTest,Uint64Test)132 TEST(RE2ArgTest, Uint64Test) {
133 PARSE_FOR_TYPE(uint64_t, 5);
134 }
135
TEST(RE2ArgTest,ParseFromTest)136 TEST(RE2ArgTest, ParseFromTest) {
137 #if !defined(_MSC_VER)
138 struct {
139 bool ParseFrom(const char* str, size_t n) {
140 LOG(INFO) << "str = " << str << ", n = " << n;
141 return true;
142 }
143 } obj1;
144 RE2::Arg arg1(&obj1);
145 EXPECT_TRUE(arg1.Parse("one", 3));
146
147 struct {
148 bool ParseFrom(const char* str, size_t n) {
149 LOG(INFO) << "str = " << str << ", n = " << n;
150 return false;
151 }
152 // Ensure that RE2::Arg works even with overloaded ParseFrom().
153 void ParseFrom(const char* str) {}
154 } obj2;
155 RE2::Arg arg2(&obj2);
156 EXPECT_FALSE(arg2.Parse("two", 3));
157 #endif
158 }
159
160 } // namespace re2
161