Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / text / CStringTest.cpp
blob087cbb63634c008046a5437ad0d11cae33e1e7a8
1 /*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
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.
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.
26 #include "config.h"
27 #include "wtf/text/CString.h"
29 #include <gtest/gtest.h>
31 namespace WTF {
33 TEST(CStringTest, NullStringConstructor)
35 CString string;
36 EXPECT_TRUE(string.isNull());
37 EXPECT_EQ(static_cast<const char*>(0), string.data());
38 EXPECT_EQ(static_cast<size_t>(0), string.length());
40 CString stringFromCharPointer(static_cast<const char*>(0));
41 EXPECT_TRUE(stringFromCharPointer.isNull());
42 EXPECT_EQ(static_cast<const char*>(0), stringFromCharPointer.data());
43 EXPECT_EQ(static_cast<size_t>(0), stringFromCharPointer.length());
45 CString stringFromCharAndLength(static_cast<const char*>(0), 0);
46 EXPECT_TRUE(stringFromCharAndLength.isNull());
47 EXPECT_EQ(static_cast<const char*>(0), stringFromCharAndLength.data());
48 EXPECT_EQ(static_cast<size_t>(0), stringFromCharAndLength.length());
51 TEST(CStringTest, EmptyEmptyConstructor)
53 const char* emptyString = "";
54 CString string(emptyString);
55 EXPECT_FALSE(string.isNull());
56 EXPECT_EQ(static_cast<size_t>(0), string.length());
57 EXPECT_EQ(0, string.data()[0]);
59 CString stringWithLength(emptyString, 0);
60 EXPECT_FALSE(stringWithLength.isNull());
61 EXPECT_EQ(static_cast<size_t>(0), stringWithLength.length());
62 EXPECT_EQ(0, stringWithLength.data()[0]);
65 TEST(CStringTest, EmptyRegularConstructor)
67 const char* referenceString = "WebKit";
69 CString string(referenceString);
70 EXPECT_FALSE(string.isNull());
71 EXPECT_EQ(strlen(referenceString), string.length());
72 EXPECT_STREQ(referenceString, string.data());
74 CString stringWithLength(referenceString, 6);
75 EXPECT_FALSE(stringWithLength.isNull());
76 EXPECT_EQ(strlen(referenceString), stringWithLength.length());
77 EXPECT_STREQ(referenceString, stringWithLength.data());
80 TEST(CStringTest, UninitializedConstructor)
82 char* buffer;
83 CString emptyString = CString::newUninitialized(0, buffer);
84 EXPECT_FALSE(emptyString.isNull());
85 EXPECT_EQ(buffer, emptyString.data());
86 EXPECT_EQ(0, buffer[0]);
88 const size_t length = 25;
89 CString uninitializedString = CString::newUninitialized(length, buffer);
90 EXPECT_FALSE(uninitializedString.isNull());
91 EXPECT_EQ(buffer, uninitializedString.data());
92 EXPECT_EQ(0, uninitializedString.data()[length]);
95 TEST(CStringTest, ZeroTerminated)
97 const char* referenceString = "WebKit";
98 CString stringWithLength(referenceString, 3);
99 EXPECT_EQ(0, stringWithLength.data()[3]);
102 TEST(CStringTest, CopyOnWrite)
104 const char* initialString = "Webkit";
105 CString string(initialString);
106 CString copy = string;
108 string.mutableData()[3] = 'K';
109 EXPECT_TRUE(string != copy);
110 EXPECT_STREQ("WebKit", string.data());
111 EXPECT_STREQ(initialString, copy.data());
114 TEST(CStringTest, Comparison)
116 // Comparison with another CString.
117 CString a;
118 CString b;
119 EXPECT_TRUE(a == b);
120 EXPECT_FALSE(a != b);
121 a = "a";
122 b = CString();
123 EXPECT_FALSE(a == b);
124 EXPECT_TRUE(a != b);
125 a = "a";
126 b = "b";
127 EXPECT_FALSE(a == b);
128 EXPECT_TRUE(a != b);
129 a = "a";
130 b = "a";
131 EXPECT_TRUE(a == b);
132 EXPECT_FALSE(a != b);
133 a = "a";
134 b = "aa";
135 EXPECT_FALSE(a == b);
136 EXPECT_TRUE(a != b);
137 a = "";
138 b = "";
139 EXPECT_TRUE(a == b);
140 EXPECT_FALSE(a != b);
141 a = "";
142 b = CString();
143 EXPECT_FALSE(a == b);
144 EXPECT_TRUE(a != b);
145 a = "a";
146 b = "";
147 EXPECT_FALSE(a == b);
148 EXPECT_TRUE(a != b);
150 // Comparison with a const char*.
151 CString c;
152 const char* d = 0;
153 EXPECT_TRUE(c == d);
154 EXPECT_FALSE(c != d);
155 c = "c";
156 d = 0;
157 EXPECT_FALSE(c == d);
158 EXPECT_TRUE(c != d);
159 c = CString();
160 d = "d";
161 EXPECT_FALSE(c == d);
162 EXPECT_TRUE(c != d);
163 c = "c";
164 d = "d";
165 EXPECT_FALSE(c == d);
166 EXPECT_TRUE(c != d);
167 c = "c";
168 d = "c";
169 EXPECT_TRUE(c == d);
170 EXPECT_FALSE(c != d);
171 c = "c";
172 d = "cc";
173 EXPECT_FALSE(c == d);
174 EXPECT_TRUE(c != d);
175 c = "cc";
176 d = "c";
177 EXPECT_FALSE(c == d);
178 EXPECT_TRUE(c != d);
179 c = "";
180 d = "";
181 EXPECT_TRUE(c == d);
182 EXPECT_FALSE(c != d);
183 c = "";
184 d = 0;
185 EXPECT_FALSE(c == d);
186 EXPECT_TRUE(c != d);
187 c = CString();
188 d = "";
189 EXPECT_FALSE(c == d);
190 EXPECT_TRUE(c != d);
191 c = "a";
192 d = "";
193 EXPECT_FALSE(c == d);
194 EXPECT_TRUE(c != d);
195 c = "";
196 d = "b";
197 EXPECT_FALSE(c == d);
198 EXPECT_TRUE(c != d);
201 } // namespace WTF