blob: a8dfc0e71469acdd8d33089cae7549be3143a2c0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Hal Canaryee08b4a2018-03-01 15:56:37 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkString.h"
11#include "src/core/SkStringUtils.h"
Hal Canary8a001442018-09-19 11:31:27 -040012
bungeman@google.comfab44db2013-10-11 18:50:45 +000013#include <stdio.h>
Mike Klein03141d22017-10-30 11:57:15 -040014#include <thread>
tomhudson@google.com47e0a092011-07-08 17:49:22 +000015
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000016DEF_TEST(String, reporter) {
reed@android.comd8730ea2009-02-27 22:06:06 +000017 SkString a;
18 SkString b((size_t)0);
19 SkString c("");
halcanary96fcdcc2015-08-27 07:41:13 -070020 SkString d(nullptr, 0);
reed@android.comd8730ea2009-02-27 22:06:06 +000021
22 REPORTER_ASSERT(reporter, a.isEmpty());
23 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
24
25 a.set("hello");
26 b.set("hellox", 5);
27 c.set(a);
28 d.resize(5);
29 memcpy(d.writable_str(), "helloz", 5);
30
31 REPORTER_ASSERT(reporter, !a.isEmpty());
32 REPORTER_ASSERT(reporter, a.size() == 5);
33 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
34 REPORTER_ASSERT(reporter, a.equals("hello", 5));
35 REPORTER_ASSERT(reporter, a.equals("hello"));
36 REPORTER_ASSERT(reporter, !a.equals("help"));
37
epoger@google.comc4ae9742012-04-27 17:11:31 +000038 REPORTER_ASSERT(reporter, a.startsWith("hell"));
epoger@google.come8ebeb12012-10-29 16:42:11 +000039 REPORTER_ASSERT(reporter, a.startsWith('h'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000040 REPORTER_ASSERT(reporter, !a.startsWith( "ell"));
epoger@google.come8ebeb12012-10-29 16:42:11 +000041 REPORTER_ASSERT(reporter, !a.startsWith( 'e'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000042 REPORTER_ASSERT(reporter, a.startsWith(""));
43 REPORTER_ASSERT(reporter, a.endsWith("llo"));
epoger@google.come8ebeb12012-10-29 16:42:11 +000044 REPORTER_ASSERT(reporter, a.endsWith('o'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000045 REPORTER_ASSERT(reporter, !a.endsWith("ll" ));
epoger@google.come8ebeb12012-10-29 16:42:11 +000046 REPORTER_ASSERT(reporter, !a.endsWith('l'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000047 REPORTER_ASSERT(reporter, a.endsWith(""));
48 REPORTER_ASSERT(reporter, a.contains("he"));
49 REPORTER_ASSERT(reporter, a.contains("ll"));
50 REPORTER_ASSERT(reporter, a.contains("lo"));
51 REPORTER_ASSERT(reporter, a.contains("hello"));
52 REPORTER_ASSERT(reporter, !a.contains("hellohello"));
53 REPORTER_ASSERT(reporter, a.contains(""));
epoger@google.come8ebeb12012-10-29 16:42:11 +000054 REPORTER_ASSERT(reporter, a.contains('e'));
55 REPORTER_ASSERT(reporter, !a.contains('z'));
rmistry@google.comd6176b02012-08-23 18:14:13 +000056
reed@android.comd8730ea2009-02-27 22:06:06 +000057 SkString e(a);
58 SkString f("hello");
59 SkString g("helloz", 5);
60
61 REPORTER_ASSERT(reporter, a == e && a == f && a == g);
62
63 b.set("world");
64 c = b;
65 REPORTER_ASSERT(reporter, a != b && a != c && b == c);
66
67 a.append(" world");
68 e.append("worldz", 5);
69 e.insert(5, " ");
70 f.set("world");
71 f.prepend("hello ");
72 REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f);
73
74 a.reset();
75 b.resize(0);
76 REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b);
77
78 a.set("a");
79 a.set("ab");
80 a.set("abc");
81 a.set("abcd");
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000082
83 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +000084 a.appendS32(0x7FFFFFFFL);
85 REPORTER_ASSERT(reporter, a.equals("2147483647"));
86 a.set("");
87 a.appendS32(0x80000001L);
88 REPORTER_ASSERT(reporter, a.equals("-2147483647"));
89 a.set("");
90 a.appendS32(0x80000000L);
91 REPORTER_ASSERT(reporter, a.equals("-2147483648"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000092
93 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +000094 a.appendU32(0x7FFFFFFFUL);
95 REPORTER_ASSERT(reporter, a.equals("2147483647"));
96 a.set("");
97 a.appendU32(0x80000001UL);
98 REPORTER_ASSERT(reporter, a.equals("2147483649"));
99 a.set("");
100 a.appendU32(0xFFFFFFFFUL);
101 REPORTER_ASSERT(reporter, a.equals("4294967295"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000102
103 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000104 a.appendS64(0x7FFFFFFFFFFFFFFFLL, 0);
105 REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
106 a.set("");
107 a.appendS64(0x8000000000000001LL, 0);
108 REPORTER_ASSERT(reporter, a.equals("-9223372036854775807"));
109 a.set("");
110 a.appendS64(0x8000000000000000LL, 0);
111 REPORTER_ASSERT(reporter, a.equals("-9223372036854775808"));
112 a.set("");
113 a.appendS64(0x0000000001000000LL, 15);
114 REPORTER_ASSERT(reporter, a.equals("000000016777216"));
115 a.set("");
116 a.appendS64(0xFFFFFFFFFF000000LL, 15);
117 REPORTER_ASSERT(reporter, a.equals("-000000016777216"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000118
119 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000120 a.appendU64(0x7FFFFFFFFFFFFFFFULL, 0);
121 REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
122 a.set("");
123 a.appendU64(0x8000000000000001ULL, 0);
124 REPORTER_ASSERT(reporter, a.equals("9223372036854775809"));
125 a.set("");
126 a.appendU64(0xFFFFFFFFFFFFFFFFULL, 0);
127 REPORTER_ASSERT(reporter, a.equals("18446744073709551615"));
128 a.set("");
129 a.appendU64(0x0000000001000000ULL, 15);
130 REPORTER_ASSERT(reporter, a.equals("000000016777216"));
reed@google.comfa06e522011-02-28 21:29:58 +0000131
halcanaryd51bdae2016-04-25 09:25:35 -0700132 a.printf("%i", 0);
133 REPORTER_ASSERT(reporter, a.equals("0"));
134 a.printf("%g", 3.14);
135 REPORTER_ASSERT(reporter, a.equals("3.14"));
136 a.printf("hello %s", "skia");
137 REPORTER_ASSERT(reporter, a.equals("hello skia"));
138
reed@google.comfa06e522011-02-28 21:29:58 +0000139 static const struct {
140 SkScalar fValue;
141 const char* fString;
142 } gRec[] = {
143 { 0, "0" },
144 { SK_Scalar1, "1" },
145 { -SK_Scalar1, "-1" },
146 { SK_Scalar1/2, "0.5" },
mtkleincc334b32015-09-22 11:43:53 -0700147 #if defined(SK_BUILD_FOR_WIN) && (_MSC_VER < 1900)
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000148 { 3.4028234e38f, "3.4028235e+038" },
149 { -3.4028234e38f, "-3.4028235e+038" },
150 #else
reed@google.com8072e4f2011-03-01 15:44:08 +0000151 { 3.4028234e38f, "3.4028235e+38" },
152 { -3.4028234e38f, "-3.4028235e+38" },
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000153 #endif
reed@google.comfa06e522011-02-28 21:29:58 +0000154 };
155 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
156 a.reset();
157 a.appendScalar(gRec[i].fValue);
reed@google.com8072e4f2011-03-01 15:44:08 +0000158 REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize);
bsalomon9bca5262015-08-06 17:56:13 -0700159 if (!a.equals(gRec[i].fString)) {
160 ERRORF(reporter, "received <%s> expected <%s>\n", a.c_str(), gRec[i].fString);
161 }
reed@google.comfa06e522011-02-28 21:29:58 +0000162 }
tomhudson@google.com3a1f6a02011-06-30 14:39:52 +0000163
164 REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0"));
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000165
halcanaryd51bdae2016-04-25 09:25:35 -0700166 // 2000 is larger than the static buffer size inside SkString.cpp
167 a = SkStringPrintf("%2000s", " ");
168 REPORTER_ASSERT(reporter, a.size() == 2000);
169 for (size_t i = 0; i < a.size(); ++i) {
170 if (a[i] != ' ') {
171 ERRORF(reporter, "SkStringPrintf fail: a[%d] = '%c'", i, a[i]);
172 break;
173 }
174 }
175 a.reset();
176 a.printf("%2000s", " ");
177 REPORTER_ASSERT(reporter, a.size() == 2000);
178 for (size_t i = 0; i < a.size(); ++i) {
179 if (a[i] != ' ') {
Brian Salomon43227522018-04-25 12:58:23 -0400180 ERRORF(reporter, "SkString::printf fail: a[%d] = '%c'", i, a[i]);
181 break;
182 }
183 }
184 a.appendf("%2000s", " ");
185 REPORTER_ASSERT(reporter, a.size() == 4000);
186 for (size_t i = 0; i < a.size(); ++i) {
187 if (a[i] != ' ') {
188 ERRORF(reporter, "SkString::appendf fail: a[%d] = '%c'", i, a[i]);
halcanaryd51bdae2016-04-25 09:25:35 -0700189 break;
190 }
191 }
reed@android.comd8730ea2009-02-27 22:06:06 +0000192}
193
rmistry@google.comd6bab022013-12-02 13:50:38 +0000194DEF_TEST(String_SkStrSplit, r) {
195 SkTArray<SkString> results;
196
197 SkStrSplit("a-_b_c-dee--f-_-_-g-", "-_", &results);
198 REPORTER_ASSERT(r, results.count() == 6);
199 REPORTER_ASSERT(r, results[0].equals("a"));
200 REPORTER_ASSERT(r, results[1].equals("b"));
201 REPORTER_ASSERT(r, results[2].equals("c"));
202 REPORTER_ASSERT(r, results[3].equals("dee"));
203 REPORTER_ASSERT(r, results[4].equals("f"));
204 REPORTER_ASSERT(r, results[5].equals("g"));
mtkleincc334b32015-09-22 11:43:53 -0700205
206 results.reset();
207 SkStrSplit("\n", "\n", &results);
kkinnunen3e980c32015-12-23 01:33:00 -0800208 REPORTER_ASSERT(r, results.count() == 0);
mtkleincc334b32015-09-22 11:43:53 -0700209
210 results.reset();
211 SkStrSplit("", "\n", &results);
212 REPORTER_ASSERT(r, results.count() == 0);
kkinnunen3e980c32015-12-23 01:33:00 -0800213
214 results.reset();
215 SkStrSplit("a", "\n", &results);
216 REPORTER_ASSERT(r, results.count() == 1);
217 REPORTER_ASSERT(r, results[0].equals("a"));
218}
219DEF_TEST(String_SkStrSplit_All, r) {
220 SkTArray<SkString> results;
221 SkStrSplit("a-_b_c-dee--f-_-_-g-", "-_", kStrict_SkStrSplitMode, &results);
222 REPORTER_ASSERT(r, results.count() == 13);
223 REPORTER_ASSERT(r, results[0].equals("a"));
224 REPORTER_ASSERT(r, results[1].equals(""));
225 REPORTER_ASSERT(r, results[2].equals("b"));
226 REPORTER_ASSERT(r, results[3].equals("c"));
227 REPORTER_ASSERT(r, results[4].equals("dee"));
228 REPORTER_ASSERT(r, results[5].equals(""));
229 REPORTER_ASSERT(r, results[6].equals("f"));
230 REPORTER_ASSERT(r, results[7].equals(""));
231 REPORTER_ASSERT(r, results[8].equals(""));
232 REPORTER_ASSERT(r, results[9].equals(""));
233 REPORTER_ASSERT(r, results[10].equals(""));
234 REPORTER_ASSERT(r, results[11].equals("g"));
235 REPORTER_ASSERT(r, results[12].equals(""));
236
237 results.reset();
238 SkStrSplit("\n", "\n", kStrict_SkStrSplitMode, &results);
239 REPORTER_ASSERT(r, results.count() == 2);
240 REPORTER_ASSERT(r, results[0].equals(""));
241 REPORTER_ASSERT(r, results[1].equals(""));
242
243 results.reset();
244 SkStrSplit("", "\n", kStrict_SkStrSplitMode, &results);
245 REPORTER_ASSERT(r, results.count() == 0);
246
247 results.reset();
248 SkStrSplit("a", "\n", kStrict_SkStrSplitMode, &results);
249 REPORTER_ASSERT(r, results.count() == 1);
250 REPORTER_ASSERT(r, results[0].equals("a"));
251
252 results.reset();
253 SkStrSplit(",,", ",", kStrict_SkStrSplitMode, &results);
254 REPORTER_ASSERT(r, results.count() == 3);
255 REPORTER_ASSERT(r, results[0].equals(""));
256 REPORTER_ASSERT(r, results[1].equals(""));
257 REPORTER_ASSERT(r, results[2].equals(""));
258
259 results.reset();
260 SkStrSplit(",a,b,", ",", kStrict_SkStrSplitMode, &results);
261 REPORTER_ASSERT(r, results.count() == 4);
262 REPORTER_ASSERT(r, results[0].equals(""));
263 REPORTER_ASSERT(r, results[1].equals("a"));
264 REPORTER_ASSERT(r, results[2].equals("b"));
265 REPORTER_ASSERT(r, results[3].equals(""));
rmistry@google.comd6bab022013-12-02 13:50:38 +0000266}
Ben Wagneraf893662017-10-03 11:08:14 -0400267
268// https://bugs.chromium.org/p/skia/issues/detail?id=7107
269DEF_TEST(String_Threaded, r) {
Mike Klein03141d22017-10-30 11:57:15 -0400270 SkString str("foo");
271
272 std::thread threads[5];
273 for (auto& thread : threads) {
274 thread = std::thread([&] {
275 SkString copy = str;
276 (void)copy.equals("test");
277 });
Ben Wagneraf893662017-10-03 11:08:14 -0400278 }
Mike Klein03141d22017-10-30 11:57:15 -0400279 for (auto& thread : threads) {
Ben Wagneraf893662017-10-03 11:08:14 -0400280 thread.join();
281 }
282}
Mike Reed33f38b02018-02-14 13:58:06 -0500283
284// Ensure that the string allocate doesn't internally overflow any calculations, and accidentally
285// let us create a string with a requested length longer than we can manage.
286DEF_TEST(String_huge, r) {
287 // start testing slightly below max 32
Ben Wagnerb0897652018-06-15 15:37:57 +0000288 size_t size = UINT32_MAX - 16;
Mike Reed33f38b02018-02-14 13:58:06 -0500289 // See where we crash, and manually check that its at the right point.
290 //
291 // To test, change the false to true
292 while (false) {
293 // On a 64bit build, this should crash when size == 1 << 32, since we can't store
294 // that length in the string's header (which has a u32 slot for the length).
295 //
296 // On a 32bit build, this should crash the first time around, since we can't allocate
297 // anywhere near this amount.
298 //
299 SkString str(size);
300 size += 1;
301 }
302}
303
Hal Canary2d0e1242018-03-01 12:32:18 -0500304DEF_TEST(String_fromUTF16, r) {
305 // test data produced with `iconv`.
306 const uint16_t test1[] = {
307 0xD835, 0xDCD0, 0xD835, 0xDCD1, 0xD835, 0xDCD2, 0xD835, 0xDCD3, 0xD835, 0xDCD4, 0x0020,
308 0xD835, 0xDCD5, 0xD835, 0xDCD6, 0xD835, 0xDCD7, 0xD835, 0xDCD8, 0xD835, 0xDCD9
309 };
Hal Canaryee08b4a2018-03-01 15:56:37 -0500310 REPORTER_ASSERT(r, SkStringFromUTF16(test1, SK_ARRAY_COUNT(test1)).equals("𝓐𝓑𝓒𝓓𝓔 π“•π“–π“—π“˜π“™"));
Hal Canary2d0e1242018-03-01 12:32:18 -0500311
312 const uint16_t test2[] = {
313 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0020, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A,
314 };
Hal Canaryee08b4a2018-03-01 15:56:37 -0500315 REPORTER_ASSERT(r, SkStringFromUTF16(test2, SK_ARRAY_COUNT(test2)).equals("ABCDE FGHIJ"));
Hal Canary2d0e1242018-03-01 12:32:18 -0500316
317 const uint16_t test3[] = {
318 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x0020, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA,
319 };
Hal Canaryee08b4a2018-03-01 15:56:37 -0500320 REPORTER_ASSERT(r, SkStringFromUTF16(test3, SK_ARRAY_COUNT(test3)).equals("αβγδΡ ΢ηθικ"));
Hal Canary2d0e1242018-03-01 12:32:18 -0500321}
322