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