epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
tomhudson@google.com | 47e0a09 | 2011-07-08 17:49:22 +0000 | [diff] [blame] | 8 | #include <stdarg.h> |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 9 | #include <stdio.h> |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "SkString.h" |
Ben Wagner | af89366 | 2017-10-03 11:08:14 -0400 | [diff] [blame] | 11 | #include "SkThreadUtils.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "Test.h" |
tomhudson@google.com | 47e0a09 | 2011-07-08 17:49:22 +0000 | [diff] [blame] | 13 | |
| 14 | // Windows vsnprintf doesn't 0-terminate safely), but is so far |
| 15 | // encapsulated in SkString that we can't test it directly. |
| 16 | |
| 17 | #ifdef SK_BUILD_FOR_WIN |
| 18 | #define VSNPRINTF(buffer, size, format, args) \ |
| 19 | vsnprintf_s(buffer, size, _TRUNCATE, format, args) |
| 20 | #else |
| 21 | #define VSNPRINTF vsnprintf |
| 22 | #endif |
| 23 | |
| 24 | #define ARGS_TO_BUFFER(format, buffer, size) \ |
| 25 | do { \ |
| 26 | va_list args; \ |
| 27 | va_start(args, format); \ |
| 28 | VSNPRINTF(buffer, size, format, args); \ |
| 29 | va_end(args); \ |
| 30 | } while (0) |
| 31 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 32 | static void printfAnalog(char* buffer, int size, const char format[], ...) { |
tomhudson@google.com | 47e0a09 | 2011-07-08 17:49:22 +0000 | [diff] [blame] | 33 | ARGS_TO_BUFFER(format, buffer, size); |
| 34 | } |
| 35 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 36 | DEF_TEST(String, reporter) { |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 37 | SkString a; |
| 38 | SkString b((size_t)0); |
| 39 | SkString c(""); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | SkString d(nullptr, 0); |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 41 | |
| 42 | REPORTER_ASSERT(reporter, a.isEmpty()); |
| 43 | REPORTER_ASSERT(reporter, a == b && a == c && a == d); |
| 44 | |
| 45 | a.set("hello"); |
| 46 | b.set("hellox", 5); |
| 47 | c.set(a); |
| 48 | d.resize(5); |
| 49 | memcpy(d.writable_str(), "helloz", 5); |
| 50 | |
| 51 | REPORTER_ASSERT(reporter, !a.isEmpty()); |
| 52 | REPORTER_ASSERT(reporter, a.size() == 5); |
| 53 | REPORTER_ASSERT(reporter, a == b && a == c && a == d); |
| 54 | REPORTER_ASSERT(reporter, a.equals("hello", 5)); |
| 55 | REPORTER_ASSERT(reporter, a.equals("hello")); |
| 56 | REPORTER_ASSERT(reporter, !a.equals("help")); |
| 57 | |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, a.startsWith("hell")); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, a.startsWith('h')); |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 60 | REPORTER_ASSERT(reporter, !a.startsWith( "ell")); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, !a.startsWith( 'e')); |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, a.startsWith("")); |
| 63 | REPORTER_ASSERT(reporter, a.endsWith("llo")); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, a.endsWith('o')); |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 65 | REPORTER_ASSERT(reporter, !a.endsWith("ll" )); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 66 | REPORTER_ASSERT(reporter, !a.endsWith('l')); |
epoger@google.com | c4ae974 | 2012-04-27 17:11:31 +0000 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, a.endsWith("")); |
| 68 | REPORTER_ASSERT(reporter, a.contains("he")); |
| 69 | REPORTER_ASSERT(reporter, a.contains("ll")); |
| 70 | REPORTER_ASSERT(reporter, a.contains("lo")); |
| 71 | REPORTER_ASSERT(reporter, a.contains("hello")); |
| 72 | REPORTER_ASSERT(reporter, !a.contains("hellohello")); |
| 73 | REPORTER_ASSERT(reporter, a.contains("")); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, a.contains('e')); |
| 75 | REPORTER_ASSERT(reporter, !a.contains('z')); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 76 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 77 | SkString e(a); |
| 78 | SkString f("hello"); |
| 79 | SkString g("helloz", 5); |
| 80 | |
| 81 | REPORTER_ASSERT(reporter, a == e && a == f && a == g); |
| 82 | |
| 83 | b.set("world"); |
| 84 | c = b; |
| 85 | REPORTER_ASSERT(reporter, a != b && a != c && b == c); |
| 86 | |
| 87 | a.append(" world"); |
| 88 | e.append("worldz", 5); |
| 89 | e.insert(5, " "); |
| 90 | f.set("world"); |
| 91 | f.prepend("hello "); |
| 92 | REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f); |
| 93 | |
| 94 | a.reset(); |
| 95 | b.resize(0); |
| 96 | REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b); |
| 97 | |
| 98 | a.set("a"); |
| 99 | a.set("ab"); |
| 100 | a.set("abc"); |
| 101 | a.set("abcd"); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 102 | |
| 103 | a.set(""); |
epoger@google.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 104 | a.appendS32(0x7FFFFFFFL); |
| 105 | REPORTER_ASSERT(reporter, a.equals("2147483647")); |
| 106 | a.set(""); |
| 107 | a.appendS32(0x80000001L); |
| 108 | REPORTER_ASSERT(reporter, a.equals("-2147483647")); |
| 109 | a.set(""); |
| 110 | a.appendS32(0x80000000L); |
| 111 | REPORTER_ASSERT(reporter, a.equals("-2147483648")); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 112 | |
| 113 | a.set(""); |
epoger@google.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 114 | a.appendU32(0x7FFFFFFFUL); |
| 115 | REPORTER_ASSERT(reporter, a.equals("2147483647")); |
| 116 | a.set(""); |
| 117 | a.appendU32(0x80000001UL); |
| 118 | REPORTER_ASSERT(reporter, a.equals("2147483649")); |
| 119 | a.set(""); |
| 120 | a.appendU32(0xFFFFFFFFUL); |
| 121 | REPORTER_ASSERT(reporter, a.equals("4294967295")); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 122 | |
| 123 | a.set(""); |
epoger@google.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 124 | a.appendS64(0x7FFFFFFFFFFFFFFFLL, 0); |
| 125 | REPORTER_ASSERT(reporter, a.equals("9223372036854775807")); |
| 126 | a.set(""); |
| 127 | a.appendS64(0x8000000000000001LL, 0); |
| 128 | REPORTER_ASSERT(reporter, a.equals("-9223372036854775807")); |
| 129 | a.set(""); |
| 130 | a.appendS64(0x8000000000000000LL, 0); |
| 131 | REPORTER_ASSERT(reporter, a.equals("-9223372036854775808")); |
| 132 | a.set(""); |
| 133 | a.appendS64(0x0000000001000000LL, 15); |
| 134 | REPORTER_ASSERT(reporter, a.equals("000000016777216")); |
| 135 | a.set(""); |
| 136 | a.appendS64(0xFFFFFFFFFF000000LL, 15); |
| 137 | REPORTER_ASSERT(reporter, a.equals("-000000016777216")); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 138 | |
| 139 | a.set(""); |
epoger@google.com | d88a3d8 | 2013-06-19 18:27:20 +0000 | [diff] [blame] | 140 | a.appendU64(0x7FFFFFFFFFFFFFFFULL, 0); |
| 141 | REPORTER_ASSERT(reporter, a.equals("9223372036854775807")); |
| 142 | a.set(""); |
| 143 | a.appendU64(0x8000000000000001ULL, 0); |
| 144 | REPORTER_ASSERT(reporter, a.equals("9223372036854775809")); |
| 145 | a.set(""); |
| 146 | a.appendU64(0xFFFFFFFFFFFFFFFFULL, 0); |
| 147 | REPORTER_ASSERT(reporter, a.equals("18446744073709551615")); |
| 148 | a.set(""); |
| 149 | a.appendU64(0x0000000001000000ULL, 15); |
| 150 | REPORTER_ASSERT(reporter, a.equals("000000016777216")); |
reed@google.com | fa06e52 | 2011-02-28 21:29:58 +0000 | [diff] [blame] | 151 | |
halcanary | d51bdae | 2016-04-25 09:25:35 -0700 | [diff] [blame] | 152 | a.printf("%i", 0); |
| 153 | REPORTER_ASSERT(reporter, a.equals("0")); |
| 154 | a.printf("%g", 3.14); |
| 155 | REPORTER_ASSERT(reporter, a.equals("3.14")); |
| 156 | a.printf("hello %s", "skia"); |
| 157 | REPORTER_ASSERT(reporter, a.equals("hello skia")); |
| 158 | |
reed@google.com | fa06e52 | 2011-02-28 21:29:58 +0000 | [diff] [blame] | 159 | static const struct { |
| 160 | SkScalar fValue; |
| 161 | const char* fString; |
| 162 | } gRec[] = { |
| 163 | { 0, "0" }, |
| 164 | { SK_Scalar1, "1" }, |
| 165 | { -SK_Scalar1, "-1" }, |
| 166 | { SK_Scalar1/2, "0.5" }, |
mtklein | cc334b3 | 2015-09-22 11:43:53 -0700 | [diff] [blame] | 167 | #if defined(SK_BUILD_FOR_WIN) && (_MSC_VER < 1900) |
tomhudson@google.com | 47e0a09 | 2011-07-08 17:49:22 +0000 | [diff] [blame] | 168 | { 3.4028234e38f, "3.4028235e+038" }, |
| 169 | { -3.4028234e38f, "-3.4028235e+038" }, |
| 170 | #else |
reed@google.com | 8072e4f | 2011-03-01 15:44:08 +0000 | [diff] [blame] | 171 | { 3.4028234e38f, "3.4028235e+38" }, |
| 172 | { -3.4028234e38f, "-3.4028235e+38" }, |
tomhudson@google.com | 47e0a09 | 2011-07-08 17:49:22 +0000 | [diff] [blame] | 173 | #endif |
reed@google.com | fa06e52 | 2011-02-28 21:29:58 +0000 | [diff] [blame] | 174 | }; |
| 175 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 176 | a.reset(); |
| 177 | a.appendScalar(gRec[i].fValue); |
reed@google.com | 8072e4f | 2011-03-01 15:44:08 +0000 | [diff] [blame] | 178 | REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize); |
bsalomon | 9bca526 | 2015-08-06 17:56:13 -0700 | [diff] [blame] | 179 | if (!a.equals(gRec[i].fString)) { |
| 180 | ERRORF(reporter, "received <%s> expected <%s>\n", a.c_str(), gRec[i].fString); |
| 181 | } |
reed@google.com | fa06e52 | 2011-02-28 21:29:58 +0000 | [diff] [blame] | 182 | } |
tomhudson@google.com | 3a1f6a0 | 2011-06-30 14:39:52 +0000 | [diff] [blame] | 183 | |
| 184 | REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0")); |
tomhudson@google.com | 47e0a09 | 2011-07-08 17:49:22 +0000 | [diff] [blame] | 185 | |
| 186 | char buffer [40]; |
| 187 | memset(buffer, 'a', 40); |
| 188 | REPORTER_ASSERT(reporter, buffer[18] == 'a'); |
| 189 | REPORTER_ASSERT(reporter, buffer[19] == 'a'); |
| 190 | REPORTER_ASSERT(reporter, buffer[20] == 'a'); |
| 191 | printfAnalog(buffer, 20, "%30d", 0); |
| 192 | REPORTER_ASSERT(reporter, buffer[18] == ' '); |
| 193 | REPORTER_ASSERT(reporter, buffer[19] == 0); |
| 194 | REPORTER_ASSERT(reporter, buffer[20] == 'a'); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 195 | |
halcanary | d51bdae | 2016-04-25 09:25:35 -0700 | [diff] [blame] | 196 | REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0")); |
| 197 | |
| 198 | // 2000 is larger than the static buffer size inside SkString.cpp |
| 199 | a = SkStringPrintf("%2000s", " "); |
| 200 | REPORTER_ASSERT(reporter, a.size() == 2000); |
| 201 | for (size_t i = 0; i < a.size(); ++i) { |
| 202 | if (a[i] != ' ') { |
| 203 | ERRORF(reporter, "SkStringPrintf fail: a[%d] = '%c'", i, a[i]); |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | a.reset(); |
| 208 | a.printf("%2000s", " "); |
| 209 | REPORTER_ASSERT(reporter, a.size() == 2000); |
| 210 | for (size_t i = 0; i < a.size(); ++i) { |
| 211 | if (a[i] != ' ') { |
| 212 | ERRORF(reporter, "SkStringPrintf fail: a[%d] = '%c'", i, a[i]); |
| 213 | break; |
| 214 | } |
| 215 | } |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 216 | } |
| 217 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 218 | DEF_TEST(String_SkStrSplit, r) { |
| 219 | SkTArray<SkString> results; |
| 220 | |
| 221 | SkStrSplit("a-_b_c-dee--f-_-_-g-", "-_", &results); |
| 222 | REPORTER_ASSERT(r, results.count() == 6); |
| 223 | REPORTER_ASSERT(r, results[0].equals("a")); |
| 224 | REPORTER_ASSERT(r, results[1].equals("b")); |
| 225 | REPORTER_ASSERT(r, results[2].equals("c")); |
| 226 | REPORTER_ASSERT(r, results[3].equals("dee")); |
| 227 | REPORTER_ASSERT(r, results[4].equals("f")); |
| 228 | REPORTER_ASSERT(r, results[5].equals("g")); |
mtklein | cc334b3 | 2015-09-22 11:43:53 -0700 | [diff] [blame] | 229 | |
| 230 | results.reset(); |
| 231 | SkStrSplit("\n", "\n", &results); |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 232 | REPORTER_ASSERT(r, results.count() == 0); |
mtklein | cc334b3 | 2015-09-22 11:43:53 -0700 | [diff] [blame] | 233 | |
| 234 | results.reset(); |
| 235 | SkStrSplit("", "\n", &results); |
| 236 | REPORTER_ASSERT(r, results.count() == 0); |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 237 | |
| 238 | results.reset(); |
| 239 | SkStrSplit("a", "\n", &results); |
| 240 | REPORTER_ASSERT(r, results.count() == 1); |
| 241 | REPORTER_ASSERT(r, results[0].equals("a")); |
| 242 | } |
| 243 | DEF_TEST(String_SkStrSplit_All, r) { |
| 244 | SkTArray<SkString> results; |
| 245 | SkStrSplit("a-_b_c-dee--f-_-_-g-", "-_", kStrict_SkStrSplitMode, &results); |
| 246 | REPORTER_ASSERT(r, results.count() == 13); |
| 247 | REPORTER_ASSERT(r, results[0].equals("a")); |
| 248 | REPORTER_ASSERT(r, results[1].equals("")); |
| 249 | REPORTER_ASSERT(r, results[2].equals("b")); |
| 250 | REPORTER_ASSERT(r, results[3].equals("c")); |
| 251 | REPORTER_ASSERT(r, results[4].equals("dee")); |
| 252 | REPORTER_ASSERT(r, results[5].equals("")); |
| 253 | REPORTER_ASSERT(r, results[6].equals("f")); |
| 254 | REPORTER_ASSERT(r, results[7].equals("")); |
| 255 | REPORTER_ASSERT(r, results[8].equals("")); |
| 256 | REPORTER_ASSERT(r, results[9].equals("")); |
| 257 | REPORTER_ASSERT(r, results[10].equals("")); |
| 258 | REPORTER_ASSERT(r, results[11].equals("g")); |
| 259 | REPORTER_ASSERT(r, results[12].equals("")); |
| 260 | |
| 261 | results.reset(); |
| 262 | SkStrSplit("\n", "\n", kStrict_SkStrSplitMode, &results); |
| 263 | REPORTER_ASSERT(r, results.count() == 2); |
| 264 | REPORTER_ASSERT(r, results[0].equals("")); |
| 265 | REPORTER_ASSERT(r, results[1].equals("")); |
| 266 | |
| 267 | results.reset(); |
| 268 | SkStrSplit("", "\n", kStrict_SkStrSplitMode, &results); |
| 269 | REPORTER_ASSERT(r, results.count() == 0); |
| 270 | |
| 271 | results.reset(); |
| 272 | SkStrSplit("a", "\n", kStrict_SkStrSplitMode, &results); |
| 273 | REPORTER_ASSERT(r, results.count() == 1); |
| 274 | REPORTER_ASSERT(r, results[0].equals("a")); |
| 275 | |
| 276 | results.reset(); |
| 277 | SkStrSplit(",,", ",", kStrict_SkStrSplitMode, &results); |
| 278 | REPORTER_ASSERT(r, results.count() == 3); |
| 279 | REPORTER_ASSERT(r, results[0].equals("")); |
| 280 | REPORTER_ASSERT(r, results[1].equals("")); |
| 281 | REPORTER_ASSERT(r, results[2].equals("")); |
| 282 | |
| 283 | results.reset(); |
| 284 | SkStrSplit(",a,b,", ",", kStrict_SkStrSplitMode, &results); |
| 285 | REPORTER_ASSERT(r, results.count() == 4); |
| 286 | REPORTER_ASSERT(r, results[0].equals("")); |
| 287 | REPORTER_ASSERT(r, results[1].equals("a")); |
| 288 | REPORTER_ASSERT(r, results[2].equals("b")); |
| 289 | REPORTER_ASSERT(r, results[3].equals("")); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 290 | } |
Ben Wagner | af89366 | 2017-10-03 11:08:14 -0400 | [diff] [blame] | 291 | |
| 292 | // https://bugs.chromium.org/p/skia/issues/detail?id=7107 |
| 293 | DEF_TEST(String_Threaded, r) { |
| 294 | SkString gString("foo"); |
| 295 | SkThread::entryPointProc readString = [](void* context) -> void { |
| 296 | SkString gStringCopy = *reinterpret_cast<SkString*>(context); |
| 297 | bool equals_string = gStringCopy.equals("test"); |
| 298 | (void)equals_string; |
| 299 | }; |
| 300 | SkThread threads[] = { |
| 301 | {readString, &gString}, |
| 302 | {readString, &gString}, |
| 303 | {readString, &gString}, |
| 304 | {readString, &gString}, |
| 305 | {readString, &gString}, |
| 306 | }; |
| 307 | for (SkThread& thread : threads) { |
| 308 | thread.start(); |
| 309 | } |
| 310 | for (SkThread& thread : threads) { |
| 311 | thread.join(); |
| 312 | } |
| 313 | } |