blob: c681a2236ea3b159eb8c8098a63f20dd4677e456 [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 "include/core/SkRefCnt.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/utils/SkRandom.h"
Herb Derbycbd235c2019-07-03 18:05:12 -040010#include "src/core/SkEnumerate.h"
Herb Derby10e48d42019-08-08 12:30:13 -040011#include "src/core/SkSpan.h"
Ben Wagner8bd6e8f2019-05-15 09:28:52 -040012#include "src/core/SkTSearch.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkTSort.h"
Herb Derbyc44ee1a2019-09-09 11:36:39 -040014#include "src/core/SkZip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
reed@android.comed673312009-02-27 16:24:51 +000016
Herb Derby10e48d42019-08-08 12:30:13 -040017#include <array>
Herb Derbyc44ee1a2019-09-09 11:36:39 -040018#include <initializer_list>
19#include <tuple>
Herb Derby10e48d42019-08-08 12:30:13 -040020#include <vector>
21
reed@android.comb00cd722010-04-16 20:35:47 +000022class RefClass : public SkRefCnt {
23public:
24 RefClass(int n) : fN(n) {}
25 int get() const { return fN; }
26
27private:
28 int fN;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000029
30 typedef SkRefCnt INHERITED;
reed@android.comb00cd722010-04-16 20:35:47 +000031};
32
reed@google.coma67573e2011-02-25 18:10:29 +000033static void test_autounref(skiatest::Reporter* reporter) {
34 RefClass obj(0);
mtkleinbbb61d72014-11-24 13:09:39 -080035 REPORTER_ASSERT(reporter, obj.unique());
reed@google.coma67573e2011-02-25 18:10:29 +000036
Hal Canary342b7ac2016-11-04 11:49:42 -040037 sk_sp<RefClass> tmp(&obj);
reed@google.coma67573e2011-02-25 18:10:29 +000038 REPORTER_ASSERT(reporter, &obj == tmp.get());
mtkleinbbb61d72014-11-24 13:09:39 -080039 REPORTER_ASSERT(reporter, obj.unique());
reed@google.coma67573e2011-02-25 18:10:29 +000040
mtklein18300a32016-03-16 13:53:35 -070041 REPORTER_ASSERT(reporter, &obj == tmp.release());
mtkleinbbb61d72014-11-24 13:09:39 -080042 REPORTER_ASSERT(reporter, obj.unique());
mtklein18300a32016-03-16 13:53:35 -070043 REPORTER_ASSERT(reporter, nullptr == tmp.release());
halcanary96fcdcc2015-08-27 07:41:13 -070044 REPORTER_ASSERT(reporter, nullptr == tmp.get());
reed@google.coma67573e2011-02-25 18:10:29 +000045
46 obj.ref();
mtkleinbbb61d72014-11-24 13:09:39 -080047 REPORTER_ASSERT(reporter, !obj.unique());
reed@google.coma67573e2011-02-25 18:10:29 +000048 {
Hal Canary342b7ac2016-11-04 11:49:42 -040049 sk_sp<RefClass> tmp2(&obj);
reed@google.coma67573e2011-02-25 18:10:29 +000050 }
mtkleinbbb61d72014-11-24 13:09:39 -080051 REPORTER_ASSERT(reporter, obj.unique());
reed@google.coma67573e2011-02-25 18:10:29 +000052}
53
robertphillips@google.com4d376732013-07-12 18:44:23 +000054static void test_autostarray(skiatest::Reporter* reporter) {
55 RefClass obj0(0);
56 RefClass obj1(1);
mtkleinbbb61d72014-11-24 13:09:39 -080057 REPORTER_ASSERT(reporter, obj0.unique());
58 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +000059
60 {
Hal Canary342b7ac2016-11-04 11:49:42 -040061 SkAutoSTArray<2, sk_sp<RefClass> > tmp;
robertphillips@google.com4d376732013-07-12 18:44:23 +000062 REPORTER_ASSERT(reporter, 0 == tmp.count());
63
64 tmp.reset(0); // test out reset(0) when already at 0
65 tmp.reset(4); // this should force a new allocation
66 REPORTER_ASSERT(reporter, 4 == tmp.count());
bungeman733418f2014-07-17 12:17:55 -070067 tmp[0].reset(SkRef(&obj0));
68 tmp[1].reset(SkRef(&obj1));
mtkleinbbb61d72014-11-24 13:09:39 -080069 REPORTER_ASSERT(reporter, !obj0.unique());
70 REPORTER_ASSERT(reporter, !obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +000071
72 // test out reset with data in the array (and a new allocation)
73 tmp.reset(0);
74 REPORTER_ASSERT(reporter, 0 == tmp.count());
mtkleinbbb61d72014-11-24 13:09:39 -080075 REPORTER_ASSERT(reporter, obj0.unique());
76 REPORTER_ASSERT(reporter, obj1.unique());
humper@google.com9c96d4b2013-07-14 01:44:59 +000077
robertphillips@google.com4d376732013-07-12 18:44:23 +000078 tmp.reset(2); // this should use the preexisting allocation
79 REPORTER_ASSERT(reporter, 2 == tmp.count());
bungeman733418f2014-07-17 12:17:55 -070080 tmp[0].reset(SkRef(&obj0));
81 tmp[1].reset(SkRef(&obj1));
robertphillips@google.com4d376732013-07-12 18:44:23 +000082 }
83
84 // test out destructor with data in the array (and using existing allocation)
mtkleinbbb61d72014-11-24 13:09:39 -080085 REPORTER_ASSERT(reporter, obj0.unique());
86 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +000087
88 {
89 // test out allocating ctor (this should allocate new memory)
Hal Canary342b7ac2016-11-04 11:49:42 -040090 SkAutoSTArray<2, sk_sp<RefClass> > tmp(4);
robertphillips@google.com4d376732013-07-12 18:44:23 +000091 REPORTER_ASSERT(reporter, 4 == tmp.count());
92
bungeman733418f2014-07-17 12:17:55 -070093 tmp[0].reset(SkRef(&obj0));
94 tmp[1].reset(SkRef(&obj1));
mtkleinbbb61d72014-11-24 13:09:39 -080095 REPORTER_ASSERT(reporter, !obj0.unique());
96 REPORTER_ASSERT(reporter, !obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +000097
98 // Test out resut with data in the array and malloced storage
99 tmp.reset(0);
mtkleinbbb61d72014-11-24 13:09:39 -0800100 REPORTER_ASSERT(reporter, obj0.unique());
101 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +0000102
103 tmp.reset(2); // this should use the preexisting storage
bungeman733418f2014-07-17 12:17:55 -0700104 tmp[0].reset(SkRef(&obj0));
105 tmp[1].reset(SkRef(&obj1));
mtkleinbbb61d72014-11-24 13:09:39 -0800106 REPORTER_ASSERT(reporter, !obj0.unique());
107 REPORTER_ASSERT(reporter, !obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +0000108
109 tmp.reset(4); // this should force a new malloc
mtkleinbbb61d72014-11-24 13:09:39 -0800110 REPORTER_ASSERT(reporter, obj0.unique());
111 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +0000112
bungeman733418f2014-07-17 12:17:55 -0700113 tmp[0].reset(SkRef(&obj0));
114 tmp[1].reset(SkRef(&obj1));
mtkleinbbb61d72014-11-24 13:09:39 -0800115 REPORTER_ASSERT(reporter, !obj0.unique());
116 REPORTER_ASSERT(reporter, !obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +0000117 }
118
mtkleinbbb61d72014-11-24 13:09:39 -0800119 REPORTER_ASSERT(reporter, obj0.unique());
120 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +0000121}
122
epoger@google.combcc56832011-05-20 17:35:46 +0000123/////////////////////////////////////////////////////////////////////////////
reed@android.comb00cd722010-04-16 20:35:47 +0000124
reed@android.comed673312009-02-27 16:24:51 +0000125#define kSEARCH_COUNT 91
126
127static void test_search(skiatest::Reporter* reporter) {
128 int i, array[kSEARCH_COUNT];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000129 SkRandom rand;
reed@android.comed673312009-02-27 16:24:51 +0000130
131 for (i = 0; i < kSEARCH_COUNT; i++) {
132 array[i] = rand.nextS();
133 }
134
135 SkTHeapSort<int>(array, kSEARCH_COUNT);
136 // make sure we got sorted properly
137 for (i = 1; i < kSEARCH_COUNT; i++) {
138 REPORTER_ASSERT(reporter, array[i-1] <= array[i]);
139 }
140
141 // make sure we can find all of our values
142 for (i = 0; i < kSEARCH_COUNT; i++) {
143 int index = SkTSearch<int>(array, kSEARCH_COUNT, array[i], sizeof(int));
144 REPORTER_ASSERT(reporter, index == i);
145 }
146
147 // make sure that random values are either found, or the correct
148 // insertion index is returned
149 for (i = 0; i < 10000; i++) {
150 int value = rand.nextS();
151 int index = SkTSearch<int>(array, kSEARCH_COUNT, value, sizeof(int));
152
153 if (index >= 0) {
154 REPORTER_ASSERT(reporter,
155 index < kSEARCH_COUNT && array[index] == value);
156 } else {
157 index = ~index;
158 REPORTER_ASSERT(reporter, index <= kSEARCH_COUNT);
159 if (index < kSEARCH_COUNT) {
160 REPORTER_ASSERT(reporter, value < array[index]);
161 if (index > 0) {
162 REPORTER_ASSERT(reporter, value > array[index - 1]);
163 }
164 } else {
165 // we should append the new value
166 REPORTER_ASSERT(reporter, value > array[kSEARCH_COUNT - 1]);
167 }
168 }
169 }
170}
171
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000172DEF_TEST(Utils, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000173 test_search(reporter);
reed@google.coma67573e2011-02-25 18:10:29 +0000174 test_autounref(reporter);
robertphillips@google.com4d376732013-07-12 18:44:23 +0000175 test_autostarray(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000176}
Herb Derby10e48d42019-08-08 12:30:13 -0400177
178DEF_TEST(SkMakeSpan, reporter) {
179 // Test constness preservation for SkMakeSpan.
180 {
181 std::vector<int> v = {{1, 2, 3, 4, 5}};
182 auto s = SkMakeSpan(v);
183 REPORTER_ASSERT(reporter, s[3] == 4);
184 s[3] = 100;
185 REPORTER_ASSERT(reporter, s[3] == 100);
186 }
187
188 {
189 std::vector<int> t = {{1, 2, 3, 4, 5}};
190 const std::vector<int>& v = t;
191 auto s = SkMakeSpan(v);
192 //s[3] = 100; // Should fail to compile
193 REPORTER_ASSERT(reporter, s[3] == 4);
194 REPORTER_ASSERT(reporter, t[3] == 4);
195 t[3] = 100;
196 REPORTER_ASSERT(reporter, s[3] == 100);
197 }
198
199 {
200 std::array<int, 5> v = {{1, 2, 3, 4, 5}};
201 auto s = SkMakeSpan(v);
202 REPORTER_ASSERT(reporter, s[3] == 4);
203 s[3] = 100;
204 REPORTER_ASSERT(reporter, s[3] == 100);
205 }
206
207 {
208 std::array<int, 5> t = {{1, 2, 3, 4, 5}};
209 const std::array<int, 5>& v = t;
210 auto s = SkMakeSpan(v);
211 //s[3] = 100; // Should fail to compile
212 REPORTER_ASSERT(reporter, s[3] == 4);
213 REPORTER_ASSERT(reporter, t[3] == 4);
214 t[3] = 100;
215 REPORTER_ASSERT(reporter, s[3] == 100);
216 }
Herb Derby986ab2c2019-09-19 11:27:43 -0400217
218 {
219 std::vector<int> v;
220 auto s = SkMakeSpan(v);
221 REPORTER_ASSERT(reporter, s.empty());
222 }
Herb Derby10e48d42019-08-08 12:30:13 -0400223}
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400224
Herb Derbycbd235c2019-07-03 18:05:12 -0400225DEF_TEST(SkEnumerate, reporter) {
226
227 int A[] = {1, 2, 3, 4};
228 auto enumeration = SkMakeEnumerate(A);
229
230 size_t check = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500231 for (auto [i, v] : enumeration) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400232 REPORTER_ASSERT(reporter, i == check);
233 REPORTER_ASSERT(reporter, v == (int)check+1);
234
235 check++;
236 }
237
238 check = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500239 for (auto [i, v] : SkMakeEnumerate(A)) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400240 REPORTER_ASSERT(reporter, i == check);
241 REPORTER_ASSERT(reporter, v == (int)check+1);
242
243 check++;
244 }
245
246 check = 0;
247 std::vector<int> vec = {1, 2, 3, 4};
Herb Derby06a62082019-11-12 14:24:41 -0500248 for (auto [i, v] : SkMakeEnumerate(vec)) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400249 REPORTER_ASSERT(reporter, i == check);
250 REPORTER_ASSERT(reporter, v == (int)check+1);
251 check++;
252 }
253 REPORTER_ASSERT(reporter, check == 4);
254
255 check = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500256 for (auto [i, v] : SkMakeEnumerate(SkMakeSpan(vec))) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400257 REPORTER_ASSERT(reporter, i == check);
258 REPORTER_ASSERT(reporter, v == (int)check+1);
259 check++;
260 }
Herb Derbyeca10912020-01-07 17:53:58 -0500261
262 {
263 auto e = SkMakeEnumerate(SkMakeSpan(vec)).first(2);
264 for (auto[i, v] : e) {
265 REPORTER_ASSERT(reporter, v == (int) i + 1);
266 }
267 REPORTER_ASSERT(reporter, e.size() == 2);
268 }
269
270 {
271 auto e = SkMakeEnumerate(SkMakeSpan(vec)).last(2);
272 for (auto[i, v] : e) {
273 REPORTER_ASSERT(reporter, v == (int) i + 1);
274 }
275 REPORTER_ASSERT(reporter, e.size() == 2);
276 }
277
278 {
279 auto e = SkMakeEnumerate(SkMakeSpan(vec)).subspan(1, 2);
280 for (auto[i, v] : e) {
281 REPORTER_ASSERT(reporter, v == (int) i + 1);
282 }
283 REPORTER_ASSERT(reporter, e.size() == 2);
284 }
Herb Derbycbd235c2019-07-03 18:05:12 -0400285}
286
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400287DEF_TEST(SkZip, reporter) {
288 uint16_t A[] = {1, 2, 3, 4};
289 const float B[] = {10.f, 20.f, 30.f, 40.f};
290 std::vector<int> C = {{20, 30, 40, 50}};
291 std::array<int, 4> D = {{100, 200, 300, 400}};
292 SkSpan<int> S = SkMakeSpan(C);
293
294 // Check SkZip calls
295 SkZip<uint16_t, const float, int, int, int>
296 z{4, &A[0], &B[0], C.data(), D.data(), S.data()};
297
298 REPORTER_ASSERT(reporter, z.size() == 4);
299 REPORTER_ASSERT(reporter, !z.empty());
300
301 {
302 // Check front
303 auto t = z.front();
304 REPORTER_ASSERT(reporter, std::get<0>(t) == 1);
305 REPORTER_ASSERT(reporter, std::get<1>(t) == 10.f);
306 REPORTER_ASSERT(reporter, std::get<2>(t) == 20);
307 REPORTER_ASSERT(reporter, std::get<3>(t) == 100);
308 REPORTER_ASSERT(reporter, std::get<4>(t) == 20);
309 }
310
311 {
312 // Check back
313 auto t = z.back();
314 REPORTER_ASSERT(reporter, std::get<0>(t) == 4);
315 REPORTER_ASSERT(reporter, std::get<1>(t) == 40.f);
316 }
317
318 {
319 // Check ranged-for
320 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500321 for (auto [a, b, c, d, s] : z) {
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400322 REPORTER_ASSERT(reporter, a == A[i]);
323 REPORTER_ASSERT(reporter, b == B[i]);
324 REPORTER_ASSERT(reporter, c == C[i]);
325 REPORTER_ASSERT(reporter, d == D[i]);
326 REPORTER_ASSERT(reporter, s == S[i]);
327
328 i++;
329 }
330 REPORTER_ASSERT(reporter, i = 4);
331 }
332
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400333 {
Herbert Derby58994852019-10-08 10:22:33 -0400334 // Check first(n)
335 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500336 for (auto [a, b, c, d, s] : z.first(2)) {
Herbert Derby58994852019-10-08 10:22:33 -0400337 REPORTER_ASSERT(reporter, a == A[i]);
338 REPORTER_ASSERT(reporter, b == B[i]);
339 REPORTER_ASSERT(reporter, c == C[i]);
340 REPORTER_ASSERT(reporter, d == D[i]);
341 REPORTER_ASSERT(reporter, s == S[i]);
342
343 i++;
344 }
345 REPORTER_ASSERT(reporter, i = 2);
346 }
347
Herb Derbyc9dcd092019-11-15 16:06:45 -0500348 {
349 // Check last(n)
350 int i = 0;
351 for (auto t : z.last(2)) {
352 uint16_t a; float b; int c; int d; int s;
353 std::tie(a, b, c, d, s) = t;
354 REPORTER_ASSERT(reporter, a == A[i + 2]);
355 REPORTER_ASSERT(reporter, b == B[i + 2]);
356 REPORTER_ASSERT(reporter, c == C[i + 2]);
357 REPORTER_ASSERT(reporter, d == D[i + 2]);
358 REPORTER_ASSERT(reporter, s == S[i + 2]);
359
360 i++;
361 }
362 REPORTER_ASSERT(reporter, i = 2);
363 }
Herbert Derby58994852019-10-08 10:22:33 -0400364
365 {
Herb Derby0d593682019-11-15 17:08:53 -0500366 // Check subspan(offset, count)
367 int i = 0;
368 for (auto t : z.subspan(1, 2)) {
369 uint16_t a; float b; int c; int d; int s;
370 std::tie(a, b, c, d, s) = t;
371 REPORTER_ASSERT(reporter, a == A[i + 1]);
372 REPORTER_ASSERT(reporter, b == B[i + 1]);
373 REPORTER_ASSERT(reporter, c == C[i + 1]);
374 REPORTER_ASSERT(reporter, d == D[i + 1]);
375 REPORTER_ASSERT(reporter, s == S[i + 1]);
376
377 i++;
378 }
379 REPORTER_ASSERT(reporter, i = 2);
380 }
381
382 {
Herbert Derby58994852019-10-08 10:22:33 -0400383 // Check copy.
384 auto zz{z};
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400385 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500386 for (auto [a, b, c, d, s] : zz) {
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400387 REPORTER_ASSERT(reporter, a == A[i]);
388 REPORTER_ASSERT(reporter, b == B[i]);
389 REPORTER_ASSERT(reporter, c == C[i]);
390 REPORTER_ASSERT(reporter, d == D[i]);
391 REPORTER_ASSERT(reporter, s == S[i]);
392
393 i++;
394 }
395 REPORTER_ASSERT(reporter, i = 4);
396 }
397
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400398 {
Herbert Derby58994852019-10-08 10:22:33 -0400399 // Check const restricting copy
400 SkZip<const uint16_t, const float, const int, int, int> cz = z;
401 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500402 for (auto [a, b, c, d, s] : cz) {
Herbert Derby58994852019-10-08 10:22:33 -0400403 REPORTER_ASSERT(reporter, a == A[i]);
404 REPORTER_ASSERT(reporter, b == B[i]);
405 REPORTER_ASSERT(reporter, c == C[i]);
406 REPORTER_ASSERT(reporter, d == D[i]);
407 REPORTER_ASSERT(reporter, s == S[i]);
408
409 i++;
410 }
411 REPORTER_ASSERT(reporter, i = 4);
412 }
413
414 {
415 // Check data() returns all the original pointers
416 auto ptrs = z.data();
417 REPORTER_ASSERT(reporter,
418 ptrs == std::make_tuple(&A[0], &B[0], C.data(), D.data(), S.data()));
419 }
420
421 {
422 // Check index getter
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400423 auto span = z.get<1>();
424 REPORTER_ASSERT(reporter, span[1] == 20.f);
425 }
426
427 // The following mutates the data.
428 {
429 // Check indexing
Herb Derby06a62082019-11-12 14:24:41 -0500430 auto [a, b, c, d, e] = z[1];
431 REPORTER_ASSERT(reporter, a == 2);
432 REPORTER_ASSERT(reporter, b == 20.f);
433 REPORTER_ASSERT(reporter, c == 30);
434 REPORTER_ASSERT(reporter, d == 200);
435 REPORTER_ASSERT(reporter, e == 30);
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400436
437 // Check correct refs returned.
Herb Derby06a62082019-11-12 14:24:41 -0500438 REPORTER_ASSERT(reporter, &a == &A[1]);
439 REPORTER_ASSERT(reporter, &b == &B[1]);
440 REPORTER_ASSERT(reporter, &c == &C[1]);
441 REPORTER_ASSERT(reporter, &d == &D[1]);
442 REPORTER_ASSERT(reporter, &e == &S[1]);
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400443
444 // Check assignment
Herb Derby06a62082019-11-12 14:24:41 -0500445 a = 20;
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400446 // std::get<1>(t) = 300.f; // is const
Herb Derby06a62082019-11-12 14:24:41 -0500447 c = 300;
448 d = 2000;
449 e = 300;
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400450
451 auto t1 = z[1];
452 REPORTER_ASSERT(reporter, std::get<0>(t1) == 20);
453 REPORTER_ASSERT(reporter, std::get<1>(t1) == 20.f);
454 REPORTER_ASSERT(reporter, std::get<2>(t1) == 300);
455 REPORTER_ASSERT(reporter, std::get<3>(t1) == 2000);
456 REPORTER_ASSERT(reporter, std::get<4>(t1) == 300);
457 }
458}
Herb Derby986ab2c2019-09-19 11:27:43 -0400459
460DEF_TEST(SkMakeZip, reporter) {
461 uint16_t A[] = {1, 2, 3, 4};
462 const float B[] = {10.f, 20.f, 30.f, 40.f};
463 const std::vector<int> C = {{20, 30, 40, 50}};
464 std::array<int, 4> D = {{100, 200, 300, 400}};
465 SkSpan<const int> S = SkMakeSpan(C);
466 uint16_t* P = &A[0];
467 {
468 // Check make zip
469 auto zz = SkMakeZip(&A[0], B, C, D, S, P);
470
471 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500472 for (auto [a, b, c, d, s, p] : zz) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400473 REPORTER_ASSERT(reporter, a == A[i]);
474 REPORTER_ASSERT(reporter, b == B[i]);
475 REPORTER_ASSERT(reporter, c == C[i]);
476 REPORTER_ASSERT(reporter, d == D[i]);
477 REPORTER_ASSERT(reporter, s == S[i]);
478 REPORTER_ASSERT(reporter, p == P[i]);
479
480 i++;
481 }
482 REPORTER_ASSERT(reporter, i = 4);
483 }
484
485 {
486 // Check SkMakeZip in ranged for check OneSize calc of B.
487 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500488 for (auto [a, b, c, d, s] : SkMakeZip(&A[0], B, C, D, S)) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400489 REPORTER_ASSERT(reporter, a == A[i]);
490 REPORTER_ASSERT(reporter, b == B[i]);
491 REPORTER_ASSERT(reporter, c == C[i]);
492 REPORTER_ASSERT(reporter, d == D[i]);
493 REPORTER_ASSERT(reporter, s == S[i]);
494
495 i++;
496 }
497 REPORTER_ASSERT(reporter, i = 4);
498 }
499
500 {
501 // Check SkMakeZip in ranged for OneSize of C
502 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500503 for (auto [a, b, c, d, s] : SkMakeZip(&A[0], &B[0], C, D, S)) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400504 REPORTER_ASSERT(reporter, a == A[i]);
505 REPORTER_ASSERT(reporter, b == B[i]);
506 REPORTER_ASSERT(reporter, c == C[i]);
507 REPORTER_ASSERT(reporter, d == D[i]);
508 REPORTER_ASSERT(reporter, s == S[i]);
509
510 i++;
511 }
512 REPORTER_ASSERT(reporter, i = 4);
513 }
514
515 {
516 // Check SkMakeZip in ranged for OneSize for S
517 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500518 for (auto [s, a, b, c, d] : SkMakeZip(S, A, B, C, D)) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400519 REPORTER_ASSERT(reporter, a == A[i]);
520 REPORTER_ASSERT(reporter, b == B[i]);
521 REPORTER_ASSERT(reporter, c == C[i]);
522 REPORTER_ASSERT(reporter, d == D[i]);
523 REPORTER_ASSERT(reporter, s == S[i]);
524
525 i++;
526 }
527 REPORTER_ASSERT(reporter, i = 4);
528 }
529
530 {
531 // Check SkMakeZip in ranged for
532 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500533 for (auto [c, s, a, b, d] : SkMakeZip(C, S, A, B, D)) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400534 REPORTER_ASSERT(reporter, a == A[i]);
535 REPORTER_ASSERT(reporter, b == B[i]);
536 REPORTER_ASSERT(reporter, c == C[i]);
537 REPORTER_ASSERT(reporter, d == D[i]);
538 REPORTER_ASSERT(reporter, s == S[i]);
539
540 i++;
541 }
542 REPORTER_ASSERT(reporter, i = 4);
543 }
544
545 {
Herb Derbycbd235c2019-07-03 18:05:12 -0400546 // Check SkEnumerate and SkMakeZip in ranged for
547 auto zz = SkMakeZip(A, B, C, D, S);
Herb Derby81777ac2019-11-14 13:51:43 -0500548 for (auto [i, a, b, c, d, s] : SkMakeEnumerate(zz)) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400549 REPORTER_ASSERT(reporter, a == A[i]);
550 REPORTER_ASSERT(reporter, b == B[i]);
551 REPORTER_ASSERT(reporter, c == C[i]);
552 REPORTER_ASSERT(reporter, d == D[i]);
553 REPORTER_ASSERT(reporter, s == S[i]);
554 }
555 }
556
557 {
558 // Check SkEnumerate and SkMakeZip in ranged for
559 const auto& zz = SkMakeZip(A, B, C, D, S);
Herb Derby81777ac2019-11-14 13:51:43 -0500560 for (auto [i, a, b, c, d, s] : SkMakeEnumerate(zz)) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400561 REPORTER_ASSERT(reporter, a == A[i]);
562 REPORTER_ASSERT(reporter, b == B[i]);
563 REPORTER_ASSERT(reporter, c == C[i]);
564 REPORTER_ASSERT(reporter, d == D[i]);
565 REPORTER_ASSERT(reporter, s == S[i]);
566 }
567 }
568
569 {
570 // Check SkEnumerate and SkMakeZip in ranged for
Herb Derby81777ac2019-11-14 13:51:43 -0500571 for (auto [i, a, b, c, d, s] : SkMakeEnumerate(SkMakeZip(A, B, C, D, S))) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400572 REPORTER_ASSERT(reporter, a == A[i]);
573 REPORTER_ASSERT(reporter, b == B[i]);
574 REPORTER_ASSERT(reporter, c == C[i]);
575 REPORTER_ASSERT(reporter, d == D[i]);
576 REPORTER_ASSERT(reporter, s == S[i]);
577 }
578 }
579
580 {
Herb Derby986ab2c2019-09-19 11:27:43 -0400581 std::vector<int>v;
582 auto z = SkMakeZip(v);
583 REPORTER_ASSERT(reporter, z.empty());
584 }
585
586 {
587 constexpr static uint16_t cA[] = {1, 2, 3, 4};
588 // Not constexpr in stdc++11 library.
589 //constexpr static std::array<int, 4> cD = {{100, 200, 300, 400}};
590 constexpr static const uint16_t* cP = &cA[0];
591 constexpr auto z = SkMakeZip(cA, cP);
592 REPORTER_ASSERT(reporter, !z.empty());
593 }
594}