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
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.
27 #include "wtf/text/CString.h"
29 #include <gtest/gtest.h>
33 TEST(CStringTest
, NullStringConstructor
)
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
)
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.
120 EXPECT_FALSE(a
!= b
);
123 EXPECT_FALSE(a
== b
);
127 EXPECT_FALSE(a
== b
);
132 EXPECT_FALSE(a
!= b
);
135 EXPECT_FALSE(a
== b
);
140 EXPECT_FALSE(a
!= b
);
143 EXPECT_FALSE(a
== b
);
147 EXPECT_FALSE(a
== b
);
150 // Comparison with a const char*.
154 EXPECT_FALSE(c
!= d
);
157 EXPECT_FALSE(c
== d
);
161 EXPECT_FALSE(c
== d
);
165 EXPECT_FALSE(c
== d
);
170 EXPECT_FALSE(c
!= d
);
173 EXPECT_FALSE(c
== d
);
177 EXPECT_FALSE(c
== d
);
182 EXPECT_FALSE(c
!= d
);
185 EXPECT_FALSE(c
== d
);
189 EXPECT_FALSE(c
== d
);
193 EXPECT_FALSE(c
== d
);
197 EXPECT_FALSE(c
== d
);