1// Copyright 2003 and onwards Google Inc.
2// Author: Sanjay Ghemawat
3
4#ifdef HAVE_CONFIG_H
5#include "config.h"
6#endif
7
8#include <stdio.h>
9#include <map>
10#include <algorithm>    // for make_pair
11
12#include "pcrecpp.h"
13#include "pcre_stringpiece.h"
14
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)
26
27using std::map;
28using std::make_pair;
29using pcrecpp::StringPiece;
30
31static void CheckSTLComparator() {
32  string s1("foo");
33  string s2("bar");
34  string s3("baz");
35
36  StringPiece p1(s1);
37  StringPiece p2(s2);
38  StringPiece p3(s3);
39
40  typedef map<StringPiece, int> TestMap;
41  TestMap map;
42
43  map.insert(make_pair(p1, 0));
44  map.insert(make_pair(p2, 1));
45  map.insert(make_pair(p3, 2));
46  CHECK(map.size() == 3);
47
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());
56
57  TestMap::iterator new_iter = map.find("zot");
58  CHECK(new_iter == map.end());
59
60  new_iter = map.find("bar");
61  CHECK(new_iter != map.end());
62
63  map.erase(new_iter);
64  CHECK(map.size() == 2);
65
66  iter = map.begin();
67  CHECK(iter->second == 2);
68  ++iter;
69  CHECK(iter->second == 0);
70  ++iter;
71  CHECK(iter == map.end());
72}
73
74static 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))
78
79#define CMP_N(op, x, y)                                         \
80  CHECK(!(StringPiece((x)) op StringPiece((y))));               \
81  CHECK(!(StringPiece((x)).compare(StringPiece((y))) op 0))
82
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");
91
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");
100
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");
110
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");
120
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");
130
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");
140
141#undef CMP_Y
142#undef CMP_N
143}
144
145int main(int argc, char** argv) {
146  CheckComparisonOperators();
147  CheckSTLComparator();
148
149  printf("OK\n");
150  return 0;
151}
152