| OLD | NEW |
| (Empty) |
| 1 // Copyright (C) 2011 Google Inc. | |
| 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 // http://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 | |
| 15 // Author: Philippe Liard | |
| 16 | |
| 17 #include <gtest/gtest.h> | |
| 18 | |
| 19 #include "stringutil.h" | |
| 20 | |
| 21 namespace i18n { | |
| 22 namespace phonenumbers { | |
| 23 | |
| 24 // Test operator+(const string&, int). | |
| 25 TEST(StringUtilTest, OperatorPlus) { | |
| 26 EXPECT_EQ("hello10", string("hello") + 10); | |
| 27 } | |
| 28 | |
| 29 // Test SimpleItoa implementation. | |
| 30 TEST(StringUtilTest, SimpleItoa) { | |
| 31 EXPECT_EQ("10", SimpleItoa(10)); | |
| 32 } | |
| 33 | |
| 34 // Test TryStripPrefixString. | |
| 35 TEST(StringUtilTest, TryStripPrefixString) { | |
| 36 string s; | |
| 37 | |
| 38 EXPECT_TRUE(TryStripPrefixString("hello world", "hello", &s)); | |
| 39 EXPECT_EQ(" world", s); | |
| 40 s.clear(); | |
| 41 | |
| 42 EXPECT_FALSE(TryStripPrefixString("hello world", "helloa", &s)); | |
| 43 s.clear(); | |
| 44 | |
| 45 EXPECT_TRUE(TryStripPrefixString("hello world", "", &s)); | |
| 46 EXPECT_EQ("hello world", s); | |
| 47 s.clear(); | |
| 48 | |
| 49 EXPECT_FALSE(TryStripPrefixString("", "hello", &s)); | |
| 50 s.clear(); | |
| 51 } | |
| 52 | |
| 53 // Test HasSuffixString. | |
| 54 TEST(StringUtilTest, HasSuffixString) { | |
| 55 EXPECT_TRUE(HasSuffixString("hello world", "hello world")); | |
| 56 EXPECT_TRUE(HasSuffixString("hello world", "world")); | |
| 57 EXPECT_FALSE(HasSuffixString("hello world", "world!")); | |
| 58 EXPECT_TRUE(HasSuffixString("hello world", "")); | |
| 59 EXPECT_FALSE(HasSuffixString("", "hello")); | |
| 60 } | |
| 61 | |
| 62 // Test safe_strto32. | |
| 63 TEST(StringUtilTest, safe_strto32) { | |
| 64 int32 n; | |
| 65 | |
| 66 safe_strto32("0", &n); | |
| 67 EXPECT_EQ(0, n); | |
| 68 | |
| 69 safe_strto32("16", &n); | |
| 70 EXPECT_EQ(16, n); | |
| 71 | |
| 72 safe_strto32("2147483647", &n); | |
| 73 EXPECT_EQ(2147483647, n); | |
| 74 | |
| 75 safe_strto32("-2147483648", &n); | |
| 76 EXPECT_EQ(-2147483648, n); | |
| 77 } | |
| 78 | |
| 79 // Test safe_strtou64. | |
| 80 TEST(StringUtilTest, safe_strtou64) { | |
| 81 uint64 n; | |
| 82 | |
| 83 safe_strtou64("0", &n); | |
| 84 EXPECT_EQ(0, n); | |
| 85 | |
| 86 safe_strtou64("16", &n); | |
| 87 EXPECT_EQ(16, n); | |
| 88 | |
| 89 safe_strtou64("18446744073709551615UL", &n); | |
| 90 EXPECT_EQ(18446744073709551615ULL, n); | |
| 91 } | |
| 92 | |
| 93 // Test strrmm. | |
| 94 TEST(StringUtilTest, strrmm) { | |
| 95 string input("hello"); | |
| 96 | |
| 97 strrmm(&input, ""); | |
| 98 EXPECT_EQ(input, input); | |
| 99 | |
| 100 string empty; | |
| 101 strrmm(&empty, ""); | |
| 102 EXPECT_EQ("", empty); | |
| 103 | |
| 104 strrmm(&empty, "aa"); | |
| 105 EXPECT_EQ("", empty); | |
| 106 | |
| 107 strrmm(&input, "h"); | |
| 108 EXPECT_EQ("ello", input); | |
| 109 | |
| 110 strrmm(&input, "el"); | |
| 111 EXPECT_EQ("o", input); | |
| 112 } | |
| 113 | |
| 114 // Test the StringHolder class. | |
| 115 TEST(StringUtilTest, StringHolder) { | |
| 116 // Test with C string. | |
| 117 static const char cstring[] = "aaa"; | |
| 118 StringHolder sh1(cstring); | |
| 119 EXPECT_EQ(cstring, sh1.GetCString()); | |
| 120 EXPECT_EQ(NULL, sh1.GetString()); | |
| 121 | |
| 122 // Test with std::string. | |
| 123 string s = "bbb"; | |
| 124 StringHolder sh2(s); | |
| 125 EXPECT_EQ(NULL, sh2.GetCString()); | |
| 126 EXPECT_EQ(&s, sh2.GetString()); | |
| 127 | |
| 128 // Test GetLength(). | |
| 129 string s2 = "hello"; | |
| 130 StringHolder sh3(s2); | |
| 131 EXPECT_EQ(5, sh3.Length()); | |
| 132 | |
| 133 // Test with uint64. | |
| 134 StringHolder sh4(42); | |
| 135 EXPECT_TRUE(sh4.GetCString() == NULL); | |
| 136 EXPECT_EQ(2, sh4.Length()); | |
| 137 EXPECT_EQ("42", *sh4.GetString()); | |
| 138 } | |
| 139 | |
| 140 // Test the operator+=(string& lhs, const StringHolder& rhs) implementation. | |
| 141 TEST(StringUtilTest, OperatorPlusEquals) { | |
| 142 // Test with a const char* string to append. | |
| 143 string s = "h"; | |
| 144 static const char append1[] = "ello"; | |
| 145 s += StringHolder(append1); // force StringHolder usage | |
| 146 | |
| 147 EXPECT_EQ("hello", s); | |
| 148 | |
| 149 // Test with a std::string to append. | |
| 150 s = "h"; | |
| 151 string append2 = "ello"; | |
| 152 s += StringHolder(append2); // force StringHolder usage | |
| 153 | |
| 154 EXPECT_EQ("hello", s); | |
| 155 } | |
| 156 | |
| 157 // Test the StrCat implementations | |
| 158 TEST(StringUtilTest, StrCat) { | |
| 159 string s; | |
| 160 | |
| 161 // Test with 2 arguments. | |
| 162 s = StrCat("a", "b"); | |
| 163 EXPECT_EQ("ab", s); | |
| 164 | |
| 165 // Test with 3 arguments. | |
| 166 s = StrCat("a", "b", "c"); | |
| 167 EXPECT_EQ("abc", s); | |
| 168 | |
| 169 // Test with 4 arguments. | |
| 170 s = StrCat("a", "b", "c", "d"); | |
| 171 EXPECT_EQ("abcd", s); | |
| 172 | |
| 173 // Test with 5 arguments. | |
| 174 s = StrCat("a", "b", "c", "d", "e"); | |
| 175 EXPECT_EQ("abcde", s); | |
| 176 | |
| 177 // Test with 6 arguments. | |
| 178 s = StrCat("a", "b", "c", "d", "e", "f"); | |
| 179 EXPECT_EQ("abcdef", s); | |
| 180 | |
| 181 // Test with 7 arguments. | |
| 182 s = StrCat("a", "b", "c", "d", "e", "f", "g"); | |
| 183 EXPECT_EQ("abcdefg", s); | |
| 184 | |
| 185 // Test with 11 arguments. | |
| 186 s = StrCat("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"); | |
| 187 EXPECT_EQ("abcdefghijk", s); | |
| 188 } | |
| 189 | |
| 190 // Test the StrAppend implementations. | |
| 191 TEST(StringUtilTest, StrAppend) { | |
| 192 string s; | |
| 193 | |
| 194 // Test with 1 argument. | |
| 195 StrAppend(&s, "a"); | |
| 196 EXPECT_EQ("a", s); | |
| 197 | |
| 198 // Test with 2 arguments. | |
| 199 StrAppend(&s, "b", "c"); | |
| 200 EXPECT_EQ("abc", s); | |
| 201 | |
| 202 // Test with int argument. | |
| 203 StrAppend(&s, 42); | |
| 204 EXPECT_EQ("abc42", s); | |
| 205 } | |
| 206 | |
| 207 } // namespace phonenumbers | |
| 208 } // namespace i18n | |
| OLD | NEW |