1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/pcre/pcre_stringpiece_unittest.cc Tue Jul 28 17:51:34 2009 -0400
1.3 @@ -0,0 +1,151 @@
1.4 +// Copyright 2003 and onwards Google Inc.
1.5 +// Author: Sanjay Ghemawat
1.6 +
1.7 +#ifdef HAVE_CONFIG_H
1.8 +#include "config.h"
1.9 +#endif
1.10 +
1.11 +#include <stdio.h>
1.12 +#include <map>
1.13 +#include <algorithm> // for make_pair
1.14 +
1.15 +#include "pcrecpp.h"
1.16 +#include "pcre_stringpiece.h"
1.17 +
1.18 +// CHECK dies with a fatal error if condition is not true. It is *not*
1.19 +// controlled by NDEBUG, so the check will be executed regardless of
1.20 +// compilation mode. Therefore, it is safe to do things like:
1.21 +// CHECK(fp->Write(x) == 4)
1.22 +#define CHECK(condition) do { \
1.23 + if (!(condition)) { \
1.24 + fprintf(stderr, "%s:%d: Check failed: %s\n", \
1.25 + __FILE__, __LINE__, #condition); \
1.26 + exit(1); \
1.27 + } \
1.28 +} while (0)
1.29 +
1.30 +using std::map;
1.31 +using std::make_pair;
1.32 +using pcrecpp::StringPiece;
1.33 +
1.34 +static void CheckSTLComparator() {
1.35 + string s1("foo");
1.36 + string s2("bar");
1.37 + string s3("baz");
1.38 +
1.39 + StringPiece p1(s1);
1.40 + StringPiece p2(s2);
1.41 + StringPiece p3(s3);
1.42 +
1.43 + typedef map<StringPiece, int> TestMap;
1.44 + TestMap map;
1.45 +
1.46 + map.insert(make_pair(p1, 0));
1.47 + map.insert(make_pair(p2, 1));
1.48 + map.insert(make_pair(p3, 2));
1.49 + CHECK(map.size() == 3);
1.50 +
1.51 + TestMap::const_iterator iter = map.begin();
1.52 + CHECK(iter->second == 1);
1.53 + ++iter;
1.54 + CHECK(iter->second == 2);
1.55 + ++iter;
1.56 + CHECK(iter->second == 0);
1.57 + ++iter;
1.58 + CHECK(iter == map.end());
1.59 +
1.60 + TestMap::iterator new_iter = map.find("zot");
1.61 + CHECK(new_iter == map.end());
1.62 +
1.63 + new_iter = map.find("bar");
1.64 + CHECK(new_iter != map.end());
1.65 +
1.66 + map.erase(new_iter);
1.67 + CHECK(map.size() == 2);
1.68 +
1.69 + iter = map.begin();
1.70 + CHECK(iter->second == 2);
1.71 + ++iter;
1.72 + CHECK(iter->second == 0);
1.73 + ++iter;
1.74 + CHECK(iter == map.end());
1.75 +}
1.76 +
1.77 +static void CheckComparisonOperators() {
1.78 +#define CMP_Y(op, x, y) \
1.79 + CHECK( (StringPiece((x)) op StringPiece((y)))); \
1.80 + CHECK( (StringPiece((x)).compare(StringPiece((y))) op 0))
1.81 +
1.82 +#define CMP_N(op, x, y) \
1.83 + CHECK(!(StringPiece((x)) op StringPiece((y)))); \
1.84 + CHECK(!(StringPiece((x)).compare(StringPiece((y))) op 0))
1.85 +
1.86 + CMP_Y(==, "", "");
1.87 + CMP_Y(==, "a", "a");
1.88 + CMP_Y(==, "aa", "aa");
1.89 + CMP_N(==, "a", "");
1.90 + CMP_N(==, "", "a");
1.91 + CMP_N(==, "a", "b");
1.92 + CMP_N(==, "a", "aa");
1.93 + CMP_N(==, "aa", "a");
1.94 +
1.95 + CMP_N(!=, "", "");
1.96 + CMP_N(!=, "a", "a");
1.97 + CMP_N(!=, "aa", "aa");
1.98 + CMP_Y(!=, "a", "");
1.99 + CMP_Y(!=, "", "a");
1.100 + CMP_Y(!=, "a", "b");
1.101 + CMP_Y(!=, "a", "aa");
1.102 + CMP_Y(!=, "aa", "a");
1.103 +
1.104 + CMP_Y(<, "a", "b");
1.105 + CMP_Y(<, "a", "aa");
1.106 + CMP_Y(<, "aa", "b");
1.107 + CMP_Y(<, "aa", "bb");
1.108 + CMP_N(<, "a", "a");
1.109 + CMP_N(<, "b", "a");
1.110 + CMP_N(<, "aa", "a");
1.111 + CMP_N(<, "b", "aa");
1.112 + CMP_N(<, "bb", "aa");
1.113 +
1.114 + CMP_Y(<=, "a", "a");
1.115 + CMP_Y(<=, "a", "b");
1.116 + CMP_Y(<=, "a", "aa");
1.117 + CMP_Y(<=, "aa", "b");
1.118 + CMP_Y(<=, "aa", "bb");
1.119 + CMP_N(<=, "b", "a");
1.120 + CMP_N(<=, "aa", "a");
1.121 + CMP_N(<=, "b", "aa");
1.122 + CMP_N(<=, "bb", "aa");
1.123 +
1.124 + CMP_N(>=, "a", "b");
1.125 + CMP_N(>=, "a", "aa");
1.126 + CMP_N(>=, "aa", "b");
1.127 + CMP_N(>=, "aa", "bb");
1.128 + CMP_Y(>=, "a", "a");
1.129 + CMP_Y(>=, "b", "a");
1.130 + CMP_Y(>=, "aa", "a");
1.131 + CMP_Y(>=, "b", "aa");
1.132 + CMP_Y(>=, "bb", "aa");
1.133 +
1.134 + CMP_N(>, "a", "a");
1.135 + CMP_N(>, "a", "b");
1.136 + CMP_N(>, "a", "aa");
1.137 + CMP_N(>, "aa", "b");
1.138 + CMP_N(>, "aa", "bb");
1.139 + CMP_Y(>, "b", "a");
1.140 + CMP_Y(>, "aa", "a");
1.141 + CMP_Y(>, "b", "aa");
1.142 + CMP_Y(>, "bb", "aa");
1.143 +
1.144 +#undef CMP_Y
1.145 +#undef CMP_N
1.146 +}
1.147 +
1.148 +int main(int argc, char** argv) {
1.149 + CheckComparisonOperators();
1.150 + CheckSTLComparator();
1.151 +
1.152 + printf("OK\n");
1.153 + return 0;
1.154 +}