pcre_stringpiece_unittest.cc

Go to the documentation of this file.
00001 // Copyright 2003 and onwards Google Inc.
00002 // Author: Sanjay Ghemawat
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include "config.h"
00006 #endif
00007 
00008 #include <stdio.h>
00009 #include <map>
00010 #include <algorithm>    // for make_pair
00011 
00012 #include "pcrecpp.h"
00013 #include "pcre_stringpiece.h"
00014 
00015 // CHECK dies with a fatal error if condition is not true.  It is *not*
00016 // controlled by NDEBUG, so the check will be executed regardless of
00017 // compilation mode.  Therefore, it is safe to do things like:
00018 //    CHECK(fp->Write(x) == 4)
00019 #define CHECK(condition) do {                           \
00020   if (!(condition)) {                                   \
00021     fprintf(stderr, "%s:%d: Check failed: %s\n",        \
00022             __FILE__, __LINE__, #condition);            \
00023     exit(1);                                            \
00024   }                                                     \
00025 } while (0)
00026 
00027 using std::map;
00028 using std::make_pair;
00029 using pcrecpp::StringPiece;
00030 
00031 static void CheckSTLComparator() {
00032   string s1("foo");
00033   string s2("bar");
00034   string s3("baz");
00035 
00036   StringPiece p1(s1);
00037   StringPiece p2(s2);
00038   StringPiece p3(s3);
00039 
00040   typedef map<StringPiece, int> TestMap;
00041   TestMap map;
00042 
00043   map.insert(make_pair(p1, 0));
00044   map.insert(make_pair(p2, 1));
00045   map.insert(make_pair(p3, 2));
00046   CHECK(map.size() == 3);
00047 
00048   TestMap::const_iterator iter = map.begin();
00049   CHECK(iter->second == 1);
00050   ++iter;
00051   CHECK(iter->second == 2);
00052   ++iter;
00053   CHECK(iter->second == 0);
00054   ++iter;
00055   CHECK(iter == map.end());
00056 
00057   TestMap::iterator new_iter = map.find("zot");
00058   CHECK(new_iter == map.end());
00059 
00060   new_iter = map.find("bar");
00061   CHECK(new_iter != map.end());
00062 
00063   map.erase(new_iter);
00064   CHECK(map.size() == 2);
00065 
00066   iter = map.begin();
00067   CHECK(iter->second == 2);
00068   ++iter;
00069   CHECK(iter->second == 0);
00070   ++iter;
00071   CHECK(iter == map.end());
00072 }
00073 
00074 static void CheckComparisonOperators() {
00075 #define CMP_Y(op, x, y)                                         \
00076   CHECK( (StringPiece((x)) op StringPiece((y))));               \
00077   CHECK( (StringPiece((x)).compare(StringPiece((y))) op 0))
00078 
00079 #define CMP_N(op, x, y)                                         \
00080   CHECK(!(StringPiece((x)) op StringPiece((y))));               \
00081   CHECK(!(StringPiece((x)).compare(StringPiece((y))) op 0))
00082 
00083   CMP_Y(==, "",   "");
00084   CMP_Y(==, "a",  "a");
00085   CMP_Y(==, "aa", "aa");
00086   CMP_N(==, "a",  "");
00087   CMP_N(==, "",   "a");
00088   CMP_N(==, "a",  "b");
00089   CMP_N(==, "a",  "aa");
00090   CMP_N(==, "aa", "a");
00091 
00092   CMP_N(!=, "",   "");
00093   CMP_N(!=, "a",  "a");
00094   CMP_N(!=, "aa", "aa");
00095   CMP_Y(!=, "a",  "");
00096   CMP_Y(!=, "",   "a");
00097   CMP_Y(!=, "a",  "b");
00098   CMP_Y(!=, "a",  "aa");
00099   CMP_Y(!=, "aa", "a");
00100 
00101   CMP_Y(<, "a",  "b");
00102   CMP_Y(<, "a",  "aa");
00103   CMP_Y(<, "aa", "b");
00104   CMP_Y(<, "aa", "bb");
00105   CMP_N(<, "a",  "a");
00106   CMP_N(<, "b",  "a");
00107   CMP_N(<, "aa", "a");
00108   CMP_N(<, "b",  "aa");
00109   CMP_N(<, "bb", "aa");
00110 
00111   CMP_Y(<=, "a",  "a");
00112   CMP_Y(<=, "a",  "b");
00113   CMP_Y(<=, "a",  "aa");
00114   CMP_Y(<=, "aa", "b");
00115   CMP_Y(<=, "aa", "bb");
00116   CMP_N(<=, "b",  "a");
00117   CMP_N(<=, "aa", "a");
00118   CMP_N(<=, "b",  "aa");
00119   CMP_N(<=, "bb", "aa");
00120 
00121   CMP_N(>=, "a",  "b");
00122   CMP_N(>=, "a",  "aa");
00123   CMP_N(>=, "aa", "b");
00124   CMP_N(>=, "aa", "bb");
00125   CMP_Y(>=, "a",  "a");
00126   CMP_Y(>=, "b",  "a");
00127   CMP_Y(>=, "aa", "a");
00128   CMP_Y(>=, "b",  "aa");
00129   CMP_Y(>=, "bb", "aa");
00130 
00131   CMP_N(>, "a",  "a");
00132   CMP_N(>, "a",  "b");
00133   CMP_N(>, "a",  "aa");
00134   CMP_N(>, "aa", "b");
00135   CMP_N(>, "aa", "bb");
00136   CMP_Y(>, "b",  "a");
00137   CMP_Y(>, "aa", "a");
00138   CMP_Y(>, "b",  "aa");
00139   CMP_Y(>, "bb", "aa");
00140 
00141 #undef CMP_Y
00142 #undef CMP_N
00143 }
00144 
00145 int main(int argc, char** argv) {
00146   CheckComparisonOperators();
00147   CheckSTLComparator();
00148 
00149   printf("OK\n");
00150   return 0;
00151 }

Generated on Tue Jul 5 14:11:57 2011 for ROOT_528-00b_version by  doxygen 1.5.1