OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2012 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 13 matching lines...) Expand all Loading... |
24 */ | 24 */ |
25 | 25 |
26 #include "config.h" | 26 #include "config.h" |
27 | 27 |
28 #include "wtf/text/StringImpl.h" | 28 #include "wtf/text/StringImpl.h" |
29 #include "wtf/text/WTFString.h" | 29 #include "wtf/text/WTFString.h" |
30 #include <gtest/gtest.h> | 30 #include <gtest/gtest.h> |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 TEST(WTF, StringImplCreationFromLiteral) | 34 TEST(WTF, StringImplCreate8Bit) |
35 { | 35 { |
36 // Constructor using the template to determine the size. | 36 RefPtr<StringImpl> testStringImpl = StringImpl::create("1224"); |
37 RefPtr<StringImpl> stringWithTemplate = StringImpl::createFromLiteral("Templ
ate Literal"); | |
38 ASSERT_EQ(strlen("Template Literal"), stringWithTemplate->length()); | |
39 ASSERT_TRUE(equal(stringWithTemplate.get(), "Template Literal")); | |
40 ASSERT_TRUE(stringWithTemplate->is8Bit()); | |
41 | |
42 // Constructor taking the size explicitely. | |
43 const char* programmaticStringData = "Explicit Size Literal"; | |
44 RefPtr<StringImpl> programmaticString = StringImpl::createFromLiteral(progra
mmaticStringData, strlen(programmaticStringData)); | |
45 ASSERT_EQ(strlen(programmaticStringData), programmaticString->length()); | |
46 ASSERT_TRUE(equal(programmaticString.get(), programmaticStringData)); | |
47 ASSERT_EQ(programmaticStringData, reinterpret_cast<const char*>(programmatic
String->characters8())); | |
48 ASSERT_TRUE(programmaticString->is8Bit()); | |
49 | |
50 // Constructor without explicit size. | |
51 const char* stringWithoutLengthLiteral = "No Size Literal"; | |
52 RefPtr<StringImpl> programmaticStringNoLength = StringImpl::createFromLitera
l(stringWithoutLengthLiteral); | |
53 ASSERT_EQ(strlen(stringWithoutLengthLiteral), programmaticStringNoLength->le
ngth()); | |
54 ASSERT_TRUE(equal(programmaticStringNoLength.get(), stringWithoutLengthLiter
al)); | |
55 ASSERT_EQ(stringWithoutLengthLiteral, reinterpret_cast<const char*>(programm
aticStringNoLength->characters8())); | |
56 ASSERT_TRUE(programmaticStringNoLength->is8Bit()); | |
57 } | |
58 | |
59 TEST(WTF, StringImplReplaceWithLiteral) | |
60 { | |
61 RefPtr<StringImpl> testStringImpl = StringImpl::createFromLiteral("1224"); | |
62 ASSERT_TRUE(testStringImpl->is8Bit()); | 37 ASSERT_TRUE(testStringImpl->is8Bit()); |
63 | |
64 // Cases for 8Bit source. | |
65 testStringImpl = testStringImpl->replace('2', "", 0); | |
66 ASSERT_TRUE(equal(testStringImpl.get(), "14")); | |
67 | |
68 testStringImpl = StringImpl::createFromLiteral("1224"); | |
69 ASSERT_TRUE(testStringImpl->is8Bit()); | |
70 | |
71 testStringImpl = testStringImpl->replace('3', "NotFound", 8); | |
72 ASSERT_TRUE(equal(testStringImpl.get(), "1224")); | |
73 | |
74 testStringImpl = testStringImpl->replace('2', "3", 1); | |
75 ASSERT_TRUE(equal(testStringImpl.get(), "1334")); | |
76 | |
77 testStringImpl = StringImpl::createFromLiteral("1224"); | |
78 ASSERT_TRUE(testStringImpl->is8Bit()); | |
79 testStringImpl = testStringImpl->replace('2', "555", 3); | |
80 ASSERT_TRUE(equal(testStringImpl.get(), "15555554")); | |
81 | |
82 // Cases for 16Bit source. | |
83 String testString = String::fromUTF8("résumé"); | |
84 ASSERT_FALSE(testString.impl()->is8Bit()); | |
85 | |
86 testStringImpl = testString.impl()->replace('2', "NotFound", 8); | |
87 ASSERT_TRUE(equal(testStringImpl.get(), String::fromUTF8("résumé").impl())); | |
88 | |
89 testStringImpl = testString.impl()->replace(UChar(0x00E9 /*U+00E9 is 'é'*/),
"e", 1); | |
90 ASSERT_TRUE(equal(testStringImpl.get(), "resume")); | |
91 | |
92 testString = String::fromUTF8("résumé"); | |
93 ASSERT_FALSE(testString.impl()->is8Bit()); | |
94 testStringImpl = testString.impl()->replace(UChar(0x00E9 /*U+00E9 is 'é'*/),
"", 0); | |
95 ASSERT_TRUE(equal(testStringImpl.get(), "rsum")); | |
96 | |
97 testString = String::fromUTF8("résumé"); | |
98 ASSERT_FALSE(testString.impl()->is8Bit()); | |
99 testStringImpl = testString.impl()->replace(UChar(0x00E9 /*U+00E9 is 'é'*/),
"555", 3); | |
100 ASSERT_TRUE(equal(testStringImpl.get(), "r555sum555")); | |
101 } | 38 } |
102 | 39 |
103 } // namespace | 40 } // namespace |
OLD | NEW |