memutil_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 // Unit test for memutil.cc
00016 
00017 #include "absl/strings/internal/memutil.h"
00018 
00019 #include <cstdlib>
00020 
00021 #include "gtest/gtest.h"
00022 #include "absl/strings/ascii.h"
00023 
00024 namespace {
00025 
00026 static char* memcasechr(const char* s, int c, size_t slen) {
00027   c = absl::ascii_tolower(c);
00028   for (; slen; ++s, --slen) {
00029     if (absl::ascii_tolower(*s) == c) return const_cast<char*>(s);
00030   }
00031   return nullptr;
00032 }
00033 
00034 static const char* memcasematch(const char* phaystack, size_t haylen,
00035                                 const char* pneedle, size_t neelen) {
00036   if (0 == neelen) {
00037     return phaystack;  // even if haylen is 0
00038   }
00039   if (haylen < neelen) return nullptr;
00040 
00041   const char* match;
00042   const char* hayend = phaystack + haylen - neelen + 1;
00043   while ((match = static_cast<char*>(
00044               memcasechr(phaystack, pneedle[0], hayend - phaystack)))) {
00045     if (absl::strings_internal::memcasecmp(match, pneedle, neelen) == 0)
00046       return match;
00047     else
00048       phaystack = match + 1;
00049   }
00050   return nullptr;
00051 }
00052 
00053 TEST(MemUtilTest, AllTests) {
00054   // check memutil functions
00055   char a[1000];
00056   absl::strings_internal::memcat(a, 0, "hello", sizeof("hello") - 1);
00057   absl::strings_internal::memcat(a, 5, " there", sizeof(" there") - 1);
00058 
00059   EXPECT_EQ(absl::strings_internal::memcasecmp(a, "heLLO there",
00060                                                sizeof("hello there") - 1),
00061             0);
00062   EXPECT_EQ(absl::strings_internal::memcasecmp(a, "heLLO therf",
00063                                                sizeof("hello there") - 1),
00064             -1);
00065   EXPECT_EQ(absl::strings_internal::memcasecmp(a, "heLLO therf",
00066                                                sizeof("hello there") - 2),
00067             0);
00068   EXPECT_EQ(absl::strings_internal::memcasecmp(a, "whatever", 0), 0);
00069 
00070   char* p = absl::strings_internal::memdup("hello", 5);
00071   free(p);
00072 
00073   p = absl::strings_internal::memrchr("hello there", 'e',
00074                                       sizeof("hello there") - 1);
00075   EXPECT_TRUE(p && p[-1] == 'r');
00076   p = absl::strings_internal::memrchr("hello there", 'e',
00077                                       sizeof("hello there") - 2);
00078   EXPECT_TRUE(p && p[-1] == 'h');
00079   p = absl::strings_internal::memrchr("hello there", 'u',
00080                                       sizeof("hello there") - 1);
00081   EXPECT_TRUE(p == nullptr);
00082 
00083   int len = absl::strings_internal::memspn("hello there",
00084                                            sizeof("hello there") - 1, "hole");
00085   EXPECT_EQ(len, sizeof("hello") - 1);
00086   len = absl::strings_internal::memspn("hello there", sizeof("hello there") - 1,
00087                                        "u");
00088   EXPECT_EQ(len, 0);
00089   len = absl::strings_internal::memspn("hello there", sizeof("hello there") - 1,
00090                                        "");
00091   EXPECT_EQ(len, 0);
00092   len = absl::strings_internal::memspn("hello there", sizeof("hello there") - 1,
00093                                        "trole h");
00094   EXPECT_EQ(len, sizeof("hello there") - 1);
00095   len = absl::strings_internal::memspn("hello there!",
00096                                        sizeof("hello there!") - 1, "trole h");
00097   EXPECT_EQ(len, sizeof("hello there") - 1);
00098   len = absl::strings_internal::memspn("hello there!",
00099                                        sizeof("hello there!") - 2, "trole h!");
00100   EXPECT_EQ(len, sizeof("hello there!") - 2);
00101 
00102   len = absl::strings_internal::memcspn("hello there",
00103                                         sizeof("hello there") - 1, "leho");
00104   EXPECT_EQ(len, 0);
00105   len = absl::strings_internal::memcspn("hello there",
00106                                         sizeof("hello there") - 1, "u");
00107   EXPECT_EQ(len, sizeof("hello there") - 1);
00108   len = absl::strings_internal::memcspn("hello there",
00109                                         sizeof("hello there") - 1, "");
00110   EXPECT_EQ(len, sizeof("hello there") - 1);
00111   len = absl::strings_internal::memcspn("hello there",
00112                                         sizeof("hello there") - 1, " ");
00113   EXPECT_EQ(len, 5);
00114 
00115   p = absl::strings_internal::mempbrk("hello there", sizeof("hello there") - 1,
00116                                       "leho");
00117   EXPECT_TRUE(p && p[1] == 'e' && p[2] == 'l');
00118   p = absl::strings_internal::mempbrk("hello there", sizeof("hello there") - 1,
00119                                       "nu");
00120   EXPECT_TRUE(p == nullptr);
00121   p = absl::strings_internal::mempbrk("hello there!",
00122                                       sizeof("hello there!") - 2, "!");
00123   EXPECT_TRUE(p == nullptr);
00124   p = absl::strings_internal::mempbrk("hello there", sizeof("hello there") - 1,
00125                                       " t ");
00126   EXPECT_TRUE(p && p[-1] == 'o' && p[1] == 't');
00127 
00128   {
00129     const char kHaystack[] = "0123456789";
00130     EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 0, "", 0), kHaystack);
00131     EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 10, "012", 3),
00132               kHaystack);
00133     EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 10, "0xx", 1),
00134               kHaystack);
00135     EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 10, "789", 3),
00136               kHaystack + 7);
00137     EXPECT_EQ(absl::strings_internal::memmem(kHaystack, 10, "9xx", 1),
00138               kHaystack + 9);
00139     EXPECT_TRUE(absl::strings_internal::memmem(kHaystack, 10, "9xx", 3) ==
00140                 nullptr);
00141     EXPECT_TRUE(absl::strings_internal::memmem(kHaystack, 10, "xxx", 1) ==
00142                 nullptr);
00143   }
00144   {
00145     const char kHaystack[] = "aBcDeFgHiJ";
00146     EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 0, "", 0),
00147               kHaystack);
00148     EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 10, "Abc", 3),
00149               kHaystack);
00150     EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 10, "Axx", 1),
00151               kHaystack);
00152     EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 10, "hIj", 3),
00153               kHaystack + 7);
00154     EXPECT_EQ(absl::strings_internal::memcasemem(kHaystack, 10, "jxx", 1),
00155               kHaystack + 9);
00156     EXPECT_TRUE(absl::strings_internal::memcasemem(kHaystack, 10, "jxx", 3) ==
00157                 nullptr);
00158     EXPECT_TRUE(absl::strings_internal::memcasemem(kHaystack, 10, "xxx", 1) ==
00159                 nullptr);
00160   }
00161   {
00162     const char kHaystack[] = "0123456789";
00163     EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 0, "", 0), kHaystack);
00164     EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 10, "012", 3),
00165               kHaystack);
00166     EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 10, "0xx", 1),
00167               kHaystack);
00168     EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 10, "789", 3),
00169               kHaystack + 7);
00170     EXPECT_EQ(absl::strings_internal::memmatch(kHaystack, 10, "9xx", 1),
00171               kHaystack + 9);
00172     EXPECT_TRUE(absl::strings_internal::memmatch(kHaystack, 10, "9xx", 3) ==
00173                 nullptr);
00174     EXPECT_TRUE(absl::strings_internal::memmatch(kHaystack, 10, "xxx", 1) ==
00175                 nullptr);
00176   }
00177 }
00178 
00179 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15