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