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.
5 // This tests to make sure numbers are parsed from strings
7 // Todo: Expand the test to validate strings parsed to the other types
8 // supported by RE2::Arg class
10 #include "util/test.h"
16 const char * value_string;
21 // Test boundary cases for different integral sizes.
22 // Specifically I want to make sure that values outside the boundries
23 // of an integral type will fail and that negative numbers will fail
24 // for unsigned types. The following table contains the boundaries for
25 // the various integral types and has entries for whether or not each
26 // type can contain the given value.
27 const SuccessTable kSuccessTable[] = {
28 // string integer value short ushort int uint int64 uint64
30 { "0", 0, { true, true, true, true, true, true }},
31 { "127", 127, { true, true, true, true, true, true }},
34 { "-1", -1, { true, false, true, false, true, false }},
35 { "-128", -128, { true, false, true, false, true, false }},
38 { "128", 128, { true, true, true, true, true, true }},
39 { "255", 255, { true, true, true, true, true, true }},
42 { "256", 256, { true, true, true, true, true, true }},
43 { "32767", 32767, { true, true, true, true, true, true }},
46 { "-129", -129, { true, false, true, false, true, false }},
47 { "-32768", -32768, { true, false, true, false, true, false }},
50 { "32768", 32768, { false, true, true, true, true, true }},
51 { "65535", 65535, { false, true, true, true, true, true }},
54 { "65536", 65536, { false, false, true, true, true, true }},
55 { "2147483647", 2147483647, { false, false, true, true, true, true }},
58 { "-32769", -32769, { false, false, true, false, true, false }},
60 static_cast<int64>(0xFFFFFFFF80000000LL),
61 { false, false, true, false, true, false }},
64 { "2147483648", 2147483648U, { false, false, false, true, true, true }},
65 { "4294967295", 4294967295U, { false, false, false, true, true, true }},
68 { "4294967296", 4294967296LL, { false, false, false, false, true, true }},
69 { "9223372036854775807",
70 9223372036854775807LL, { false, false, false, false, true, true }},
73 { "-2147483649", -2147483649LL, { false, false, false, false, true, false }},
74 { "-9223372036854775808", static_cast<int64>(0x8000000000000000LL),
75 { false, false, false, false, true, false }},
78 { "9223372036854775808", static_cast<int64>(9223372036854775808ULL),
79 { false, false, false, false, false, true }},
80 { "18446744073709551615", static_cast<int64>(18446744073709551615ULL),
81 { false, false, false, false, false, true }},
84 { "18446744073709551616", 0, { false, false, false, false, false, false }},
87 const int kNumStrings = ARRAYSIZE(kSuccessTable);
89 // It's ugly to use a macro, but we apparently can't use the ASSERT_TRUE_M
90 // macro outside of a TEST block and this seems to be the only way to
91 // avoid code duplication. I can also pull off a couple nice tricks
92 // using concatenation for the type I'm checking against.
93 #define PARSE_FOR_TYPE(type, column) { \
95 for ( int i = 0; i < kNumStrings; ++i ) { \
97 const char* const p = kSuccessTable[i].value_string; \
98 bool retval = arg.Parse(p, strlen(p)); \
99 bool success = kSuccessTable[i].success[column]; \
100 ASSERT_TRUE_M(retval == success, \
101 StringPrintf("Parsing '%s' for type " #type " should return %d", \
102 p, success).c_str()); \
104 ASSERT_EQUALS(r, kSuccessTable[i].value); \
109 TEST(REArgTest, Int16Test) {
110 PARSE_FOR_TYPE(int16, 0);
113 TEST(REArgTest, Uint16Test) {
114 PARSE_FOR_TYPE(uint16, 1);
117 TEST(REArgTest, IntTest) {
118 PARSE_FOR_TYPE(int, 2);
121 TEST(REArgTest, UInt32Test) {
122 PARSE_FOR_TYPE(uint32, 3);
125 TEST(REArgTest, Iint64Test) {
126 PARSE_FOR_TYPE(int64, 4);
129 TEST(REArgTest, Uint64Test) {
130 PARSE_FOR_TYPE(uint64, 5);