1 // Copyright 2003 and onwards Google Inc.
2 // Author: Sanjay Ghemawat
10 #include <algorithm> // for make_pair
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 { \
21 fprintf(stderr, "%s:%d: Check failed: %s\n", \
22 __FILE__, __LINE__, #condition); \
28 using pcrecpp::StringPiece
;
30 static void CheckSTLComparator() {
39 typedef std::map
<StringPiece
, int> TestMap
;
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);
51 CHECK(iter
->second
== 2);
53 CHECK(iter
->second
== 0);
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());
64 CHECK(map
.size() == 2);
67 CHECK(iter
->second
== 2);
69 CHECK(iter
->second
== 0);
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))
85 CMP_Y(==, "aa", "aa");
94 CMP_N(!=, "aa", "aa");
104 CMP_Y(<, "aa", "bb");
109 CMP_N(<, "bb", "aa");
113 CMP_Y(<=, "a", "aa");
114 CMP_Y(<=, "aa", "b");
115 CMP_Y(<=, "aa", "bb");
117 CMP_N(<=, "aa", "a");
118 CMP_N(<=, "b", "aa");
119 CMP_N(<=, "bb", "aa");
122 CMP_N(>=, "a", "aa");
123 CMP_N(>=, "aa", "b");
124 CMP_N(>=, "aa", "bb");
127 CMP_Y(>=, "aa", "a");
128 CMP_Y(>=, "b", "aa");
129 CMP_Y(>=, "bb", "aa");
135 CMP_N(>, "aa", "bb");
139 CMP_Y(>, "bb", "aa");
145 int main(int argc
, char** argv
) {
148 CheckComparisonOperators();
149 CheckSTLComparator();