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 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 8 | #include "SkRandom.h" |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 9 | #include "SkRefCnt.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 10 | #include "SkTSearch.h" |
| 11 | #include "SkTSort.h" |
| 12 | #include "SkUtils.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "Test.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 14 | |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 15 | class RefClass : public SkRefCnt { |
| 16 | public: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 17 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 18 | |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 19 | RefClass(int n) : fN(n) {} |
| 20 | int get() const { return fN; } |
| 21 | |
| 22 | private: |
| 23 | int fN; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 24 | |
| 25 | typedef SkRefCnt INHERITED; |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 28 | static void test_autounref(skiatest::Reporter* reporter) { |
| 29 | RefClass obj(0); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 30 | REPORTER_ASSERT(reporter, obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 31 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 32 | sk_sp<RefClass> tmp(&obj); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 33 | REPORTER_ASSERT(reporter, &obj == tmp.get()); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 34 | REPORTER_ASSERT(reporter, obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 35 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, &obj == tmp.release()); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, obj.unique()); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, nullptr == tmp.release()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | REPORTER_ASSERT(reporter, nullptr == tmp.get()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 40 | |
| 41 | obj.ref(); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 42 | REPORTER_ASSERT(reporter, !obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 43 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 44 | sk_sp<RefClass> tmp2(&obj); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 45 | } |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 46 | REPORTER_ASSERT(reporter, obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 47 | } |
| 48 | |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 49 | static void test_autostarray(skiatest::Reporter* reporter) { |
| 50 | RefClass obj0(0); |
| 51 | RefClass obj1(1); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 52 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 53 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 54 | |
| 55 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 56 | SkAutoSTArray<2, sk_sp<RefClass> > tmp; |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, 0 == tmp.count()); |
| 58 | |
| 59 | tmp.reset(0); // test out reset(0) when already at 0 |
| 60 | tmp.reset(4); // this should force a new allocation |
| 61 | REPORTER_ASSERT(reporter, 4 == tmp.count()); |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 62 | tmp[0].reset(SkRef(&obj0)); |
| 63 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 65 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 66 | |
| 67 | // test out reset with data in the array (and a new allocation) |
| 68 | tmp.reset(0); |
| 69 | REPORTER_ASSERT(reporter, 0 == tmp.count()); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 71 | REPORTER_ASSERT(reporter, obj1.unique()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 72 | |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 73 | tmp.reset(2); // this should use the preexisting allocation |
| 74 | REPORTER_ASSERT(reporter, 2 == tmp.count()); |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 75 | tmp[0].reset(SkRef(&obj0)); |
| 76 | tmp[1].reset(SkRef(&obj1)); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // test out destructor with data in the array (and using existing allocation) |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 80 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 81 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 82 | |
| 83 | { |
| 84 | // test out allocating ctor (this should allocate new memory) |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 85 | SkAutoSTArray<2, sk_sp<RefClass> > tmp(4); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 86 | REPORTER_ASSERT(reporter, 4 == tmp.count()); |
| 87 | |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 88 | tmp[0].reset(SkRef(&obj0)); |
| 89 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 90 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 91 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 92 | |
| 93 | // Test out resut with data in the array and malloced storage |
| 94 | tmp.reset(0); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 96 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 97 | |
| 98 | tmp.reset(2); // this should use the preexisting storage |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 99 | tmp[0].reset(SkRef(&obj0)); |
| 100 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 101 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 102 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 103 | |
| 104 | tmp.reset(4); // this should force a new malloc |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 106 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 107 | |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 108 | tmp[0].reset(SkRef(&obj0)); |
| 109 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 110 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 111 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 112 | } |
| 113 | |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 114 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 115 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 116 | } |
| 117 | |
epoger@google.com | bcc5683 | 2011-05-20 17:35:46 +0000 | [diff] [blame] | 118 | ///////////////////////////////////////////////////////////////////////////// |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 119 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 120 | #define kSEARCH_COUNT 91 |
| 121 | |
| 122 | static void test_search(skiatest::Reporter* reporter) { |
| 123 | int i, array[kSEARCH_COUNT]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 124 | SkRandom rand; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 125 | |
| 126 | for (i = 0; i < kSEARCH_COUNT; i++) { |
| 127 | array[i] = rand.nextS(); |
| 128 | } |
| 129 | |
| 130 | SkTHeapSort<int>(array, kSEARCH_COUNT); |
| 131 | // make sure we got sorted properly |
| 132 | for (i = 1; i < kSEARCH_COUNT; i++) { |
| 133 | REPORTER_ASSERT(reporter, array[i-1] <= array[i]); |
| 134 | } |
| 135 | |
| 136 | // make sure we can find all of our values |
| 137 | for (i = 0; i < kSEARCH_COUNT; i++) { |
| 138 | int index = SkTSearch<int>(array, kSEARCH_COUNT, array[i], sizeof(int)); |
| 139 | REPORTER_ASSERT(reporter, index == i); |
| 140 | } |
| 141 | |
| 142 | // make sure that random values are either found, or the correct |
| 143 | // insertion index is returned |
| 144 | for (i = 0; i < 10000; i++) { |
| 145 | int value = rand.nextS(); |
| 146 | int index = SkTSearch<int>(array, kSEARCH_COUNT, value, sizeof(int)); |
| 147 | |
| 148 | if (index >= 0) { |
| 149 | REPORTER_ASSERT(reporter, |
| 150 | index < kSEARCH_COUNT && array[index] == value); |
| 151 | } else { |
| 152 | index = ~index; |
| 153 | REPORTER_ASSERT(reporter, index <= kSEARCH_COUNT); |
| 154 | if (index < kSEARCH_COUNT) { |
| 155 | REPORTER_ASSERT(reporter, value < array[index]); |
| 156 | if (index > 0) { |
| 157 | REPORTER_ASSERT(reporter, value > array[index - 1]); |
| 158 | } |
| 159 | } else { |
| 160 | // we should append the new value |
| 161 | REPORTER_ASSERT(reporter, value > array[kSEARCH_COUNT - 1]); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void test_utf16(skiatest::Reporter* reporter) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 168 | // Test non-basic-multilingual-plane unicode. |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 169 | static const SkUnichar gUni[] = { |
| 170 | 0x10000, 0x18080, 0x20202, 0xFFFFF, 0x101234 |
| 171 | }; |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 172 | for (SkUnichar uni : gUni) { |
| 173 | uint16_t buf[2]; |
| 174 | size_t count = SkUTF::ToUTF16(uni, buf); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 175 | REPORTER_ASSERT(reporter, count == 2); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 176 | size_t count2 = SkUTF::CountUTF16(buf, sizeof(buf)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 177 | REPORTER_ASSERT(reporter, count2 == 1); |
| 178 | const uint16_t* ptr = buf; |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 179 | SkUnichar c = SkUTF::NextUTF16(&ptr, buf + SK_ARRAY_COUNT(buf)); |
| 180 | REPORTER_ASSERT(reporter, c == uni); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 181 | REPORTER_ASSERT(reporter, ptr - buf == 2); |
| 182 | } |
| 183 | } |
| 184 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 185 | DEF_TEST(Utils, reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 186 | static const struct { |
| 187 | const char* fUtf8; |
| 188 | SkUnichar fUni; |
| 189 | } gTest[] = { |
| 190 | { "a", 'a' }, |
| 191 | { "\x7f", 0x7f }, |
| 192 | { "\xC2\x80", 0x80 }, |
| 193 | { "\xC3\x83", (3 << 6) | 3 }, |
| 194 | { "\xDF\xBF", 0x7ff }, |
| 195 | { "\xE0\xA0\x80", 0x800 }, |
| 196 | { "\xE0\xB0\xB8", 0xC38 }, |
| 197 | { "\xE3\x83\x83", (3 << 12) | (3 << 6) | 3 }, |
| 198 | { "\xEF\xBF\xBF", 0xFFFF }, |
| 199 | { "\xF0\x90\x80\x80", 0x10000 }, |
| 200 | { "\xF3\x83\x83\x83", (3 << 18) | (3 << 12) | (3 << 6) | 3 } |
| 201 | }; |
| 202 | |
| 203 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTest); i++) { |
| 204 | const char* p = gTest[i].fUtf8; |
Hal Canary | 4014ba6 | 2018-07-24 11:33:21 -0400 | [diff] [blame] | 205 | const char* stop = p + strlen(p); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 206 | int n = SkUTF::CountUTF8(p, strlen(p)); |
| 207 | SkUnichar u1 = SkUTF::NextUTF8(&p, stop); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 208 | |
| 209 | REPORTER_ASSERT(reporter, n == 1); |
Hal Canary | 4014ba6 | 2018-07-24 11:33:21 -0400 | [diff] [blame] | 210 | REPORTER_ASSERT(reporter, u1 == gTest[i].fUni); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 211 | REPORTER_ASSERT(reporter, |
| 212 | p - gTest[i].fUtf8 == (int)strlen(gTest[i].fUtf8)); |
| 213 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 214 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 215 | test_utf16(reporter); |
| 216 | test_search(reporter); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 217 | test_autounref(reporter); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 218 | test_autostarray(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 219 | } |
Hal Canary | d1c8e56 | 2017-01-11 15:53:25 -0500 | [diff] [blame] | 220 | |
| 221 | #define ASCII_BYTE "X" |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 222 | #define CONTINUATION_BYTE "\xA1" |
| 223 | #define LEADING_TWO_BYTE "\xC2" |
| 224 | #define LEADING_THREE_BYTE "\xE1" |
Hal Canary | d1c8e56 | 2017-01-11 15:53:25 -0500 | [diff] [blame] | 225 | #define LEADING_FOUR_BYTE "\xF0" |
| 226 | #define INVALID_BYTE "\xFC" |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 227 | DEF_TEST(SkUTF_CountUTF8, r) { |
| 228 | struct { |
| 229 | int expectedCount; |
| 230 | const char* utf8String; |
| 231 | } testCases[] = { |
| 232 | { 0, "" }, |
| 233 | { 1, ASCII_BYTE }, |
| 234 | { 2, ASCII_BYTE ASCII_BYTE }, |
| 235 | { 1, LEADING_TWO_BYTE CONTINUATION_BYTE }, |
| 236 | { 2, ASCII_BYTE LEADING_TWO_BYTE CONTINUATION_BYTE }, |
| 237 | { 3, ASCII_BYTE ASCII_BYTE LEADING_TWO_BYTE CONTINUATION_BYTE }, |
| 238 | { 1, LEADING_THREE_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 239 | { 2, ASCII_BYTE LEADING_THREE_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 240 | { 3, ASCII_BYTE ASCII_BYTE LEADING_THREE_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 241 | { 1, LEADING_FOUR_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 242 | { 2, ASCII_BYTE LEADING_FOUR_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 243 | { 3, ASCII_BYTE ASCII_BYTE LEADING_FOUR_BYTE CONTINUATION_BYTE CONTINUATION_BYTE |
| 244 | CONTINUATION_BYTE }, |
| 245 | { -1, INVALID_BYTE }, |
| 246 | { -1, INVALID_BYTE CONTINUATION_BYTE }, |
| 247 | { -1, INVALID_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 248 | { -1, INVALID_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 249 | { -1, LEADING_TWO_BYTE }, |
| 250 | { -1, CONTINUATION_BYTE }, |
| 251 | { -1, CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 252 | { -1, LEADING_THREE_BYTE CONTINUATION_BYTE }, |
| 253 | { -1, CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 254 | { -1, LEADING_FOUR_BYTE CONTINUATION_BYTE }, |
| 255 | { -1, CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
Hal Canary | d1c8e56 | 2017-01-11 15:53:25 -0500 | [diff] [blame] | 256 | }; |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 257 | for (auto testCase : testCases) { |
| 258 | const char* str = testCase.utf8String; |
| 259 | REPORTER_ASSERT(r, testCase.expectedCount == SkUTF::CountUTF8(str, strlen(str))); |
Hal Canary | d1c8e56 | 2017-01-11 15:53:25 -0500 | [diff] [blame] | 260 | } |
Hal Canary | d1c8e56 | 2017-01-11 15:53:25 -0500 | [diff] [blame] | 261 | } |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 262 | |
| 263 | DEF_TEST(SkUTF_NextUTF8_ToUTF8, r) { |
| 264 | struct { |
| 265 | SkUnichar expected; |
| 266 | const char* utf8String; |
| 267 | } testCases[] = { |
| 268 | { -1, INVALID_BYTE }, |
| 269 | { -1, "" }, |
| 270 | { 0x0058, ASCII_BYTE }, |
| 271 | { 0x00A1, LEADING_TWO_BYTE CONTINUATION_BYTE }, |
| 272 | { 0x1861, LEADING_THREE_BYTE CONTINUATION_BYTE CONTINUATION_BYTE }, |
| 273 | { 0x010330, LEADING_FOUR_BYTE "\x90\x8C\xB0" }, |
| 274 | }; |
| 275 | for (auto testCase : testCases) { |
| 276 | const char* str = testCase.utf8String; |
| 277 | SkUnichar uni = SkUTF::NextUTF8(&str, str + strlen(str)); |
| 278 | REPORTER_ASSERT(r, str == testCase.utf8String + strlen(testCase.utf8String)); |
| 279 | REPORTER_ASSERT(r, uni == testCase.expected); |
| 280 | char buff[5] = {0, 0, 0, 0, 0}; |
| 281 | size_t len = SkUTF::ToUTF8(uni, buff); |
| 282 | if (buff[len] != 0) { |
| 283 | ERRORF(r, "unexpected write"); |
| 284 | continue; |
| 285 | } |
| 286 | if (uni == -1) { |
| 287 | REPORTER_ASSERT(r, len == 0); |
| 288 | continue; |
| 289 | } |
| 290 | if (len == 0) { |
| 291 | ERRORF(r, "unexpected failure."); |
| 292 | continue; |
| 293 | } |
| 294 | if (len > 4) { |
| 295 | ERRORF(r, "wrote too much"); |
| 296 | continue; |
| 297 | } |
| 298 | str = testCase.utf8String; |
| 299 | REPORTER_ASSERT(r, len == strlen(buff)); |
| 300 | REPORTER_ASSERT(r, len == strlen(str)); |
| 301 | REPORTER_ASSERT(r, 0 == strcmp(str, buff)); |
| 302 | } |
| 303 | } |
| 304 | #undef ASCII_BYTE |
| 305 | #undef CONTINUATION_BYTE |
| 306 | #undef LEADING_TWO_BYTE |
| 307 | #undef LEADING_THREE_BYTE |
| 308 | #undef LEADING_FOUR_BYTE |
| 309 | #undef INVALID_BYTE |