tgupdate: merge pcreposix-compat base into pcreposix-compat
[pcreposix-compat.git] / pcre_stringpiece_unittest.cc
blob88e73a1f976b7491b901ab3170192569852e235c
1 // Copyright 2003 and onwards Google Inc.
2 // Author: Sanjay Ghemawat
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
8 #include <stdio.h>
9 #include <map>
10 #include <algorithm> // for make_pair
12 #include "pcrecpp.h"
13 #include "pcre_stringpiece.h"
15 // CHECK dies with a fatal error if condition is not true. It is *not*
16 // controlled by NDEBUG, so the check will be executed regardless of
17 // compilation mode. Therefore, it is safe to do things like:
18 // CHECK(fp->Write(x) == 4)
19 #define CHECK(condition) do { \
20 if (!(condition)) { \
21 fprintf(stderr, "%s:%d: Check failed: %s\n", \
22 __FILE__, __LINE__, #condition); \
23 exit(1); \
24 } \
25 } while (0)
27 using std::string;
28 using pcrecpp::StringPiece;
30 static void CheckSTLComparator() {
31 string s1("foo");
32 string s2("bar");
33 string s3("baz");
35 StringPiece p1(s1);
36 StringPiece p2(s2);
37 StringPiece p3(s3);
39 typedef std::map<StringPiece, int> TestMap;
40 TestMap map;
42 map.insert(std::make_pair(p1, 0));
43 map.insert(std::make_pair(p2, 1));
44 map.insert(std::make_pair(p3, 2));
46 CHECK(map.size() == 3);
48 TestMap::const_iterator iter = map.begin();
49 CHECK(iter->second == 1);
50 ++iter;
51 CHECK(iter->second == 2);
52 ++iter;
53 CHECK(iter->second == 0);
54 ++iter;
55 CHECK(iter == map.end());
57 TestMap::iterator new_iter = map.find("zot");
58 CHECK(new_iter == map.end());
60 new_iter = map.find("bar");
61 CHECK(new_iter != map.end());
63 map.erase(new_iter);
64 CHECK(map.size() == 2);
66 iter = map.begin();
67 CHECK(iter->second == 2);
68 ++iter;
69 CHECK(iter->second == 0);
70 ++iter;
71 CHECK(iter == map.end());
74 static void CheckComparisonOperators() {
75 #define CMP_Y(op, x, y) \
76 CHECK( (StringPiece((x)) op StringPiece((y)))); \
77 CHECK( (StringPiece((x)).compare(StringPiece((y))) op 0))
79 #define CMP_N(op, x, y) \
80 CHECK(!(StringPiece((x)) op StringPiece((y)))); \
81 CHECK(!(StringPiece((x)).compare(StringPiece((y))) op 0))
83 CMP_Y(==, "", "");
84 CMP_Y(==, "a", "a");
85 CMP_Y(==, "aa", "aa");
86 CMP_N(==, "a", "");
87 CMP_N(==, "", "a");
88 CMP_N(==, "a", "b");
89 CMP_N(==, "a", "aa");
90 CMP_N(==, "aa", "a");
92 CMP_N(!=, "", "");
93 CMP_N(!=, "a", "a");
94 CMP_N(!=, "aa", "aa");
95 CMP_Y(!=, "a", "");
96 CMP_Y(!=, "", "a");
97 CMP_Y(!=, "a", "b");
98 CMP_Y(!=, "a", "aa");
99 CMP_Y(!=, "aa", "a");
101 CMP_Y(<, "a", "b");
102 CMP_Y(<, "a", "aa");
103 CMP_Y(<, "aa", "b");
104 CMP_Y(<, "aa", "bb");
105 CMP_N(<, "a", "a");
106 CMP_N(<, "b", "a");
107 CMP_N(<, "aa", "a");
108 CMP_N(<, "b", "aa");
109 CMP_N(<, "bb", "aa");
111 CMP_Y(<=, "a", "a");
112 CMP_Y(<=, "a", "b");
113 CMP_Y(<=, "a", "aa");
114 CMP_Y(<=, "aa", "b");
115 CMP_Y(<=, "aa", "bb");
116 CMP_N(<=, "b", "a");
117 CMP_N(<=, "aa", "a");
118 CMP_N(<=, "b", "aa");
119 CMP_N(<=, "bb", "aa");
121 CMP_N(>=, "a", "b");
122 CMP_N(>=, "a", "aa");
123 CMP_N(>=, "aa", "b");
124 CMP_N(>=, "aa", "bb");
125 CMP_Y(>=, "a", "a");
126 CMP_Y(>=, "b", "a");
127 CMP_Y(>=, "aa", "a");
128 CMP_Y(>=, "b", "aa");
129 CMP_Y(>=, "bb", "aa");
131 CMP_N(>, "a", "a");
132 CMP_N(>, "a", "b");
133 CMP_N(>, "a", "aa");
134 CMP_N(>, "aa", "b");
135 CMP_N(>, "aa", "bb");
136 CMP_Y(>, "b", "a");
137 CMP_Y(>, "aa", "a");
138 CMP_Y(>, "b", "aa");
139 CMP_Y(>, "bb", "aa");
141 #undef CMP_Y
142 #undef CMP_N
145 int main(int argc, char** argv) {
146 (void)argc;
147 (void)argv;
148 CheckComparisonOperators();
149 CheckSTLComparator();
151 printf("OK\n");
152 return 0;