Add unit test for existing StringBuilder
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 23 Sep 2011 01:42:34 +0000 (01:42 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 23 Sep 2011 01:42:34 +0000 (01:42 +0000)
https://bugs.webkit.org/show_bug.cgi?id=67080

Patch by Xianzhu Wang <wangxianzhu@chromium.org> on 2011-09-22
Reviewed by Darin Adler.

* TestWebKitAPI/TestWebKitAPI.gypi:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/StringBuilder.cpp: Added.
(operator<<):
(TestWebKitAPI::expectBuilderContent):
(TestWebKitAPI::expectEmpty):
(TestWebKitAPI::TEST):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@95770 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.gypi
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp [new file with mode: 0644]

index dafdd46..87cf007 100644 (file)
@@ -1,3 +1,18 @@
+2011-09-22  Xianzhu Wang  <wangxianzhu@chromium.org>
+
+        Add unit test for existing StringBuilder
+        https://bugs.webkit.org/show_bug.cgi?id=67080
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/TestWebKitAPI.gypi:
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WTF/StringBuilder.cpp: Added.
+        (operator<<):
+        (TestWebKitAPI::expectBuilderContent):
+        (TestWebKitAPI::expectEmpty):
+        (TestWebKitAPI::TEST):
+
 2011-09-22  Raphael Kubo da Costa  <kubo@profusion.mobi>
 
         [EFL] Use ewk_view to navigate in history instead of ewk_history.
index 3e1d534..85aa29c 100644 (file)
@@ -33,6 +33,7 @@
         'TestWebKitAPI_files': [
             'Tests/WTF/CheckedArithmeticOperations.cpp',
             'Tests/WTF/RedBlackTree.cpp',
+            'Tests/WTF/StringBuilder.cpp',
             'Tests/WTF/StringOperators.cpp',
             'Tests/WTF/VectorBasic.cpp',
             'Tests/WTF/VectorReverse.cpp',
index a74ed1a..701685a 100644 (file)
@@ -29,6 +29,7 @@
                37DC6791140D7D7600ABCCDB /* DOMRangeOfString.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 37DC678F140D7D3A00ABCCDB /* DOMRangeOfString.html */; };
                4BFDFFA71314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BFDFFA61314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp */; };
                4BFDFFA9131477770061F24B /* HitTestResultNodeHandle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BFDFFA8131477770061F24B /* HitTestResultNodeHandle.cpp */; };
+               81B50193140F232300D9EB58 /* StringBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 81B50192140F232300D9EB58 /* StringBuilder.cpp */; };
                520BCF4C141EB09E00937EA8 /* WebArchive_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 520BCF4A141EB09E00937EA8 /* WebArchive_Bundle.cpp */; };
                520BCF4D141EB09E00937EA8 /* WebArchive.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 520BCF4B141EB09E00937EA8 /* WebArchive.cpp */; };
                939BA91714103412001A01BD /* DeviceScaleFactorOnBack.mm in Sources */ = {isa = PBXBuildFile; fileRef = 939BA91614103412001A01BD /* DeviceScaleFactorOnBack.mm */; };
                37DC678F140D7D3A00ABCCDB /* DOMRangeOfString.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = DOMRangeOfString.html; sourceTree = "<group>"; };
                4BFDFFA61314776C0061F24B /* HitTestResultNodeHandle_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HitTestResultNodeHandle_Bundle.cpp; sourceTree = "<group>"; };
                4BFDFFA8131477770061F24B /* HitTestResultNodeHandle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HitTestResultNodeHandle.cpp; sourceTree = "<group>"; };
+               81B50192140F232300D9EB58 /* StringBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = StringBuilder.cpp; path = WTF/StringBuilder.cpp; sourceTree = "<group>"; };
                520BCF4A141EB09E00937EA8 /* WebArchive_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebArchive_Bundle.cpp; sourceTree = "<group>"; };
                520BCF4B141EB09E00937EA8 /* WebArchive.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebArchive.cpp; sourceTree = "<group>"; };
                8DD76FA10486AA7600D96B5E /* TestWebKitAPI */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = TestWebKitAPI; sourceTree = BUILT_PRODUCTS_DIR; };
                                0FC6C4CE141034AD005B7F0C /* MetaAllocator.cpp */,
                                0FC6C4CB141027E0005B7F0C /* RedBlackTree.cpp */,
                                A7A966DA140ECCC8005EF9B4 /* CheckedArithmeticOperations.cpp */,
