blob: 7a46226bdbc8b5f1ccc7524566d7ec4f9480eab3 [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"
John Stiles6e9ead92020-07-14 00:13:51 +000013#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
John Stiles6e9ead92020-07-14 00:13:51 +0000135 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 }
reed@android.comed673312009-02-27 16:24:51 +0000140
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);
Herb Derby537dc052020-05-13 12:01:24 -0400205 auto s1 = s.subspan(1,3);
206 REPORTER_ASSERT(reporter, s1.size() == 3);
207 REPORTER_ASSERT(reporter, s1.front() == 2);
208 REPORTER_ASSERT(reporter, s1.back() == 100);
Herb Derby10e48d42019-08-08 12:30:13 -0400209 }
210
211 {
212 std::array<int, 5> t = {{1, 2, 3, 4, 5}};
213 const std::array<int, 5>& v = t;
214 auto s = SkMakeSpan(v);
215 //s[3] = 100; // Should fail to compile
216 REPORTER_ASSERT(reporter, s[3] == 4);
217 REPORTER_ASSERT(reporter, t[3] == 4);
218 t[3] = 100;
219 REPORTER_ASSERT(reporter, s[3] == 100);
220 }
Herb Derby986ab2c2019-09-19 11:27:43 -0400221
222 {
223 std::vector<int> v;
224 auto s = SkMakeSpan(v);
225 REPORTER_ASSERT(reporter, s.empty());
226 }
Herb Derby10e48d42019-08-08 12:30:13 -0400227}
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400228
Herb Derbycbd235c2019-07-03 18:05:12 -0400229DEF_TEST(SkEnumerate, reporter) {
230
231 int A[] = {1, 2, 3, 4};
232 auto enumeration = SkMakeEnumerate(A);
233
234 size_t check = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500235 for (auto [i, v] : enumeration) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400236 REPORTER_ASSERT(reporter, i == check);
237 REPORTER_ASSERT(reporter, v == (int)check+1);
238
239 check++;
240 }
241
242 check = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500243 for (auto [i, v] : SkMakeEnumerate(A)) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400244 REPORTER_ASSERT(reporter, i == check);
245 REPORTER_ASSERT(reporter, v == (int)check+1);
246
247 check++;
248 }
249
250 check = 0;
251 std::vector<int> vec = {1, 2, 3, 4};
Herb Derby06a62082019-11-12 14:24:41 -0500252 for (auto [i, v] : SkMakeEnumerate(vec)) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400253 REPORTER_ASSERT(reporter, i == check);
254 REPORTER_ASSERT(reporter, v == (int)check+1);
255 check++;
256 }
257 REPORTER_ASSERT(reporter, check == 4);
258
259 check = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500260 for (auto [i, v] : SkMakeEnumerate(SkMakeSpan(vec))) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400261 REPORTER_ASSERT(reporter, i == check);
262 REPORTER_ASSERT(reporter, v == (int)check+1);
263 check++;
264 }
Herb Derbyeca10912020-01-07 17:53:58 -0500265
266 {
267 auto e = SkMakeEnumerate(SkMakeSpan(vec)).first(2);
268 for (auto[i, v] : e) {
269 REPORTER_ASSERT(reporter, v == (int) i + 1);
270 }
271 REPORTER_ASSERT(reporter, e.size() == 2);
272 }
273
274 {
275 auto e = SkMakeEnumerate(SkMakeSpan(vec)).last(2);
276 for (auto[i, v] : e) {
277 REPORTER_ASSERT(reporter, v == (int) i + 1);
278 }
279 REPORTER_ASSERT(reporter, e.size() == 2);
280 }
281
282 {
283 auto e = SkMakeEnumerate(SkMakeSpan(vec)).subspan(1, 2);
284 for (auto[i, v] : e) {
285 REPORTER_ASSERT(reporter, v == (int) i + 1);
286 }
287 REPORTER_ASSERT(reporter, e.size() == 2);
288 }
Herb Derbycbd235c2019-07-03 18:05:12 -0400289}
290
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400291DEF_TEST(SkZip, reporter) {
292 uint16_t A[] = {1, 2, 3, 4};
293 const float B[] = {10.f, 20.f, 30.f, 40.f};
294 std::vector<int> C = {{20, 30, 40, 50}};
295 std::array<int, 4> D = {{100, 200, 300, 400}};
296 SkSpan<int> S = SkMakeSpan(C);
297
298 // Check SkZip calls
299 SkZip<uint16_t, const float, int, int, int>
300 z{4, &A[0], &B[0], C.data(), D.data(), S.data()};
301
302 REPORTER_ASSERT(reporter, z.size() == 4);
303 REPORTER_ASSERT(reporter, !z.empty());
304
305 {
306 // Check front
307 auto t = z.front();
308 REPORTER_ASSERT(reporter, std::get<0>(t) == 1);
309 REPORTER_ASSERT(reporter, std::get<1>(t) == 10.f);
310 REPORTER_ASSERT(reporter, std::get<2>(t) == 20);
311 REPORTER_ASSERT(reporter, std::get<3>(t) == 100);
312 REPORTER_ASSERT(reporter, std::get<4>(t) == 20);
313 }
314
315 {
316 // Check back
317 auto t = z.back();
318 REPORTER_ASSERT(reporter, std::get<0>(t) == 4);
319 REPORTER_ASSERT(reporter, std::get<1>(t) == 40.f);
320 }
321
322 {
323 // Check ranged-for
324 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500325 for (auto [a, b, c, d, s] : z) {
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400326 REPORTER_ASSERT(reporter, a == A[i]);
327 REPORTER_ASSERT(reporter, b == B[i]);
328 REPORTER_ASSERT(reporter, c == C[i]);
329 REPORTER_ASSERT(reporter, d == D[i]);
330 REPORTER_ASSERT(reporter, s == S[i]);
331
332 i++;
333 }
334 REPORTER_ASSERT(reporter, i = 4);
335 }
336
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400337 {
Herbert Derby58994852019-10-08 10:22:33 -0400338 // Check first(n)
339 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500340 for (auto [a, b, c, d, s] : z.first(2)) {
Herbert Derby58994852019-10-08 10:22:33 -0400341 REPORTER_ASSERT(reporter, a == A[i]);
342 REPORTER_ASSERT(reporter, b == B[i]);
343 REPORTER_ASSERT(reporter, c == C[i]);
344 REPORTER_ASSERT(reporter, d == D[i]);
345 REPORTER_ASSERT(reporter, s == S[i]);
346
347 i++;
348 }
349 REPORTER_ASSERT(reporter, i = 2);
350 }
351
Herb Derbyc9dcd092019-11-15 16:06:45 -0500352 {
353 // Check last(n)
354 int i = 0;
355 for (auto t : z.last(2)) {
356 uint16_t a; float b; int c; int d; int s;
357 std::tie(a, b, c, d, s) = t;
358 REPORTER_ASSERT(reporter, a == A[i + 2]);
359 REPORTER_ASSERT(reporter, b == B[i + 2]);
360 REPORTER_ASSERT(reporter, c == C[i + 2]);
361 REPORTER_ASSERT(reporter, d == D[i + 2]);
362 REPORTER_ASSERT(reporter, s == S[i + 2]);
363
364 i++;
365 }
366 REPORTER_ASSERT(reporter, i = 2);
367 }
Herbert Derby58994852019-10-08 10:22:33 -0400368
369 {
Herb Derby0d593682019-11-15 17:08:53 -0500370 // Check subspan(offset, count)
371 int i = 0;
372 for (auto t : z.subspan(1, 2)) {
373 uint16_t a; float b; int c; int d; int s;
374 std::tie(a, b, c, d, s) = t;
375 REPORTER_ASSERT(reporter, a == A[i + 1]);
376 REPORTER_ASSERT(reporter, b == B[i + 1]);
377 REPORTER_ASSERT(reporter, c == C[i + 1]);
378 REPORTER_ASSERT(reporter, d == D[i + 1]);
379 REPORTER_ASSERT(reporter, s == S[i + 1]);
380
381 i++;
382 }
383 REPORTER_ASSERT(reporter, i = 2);
384 }
385
386 {
Herbert Derby58994852019-10-08 10:22:33 -0400387 // Check copy.
388 auto zz{z};
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400389 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500390 for (auto [a, b, c, d, s] : zz) {
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400391 REPORTER_ASSERT(reporter, a == A[i]);
392 REPORTER_ASSERT(reporter, b == B[i]);
393 REPORTER_ASSERT(reporter, c == C[i]);
394 REPORTER_ASSERT(reporter, d == D[i]);
395 REPORTER_ASSERT(reporter, s == S[i]);
396
397 i++;
398 }
399 REPORTER_ASSERT(reporter, i = 4);
400 }
401
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400402 {
Herbert Derby58994852019-10-08 10:22:33 -0400403 // Check const restricting copy
404 SkZip<const uint16_t, const float, const int, int, int> cz = z;
405 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500406 for (auto [a, b, c, d, s] : cz) {
Herbert Derby58994852019-10-08 10:22:33 -0400407 REPORTER_ASSERT(reporter, a == A[i]);
408 REPORTER_ASSERT(reporter, b == B[i]);
409 REPORTER_ASSERT(reporter, c == C[i]);
410 REPORTER_ASSERT(reporter, d == D[i]);
411 REPORTER_ASSERT(reporter, s == S[i]);
412
413 i++;
414 }
415 REPORTER_ASSERT(reporter, i = 4);
416 }
417
418 {
419 // Check data() returns all the original pointers
420 auto ptrs = z.data();
421 REPORTER_ASSERT(reporter,
422 ptrs == std::make_tuple(&A[0], &B[0], C.data(), D.data(), S.data()));
423 }
424
425 {
426 // Check index getter
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400427 auto span = z.get<1>();
428 REPORTER_ASSERT(reporter, span[1] == 20.f);
429 }
430
431 // The following mutates the data.
432 {
433 // Check indexing
Herb Derby06a62082019-11-12 14:24:41 -0500434 auto [a, b, c, d, e] = z[1];
435 REPORTER_ASSERT(reporter, a == 2);
436 REPORTER_ASSERT(reporter, b == 20.f);
437 REPORTER_ASSERT(reporter, c == 30);
438 REPORTER_ASSERT(reporter, d == 200);
439 REPORTER_ASSERT(reporter, e == 30);
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400440
441 // Check correct refs returned.
Herb Derby06a62082019-11-12 14:24:41 -0500442 REPORTER_ASSERT(reporter, &a == &A[1]);
443 REPORTER_ASSERT(reporter, &b == &B[1]);
444 REPORTER_ASSERT(reporter, &c == &C[1]);
445 REPORTER_ASSERT(reporter, &d == &D[1]);
446 REPORTER_ASSERT(reporter, &e == &S[1]);
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400447
448 // Check assignment
Herb Derby06a62082019-11-12 14:24:41 -0500449 a = 20;
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400450 // std::get<1>(t) = 300.f; // is const
Herb Derby06a62082019-11-12 14:24:41 -0500451 c = 300;
452 d = 2000;
453 e = 300;
Herb Derbyc44ee1a2019-09-09 11:36:39 -0400454
455 auto t1 = z[1];
456 REPORTER_ASSERT(reporter, std::get<0>(t1) == 20);
457 REPORTER_ASSERT(reporter, std::get<1>(t1) == 20.f);
458 REPORTER_ASSERT(reporter, std::get<2>(t1) == 300);
459 REPORTER_ASSERT(reporter, std::get<3>(t1) == 2000);
460 REPORTER_ASSERT(reporter, std::get<4>(t1) == 300);
461 }
462}
Herb Derby986ab2c2019-09-19 11:27:43 -0400463
464DEF_TEST(SkMakeZip, reporter) {
465 uint16_t A[] = {1, 2, 3, 4};
466 const float B[] = {10.f, 20.f, 30.f, 40.f};
467 const std::vector<int> C = {{20, 30, 40, 50}};
468 std::array<int, 4> D = {{100, 200, 300, 400}};
469 SkSpan<const int> S = SkMakeSpan(C);
470 uint16_t* P = &A[0];
471 {
472 // Check make zip
473 auto zz = SkMakeZip(&A[0], B, C, D, S, P);
474
475 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500476 for (auto [a, b, c, d, s, p] : zz) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400477 REPORTER_ASSERT(reporter, a == A[i]);
478 REPORTER_ASSERT(reporter, b == B[i]);
479 REPORTER_ASSERT(reporter, c == C[i]);
480 REPORTER_ASSERT(reporter, d == D[i]);
481 REPORTER_ASSERT(reporter, s == S[i]);
482 REPORTER_ASSERT(reporter, p == P[i]);
483
484 i++;
485 }
486 REPORTER_ASSERT(reporter, i = 4);
487 }
488
489 {
490 // Check SkMakeZip in ranged for check OneSize calc of B.
491 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500492 for (auto [a, b, c, d, s] : SkMakeZip(&A[0], B, C, D, S)) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400493 REPORTER_ASSERT(reporter, a == A[i]);
494 REPORTER_ASSERT(reporter, b == B[i]);
495 REPORTER_ASSERT(reporter, c == C[i]);
496 REPORTER_ASSERT(reporter, d == D[i]);
497 REPORTER_ASSERT(reporter, s == S[i]);
498
499 i++;
500 }
501 REPORTER_ASSERT(reporter, i = 4);
502 }
503
504 {
505 // Check SkMakeZip in ranged for OneSize of C
506 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500507 for (auto [a, b, c, d, s] : SkMakeZip(&A[0], &B[0], C, D, S)) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400508 REPORTER_ASSERT(reporter, a == A[i]);
509 REPORTER_ASSERT(reporter, b == B[i]);
510 REPORTER_ASSERT(reporter, c == C[i]);
511 REPORTER_ASSERT(reporter, d == D[i]);
512 REPORTER_ASSERT(reporter, s == S[i]);
513
514 i++;
515 }
516 REPORTER_ASSERT(reporter, i = 4);
517 }
518
519 {
520 // Check SkMakeZip in ranged for OneSize for S
521 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500522 for (auto [s, a, b, c, d] : SkMakeZip(S, A, B, C, D)) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400523 REPORTER_ASSERT(reporter, a == A[i]);
524 REPORTER_ASSERT(reporter, b == B[i]);
525 REPORTER_ASSERT(reporter, c == C[i]);
526 REPORTER_ASSERT(reporter, d == D[i]);
527 REPORTER_ASSERT(reporter, s == S[i]);
528
529 i++;
530 }
531 REPORTER_ASSERT(reporter, i = 4);
532 }
533
534 {
535 // Check SkMakeZip in ranged for
536 int i = 0;
Herb Derby06a62082019-11-12 14:24:41 -0500537 for (auto [c, s, a, b, d] : SkMakeZip(C, S, A, B, D)) {
Herb Derby986ab2c2019-09-19 11:27:43 -0400538 REPORTER_ASSERT(reporter, a == A[i]);
539 REPORTER_ASSERT(reporter, b == B[i]);
540 REPORTER_ASSERT(reporter, c == C[i]);
541 REPORTER_ASSERT(reporter, d == D[i]);
542 REPORTER_ASSERT(reporter, s == S[i]);
543
544 i++;
545 }
546 REPORTER_ASSERT(reporter, i = 4);
547 }
548
549 {
Herb Derbycbd235c2019-07-03 18:05:12 -0400550 // Check SkEnumerate and SkMakeZip in ranged for
551 auto zz = SkMakeZip(A, B, C, D, S);
Herb Derby81777ac2019-11-14 13:51:43 -0500552 for (auto [i, a, b, c, d, s] : SkMakeEnumerate(zz)) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400553 REPORTER_ASSERT(reporter, a == A[i]);
554 REPORTER_ASSERT(reporter, b == B[i]);
555 REPORTER_ASSERT(reporter, c == C[i]);
556 REPORTER_ASSERT(reporter, d == D[i]);
557 REPORTER_ASSERT(reporter, s == S[i]);
558 }
559 }
560
561 {
562 // Check SkEnumerate and SkMakeZip in ranged for
563 const auto& zz = SkMakeZip(A, B, C, D, S);
Herb Derby81777ac2019-11-14 13:51:43 -0500564 for (auto [i, a, b, c, d, s] : SkMakeEnumerate(zz)) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400565 REPORTER_ASSERT(reporter, a == A[i]);
566 REPORTER_ASSERT(reporter, b == B[i]);
567 REPORTER_ASSERT(reporter, c == C[i]);
568 REPORTER_ASSERT(reporter, d == D[i]);
569 REPORTER_ASSERT(reporter, s == S[i]);
570 }
571 }
572
573 {
574 // Check SkEnumerate and SkMakeZip in ranged for
Herb Derby81777ac2019-11-14 13:51:43 -0500575 for (auto [i, a, b, c, d, s] : SkMakeEnumerate(SkMakeZip(A, B, C, D, S))) {
Herb Derbycbd235c2019-07-03 18:05:12 -0400576 REPORTER_ASSERT(reporter, a == A[i]);
577 REPORTER_ASSERT(reporter, b == B[i]);
578 REPORTER_ASSERT(reporter, c == C[i]);
579 REPORTER_ASSERT(reporter, d == D[i]);
580 REPORTER_ASSERT(reporter, s == S[i]);
581 }
582 }
583
584 {
Herb Derby986ab2c2019-09-19 11:27:43 -0400585 std::vector<int>v;
586 auto z = SkMakeZip(v);
587 REPORTER_ASSERT(reporter, z.empty());
588 }
589
590 {
591 constexpr static uint16_t cA[] = {1, 2, 3, 4};
592 // Not constexpr in stdc++11 library.
593 //constexpr static std::array<int, 4> cD = {{100, 200, 300, 400}};
594 constexpr static const uint16_t* cP = &cA[0];
595 constexpr auto z = SkMakeZip(cA, cP);
596 REPORTER_ASSERT(reporter, !z.empty());
597 }
598}