pcre_stringpiece_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2003 and onwards Google Inc.
  2. // Author: Sanjay Ghemawat
  3. #ifdef HAVE_CONFIG_H
  4. #include "config.h"
  5. #endif
  6. #include <stdio.h>
  7. #include <map>
  8. #include <algorithm> // for make_pair
  9. #include "pcrecpp.h"
  10. #include "pcre_stringpiece.h"
  11. // CHECK dies with a fatal error if condition is not true. It is *not*
  12. // controlled by NDEBUG, so the check will be executed regardless of
  13. // compilation mode. Therefore, it is safe to do things like:
  14. // CHECK(fp->Write(x) == 4)
  15. #define CHECK(condition) do { \
  16. if (!(condition)) { \
  17. fprintf(stderr, "%s:%d: Check failed: %s\n", \
  18. __FILE__, __LINE__, #condition); \
  19. exit(1); \
  20. } \
  21. } while (0)
  22. using std::string;
  23. using pcrecpp::StringPiece;
  24. static void CheckSTLComparator() {
  25. string s1("foo");
  26. string s2("bar");
  27. string s3("baz");
  28. StringPiece p1(s1);
  29. StringPiece p2(s2);
  30. StringPiece p3(s3);
  31. typedef std::map<StringPiece, int> TestMap;
  32. TestMap map;
  33. map.insert(std::make_pair(p1, 0));
  34. map.insert(std::make_pair(p2, 1));
  35. map.insert(std::make_pair(p3, 2));
  36. CHECK(map.size() == 3);
  37. TestMap::const_iterator iter = map.begin();
  38. CHECK(iter->second == 1);
  39. ++iter;
  40. CHECK(iter->second == 2);
  41. ++iter;
  42. CHECK(iter->second == 0);
  43. ++iter;
  44. CHECK(iter == map.end());
  45. TestMap::iterator new_iter = map.find("zot");
  46. CHECK(new_iter == map.end());
  47. new_iter = map.find("bar");
  48. CHECK(new_iter != map.end());
  49. map.erase(new_iter);
  50. CHECK(map.size() == 2);
  51. iter = map.begin();
  52. CHECK(iter->second == 2);
  53. ++iter;
  54. CHECK(iter->second == 0);
  55. ++iter;
  56. CHECK(iter == map.end());
  57. }
  58. static void CheckComparisonOperators() {
  59. #define CMP_Y(op, x, y) \
  60. CHECK( (StringPiece((x)) op StringPiece((y)))); \
  61. CHECK( (StringPiece((x)).compare(StringPiece((y))) op 0))
  62. #define CMP_N(op, x, y) \
  63. CHECK(!(StringPiece((x)) op StringPiece((y)))); \
  64. CHECK(!(StringPiece((x)).compare(StringPiece((y))) op 0))
  65. CMP_Y(==, "", "");
  66. CMP_Y(==, "a", "a");
  67. CMP_Y(==, "aa", "aa");
  68. CMP_N(==, "a", "");
  69. CMP_N(==, "", "a");
  70. CMP_N(==, "a", "b");
  71. CMP_N(==, "a", "aa");
  72. CMP_N(==, "aa", "a");
  73. CMP_N(!=, "", "");
  74. CMP_N(!=, "a", "a");
  75. CMP_N(!=, "aa", "aa");
  76. CMP_Y(!=, "a", "");
  77. CMP_Y(!=, "", "a");
  78. CMP_Y(!=, "a", "b");
  79. CMP_Y(!=, "a", "aa");
  80. CMP_Y(!=, "aa", "a");
  81. CMP_Y(<, "a", "b");
  82. CMP_Y(<, "a", "aa");
  83. CMP_Y(<, "aa", "b");
  84. CMP_Y(<, "aa", "bb");
  85. CMP_N(<, "a", "a");
  86. CMP_N(<, "b", "a");
  87. CMP_N(<, "aa", "a");
  88. CMP_N(<, "b", "aa");
  89. CMP_N(<, "bb", "aa");
  90. CMP_Y(<=, "a", "a");
  91. CMP_Y(<=, "a", "b");
  92. CMP_Y(<=, "a", "aa");
  93. CMP_Y(<=, "aa", "b");
  94. CMP_Y(<=, "aa", "bb");
  95. CMP_N(<=, "b", "a");
  96. CMP_N(<=, "aa", "a");
  97. CMP_N(<=, "b", "aa");
  98. CMP_N(<=, "bb", "aa");
  99. CMP_N(>=, "a", "b");
  100. CMP_N(>=, "a", "aa");
  101. CMP_N(>=, "aa", "b");
  102. CMP_N(>=, "aa", "bb");
  103. CMP_Y(>=, "a", "a");
  104. CMP_Y(>=, "b", "a");
  105. CMP_Y(>=, "aa", "a");
  106. CMP_Y(>=, "b", "aa");
  107. CMP_Y(>=, "bb", "aa");
  108. CMP_N(>, "a", "a");
  109. CMP_N(>, "a", "b");
  110. CMP_N(>, "a", "aa");
  111. CMP_N(>, "aa", "b");
  112. CMP_N(>, "aa", "bb");
  113. CMP_Y(>, "b", "a");
  114. CMP_Y(>, "aa", "a");
  115. CMP_Y(>, "b", "aa");
  116. CMP_Y(>, "bb", "aa");
  117. #undef CMP_Y
  118. #undef CMP_N
  119. }
  120. int main(int argc, char** argv) {
  121. (void)argc;
  122. (void)argv;
  123. CheckComparisonOperators();
  124. CheckSTLComparator();
  125. printf("OK\n");
  126. return 0;
  127. }