"Fossies" - the Fresh Open Source Software Archive

Member "pcre-8.43/pcre_stringpiece_unittest.cc" (21 Apr 2017, 3624 Bytes) of package /linux/misc/pcre-8.43.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pcre_stringpiece_unittest.cc" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 8.40_vs_8.41.

    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 std::string;
   28 using pcrecpp::StringPiece;
   29 
   30 static void CheckSTLComparator() {
   31   string s1("foo");
   32   string s2("bar");
   33   string s3("baz");
   34 
   35   StringPiece p1(s1);
   36   StringPiece p2(s2);
   37   StringPiece p3(s3);
   38 
   39   typedef std::map<StringPiece, int> TestMap;
   40   TestMap map;
   41 
   42   map.insert(std::make_pair(p1, 0));
   43   map.insert(std::make_pair(p2, 1));
   44   map.insert(std::make_pair(p3, 2));
   45 
   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 
   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))
   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 
  145 int main(int argc, char** argv) {
  146   (void)argc;
  147   (void)argv;
  148   CheckComparisonOperators();
  149   CheckSTLComparator();
  150 
  151   printf("OK\n");
  152   return 0;
  153 }