This source file includes following definitions.
- expectBuilderContent
- expectEmpty
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "config.h"
#include "wtf/Assertions.h"
#include "wtf/text/CString.h"
#include "wtf/text/StringBuilder.h"
#include "wtf/text/WTFString.h"
#include "wtf/unicode/CharacterNames.h"
#include <gtest/gtest.h>
namespace WTF {
static std::ostream& operator<<(std::ostream& os, const String& string)
{
return os << string.utf8().data();
}
}
namespace {
static void expectBuilderContent(const String& expected, const StringBuilder& builder)
{
if (builder.is8Bit())
EXPECT_EQ(expected, String(builder.characters8(), builder.length()));
else
EXPECT_EQ(expected, String(builder.characters16(), builder.length()));
}
void expectEmpty(const StringBuilder& builder)
{
EXPECT_EQ(0U, builder.length());
EXPECT_TRUE(builder.isEmpty());
EXPECT_EQ(0, builder.characters8());
}
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();
StringBuilder builder1;
builder.append("", 0);
expectBuilderContent("0123456789abcdefg#", builder);
builder1.append(builder.characters8(), builder.length());
builder1.append("XYZ");
builder.append(builder1.characters8(), builder1.length());
expectBuilderContent("0123456789abcdefg#0123456789abcdefg#XYZ", builder);
StringBuilder builder2;
builder2.reserveCapacity(100);
builder2.append("xyz");
const LChar* characters = builder2.characters8();
builder2.append("0123456789");
ASSERT_EQ(characters, builder2.characters8());
StringBuilder builderForUChar32Append;
UChar32 frakturAChar = 0x1D504;
builderForUChar32Append.append(frakturAChar);
ASSERT_EQ(2U, builderForUChar32Append.length());
builderForUChar32Append.append(static_cast<UChar32>('A'));
ASSERT_EQ(3U, builderForUChar32Append.length());
const UChar resultArray[] = { U16_LEAD(frakturAChar), U16_TRAIL(frakturAChar), 'A' };
expectBuilderContent(String(resultArray, WTF_ARRAY_LENGTH(resultArray)), builderForUChar32Append);
}
TEST(StringBuilderTest, ToString)
{
StringBuilder builder;
builder.append("0123456789");
String string = builder.toString();
ASSERT_EQ(String("0123456789"), string);
ASSERT_EQ(string.impl(), builder.toString().impl());
builder.append("abcdefghijklmnopqrstuvwxyz");
ASSERT_EQ(String("0123456789"), string);
builder.reserveCapacity(200);
string = builder.toString();
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string);
builder.append("ABC");
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyz"), string);
String string1 = builder.toString();
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), string1);
string1.append("DEF");
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABC"), builder.toString());
ASSERT_EQ(String("0123456789abcdefghijklmnopqrstuvwxyzABCDEF"), string1);
string1 = builder.toString();
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();
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);
}
TEST(StringBuilderTest, Equal)
{
StringBuilder builder1;
StringBuilder builder2;
ASSERT_TRUE(builder1 == builder2);
ASSERT_TRUE(equal(builder1, static_cast<LChar*>(0), 0));
ASSERT_TRUE(builder1 == String());
ASSERT_TRUE(String() == builder1);
ASSERT_TRUE(builder1 != String("abc"));
builder1.append("123");
builder1.reserveCapacity(32);
builder2.append("123");
builder1.reserveCapacity(64);
ASSERT_TRUE(builder1 == builder2);
ASSERT_TRUE(builder1 == String("123"));
ASSERT_TRUE(String("123") == builder1);
builder2.append("456");
ASSERT_TRUE(builder1 != builder2);
ASSERT_TRUE(builder2 != builder1);
ASSERT_TRUE(String("123") != builder2);
ASSERT_TRUE(builder2 != String("123"));
builder2.toString();
ASSERT_TRUE(builder1 != builder2);
builder2.resize(3);
ASSERT_TRUE(builder1 == builder2);
builder1.toString();
ASSERT_TRUE(builder1 == builder2);
}
TEST(StringBuilderTest, CanShrink)
{
StringBuilder builder;
builder.reserveCapacity(256);
ASSERT_TRUE(builder.canShrink());
for (int i = 0; i < 256; i++)
builder.append('x');
ASSERT_EQ(builder.length(), builder.capacity());
ASSERT_FALSE(builder.canShrink());
}
TEST(StringBuilderTest, ToAtomicString)
{
StringBuilder builder;
builder.append("123");
AtomicString atomicString = builder.toAtomicString();
ASSERT_EQ(String("123"), atomicString);
builder.reserveCapacity(256);
ASSERT_TRUE(builder.canShrink());
for (int i = builder.length(); i < 128; i++)
builder.append('x');
AtomicString atomicString1 = builder.toAtomicString();
ASSERT_EQ(128u, atomicString1.length());
ASSERT_EQ('x', atomicString1[127]);
for (int i = builder.length(); i < 256; i++)
builder.append('x');
ASSERT_EQ(128u, atomicString1.length());
ASSERT_FALSE(builder.canShrink());
String string = builder.toString();
AtomicString atomicString2 = builder.toAtomicString();
ASSERT_EQ(atomicString2.impl(), string.impl());
}
TEST(StringBuilderTest, ToAtomicStringOnEmpty)
{
{
StringBuilder builder;
AtomicString atomicString = builder.toAtomicString();
ASSERT_EQ(emptyAtom, atomicString);
}
{
StringBuilder builder;
builder.reserveCapacity(64);
AtomicString atomicString = builder.toAtomicString();
ASSERT_EQ(emptyAtom, atomicString);
}
{
StringBuilder builder;
builder.append(String());
AtomicString atomicString = builder.toAtomicString();
ASSERT_EQ(emptyAtom, atomicString);
}
{
StringBuilder builder;
builder.append(emptyString());
AtomicString atomicString = builder.toAtomicString();
ASSERT_EQ(emptyAtom, atomicString);
}
{
StringBuilder builder;
StringBuilder emptyBuilder;
builder.append(emptyBuilder);
AtomicString atomicString = builder.toAtomicString();
ASSERT_EQ(emptyAtom, atomicString);
}
{
StringBuilder builder;
builder.append("", 0);
AtomicString atomicString = builder.toAtomicString();
ASSERT_EQ(emptyAtom, atomicString);
}
{
StringBuilder builder;
builder.appendLiteral("WebKit");
builder.clear();
AtomicString atomicString = builder.toAtomicString();
ASSERT_EQ(emptyAtom, atomicString);
}
}
TEST(StringBuilderTest, Substring)
{
{
StringBuilder builder;
String substring = builder.substring(0, 10);
ASSERT_EQ(emptyString(), substring);
}
{
StringBuilder builder;
builder.reserveCapacity(64);
builder.append("abc");
String substring = builder.substring(2, 10);
ASSERT_EQ(String("c"), substring);
}
}
TEST(StringBuilderTest, AppendNumberDoubleUChar)
{
const double someNumber = 1.2345;
StringBuilder reference;
reference.append(replacementCharacter);
reference.append(String::number(someNumber));
StringBuilder test;
test.append(replacementCharacter);
test.appendNumber(someNumber);
ASSERT_EQ(reference, test);
}
}