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