| 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 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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 "wtf/text/CString.h" | 26 #include "wtf/text/CString.h" |
| 27 | 27 |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 29 |
| 30 namespace WTF { | 30 namespace WTF { |
| 31 | 31 |
| 32 TEST(CStringTest, NullStringConstructor) | 32 TEST(CStringTest, NullStringConstructor) { |
| 33 { | 33 CString string; |
| 34 CString string; | 34 EXPECT_TRUE(string.isNull()); |
| 35 EXPECT_TRUE(string.isNull()); | 35 EXPECT_EQ(static_cast<const char*>(0), string.data()); |
| 36 EXPECT_EQ(static_cast<const char*>(0), string.data()); | 36 EXPECT_EQ(static_cast<size_t>(0), string.length()); |
| 37 EXPECT_EQ(static_cast<size_t>(0), string.length()); | |
| 38 | 37 |
| 39 CString stringFromCharPointer(static_cast<const char*>(0)); | 38 CString stringFromCharPointer(static_cast<const char*>(0)); |
| 40 EXPECT_TRUE(stringFromCharPointer.isNull()); | 39 EXPECT_TRUE(stringFromCharPointer.isNull()); |
| 41 EXPECT_EQ(static_cast<const char*>(0), stringFromCharPointer.data()); | 40 EXPECT_EQ(static_cast<const char*>(0), stringFromCharPointer.data()); |
| 42 EXPECT_EQ(static_cast<size_t>(0), stringFromCharPointer.length()); | 41 EXPECT_EQ(static_cast<size_t>(0), stringFromCharPointer.length()); |
| 43 | 42 |
| 44 CString stringFromCharAndLength(static_cast<const char*>(0), 0); | 43 CString stringFromCharAndLength(static_cast<const char*>(0), 0); |
| 45 EXPECT_TRUE(stringFromCharAndLength.isNull()); | 44 EXPECT_TRUE(stringFromCharAndLength.isNull()); |
| 46 EXPECT_EQ(static_cast<const char*>(0), stringFromCharAndLength.data()); | 45 EXPECT_EQ(static_cast<const char*>(0), stringFromCharAndLength.data()); |
| 47 EXPECT_EQ(static_cast<size_t>(0), stringFromCharAndLength.length()); | 46 EXPECT_EQ(static_cast<size_t>(0), stringFromCharAndLength.length()); |
| 48 } | 47 } |
| 49 | 48 |
| 50 TEST(CStringTest, EmptyEmptyConstructor) | 49 TEST(CStringTest, EmptyEmptyConstructor) { |
| 51 { | 50 const char* emptyString = ""; |
| 52 const char* emptyString = ""; | 51 CString string(emptyString); |
| 53 CString string(emptyString); | 52 EXPECT_FALSE(string.isNull()); |
| 54 EXPECT_FALSE(string.isNull()); | 53 EXPECT_EQ(static_cast<size_t>(0), string.length()); |
| 55 EXPECT_EQ(static_cast<size_t>(0), string.length()); | 54 EXPECT_EQ(0, string.data()[0]); |
| 56 EXPECT_EQ(0, string.data()[0]); | |
| 57 | 55 |
| 58 CString stringWithLength(emptyString, 0); | 56 CString stringWithLength(emptyString, 0); |
| 59 EXPECT_FALSE(stringWithLength.isNull()); | 57 EXPECT_FALSE(stringWithLength.isNull()); |
| 60 EXPECT_EQ(static_cast<size_t>(0), stringWithLength.length()); | 58 EXPECT_EQ(static_cast<size_t>(0), stringWithLength.length()); |
| 61 EXPECT_EQ(0, stringWithLength.data()[0]); | 59 EXPECT_EQ(0, stringWithLength.data()[0]); |
| 62 } | 60 } |
| 63 | 61 |
| 64 TEST(CStringTest, EmptyRegularConstructor) | 62 TEST(CStringTest, EmptyRegularConstructor) { |
| 65 { | 63 const char* referenceString = "WebKit"; |
| 66 const char* referenceString = "WebKit"; | |
| 67 | 64 |
| 68 CString string(referenceString); | 65 CString string(referenceString); |
| 69 EXPECT_FALSE(string.isNull()); | 66 EXPECT_FALSE(string.isNull()); |
| 70 EXPECT_EQ(strlen(referenceString), string.length()); | 67 EXPECT_EQ(strlen(referenceString), string.length()); |
| 71 EXPECT_STREQ(referenceString, string.data()); | 68 EXPECT_STREQ(referenceString, string.data()); |
| 72 | 69 |
| 73 CString stringWithLength(referenceString, 6); | 70 CString stringWithLength(referenceString, 6); |
| 74 EXPECT_FALSE(stringWithLength.isNull()); | 71 EXPECT_FALSE(stringWithLength.isNull()); |
| 75 EXPECT_EQ(strlen(referenceString), stringWithLength.length()); | 72 EXPECT_EQ(strlen(referenceString), stringWithLength.length()); |
| 76 EXPECT_STREQ(referenceString, stringWithLength.data()); | 73 EXPECT_STREQ(referenceString, stringWithLength.data()); |
| 77 } | 74 } |
| 78 | 75 |
| 79 TEST(CStringTest, UninitializedConstructor) | 76 TEST(CStringTest, UninitializedConstructor) { |
| 80 { | 77 char* buffer; |
| 81 char* buffer; | 78 CString emptyString = CString::newUninitialized(0, buffer); |
| 82 CString emptyString = CString::newUninitialized(0, buffer); | 79 EXPECT_FALSE(emptyString.isNull()); |
| 83 EXPECT_FALSE(emptyString.isNull()); | 80 EXPECT_EQ(buffer, emptyString.data()); |
| 84 EXPECT_EQ(buffer, emptyString.data()); | 81 EXPECT_EQ(0, buffer[0]); |
| 85 EXPECT_EQ(0, buffer[0]); | |
| 86 | 82 |
| 87 const size_t length = 25; | 83 const size_t length = 25; |
| 88 CString uninitializedString = CString::newUninitialized(length, buffer); | 84 CString uninitializedString = CString::newUninitialized(length, buffer); |
| 89 EXPECT_FALSE(uninitializedString.isNull()); | 85 EXPECT_FALSE(uninitializedString.isNull()); |
| 90 EXPECT_EQ(buffer, uninitializedString.data()); | 86 EXPECT_EQ(buffer, uninitializedString.data()); |
| 91 EXPECT_EQ(0, uninitializedString.data()[length]); | 87 EXPECT_EQ(0, uninitializedString.data()[length]); |
| 92 } | 88 } |
| 93 | 89 |
| 94 TEST(CStringTest, ZeroTerminated) | 90 TEST(CStringTest, ZeroTerminated) { |
| 95 { | 91 const char* referenceString = "WebKit"; |
| 96 const char* referenceString = "WebKit"; | 92 CString stringWithLength(referenceString, 3); |
| 97 CString stringWithLength(referenceString, 3); | 93 EXPECT_EQ(0, stringWithLength.data()[3]); |
| 98 EXPECT_EQ(0, stringWithLength.data()[3]); | |
| 99 } | 94 } |
| 100 | 95 |
| 101 TEST(CStringTest, CopyOnWrite) | 96 TEST(CStringTest, CopyOnWrite) { |
| 102 { | 97 const char* initialString = "Webkit"; |
| 103 const char* initialString = "Webkit"; | 98 CString string(initialString); |
| 104 CString string(initialString); | 99 CString copy = string; |
| 105 CString copy = string; | |
| 106 | 100 |
| 107 string.mutableData()[3] = 'K'; | 101 string.mutableData()[3] = 'K'; |
| 108 EXPECT_TRUE(string != copy); | 102 EXPECT_TRUE(string != copy); |
| 109 EXPECT_STREQ("WebKit", string.data()); | 103 EXPECT_STREQ("WebKit", string.data()); |
| 110 EXPECT_STREQ(initialString, copy.data()); | 104 EXPECT_STREQ(initialString, copy.data()); |
| 111 } | 105 } |
| 112 | 106 |
| 113 TEST(CStringTest, Comparison) | 107 TEST(CStringTest, Comparison) { |
| 114 { | 108 // Comparison with another CString. |
| 115 // Comparison with another CString. | 109 CString a; |
| 116 CString a; | 110 CString b; |
| 117 CString b; | 111 EXPECT_TRUE(a == b); |
| 118 EXPECT_TRUE(a == b); | 112 EXPECT_FALSE(a != b); |
| 119 EXPECT_FALSE(a != b); | 113 a = "a"; |
| 120 a = "a"; | 114 b = CString(); |
| 121 b = CString(); | 115 EXPECT_FALSE(a == b); |
| 122 EXPECT_FALSE(a == b); | 116 EXPECT_TRUE(a != b); |
| 123 EXPECT_TRUE(a != b); | 117 a = "a"; |
| 124 a = "a"; | 118 b = "b"; |
| 125 b = "b"; | 119 EXPECT_FALSE(a == b); |
| 126 EXPECT_FALSE(a == b); | 120 EXPECT_TRUE(a != b); |
| 127 EXPECT_TRUE(a != b); | 121 a = "a"; |
| 128 a = "a"; | 122 b = "a"; |
| 129 b = "a"; | 123 EXPECT_TRUE(a == b); |
| 130 EXPECT_TRUE(a == b); | 124 EXPECT_FALSE(a != b); |
| 131 EXPECT_FALSE(a != b); | 125 a = "a"; |
| 132 a = "a"; | 126 b = "aa"; |
| 133 b = "aa"; | 127 EXPECT_FALSE(a == b); |
| 134 EXPECT_FALSE(a == b); | 128 EXPECT_TRUE(a != b); |
| 135 EXPECT_TRUE(a != b); | 129 a = ""; |
| 136 a = ""; | 130 b = ""; |
| 137 b = ""; | 131 EXPECT_TRUE(a == b); |
| 138 EXPECT_TRUE(a == b); | 132 EXPECT_FALSE(a != b); |
| 139 EXPECT_FALSE(a != b); | 133 a = ""; |
| 140 a = ""; | 134 b = CString(); |
| 141 b = CString(); | 135 EXPECT_FALSE(a == b); |
| 142 EXPECT_FALSE(a == b); | 136 EXPECT_TRUE(a != b); |
| 143 EXPECT_TRUE(a != b); | 137 a = "a"; |
| 144 a = "a"; | 138 b = ""; |
| 145 b = ""; | 139 EXPECT_FALSE(a == b); |
| 146 EXPECT_FALSE(a == b); | 140 EXPECT_TRUE(a != b); |
| 147 EXPECT_TRUE(a != b); | |
| 148 | 141 |
| 149 // Comparison with a const char*. | 142 // Comparison with a const char*. |
| 150 CString c; | 143 CString c; |
| 151 const char* d = 0; | 144 const char* d = 0; |
| 152 EXPECT_TRUE(c == d); | 145 EXPECT_TRUE(c == d); |
| 153 EXPECT_FALSE(c != d); | 146 EXPECT_FALSE(c != d); |
| 154 c = "c"; | 147 c = "c"; |
| 155 d = 0; | 148 d = 0; |
| 156 EXPECT_FALSE(c == d); | 149 EXPECT_FALSE(c == d); |
| 157 EXPECT_TRUE(c != d); | 150 EXPECT_TRUE(c != d); |
| 158 c = CString(); | 151 c = CString(); |
| 159 d = "d"; | 152 d = "d"; |
| 160 EXPECT_FALSE(c == d); | 153 EXPECT_FALSE(c == d); |
| 161 EXPECT_TRUE(c != d); | 154 EXPECT_TRUE(c != d); |
| 162 c = "c"; | 155 c = "c"; |
| 163 d = "d"; | 156 d = "d"; |
| 164 EXPECT_FALSE(c == d); | 157 EXPECT_FALSE(c == d); |
| 165 EXPECT_TRUE(c != d); | 158 EXPECT_TRUE(c != d); |
| 166 c = "c"; | 159 c = "c"; |
| 167 d = "c"; | 160 d = "c"; |
| 168 EXPECT_TRUE(c == d); | 161 EXPECT_TRUE(c == d); |
| 169 EXPECT_FALSE(c != d); | 162 EXPECT_FALSE(c != d); |
| 170 c = "c"; | 163 c = "c"; |
| 171 d = "cc"; | 164 d = "cc"; |
| 172 EXPECT_FALSE(c == d); | 165 EXPECT_FALSE(c == d); |
| 173 EXPECT_TRUE(c != d); | 166 EXPECT_TRUE(c != d); |
| 174 c = "cc"; | 167 c = "cc"; |
| 175 d = "c"; | 168 d = "c"; |
| 176 EXPECT_FALSE(c == d); | 169 EXPECT_FALSE(c == d); |
| 177 EXPECT_TRUE(c != d); | 170 EXPECT_TRUE(c != d); |
| 178 c = ""; | 171 c = ""; |
| 179 d = ""; | 172 d = ""; |
| 180 EXPECT_TRUE(c == d); | 173 EXPECT_TRUE(c == d); |
| 181 EXPECT_FALSE(c != d); | 174 EXPECT_FALSE(c != d); |
| 182 c = ""; | 175 c = ""; |
| 183 d = 0; | 176 d = 0; |
| 184 EXPECT_FALSE(c == d); | 177 EXPECT_FALSE(c == d); |
| 185 EXPECT_TRUE(c != d); | 178 EXPECT_TRUE(c != d); |
| 186 c = CString(); | 179 c = CString(); |
| 187 d = ""; | 180 d = ""; |
| 188 EXPECT_FALSE(c == d); | 181 EXPECT_FALSE(c == d); |
| 189 EXPECT_TRUE(c != d); | 182 EXPECT_TRUE(c != d); |
| 190 c = "a"; | 183 c = "a"; |
| 191 d = ""; | 184 d = ""; |
| 192 EXPECT_FALSE(c == d); | 185 EXPECT_FALSE(c == d); |
| 193 EXPECT_TRUE(c != d); | 186 EXPECT_TRUE(c != d); |
| 194 c = ""; | 187 c = ""; |
| 195 d = "b"; | 188 d = "b"; |
| 196 EXPECT_FALSE(c == d); | 189 EXPECT_FALSE(c == d); |
| 197 EXPECT_TRUE(c != d); | 190 EXPECT_TRUE(c != d); |
| 198 } | 191 } |
| 199 | 192 |
| 200 } // namespace WTF | 193 } // namespace WTF |
| OLD | NEW |