| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2012 Apple Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #include "wtf/text/CString.h" | |
| 27 | |
| 28 #include "testing/gtest/include/gtest/gtest.h" | |
| 29 #include <sstream> | |
| 30 | |
| 31 namespace WTF { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 CString printedString(const CString& string) { | |
| 36 std::ostringstream output; | |
| 37 output << string; | |
| 38 const std::string& result = output.str(); | |
| 39 return CString(result.data(), result.length()); | |
| 40 } | |
| 41 | |
| 42 } // anonymous namespace | |
| 43 | |
| 44 TEST(CStringTest, NullStringConstructor) { | |
| 45 CString string; | |
| 46 EXPECT_TRUE(string.isNull()); | |
| 47 EXPECT_EQ(static_cast<const char*>(0), string.data()); | |
| 48 EXPECT_EQ(static_cast<size_t>(0), string.length()); | |
| 49 | |
| 50 CString stringFromCharPointer(static_cast<const char*>(0)); | |
| 51 EXPECT_TRUE(stringFromCharPointer.isNull()); | |
| 52 EXPECT_EQ(static_cast<const char*>(0), stringFromCharPointer.data()); | |
| 53 EXPECT_EQ(static_cast<size_t>(0), stringFromCharPointer.length()); | |
| 54 | |
| 55 CString stringFromCharAndLength(static_cast<const char*>(0), 0); | |
| 56 EXPECT_TRUE(stringFromCharAndLength.isNull()); | |
| 57 EXPECT_EQ(static_cast<const char*>(0), stringFromCharAndLength.data()); | |
| 58 EXPECT_EQ(static_cast<size_t>(0), stringFromCharAndLength.length()); | |
| 59 } | |
| 60 | |
| 61 TEST(CStringTest, EmptyEmptyConstructor) { | |
| 62 const char* emptyString = ""; | |
| 63 CString string(emptyString); | |
| 64 EXPECT_FALSE(string.isNull()); | |
| 65 EXPECT_EQ(static_cast<size_t>(0), string.length()); | |
| 66 EXPECT_EQ(0, string.data()[0]); | |
| 67 | |
| 68 CString stringWithLength(emptyString, 0); | |
| 69 EXPECT_FALSE(stringWithLength.isNull()); | |
| 70 EXPECT_EQ(static_cast<size_t>(0), stringWithLength.length()); | |
| 71 EXPECT_EQ(0, stringWithLength.data()[0]); | |
| 72 } | |
| 73 | |
| 74 TEST(CStringTest, EmptyRegularConstructor) { | |
| 75 const char* referenceString = "WebKit"; | |
| 76 | |
| 77 CString string(referenceString); | |
| 78 EXPECT_FALSE(string.isNull()); | |
| 79 EXPECT_EQ(strlen(referenceString), string.length()); | |
| 80 EXPECT_STREQ(referenceString, string.data()); | |
| 81 | |
| 82 CString stringWithLength(referenceString, 6); | |
| 83 EXPECT_FALSE(stringWithLength.isNull()); | |
| 84 EXPECT_EQ(strlen(referenceString), stringWithLength.length()); | |
| 85 EXPECT_STREQ(referenceString, stringWithLength.data()); | |
| 86 } | |
| 87 | |
| 88 TEST(CStringTest, UninitializedConstructor) { | |
| 89 char* buffer; | |
| 90 CString emptyString = CString::createUninitialized(0, buffer); | |
| 91 EXPECT_FALSE(emptyString.isNull()); | |
| 92 EXPECT_EQ(buffer, emptyString.data()); | |
| 93 EXPECT_EQ(0, buffer[0]); | |
| 94 | |
| 95 const size_t length = 25; | |
| 96 CString uninitializedString = CString::createUninitialized(length, buffer); | |
| 97 EXPECT_FALSE(uninitializedString.isNull()); | |
| 98 EXPECT_EQ(buffer, uninitializedString.data()); | |
| 99 EXPECT_EQ(0, uninitializedString.data()[length]); | |
| 100 } | |
| 101 | |
| 102 TEST(CStringTest, ZeroTerminated) { | |
| 103 const char* referenceString = "WebKit"; | |
| 104 CString stringWithLength(referenceString, 3); | |
| 105 EXPECT_EQ(0, stringWithLength.data()[3]); | |
| 106 } | |
| 107 | |
| 108 TEST(CStringTest, Comparison) { | |
| 109 // Comparison with another CString. | |
| 110 CString a; | |
| 111 CString b; | |
| 112 EXPECT_TRUE(a == b); | |
| 113 EXPECT_FALSE(a != b); | |
| 114 a = "a"; | |
| 115 b = CString(); | |
| 116 EXPECT_FALSE(a == b); | |
| 117 EXPECT_TRUE(a != b); | |
| 118 a = "a"; | |
| 119 b = "b"; | |
| 120 EXPECT_FALSE(a == b); | |
| 121 EXPECT_TRUE(a != b); | |
| 122 a = "a"; | |
| 123 b = "a"; | |
| 124 EXPECT_TRUE(a == b); | |
| 125 EXPECT_FALSE(a != b); | |
| 126 a = "a"; | |
| 127 b = "aa"; | |
| 128 EXPECT_FALSE(a == b); | |
| 129 EXPECT_TRUE(a != b); | |
| 130 a = ""; | |
| 131 b = ""; | |
| 132 EXPECT_TRUE(a == b); | |
| 133 EXPECT_FALSE(a != b); | |
| 134 a = ""; | |
| 135 b = CString(); | |
| 136 EXPECT_FALSE(a == b); | |
| 137 EXPECT_TRUE(a != b); | |
| 138 a = "a"; | |
| 139 b = ""; | |
| 140 EXPECT_FALSE(a == b); | |
| 141 EXPECT_TRUE(a != b); | |
| 142 | |
| 143 // Comparison with a const char*. | |
| 144 CString c; | |
| 145 const char* d = 0; | |
| 146 EXPECT_TRUE(c == d); | |
| 147 EXPECT_FALSE(c != d); | |
| 148 c = "c"; | |
| 149 d = 0; | |
| 150 EXPECT_FALSE(c == d); | |
| 151 EXPECT_TRUE(c != d); | |
| 152 c = CString(); | |
| 153 d = "d"; | |
| 154 EXPECT_FALSE(c == d); | |
| 155 EXPECT_TRUE(c != d); | |
| 156 c = "c"; | |
| 157 d = "d"; | |
| 158 EXPECT_FALSE(c == d); | |
| 159 EXPECT_TRUE(c != d); | |
| 160 c = "c"; | |
| 161 d = "c"; | |
| 162 EXPECT_TRUE(c == d); | |
| 163 EXPECT_FALSE(c != d); | |
| 164 c = "c"; | |
| 165 d = "cc"; | |
| 166 EXPECT_FALSE(c == d); | |
| 167 EXPECT_TRUE(c != d); | |
| 168 c = "cc"; | |
| 169 d = "c"; | |
| 170 EXPECT_FALSE(c == d); | |
| 171 EXPECT_TRUE(c != d); | |
| 172 c = ""; | |
| 173 d = ""; | |
| 174 EXPECT_TRUE(c == d); | |
| 175 EXPECT_FALSE(c != d); | |
| 176 c = ""; | |
| 177 d = 0; | |
| 178 EXPECT_FALSE(c == d); | |
| 179 EXPECT_TRUE(c != d); | |
| 180 c = CString(); | |
| 181 d = ""; | |
| 182 EXPECT_FALSE(c == d); | |
| 183 EXPECT_TRUE(c != d); | |
| 184 c = "a"; | |
| 185 d = ""; | |
| 186 EXPECT_FALSE(c == d); | |
| 187 EXPECT_TRUE(c != d); | |
| 188 c = ""; | |
| 189 d = "b"; | |
| 190 EXPECT_FALSE(c == d); | |
| 191 EXPECT_TRUE(c != d); | |
| 192 } | |
| 193 | |
| 194 TEST(CStringTest, Printer) { | |
| 195 EXPECT_STREQ("<null>", printedString(CString()).data()); | |
| 196 EXPECT_STREQ("\"abc\"", printedString("abc").data()); | |
| 197 EXPECT_STREQ("\"\\t\\n\\r\\\"\\\\\"", printedString("\t\n\r\"\\").data()); | |
| 198 EXPECT_STREQ("\"\\xFF\\x00\\x01xyz\"", | |
| 199 printedString(CString("\xff\0\x01xyz", 6)).data()); | |
| 200 } | |
| 201 | |
| 202 } // namespace WTF | |
| OLD | NEW |