Home | History | Annotate | Download | only in dist
      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 
     27 using pcrecpp::StringPiece;
     28 
     29 static void CheckSTLComparator() {
     30   string s1("foo");
     31   string s2("bar");
     32   string s3("baz");
     33 
     34   StringPiece p1(s1);
     35   StringPiece p2(s2);
     36   StringPiece p3(s3);
     37 
     38   typedef std::map<StringPiece, int> TestMap;
     39   TestMap map;
     40 
     41   map.insert(std::make_pair(p1, 0));
     42   map.insert(std::make_pair(p2, 1));
     43   map.insert(std::make_pair(p3, 2));
     44 
     45   CHECK(map.size() == 3);
     46 
     47   TestMap::const_iterator iter = map.begin();
     48   CHECK(iter->second == 1);
     49   ++iter;
     50   CHECK(iter->second == 2);
     51   ++iter;
     52   CHECK(iter->second == 0);
     53   ++iter;
     54   CHECK(iter == map.end());
     55 
     56   TestMap::iterator new_iter = map.find("zot");
     57   CHECK(new_iter == map.end());
     58 
     59   new_iter = map.find("bar");
     60   CHECK(new_iter != map.end());
     61 
     62   map.erase(new_iter);
     63   CHECK(map.size() == 2);
     64 
     65   iter = map.begin();
     66   CHECK(iter->second == 2);
     67   ++iter;
     68   CHECK(iter->second == 0);
     69   ++iter;
     70   CHECK(iter == map.end());
     71 }
     72 
     73 static void CheckComparisonOperators() {
     74 #define CMP_Y(op, x, y)                                         \
     75   CHECK( (StringPiece((x)) op StringPiece((y))));               \
     76   CHECK( (StringPiece((x)).compare(StringPiece((y))) op 0))
     77 
     78 #define CMP_N(op, x, y)                                         \
     79   CHECK(!(StringPiece((x)) op StringPiece((y))));               \
     80   CHECK(!(StringPiece((x)).compare(StringPiece((y))) op 0))
     81 
     82   CMP_Y(==, "",   "");
     83   CMP_Y(==, "a",  "a");
     84   CMP_Y(==, "aa", "aa");
     85   CMP_N(==, "a",  "");
     86   CMP_N(==, "",   "a");
     87   CMP_N(==, "a",  "b");
     88   CMP_N(==, "a",  "aa");
     89   CMP_N(==, "aa", "a");
     90 
     91   CMP_N(!=, "",   "");
     92   CMP_N(!=, "a",  "a");
     93   CMP_N(!=, "aa", "aa");
     94   CMP_Y(!=, "a",  "");
     95   CMP_Y(!=, "",   "a");
     96   CMP_Y(!=, "a",  "b");
     97   CMP_Y(!=, "a",  "aa");
     98   CMP_Y(!=, "aa", "a");
     99 
    100   CMP_Y(<, "a",  "b");
    101   CMP_Y(<, "a",  "aa");
    102   CMP_Y(<, "aa", "b");
    103   CMP_Y(<, "aa", "bb");
    104   CMP_N(<, "a",  "a");
    105   CMP_N(<, "b",  "a");
    106   CMP_N(<, "aa", "a");
    107   CMP_N(<, "b",  "aa");
    108   CMP_N(<, "bb", "aa");
    109 
    110   CMP_Y(<=, "a",  "a");
    111   CMP_Y(<=, "a",  "b");
    112   CMP_Y(<=, "a",  "aa");
    113   CMP_Y(<=, "aa", "b");
    114   CMP_Y(<=, "aa", "bb");
    115   CMP_N(<=, "b",  "a");
    116   CMP_N(<=, "aa", "a");
    117   CMP_N(<=, "b",  "aa");
    118   CMP_N(<=, "bb", "aa");
    119 
    120   CMP_N(>=, "a",  "b");
    121   CMP_N(>=, "a",  "aa");
    122   CMP_N(>=, "aa", "b");
    123   CMP_N(>=, "aa", "bb");
    124   CMP_Y(>=, "a",  "a");
    125   CMP_Y(>=, "b",  "a");
    126   CMP_Y(>=, "aa", "a");
    127   CMP_Y(>=, "b",  "aa");
    128   CMP_Y(>=, "bb", "aa");
    129 
    130   CMP_N(>, "a",  "a");
    131   CMP_N(>, "a",  "b");
    132   CMP_N(>, "a",  "aa");
    133   CMP_N(>, "aa", "b");
    134   CMP_N(>, "aa", "bb");
    135   CMP_Y(>, "b",  "a");
    136   CMP_Y(>, "aa", "a");
    137   CMP_Y(>, "b",  "aa");
    138   CMP_Y(>, "bb", "aa");
    139 
    140 #undef CMP_Y
    141 #undef CMP_N
    142 }
    143 
    144 int main(int argc, char** argv) {
    145   (void)argc;
    146   (void)argv;
    147   CheckComparisonOperators();
    148   CheckSTLComparator();
    149 
    150   printf("OK\n");
    151   return 0;
    152 }
    153