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