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