+                               81B50192140F232300D9EB58 /* StringBuilder.cpp */,
                                C01363C713C3997300EF3964 /* StringOperators.cpp */,
                                BC90964B125561BF00083756 /* VectorBasic.cpp */,
                                37200B9113A16230007A4FAD /* VectorReverse.cpp */,
                                C08587FC13FEC39B001EF4E5 /* InstanceMethodSwizzler.mm in Sources */,
                                C085880013FEC3A6001EF4E5 /* InstanceMethodSwizzler.mm in Sources */,
                                37DC678D140D7C5000ABCCDB /* DOMRangeOfString.mm in Sources */,
+                               81B50193140F232300D9EB58 /* StringBuilder.cpp in Sources */,
                                0FC6C4CC141027E0005B7F0C /* RedBlackTree.cpp in Sources */,
                                0FC6C4CF141034AD005B7F0C /* MetaAllocator.cpp in Sources */,
                                A7A966DB140ECCC8005EF9B4 /* CheckedArithmeticOperations.cpp in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp b/Tools/TestWebKitAPI/Tests/WTF/StringBuilder.cpp
new file mode 100644 (file)
index 0000000..f826f3b
--- /dev/null
@@ -0,0 +1,197 @@
+/*
+ * Copyright (C) 2011 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include <wtf/Assertions.h>
+#include <wtf/text/CString.h>
+#include <wtf/text/StringBuilder.h>
+#include <wtf/text/WTFString.h>
+
+// For EXPECT_EQ(String, String)
+std::ostream& operator<<(std::ostream& os, const String& string)
+{
+    return os << string.utf8().data();
+}
+
+namespace TestWebKitAPI {
+
+void expectBuilderContent(const char* expected, const StringBuilder& builder)
+{
+    // Not using builder.toString() or builder.toStringPreserveCapacity() because they all
+    // change internal state of builder.
+    EXPECT_EQ(String(expected), String(builder.characters(), builder.length()));
+}
+
+void expectEmpty(const StringBuilder& builder)
+{
+    EXPECT_EQ(0U, builder.length());
+    EXPECT_TRUE(builder.isEmpty());
+    EXPECT_EQ(0, builder.characters());
+}
+
+TEST(StringBuilderTest, DefaultConstructor)
+{
+    StringBuilder builder;
+    expectEmpty(builder);
+}
+
+TEST(StringBuilderTest, Append)
+{
+    StringBuilder builder;
+    builder.append(String("0123456789"));
+    expectBuilderContent("0123456789", builder);
+    builder.append("abcd");
+    expectBuilderContent("0123456789abcd", builder);
+    builder.append("efgh", 3);
+    expectBuilderContent("0123456789abcdefg", builder);
+    builder.append("");
+    expectBuilderContent("0123456789abcdefg", builder);
+    builder.append('#');
+    expectBuilderContent("0123456789abcdefg#", builder);
+
+    builder.toString(); // Test after reifyString().
+    StringBuilder builder1;
+    builder.append("", 0);
+    expectBuilderContent("0123456789abcdefg#", builder);
+    builder1.append(builder.characters(), builder.length());
+    builder1.append("XYZ");
+    builder.append(builder1.characters(), builder1.length());
+    expectBuilderContent("0123456789abcdefg#0123456789abcdefg#XYZ", builder);
+}
+
+TEST(StringBuilderTest, ToString)
+{
+    StringBuilder builder;
+    builder.append("0123456789");
+    String string = builder.toString();
+    ASSERT_EQ(String("0123456789"), string);
+    ASSERT_EQ(string.impl(), builder.toString().impl());
+
+    // Changing the StringBuilder should not affect the original result of toString().
+    builder.append("abcdefghijklmnopqrstuvwxyz");
+    ASSERT_EQ(String("0123456789"), string);
+
+    // Changing the StringBuilder should not affect the original result of toString() in case the capacity is not changed.
+    builder.reserveCapacity(200);
+    string = builder.toString();
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string);
+    builder.append("ABC");
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string);
+
+    // Changing the original result of toString() should not affect the content of the StringBuilder.
+    String string1 = builder.toString();
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), string1);
+    string1.append("DEF");
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), builder.toString());
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABCDEF"), string1);
+
+    // Resizing the StringBuilder should not affect the original result of toString().
+    string1 = builder.toString();
+    builder.resize(10);
+    builder.append("###");
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), string1);
+}
+
+TEST(StringBuilderTest, ToStringPreserveCapacity)
+{
+    StringBuilder builder;
+    builder.append("0123456789");
+    String string = builder.toStringPreserveCapacity();
+    ASSERT_EQ(String("0123456789"), string);
+    ASSERT_EQ(string.impl(), builder.toStringPreserveCapacity().impl());
+    ASSERT_EQ(string.characters(), builder.characters());
+
+    // Changing the StringBuilder should not affect the original result of toStringPreserveCapacity().
+    builder.append("abcdefghijklmnopqrstuvwxyz");
+    ASSERT_EQ(String("0123456789"), string);
+
+    // Changing the StringBuilder should not affect the original result of toStringPreserveCapacity() in case the capacity is not changed.
+    builder.reserveCapacity(200);
+    string = builder.toStringPreserveCapacity();
+    ASSERT_EQ(string.characters(), builder.characters());
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string);
+    builder.append("ABC");
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string);
+
+    // Changing the original result of toStringPreserveCapacity() should not affect the content of the StringBuilder.
+    String string1 = builder.toStringPreserveCapacity();
+    ASSERT_EQ(string1.characters(), builder.characters());
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), string1);
+    string1.append("DEF");
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), builder.toStringPreserveCapacity());
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABCDEF"), string1);
+
+    // Resizing the StringBuilder should not affect the original result of toStringPreserveCapacity().
+    string1 = builder.toStringPreserveCapacity();
+    ASSERT_EQ(string.characters(), builder.characters());
+    builder.resize(10);
+    builder.append("###");
+    ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), string1);
+}
+
+TEST(StringBuilderTest, Clear)
+{
+    StringBuilder builder;
+    builder.append("0123456789");
+    builder.clear();
+    expectEmpty(builder);
+}
+
+TEST(StringBuilderTest, Array)
+{
+    StringBuilder builder;
+    builder.append("0123456789");
+    EXPECT_EQ('0', static_cast<char>(builder[0]));
+    EXPECT_EQ('9', static_cast<char>(builder[9]));
+    builder.toString(); // Test after reifyString().
+    EXPECT_EQ('0', static_cast<char>(builder[0]));
+    EXPECT_EQ('9', static_cast<char>(builder[9]));
+}
+
+TEST(StringBuilderTest, Resize)
+{
+    StringBuilder builder;
+    builder.append("0123456789");
+    builder.resize(10);
+    EXPECT_EQ(10U, builder.length());
+    expectBuilderContent("0123456789", builder);
+    builder.resize(8);
+    EXPECT_EQ(8U, builder.length());
+    expectBuilderContent("01234567", builder);
+
+    builder.toString();
+    builder.resize(7);
+    EXPECT_EQ(7U, builder.length());
+    expectBuilderContent("0123456", builder);
+    builder.resize(0);
+    expectEmpty(builder);
+}
+
+} // namespace