re2/re2/testing/re2_arg_test.cc
Go to the documentation of this file.
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 
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 
112 TEST(RE2ArgTest, Int16Test) {
114 }
115 
116 TEST(RE2ArgTest, Uint16Test) {
118 }
119 
120 TEST(RE2ArgTest, Int32Test) {
122 }
123 
124 TEST(RE2ArgTest, Uint32Test) {
126 }
127 
128 TEST(RE2ArgTest, Int64Test) {
130 }
131 
132 TEST(RE2ArgTest, Uint64Test) {
134 }
135 
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
xds_interop_client.str
str
Definition: xds_interop_client.py:487
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
re2::RE2::Arg::Parse
bool Parse(const char *str, size_t n) const
Definition: bloaty/third_party/re2/re2/re2.h:871
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
string.h
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
ULL
#define ULL(x)
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:57
int16_t
signed short int16_t
Definition: stdint-msvc2008.h:76
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
python_utils.jobset.INFO
INFO
Definition: jobset.py:111
LOG
#define LOG(severity)
Definition: bloaty/third_party/re2/util/logging.h:53
PARSE_FOR_TYPE
#define PARSE_FOR_TYPE(type, column)
Definition: re2/re2/testing/re2_arg_test.cc:96
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
re2::kNumStrings
const int kNumStrings
Definition: bloaty/third_party/re2/re2/testing/re2_arg_test.cc:89
re2::SuccessTable::value
int64_t value
Definition: bloaty/third_party/re2/re2/testing/re2_arg_test.cc:20
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
stdint.h
arraysize
#define arraysize(array)
Definition: benchmark/src/arraysize.h:28
re2::SuccessTable::value_string
const char * value_string
Definition: bloaty/third_party/re2/re2/testing/re2_arg_test.cc:19
re2::SuccessTable::success
bool success[6]
Definition: bloaty/third_party/re2/re2/testing/re2_arg_test.cc:21
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
re2::kSuccessTable
const SuccessTable kSuccessTable[]
Definition: bloaty/third_party/re2/re2/testing/re2_arg_test.cc:30
re2::TEST
TEST(TestCharClassBuilder, Adds)
Definition: bloaty/third_party/re2/re2/testing/charclass_test.cc:198
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
re2::RE2::Arg
Definition: bloaty/third_party/re2/re2/re2.h:786
LL
#define LL(x)


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:00