• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "absl/base/macros.h"
14 #include "absl/log/absl_log.h"
15 #include "absl/types/optional.h"
16 #include "gtest/gtest.h"
17 #include "re2/re2.h"
18 
19 namespace re2 {
20 
21 struct SuccessTable {
22   const char * value_string;
23   int64_t value;
24   bool success[6];
25 };
26 
27 // Test boundary cases for different integral sizes.
28 // Specifically I want to make sure that values outside the boundries
29 // of an integral type will fail and that negative numbers will fail
30 // for unsigned types. The following table contains the boundaries for
31 // the various integral types and has entries for whether or not each
32 // type can contain the given value.
33 const SuccessTable kSuccessTable[] = {
34 // string       integer value     i16    u16    i32    u32    i64    u64
35 // 0 to 2^7-1
36 { "0",          0,              { true,  true,  true,  true,  true,  true  }},
37 { "127",        127,            { true,  true,  true,  true,  true,  true  }},
38 
39 // -1 to -2^7
40 { "-1",         -1,             { true,  false, true,  false, true,  false }},
41 { "-128",       -128,           { true,  false, true,  false, true,  false }},
42 
43 // 2^7 to 2^8-1
44 { "128",        128,            { true,  true,  true,  true,  true,  true  }},
45 { "255",        255,            { true,  true,  true,  true,  true,  true  }},
46 
47 // 2^8 to 2^15-1
48 { "256",        256,            { true,  true,  true,  true,  true,  true  }},
49 { "32767",      32767,          { true,  true,  true,  true,  true,  true  }},
50 
51 // -2^7-1 to -2^15
52 { "-129",       -129,           { true,  false, true,  false, true,  false }},
53 { "-32768",     -32768,         { true,  false, true,  false, true,  false }},
54 
55 // 2^15 to 2^16-1
56 { "32768",      32768,          { false, true,  true,  true,  true,  true  }},
57 { "65535",      65535,          { false, true,  true,  true,  true,  true  }},
58 
59 // 2^16 to 2^31-1
60 { "65536",      65536,          { false, false, true,  true,  true,  true  }},
61 { "2147483647", 2147483647,     { false, false, true,  true,  true,  true  }},
62 
63 // -2^15-1 to -2^31
64 { "-32769",     -32769,         { false, false, true,  false, true,  false }},
65 { "-2147483648", static_cast<int64_t>(0xFFFFFFFF80000000LL),
66   { false, false, true,  false, true,  false }},
67 
68 // 2^31 to 2^32-1
69 { "2147483648", 2147483648U,    { false, false, false, true,  true,  true  }},
70 { "4294967295", 4294967295U,    { false, false, false, true,  true,  true  }},
71 
72 // 2^32 to 2^63-1
73 { "4294967296", 4294967296LL,   { false, false, false, false, true,  true  }},
74 { "9223372036854775807",
75   9223372036854775807LL,        { false, false, false, false, true,  true  }},
76 
77 // -2^31-1 to -2^63
78 { "-2147483649", -2147483649LL, { false, false, false, false, true,  false }},
79 { "-9223372036854775808", static_cast<int64_t>(0x8000000000000000LL),
80   { false, false, false, false, true,  false }},
81 
82 // 2^63 to 2^64-1
83 { "9223372036854775808", static_cast<int64_t>(9223372036854775808ULL),
84   { false, false, false, false, false, true  }},
85 { "18446744073709551615", static_cast<int64_t>(18446744073709551615ULL),
86   { false, false, false, false, false, true  }},
87 
88 // >= 2^64
89 { "18446744073709551616", 0,    { false, false, false, false, false, false }},
90 };
91 
92 const int kNumStrings = ABSL_ARRAYSIZE(kSuccessTable);
93 
94 // It's ugly to use a macro, but we apparently can't use the EXPECT_EQ
95 // macro outside of a TEST block and this seems to be the only way to
96 // avoid code duplication.  I can also pull off a couple nice tricks
97 // using concatenation for the type I'm checking against.
98 #define PARSE_FOR_TYPE(type, column) {                                   \
99   type r;                                                                \
100   for (int i = 0; i < kNumStrings; ++i) {                                \
101     RE2::Arg arg(&r);                                                    \
102     const char* const p = kSuccessTable[i].value_string;                 \
103     bool retval = arg.Parse(p, strlen(p));                               \
104     bool success = kSuccessTable[i].success[column];                     \
105     EXPECT_EQ(retval, success)                                           \
106         << "Parsing '" << p << "' for type " #type " should return "     \
107         << success;                                                      \
108     if (success) {                                                       \
109       EXPECT_EQ(r, (type)kSuccessTable[i].value);                        \
110     }                                                                    \
111   }                                                                      \
112 }
113 
TEST(RE2ArgTest,Int16Test)114 TEST(RE2ArgTest, Int16Test) {
115   PARSE_FOR_TYPE(int16_t, 0);
116 }
117 
TEST(RE2ArgTest,Uint16Test)118 TEST(RE2ArgTest, Uint16Test) {
119   PARSE_FOR_TYPE(uint16_t, 1);
120 }
121 
TEST(RE2ArgTest,Int32Test)122 TEST(RE2ArgTest, Int32Test) {
123   PARSE_FOR_TYPE(int32_t, 2);
124 }
125 
TEST(RE2ArgTest,Uint32Test)126 TEST(RE2ArgTest, Uint32Test) {
127   PARSE_FOR_TYPE(uint32_t, 3);
128 }
129 
TEST(RE2ArgTest,Int64Test)130 TEST(RE2ArgTest, Int64Test) {
131   PARSE_FOR_TYPE(int64_t, 4);
132 }
133 
TEST(RE2ArgTest,Uint64Test)134 TEST(RE2ArgTest, Uint64Test) {
135   PARSE_FOR_TYPE(uint64_t, 5);
136 }
137 
TEST(RE2ArgTest,ParseFromTest)138 TEST(RE2ArgTest, ParseFromTest) {
139   struct {
140     bool ParseFrom(const char* str, size_t n) {
141       ABSL_LOG(INFO) << "str = " << str << ", n = " << n;
142       return true;
143     }
144   } obj1;
145   RE2::Arg arg1(&obj1);
146   EXPECT_TRUE(arg1.Parse("one", 3));
147 
148   struct {
149     bool ParseFrom(const char* str, size_t n) {
150       ABSL_LOG(INFO) << "str = " << str << ", n = " << n;
151       return false;
152     }
153     // Ensure that RE2::Arg works even with overloaded ParseFrom().
154     void ParseFrom(const char* str) {}
155   } obj2;
156   RE2::Arg arg2(&obj2);
157   EXPECT_FALSE(arg2.Parse("two", 3));
158 }
159 
TEST(RE2ArgTest,OptionalDoubleTest)160 TEST(RE2ArgTest, OptionalDoubleTest) {
161   absl::optional<double> opt;
162   RE2::Arg arg(&opt);
163   EXPECT_TRUE(arg.Parse(NULL, 0));
164   EXPECT_FALSE(opt.has_value());
165   EXPECT_FALSE(arg.Parse("", 0));
166   EXPECT_TRUE(arg.Parse("28.30", 5));
167   EXPECT_TRUE(opt.has_value());
168   EXPECT_EQ(*opt, 28.30);
169 }
170 
TEST(RE2ArgTest,OptionalIntWithCRadixTest)171 TEST(RE2ArgTest, OptionalIntWithCRadixTest) {
172   absl::optional<int> opt;
173   RE2::Arg arg = RE2::CRadix(&opt);
174   EXPECT_TRUE(arg.Parse(NULL, 0));
175   EXPECT_FALSE(opt.has_value());
176   EXPECT_FALSE(arg.Parse("", 0));
177   EXPECT_TRUE(arg.Parse("0xb0e", 5));
178   EXPECT_TRUE(opt.has_value());
179   EXPECT_EQ(*opt, 2830);
180 }
181 
182 }  // namespace re2
183