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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkRefCnt.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/utils/SkRandom.h" |
Herb Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 10 | #include "src/core/SkEnumerate.h" |
Herb Derby | 10e48d4 | 2019-08-08 12:30:13 -0400 | [diff] [blame] | 11 | #include "src/core/SkSpan.h" |
Ben Wagner | 8bd6e8f | 2019-05-15 09:28:52 -0400 | [diff] [blame] | 12 | #include "src/core/SkTSearch.h" |
John Stiles | 6e9ead9 | 2020-07-14 00:13:51 +0000 | [diff] [blame] | 13 | #include "src/core/SkTSort.h" |
Herb Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 14 | #include "src/core/SkZip.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "tests/Test.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 16 | |
Herb Derby | 10e48d4 | 2019-08-08 12:30:13 -0400 | [diff] [blame] | 17 | #include <array> |
Herb Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 18 | #include <initializer_list> |
| 19 | #include <tuple> |
Herb Derby | 10e48d4 | 2019-08-08 12:30:13 -0400 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 22 | class RefClass : public SkRefCnt { |
| 23 | public: |
| 24 | RefClass(int n) : fN(n) {} |
| 25 | int get() const { return fN; } |
| 26 | |
| 27 | private: |
| 28 | int fN; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 29 | |
| 30 | typedef SkRefCnt INHERITED; |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 33 | static void test_autounref(skiatest::Reporter* reporter) { |
| 34 | RefClass obj(0); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 35 | REPORTER_ASSERT(reporter, obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 36 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 37 | sk_sp<RefClass> tmp(&obj); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, &obj == tmp.get()); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 39 | REPORTER_ASSERT(reporter, obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 40 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, &obj == tmp.release()); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 42 | REPORTER_ASSERT(reporter, obj.unique()); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 43 | REPORTER_ASSERT(reporter, nullptr == tmp.release()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, nullptr == tmp.get()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 45 | |
| 46 | obj.ref(); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, !obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 48 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 49 | sk_sp<RefClass> tmp2(&obj); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 50 | } |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 52 | } |
| 53 | |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 54 | static void test_autostarray(skiatest::Reporter* reporter) { |
| 55 | RefClass obj0(0); |
| 56 | RefClass obj1(1); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 58 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 59 | |
| 60 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 61 | SkAutoSTArray<2, sk_sp<RefClass> > tmp; |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 62 | 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()); |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 67 | tmp[0].reset(SkRef(&obj0)); |
| 68 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 70 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 71 | |
| 72 | // test out reset with data in the array (and a new allocation) |
| 73 | tmp.reset(0); |
| 74 | REPORTER_ASSERT(reporter, 0 == tmp.count()); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 76 | REPORTER_ASSERT(reporter, obj1.unique()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 77 | |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 78 | tmp.reset(2); // this should use the preexisting allocation |
| 79 | REPORTER_ASSERT(reporter, 2 == tmp.count()); |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 80 | tmp[0].reset(SkRef(&obj0)); |
| 81 | tmp[1].reset(SkRef(&obj1)); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // test out destructor with data in the array (and using existing allocation) |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 86 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 87 | |
| 88 | { |
| 89 | // test out allocating ctor (this should allocate new memory) |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 90 | SkAutoSTArray<2, sk_sp<RefClass> > tmp(4); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, 4 == tmp.count()); |
| 92 | |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 93 | tmp[0].reset(SkRef(&obj0)); |
| 94 | tmp[1].reset(SkRef(&obj1)); |
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 | // Test out resut with data in the array and malloced storage |
| 99 | tmp.reset(0); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 101 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 102 | |
| 103 | tmp.reset(2); // this should use the preexisting storage |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 104 | tmp[0].reset(SkRef(&obj0)); |
| 105 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 107 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 108 | |
| 109 | tmp.reset(4); // this should force a new malloc |
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 | |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 113 | tmp[0].reset(SkRef(&obj0)); |
| 114 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 115 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 116 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 117 | } |
| 118 | |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 119 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 120 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 121 | } |
| 122 | |
epoger@google.com | bcc5683 | 2011-05-20 17:35:46 +0000 | [diff] [blame] | 123 | ///////////////////////////////////////////////////////////////////////////// |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 124 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 125 | #define kSEARCH_COUNT 91 |
| 126 | |
| 127 | static void test_search(skiatest::Reporter* reporter) { |
| 128 | int i, array[kSEARCH_COUNT]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 129 | SkRandom rand; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 130 | |
| 131 | for (i = 0; i < kSEARCH_COUNT; i++) { |
| 132 | array[i] = rand.nextS(); |
| 133 | } |
| 134 | |
John Stiles | 6e9ead9 | 2020-07-14 00:13:51 +0000 | [diff] [blame] | 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 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 172 | DEF_TEST(Utils, reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 173 | test_search(reporter); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 174 | test_autounref(reporter); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 175 | test_autostarray(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 176 | } |
Herb Derby | 10e48d4 | 2019-08-08 12:30:13 -0400 | [diff] [blame] | 177 | |
| 178 | DEF_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 Derby | 537dc05 | 2020-05-13 12:01:24 -0400 | [diff] [blame] | 205 | 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 Derby | 10e48d4 | 2019-08-08 12:30:13 -0400 | [diff] [blame] | 209 | } |
| 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 Derby | 986ab2c | 2019-09-19 11:27:43 -0400 | [diff] [blame] | 221 | |
| 222 | { |
| 223 | std::vector<int> v; |
| 224 | auto s = SkMakeSpan(v); |
| 225 | REPORTER_ASSERT(reporter, s.empty()); |
| 226 | } |
Herb Derby | 10e48d4 | 2019-08-08 12:30:13 -0400 | [diff] [blame] | 227 | } |
Herb Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 228 | |
Herb Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 229 | DEF_TEST(SkEnumerate, reporter) { |
| 230 | |
| 231 | int A[] = {1, 2, 3, 4}; |
| 232 | auto enumeration = SkMakeEnumerate(A); |
| 233 | |
| 234 | size_t check = 0; |
Herb Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 235 | for (auto [i, v] : enumeration) { |
Herb Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 236 | REPORTER_ASSERT(reporter, i == check); |
| 237 | REPORTER_ASSERT(reporter, v == (int)check+1); |
| 238 | |
| 239 | check++; |
| 240 | } |
| 241 | |
| 242 | check = 0; |
Herb Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 243 | for (auto [i, v] : SkMakeEnumerate(A)) { |
Herb Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 244 | 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 Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 252 | for (auto [i, v] : SkMakeEnumerate(vec)) { |
Herb Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 253 | 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 Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 260 | for (auto [i, v] : SkMakeEnumerate(SkMakeSpan(vec))) { |
Herb Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 261 | REPORTER_ASSERT(reporter, i == check); |
| 262 | REPORTER_ASSERT(reporter, v == (int)check+1); |
| 263 | check++; |
| 264 | } |
Herb Derby | eca1091 | 2020-01-07 17:53:58 -0500 | [diff] [blame] | 265 | |
| 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 Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Herb Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 291 | DEF_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 Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 325 | for (auto [a, b, c, d, s] : z) { |
Herb Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 326 | 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 Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 337 | { |
Herbert Derby | 5899485 | 2019-10-08 10:22:33 -0400 | [diff] [blame] | 338 | // Check first(n) |
| 339 | int i = 0; |
Herb Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 340 | for (auto [a, b, c, d, s] : z.first(2)) { |
Herbert Derby | 5899485 | 2019-10-08 10:22:33 -0400 | [diff] [blame] | 341 | 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 Derby | c9dcd09 | 2019-11-15 16:06:45 -0500 | [diff] [blame] | 352 | { |
| 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 Derby | 5899485 | 2019-10-08 10:22:33 -0400 | [diff] [blame] | 368 | |
| 369 | { |
Herb Derby | 0d59368 | 2019-11-15 17:08:53 -0500 | [diff] [blame] | 370 | // 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 Derby | 5899485 | 2019-10-08 10:22:33 -0400 | [diff] [blame] | 387 | // Check copy. |
| 388 | auto zz{z}; |
Herb Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 389 | int i = 0; |
Herb Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 390 | for (auto [a, b, c, d, s] : zz) { |
Herb Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 391 | 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 Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 402 | { |
Herbert Derby | 5899485 | 2019-10-08 10:22:33 -0400 | [diff] [blame] | 403 | // Check const restricting copy |
| 404 | SkZip<const uint16_t, const float, const int, int, int> cz = z; |
| 405 | int i = 0; |
Herb Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 406 | for (auto [a, b, c, d, s] : cz) { |
Herbert Derby | 5899485 | 2019-10-08 10:22:33 -0400 | [diff] [blame] | 407 | 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 Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 427 | 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 Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 434 | 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 Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 440 | |
| 441 | // Check correct refs returned. |
Herb Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 442 | 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 Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 447 | |
| 448 | // Check assignment |
Herb Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 449 | a = 20; |
Herb Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 450 | // std::get<1>(t) = 300.f; // is const |
Herb Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 451 | c = 300; |
| 452 | d = 2000; |
| 453 | e = 300; |
Herb Derby | c44ee1a | 2019-09-09 11:36:39 -0400 | [diff] [blame] | 454 | |
| 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 Derby | 986ab2c | 2019-09-19 11:27:43 -0400 | [diff] [blame] | 463 | |
| 464 | DEF_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 Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 476 | for (auto [a, b, c, d, s, p] : zz) { |
Herb Derby | 986ab2c | 2019-09-19 11:27:43 -0400 | [diff] [blame] | 477 | 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 Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 492 | for (auto [a, b, c, d, s] : SkMakeZip(&A[0], B, C, D, S)) { |
Herb Derby | 986ab2c | 2019-09-19 11:27:43 -0400 | [diff] [blame] | 493 | 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 Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 507 | for (auto [a, b, c, d, s] : SkMakeZip(&A[0], &B[0], C, D, S)) { |
Herb Derby | 986ab2c | 2019-09-19 11:27:43 -0400 | [diff] [blame] | 508 | 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 Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 522 | for (auto [s, a, b, c, d] : SkMakeZip(S, A, B, C, D)) { |
Herb Derby | 986ab2c | 2019-09-19 11:27:43 -0400 | [diff] [blame] | 523 | 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 Derby | 06a6208 | 2019-11-12 14:24:41 -0500 | [diff] [blame] | 537 | for (auto [c, s, a, b, d] : SkMakeZip(C, S, A, B, D)) { |
Herb Derby | 986ab2c | 2019-09-19 11:27:43 -0400 | [diff] [blame] | 538 | 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 Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 550 | // Check SkEnumerate and SkMakeZip in ranged for |
| 551 | auto zz = SkMakeZip(A, B, C, D, S); |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 552 | for (auto [i, a, b, c, d, s] : SkMakeEnumerate(zz)) { |
Herb Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 553 | 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 Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 564 | for (auto [i, a, b, c, d, s] : SkMakeEnumerate(zz)) { |
Herb Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 565 | 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 Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 575 | for (auto [i, a, b, c, d, s] : SkMakeEnumerate(SkMakeZip(A, B, C, D, S))) { |
Herb Derby | cbd235c | 2019-07-03 18:05:12 -0400 | [diff] [blame] | 576 | 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 Derby | 986ab2c | 2019-09-19 11:27:43 -0400 | [diff] [blame] | 585 | 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 | } |