memutil_test.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Unit test for memutil.cc
  15. #include "absl/strings/internal/memutil.h"
  16. #include <cstdlib>
  17. #include "gtest/gtest.h"
  18. #include "absl/strings/ascii.h"
  19. namespace {
  20. static char* memcasechr(const char* s, int c, size_t slen) {
  21. c = absl::ascii_tolower(c);
  22. for (; slen; ++s, --slen) {
  23. if (absl::ascii_tolower(*s) == c) return const_cast<char*>(s);
  24. }
  25. return nullptr;
  26. }
  27. static const char* memcasematch(const char* phaystack, size_t haylen,
  28. const char* pneedle, size_t neelen) {
  29. if (0 == neelen) {
  30. return phaystack; // even if haylen is 0
  31. }
  32. if (haylen < neelen) return nullptr;
  33. const char* match;
  34. const char* hayend = phaystack + haylen - neelen + 1;
  35. while ((match = static_cast<char*>(
  36. memcasechr(phaystack, pneedle[0], hayend - phaystack)))) {
  37. if (absl::strings_internal::memcasecmp(match, pneedle, neelen) == 0)
  38. return match;
  39. else
  40. phaystack = match + 1;
  41. }
  42. return nullptr;
  43. }
  44. TEST(MemUtilTest, AllTests) {
  45. // check memutil functions
  46. char a[1000];
  47. absl::strings_internal::memcat(a, 0, "hello", sizeof("hello") - 1);
  48. absl::strings_internal::memcat(a, 5, " there", sizeof(" there") - 1);
  49. EXPECT_EQ(absl::strings_internal::memcasecmp(a, "heLLO there",
  50. sizeof("hello there") - 1),
  51. 0);
  52. EXPECT_EQ(absl::strings_internal::memcasecmp(a, "heLLO therf",
  53. sizeof("hello there") - 1),
  54. -1);
  55. EXPECT_EQ(absl::strings_internal::memcasecmp(a, "heLLO therf",
  56. sizeof("hello there") - 2),
  57. 0);
  58. EXPECT_EQ(absl::strings_internal::memcasecmp(a, "whatever", 0), 0);
  59. char* p = absl::strings_internal::memdup("hello", 5);
  60. free(p);
  61. p = absl::strings_internal::memrchr("hello there", 'e',
  62. sizeof("hello there") - 1);
  63. EXPECT_TRUE(p && p[-1] == 'r');
  64. p = absl::strings_internal::memrchr("hello there", 'e',
  65. sizeof("hello there") - 2);
  66. EXPECT_TRUE(p && p[-1] == 'h');
  67. p = absl::strings_internal::memrchr("hello there", 'u',
  68. sizeof("hello there") - 1);
  69. EXPECT_TRUE(p == nullptr);
  70. int len = absl::strings_internal::memspn("hello there",
  71. sizeof("hello there") - 1, "hole");
  72. EXPECT_EQ(len, sizeof("hello") - 1);
  73. len = absl::strings_internal::memspn("hello there", sizeof("hello there") - 1,
  74. "u");
  75. EXPECT_EQ(len, 0);
  76. len = absl::strings_internal::memspn("hello there", sizeof("hello there") - 1,
  77. "");
  78. EXPECT_EQ(len, 0);
  79. len = absl::strings_internal::memspn("hello there", sizeof("hello there") - 1,
  80. "trole h");
  81. EXPECT_EQ(len, sizeof("hello there") - 1);
  82. len = absl::strings_internal::memspn("hello there!",
  83. sizeof("hello there!") - 1, "trole h");
  84. EXPECT_EQ(len, sizeof("hello there") - 1);
  85. len = absl::strings_internal::memspn("hello there!",
  86. sizeof("hello there!") - 2, "trole h!");
  87. EXPECT_EQ(len, sizeof("hello there!") - 2);
  88. len = absl::strings_internal::memcspn("hello there",
  89. sizeof("hello there") - 1, "leho");
  90. EXPECT_EQ(len, 0);
  91. len = absl::strings_internal::memcspn("hello there",
  92. sizeof("hello there") - 1, "u");
  93. EXPECT_EQ(len, sizeof("hello there") - 1);
  94. len = absl::strings_internal::memcspn("hello there",
  95. sizeof("hello there") - 1, "");
  96. EXPECT_EQ(len, sizeof("hello there") - 1);
  97. len = absl::strings_internal::memcspn("hello there",
  98. sizeof("hello there") - 1, " ");
  99. EXPECT_EQ(len, 5);
  100. p = absl::strings_internal::mempbrk("hello there", sizeof("hello there") - 1,
  101. "leho");
  102. EXPECT_TRUE(p && p[1] == 'e' && p[2] == 'l');
  103. p = absl::strings_internal::mempbrk("hello there", sizeof("hello there") - 1,
  104. "nu");
  105. EXPECT_TRUE(p == nullptr);
  106. p = absl::strings_internal::mempbrk("hello there!",
  107. sizeof("hello there!") - 2, "!");
  108. EXPECT_TRUE(p == nullptr);
  109. p = absl::strings_internal::mempbrk("hello there", sizeof("hello there") - 1,
  110. " t ");
  111. EXPECT_TRUE(p && p[-1] == 'o' && p[1] == 't');
  112. {
  113. const char kHaystack[] = "0123456789";
  114. EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 0, "", 0), kHaystack);
  115. EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 10, "012", 3),
  116. kHaystack);
  117. EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 10, "0xx", 1),
  118. kHaystack);
  119. EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 10, "789", 3),
  120. kHaystack + 7);
  121. EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 10, "9xx", 1),
  122. kHaystack + 9);
  123. EXPECT_TRUE(absl::strings_internal::memmem(kHaystack, 10, "9xx", 3) ==
  124. nullptr);
  125. EXPECT_TRUE(absl::strings_internal::memmem(kHaystack, 10, "xxx", 1) ==
  126. nullptr);
  127. }
  128. {
  129. const char kHaystack[] = "aBcDeFgHiJ";
  130. EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 0, "", 0),
  131. kHaystack);
  132. EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 10, "Abc", 3),
  133. kHaystack);
  134. EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 10, "Axx", 1),
  135. kHaystack);
  136. EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 10, "hIj", 3),
  137. kHaystack + 7);
  138. EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 10, "jxx", 1),
  139. kHaystack + 9);
  140. EXPECT_TRUE(absl::strings_internal::memcasemem(kHaystack, 10, "jxx", 3) ==
  141. nullptr);
  142. EXPECT_TRUE(absl::strings_internal::memcasemem(kHaystack, 10, "xxx", 1) ==
  143. nullptr);
  144. }
  145. {
  146. const char kHaystack[] = "0123456789";
  147. EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 0, "", 0), kHaystack);
  148. EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 10, "012", 3),
  149. kHaystack);
  150. EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 10, "0xx", 1),
  151. kHaystack);
  152. EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 10, "789", 3),
  153. kHaystack + 7);
  154. EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 10, "9xx", 1),
  155. kHaystack + 9);
  156. EXPECT_TRUE(absl::strings_internal::memmatch(kHaystack, 10, "9xx", 3) ==
  157. nullptr);
  158. EXPECT_TRUE(absl::strings_internal::memmatch(kHaystack, 10, "xxx", 1) ==
  159. nullptr);
  160. }
  161. }
  162. } // namespace