1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkRandom.h"
#include "SkRefCnt.h"
#include "SkTSearch.h"
#include "SkTSort.h"
#include "SkUtils.h"
#include "Test.h"
class RefClass : public SkRefCnt {
public:
RefClass(int n) : fN(n) {}
int get() const { return fN; }
private:
int fN;
typedef SkRefCnt INHERITED;
};
static void test_autounref(skiatest::Reporter* reporter) {
RefClass obj(0);
REPORTER_ASSERT(reporter, obj.unique());
sk_sp<RefClass> tmp(&obj);
REPORTER_ASSERT(reporter, &obj == tmp.get());
REPORTER_ASSERT(reporter, obj.unique());
REPORTER_ASSERT(reporter, &obj == tmp.release());
REPORTER_ASSERT(reporter, obj.unique());
REPORTER_ASSERT(reporter, nullptr == tmp.release());
REPORTER_ASSERT(reporter, nullptr == tmp.get());
obj.ref();
REPORTER_ASSERT(reporter, !obj.unique());
{
sk_sp<RefClass> tmp2(&obj);
}
REPORTER_ASSERT(reporter, obj.unique());
}
static void test_autostarray(skiatest::Reporter* reporter) {
RefClass obj0(0);
RefClass obj1(1);
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
{
SkAutoSTArray<2, sk_sp<RefClass> > tmp;
REPORTER_ASSERT(reporter, 0 == tmp.count());
tmp.reset(0); // test out reset(0) when already at 0
tmp.reset(4); // this should force a new allocation
REPORTER_ASSERT(reporter, 4 == tmp.count());
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
REPORTER_ASSERT(reporter, !obj0.unique());
REPORTER_ASSERT(reporter, !obj1.unique());
// test out reset with data in the array (and a new allocation)
tmp.reset(0);
REPORTER_ASSERT(reporter, 0 == tmp.count());
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
tmp.reset(2); // this should use the preexisting allocation
REPORTER_ASSERT(reporter, 2 == tmp.count());
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
}
// test out destructor with data in the array (and using existing allocation)
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
{
// test out allocating ctor (this should allocate new memory)
SkAutoSTArray<2, sk_sp<RefClass> > tmp(4);
REPORTER_ASSERT(reporter, 4 == tmp.count());
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
REPORTER_ASSERT(reporter, !obj0.unique());
REPORTER_ASSERT(reporter, !obj1.unique());
// Test out resut with data in the array and malloced storage
tmp.reset(0);
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
tmp.reset(2); // this should use the preexisting storage
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
REPORTER_ASSERT(reporter, !obj0.unique());
REPORTER_ASSERT(reporter, !obj1.unique());
tmp.reset(4); // this should force a new malloc
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
REPORTER_ASSERT(reporter, !obj0.unique());
REPORTER_ASSERT(reporter, !obj1.unique());
}
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
}
/////////////////////////////////////////////////////////////////////////////
#define kSEARCH_COUNT 91
static void test_search(skiatest::Reporter* reporter) {
int i, array[kSEARCH_COUNT];
SkRandom rand;
for (i = 0; i < kSEARCH_COUNT; i++) {
array[i] = rand.nextS();
}
SkTHeapSort<int>(array, kSEARCH_COUNT);
// make sure we got sorted properly
for (i = 1; i < kSEARCH_COUNT; i++) {
REPORTER_ASSERT(reporter, array[i-1] <= array[i]);
}
// make sure we can find all of our values
for (i = 0; i < kSEARCH_COUNT; i++) {
int index = SkTSearch<int>(array, kSEARCH_COUNT, array[i], sizeof(int));
REPORTER_ASSERT(reporter, index == i);
}
// make sure that random values are either found, or the correct
// insertion index is returned
for (i = 0; i < 10000; i++) {
int value = rand.nextS();
int index = SkTSearch<int>(array, kSEARCH_COUNT, value, sizeof(int));
if (index >= 0) {
REPORTER_ASSERT(reporter,
index < kSEARCH_COUNT && array[index] == value);
} else {
index = ~index;
REPORTER_ASSERT(reporter, index <= kSEARCH_COUNT);
if (index < kSEARCH_COUNT) {
REPORTER_ASSERT(reporter, value < array[index]);
if (index > 0) {
REPORTER_ASSERT(reporter, value > array[index - 1]);
}
} else {
// we should append the new value
REPORTER_ASSERT(reporter, value > array[kSEARCH_COUNT - 1]);
}
}
}
}
static void test_utf16(skiatest::Reporter* reporter) {
static const SkUnichar gUni[] = {
0x10000, 0x18080, 0x20202, 0xFFFFF, 0x101234
};
uint16_t buf[2];
for (size_t i = 0; i < SK_ARRAY_COUNT(gUni); i++) {
size_t count = SkUTF16_FromUnichar(gUni[i], buf);
REPORTER_ASSERT(reporter, count == 2);
size_t count2 = SkUTF16_CountUnichars(buf, 2 * sizeof(uint16_t));
REPORTER_ASSERT(reporter, count2 == 1);
const uint16_t* ptr = buf;
SkUnichar c = SkUTF16_NextUnichar(&ptr);
REPORTER_ASSERT(reporter, c == gUni[i]);
REPORTER_ASSERT(reporter, ptr - buf == 2);
}
}
DEF_TEST(Utils, reporter) {
static const struct {
const char* fUtf8;
SkUnichar fUni;
} gTest[] = {
{ "a", 'a' },
{ "\x7f", 0x7f },
{ "\xC2\x80", 0x80 },
{ "\xC3\x83", (3 << 6) | 3 },
{ "\xDF\xBF", 0x7ff },
{ "\xE0\xA0\x80", 0x800 },
{ "\xE0\xB0\xB8", 0xC38 },
{ "\xE3\x83\x83", (3 << 12) | (3 << 6) | 3 },
{ "\xEF\xBF\xBF", 0xFFFF },
{ "\xF0\x90\x80\x80", 0x10000 },
{ "\xF3\x83\x83\x83", (3 << 18) | (3 << 12) | (3 << 6) | 3 }
};
for (size_t i = 0; i < SK_ARRAY_COUNT(gTest); i++) {
const char* p = gTest[i].fUtf8;
int n = SkUTF8_CountUnichars(p);
SkUnichar u0 = SkUTF8_ToUnichar(gTest[i].fUtf8);
SkUnichar u1 = SkUTF8_NextUnichar(&p);
REPORTER_ASSERT(reporter, n == 1);
REPORTER_ASSERT(reporter, u0 == u1);
REPORTER_ASSERT(reporter, u0 == gTest[i].fUni);
REPORTER_ASSERT(reporter,
p - gTest[i].fUtf8 == (int)strlen(gTest[i].fUtf8));
}
test_utf16(reporter);
test_search(reporter);
test_autounref(reporter);
test_autostarray(reporter);
}
#define ASCII_BYTE "X"
#define CONTINUATION_BYTE "\x80"
#define LEADING_TWO_BYTE "\xC4"
#define LEADING_THREE_BYTE "\xE0"
#define LEADING_FOUR_BYTE "\xF0"
#define INVALID_BYTE "\xFC"
static bool valid_utf8(const char* p, size_t l) {
return SkUTF8_CountUnichars(p, l) >= 0;
}
DEF_TEST(Utils_UTF8_ValidLength, r) {
const char* goodTestcases[] = {
"",
ASCII_BYTE,
ASCII_BYTE ASCII_BYTE,
LEADING_TWO_BYTE CONTINUATION_BYTE,
ASCII_BYTE LEADING_TWO_BYTE CONTINUATION_BYTE,
ASCII_BYTE ASCII_BYTE LEADING_TWO_BYTE CONTINUATION_BYTE,
LEADING_THREE_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
ASCII_BYTE LEADING_THREE_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
ASCII_BYTE ASCII_BYTE LEADING_THREE_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
LEADING_FOUR_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
ASCII_BYTE LEADING_FOUR_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
ASCII_BYTE ASCII_BYTE LEADING_FOUR_BYTE CONTINUATION_BYTE CONTINUATION_BYTE
CONTINUATION_BYTE,
};
for (const char* testcase : goodTestcases) {
REPORTER_ASSERT(r, valid_utf8(testcase, strlen(testcase)));
}
const char* badTestcases[] = {
INVALID_BYTE,
INVALID_BYTE CONTINUATION_BYTE,
INVALID_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
INVALID_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
LEADING_TWO_BYTE,
CONTINUATION_BYTE,
CONTINUATION_BYTE CONTINUATION_BYTE,
LEADING_THREE_BYTE CONTINUATION_BYTE,
CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
LEADING_FOUR_BYTE CONTINUATION_BYTE,
CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
ASCII_BYTE INVALID_BYTE,
ASCII_BYTE INVALID_BYTE CONTINUATION_BYTE,
ASCII_BYTE INVALID_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
ASCII_BYTE INVALID_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
ASCII_BYTE LEADING_TWO_BYTE,
ASCII_BYTE CONTINUATION_BYTE,
ASCII_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
ASCII_BYTE LEADING_THREE_BYTE CONTINUATION_BYTE,
ASCII_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
ASCII_BYTE LEADING_FOUR_BYTE CONTINUATION_BYTE,
ASCII_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE CONTINUATION_BYTE,
// LEADING_FOUR_BYTE LEADING_TWO_BYTE CONTINUATION_BYTE,
};
for (const char* testcase : badTestcases) {
REPORTER_ASSERT(r, !valid_utf8(testcase, strlen(testcase)));
}
}
|