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