2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
2019-07-03 22:05:12 +00:00
|
|
|
#include "src/core/SkEnumerate.h"
|
2019-08-08 16:30:13 +00:00
|
|
|
#include "src/core/SkSpan.h"
|
2019-05-15 13:28:52 +00:00
|
|
|
#include "src/core/SkTSearch.h"
|
2020-07-14 00:13:51 +00:00
|
|
|
#include "src/core/SkTSort.h"
|
2019-09-09 15:36:39 +00:00
|
|
|
#include "src/core/SkZip.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2009-02-27 16:24:51 +00:00
|
|
|
|
2019-08-08 16:30:13 +00:00
|
|
|
#include <array>
|
2019-09-09 15:36:39 +00:00
|
|
|
#include <initializer_list>
|
|
|
|
#include <tuple>
|
2019-08-08 16:30:13 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2010-04-16 20:35:47 +00:00
|
|
|
class RefClass : public SkRefCnt {
|
|
|
|
public:
|
|
|
|
RefClass(int n) : fN(n) {}
|
|
|
|
int get() const { return fN; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fN;
|
2012-08-16 14:58:06 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkRefCnt;
|
2010-04-16 20:35:47 +00:00
|
|
|
};
|
|
|
|
|
2011-02-25 18:10:29 +00:00
|
|
|
static void test_autounref(skiatest::Reporter* reporter) {
|
|
|
|
RefClass obj(0);
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj.unique());
|
2011-02-25 18:10:29 +00:00
|
|
|
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<RefClass> tmp(&obj);
|
2011-02-25 18:10:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, &obj == tmp.get());
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj.unique());
|
2011-02-25 18:10:29 +00:00
|
|
|
|
2016-03-16 20:53:35 +00:00
|
|
|
REPORTER_ASSERT(reporter, &obj == tmp.release());
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj.unique());
|
2016-03-16 20:53:35 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == tmp.release());
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == tmp.get());
|
2011-02-25 18:10:29 +00:00
|
|
|
|
|
|
|
obj.ref();
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj.unique());
|
2011-02-25 18:10:29 +00:00
|
|
|
{
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<RefClass> tmp2(&obj);
|
2011-02-25 18:10:29 +00:00
|
|
|
}
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj.unique());
|
2011-02-25 18:10:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 18:44:23 +00:00
|
|
|
static void test_autostarray(skiatest::Reporter* reporter) {
|
|
|
|
RefClass obj0(0);
|
|
|
|
RefClass obj1(1);
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
{
|
2016-11-04 15:49:42 +00:00
|
|
|
SkAutoSTArray<2, sk_sp<RefClass> > tmp;
|
2013-07-12 18:44:23 +00:00
|
|
|
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());
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, !obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
// test out reset with data in the array (and a new allocation)
|
|
|
|
tmp.reset(0);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == tmp.count());
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2013-07-12 18:44:23 +00:00
|
|
|
tmp.reset(2); // this should use the preexisting allocation
|
|
|
|
REPORTER_ASSERT(reporter, 2 == tmp.count());
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2013-07-12 18:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test out destructor with data in the array (and using existing allocation)
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// test out allocating ctor (this should allocate new memory)
|
2016-11-04 15:49:42 +00:00
|
|
|
SkAutoSTArray<2, sk_sp<RefClass> > tmp(4);
|
2013-07-12 18:44:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, 4 == tmp.count());
|
|
|
|
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, !obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
// Test out resut with data in the array and malloced storage
|
|
|
|
tmp.reset(0);
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
tmp.reset(2); // this should use the preexisting storage
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, !obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
tmp.reset(4); // this should force a new malloc
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, !obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
}
|
|
|
|
|
2011-05-20 17:35:46 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2010-04-16 20:35:47 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
#define kSEARCH_COUNT 91
|
|
|
|
|
|
|
|
static void test_search(skiatest::Reporter* reporter) {
|
|
|
|
int i, array[kSEARCH_COUNT];
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2009-02-27 16:24:51 +00:00
|
|
|
|
|
|
|
for (i = 0; i < kSEARCH_COUNT; i++) {
|
|
|
|
array[i] = rand.nextS();
|
|
|
|
}
|
|
|
|
|
2020-07-14 00:13:51 +00:00
|
|
|
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]);
|
|
|
|
}
|
2009-02-27 16:24:51 +00:00
|
|
|
|
|
|
|
// 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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Utils, reporter) {
|
2009-02-27 16:24:51 +00:00
|
|
|
test_search(reporter);
|
2011-02-25 18:10:29 +00:00
|
|
|
test_autounref(reporter);
|
2013-07-12 18:44:23 +00:00
|
|
|
test_autostarray(reporter);
|
2009-02-27 16:24:51 +00:00
|
|
|
}
|
2019-08-08 16:30:13 +00:00
|
|
|
|
2020-05-26 16:58:59 +00:00
|
|
|
DEF_TEST(SkSpan, reporter) {
|
2019-08-08 16:30:13 +00:00
|
|
|
// Test constness preservation for SkMakeSpan.
|
|
|
|
{
|
|
|
|
std::vector<int> v = {{1, 2, 3, 4, 5}};
|
2020-05-26 16:58:59 +00:00
|
|
|
auto s = SkSpan(v);
|
2019-08-08 16:30:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, s[3] == 4);
|
|
|
|
s[3] = 100;
|
|
|
|
REPORTER_ASSERT(reporter, s[3] == 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<int> t = {{1, 2, 3, 4, 5}};
|
|
|
|
const std::vector<int>& v = t;
|
2020-05-26 16:58:59 +00:00
|
|
|
auto s = SkSpan(v);
|
2019-08-08 16:30:13 +00:00
|
|
|
//s[3] = 100; // Should fail to compile
|
|
|
|
REPORTER_ASSERT(reporter, s[3] == 4);
|
|
|
|
REPORTER_ASSERT(reporter, t[3] == 4);
|
|
|
|
t[3] = 100;
|
|
|
|
REPORTER_ASSERT(reporter, s[3] == 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::array<int, 5> v = {{1, 2, 3, 4, 5}};
|
2020-05-26 16:58:59 +00:00
|
|
|
auto s = SkSpan(v);
|
2019-08-08 16:30:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, s[3] == 4);
|
|
|
|
s[3] = 100;
|
|
|
|
REPORTER_ASSERT(reporter, s[3] == 100);
|
2020-05-13 16:01:24 +00:00
|
|
|
auto s1 = s.subspan(1,3);
|
|
|
|
REPORTER_ASSERT(reporter, s1.size() == 3);
|
|
|
|
REPORTER_ASSERT(reporter, s1.front() == 2);
|
|
|
|
REPORTER_ASSERT(reporter, s1.back() == 100);
|
2019-08-08 16:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::array<int, 5> t = {{1, 2, 3, 4, 5}};
|
|
|
|
const std::array<int, 5>& v = t;
|
2020-05-26 16:58:59 +00:00
|
|
|
auto s = SkSpan(v);
|
2019-08-08 16:30:13 +00:00
|
|
|
//s[3] = 100; // Should fail to compile
|
|
|
|
REPORTER_ASSERT(reporter, s[3] == 4);
|
|
|
|
REPORTER_ASSERT(reporter, t[3] == 4);
|
|
|
|
t[3] = 100;
|
|
|
|
REPORTER_ASSERT(reporter, s[3] == 100);
|
|
|
|
}
|
2019-09-19 15:27:43 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<int> v;
|
2020-05-26 16:58:59 +00:00
|
|
|
auto s = SkSpan(v);
|
2019-09-19 15:27:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, s.empty());
|
|
|
|
}
|
2019-08-08 16:30:13 +00:00
|
|
|
}
|
2019-09-09 15:36:39 +00:00
|
|
|
|
2019-07-03 22:05:12 +00:00
|
|
|
DEF_TEST(SkEnumerate, reporter) {
|
|
|
|
|
|
|
|
int A[] = {1, 2, 3, 4};
|
|
|
|
auto enumeration = SkMakeEnumerate(A);
|
|
|
|
|
|
|
|
size_t check = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [i, v] : enumeration) {
|
2019-07-03 22:05:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, i == check);
|
|
|
|
REPORTER_ASSERT(reporter, v == (int)check+1);
|
|
|
|
|
|
|
|
check++;
|
|
|
|
}
|
|
|
|
|
|
|
|
check = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [i, v] : SkMakeEnumerate(A)) {
|
2019-07-03 22:05:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, i == check);
|
|
|
|
REPORTER_ASSERT(reporter, v == (int)check+1);
|
|
|
|
|
|
|
|
check++;
|
|
|
|
}
|
|
|
|
|
|
|
|
check = 0;
|
|
|
|
std::vector<int> vec = {1, 2, 3, 4};
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [i, v] : SkMakeEnumerate(vec)) {
|
2019-07-03 22:05:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, i == check);
|
|
|
|
REPORTER_ASSERT(reporter, v == (int)check+1);
|
|
|
|
check++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, check == 4);
|
|
|
|
|
|
|
|
check = 0;
|
2020-05-26 16:58:59 +00:00
|
|
|
for (auto [i, v] : SkMakeEnumerate(SkSpan(vec))) {
|
2019-07-03 22:05:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, i == check);
|
|
|
|
REPORTER_ASSERT(reporter, v == (int)check+1);
|
|
|
|
check++;
|
|
|
|
}
|
2020-01-07 22:53:58 +00:00
|
|
|
|
|
|
|
{
|
2020-05-26 16:58:59 +00:00
|
|
|
auto e = SkMakeEnumerate(SkSpan(vec)).first(2);
|
2020-01-07 22:53:58 +00:00
|
|
|
for (auto[i, v] : e) {
|
|
|
|
REPORTER_ASSERT(reporter, v == (int) i + 1);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, e.size() == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-05-26 16:58:59 +00:00
|
|
|
auto e = SkMakeEnumerate(SkSpan(vec)).last(2);
|
2020-01-07 22:53:58 +00:00
|
|
|
for (auto[i, v] : e) {
|
|
|
|
REPORTER_ASSERT(reporter, v == (int) i + 1);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, e.size() == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-05-26 16:58:59 +00:00
|
|
|
auto e = SkMakeEnumerate(SkSpan(vec)).subspan(1, 2);
|
2020-01-07 22:53:58 +00:00
|
|
|
for (auto[i, v] : e) {
|
|
|
|
REPORTER_ASSERT(reporter, v == (int) i + 1);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, e.size() == 2);
|
|
|
|
}
|
2019-07-03 22:05:12 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:36:39 +00:00
|
|
|
DEF_TEST(SkZip, reporter) {
|
|
|
|
uint16_t A[] = {1, 2, 3, 4};
|
|
|
|
const float B[] = {10.f, 20.f, 30.f, 40.f};
|
|
|
|
std::vector<int> C = {{20, 30, 40, 50}};
|
|
|
|
std::array<int, 4> D = {{100, 200, 300, 400}};
|
2020-05-26 16:58:59 +00:00
|
|
|
SkSpan<int> S = SkSpan(C);
|
2019-09-09 15:36:39 +00:00
|
|
|
|
|
|
|
// Check SkZip calls
|
|
|
|
SkZip<uint16_t, const float, int, int, int>
|
|
|
|
z{4, &A[0], &B[0], C.data(), D.data(), S.data()};
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, z.size() == 4);
|
|
|
|
REPORTER_ASSERT(reporter, !z.empty());
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check front
|
|
|
|
auto t = z.front();
|
|
|
|
REPORTER_ASSERT(reporter, std::get<0>(t) == 1);
|
|
|
|
REPORTER_ASSERT(reporter, std::get<1>(t) == 10.f);
|
|
|
|
REPORTER_ASSERT(reporter, std::get<2>(t) == 20);
|
|
|
|
REPORTER_ASSERT(reporter, std::get<3>(t) == 100);
|
|
|
|
REPORTER_ASSERT(reporter, std::get<4>(t) == 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check back
|
|
|
|
auto t = z.back();
|
|
|
|
REPORTER_ASSERT(reporter, std::get<0>(t) == 4);
|
|
|
|
REPORTER_ASSERT(reporter, std::get<1>(t) == 40.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check ranged-for
|
|
|
|
int i = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [a, b, c, d, s] : z) {
|
2019-09-09 15:36:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-10-08 14:22:33 +00:00
|
|
|
// Check first(n)
|
|
|
|
int i = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [a, b, c, d, s] : z.first(2)) {
|
2019-10-08 14:22:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 2);
|
|
|
|
}
|
|
|
|
|
2019-11-15 21:06:45 +00:00
|
|
|
{
|
|
|
|
// Check last(n)
|
|
|
|
int i = 0;
|
|
|
|
for (auto t : z.last(2)) {
|
|
|
|
uint16_t a; float b; int c; int d; int s;
|
|
|
|
std::tie(a, b, c, d, s) = t;
|
|
|
|
REPORTER_ASSERT(reporter, a == A[i + 2]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i + 2]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i + 2]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i + 2]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i + 2]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 2);
|
|
|
|
}
|
2019-10-08 14:22:33 +00:00
|
|
|
|
2019-11-15 22:08:53 +00:00
|
|
|
{
|
|
|
|
// Check subspan(offset, count)
|
|
|
|
int i = 0;
|
|
|
|
for (auto t : z.subspan(1, 2)) {
|
|
|
|
uint16_t a; float b; int c; int d; int s;
|
|
|
|
std::tie(a, b, c, d, s) = t;
|
|
|
|
REPORTER_ASSERT(reporter, a == A[i + 1]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i + 1]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i + 1]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i + 1]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i + 1]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 2);
|
|
|
|
}
|
|
|
|
|
2019-10-08 14:22:33 +00:00
|
|
|
{
|
|
|
|
// Check copy.
|
|
|
|
auto zz{z};
|
2019-09-09 15:36:39 +00:00
|
|
|
int i = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [a, b, c, d, s] : zz) {
|
2019-09-09 15:36:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-10-08 14:22:33 +00:00
|
|
|
// Check const restricting copy
|
|
|
|
SkZip<const uint16_t, const float, const int, int, int> cz = z;
|
|
|
|
int i = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [a, b, c, d, s] : cz) {
|
2019-10-08 14:22:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check data() returns all the original pointers
|
|
|
|
auto ptrs = z.data();
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
ptrs == std::make_tuple(&A[0], &B[0], C.data(), D.data(), S.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check index getter
|
2019-09-09 15:36:39 +00:00
|
|
|
auto span = z.get<1>();
|
|
|
|
REPORTER_ASSERT(reporter, span[1] == 20.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following mutates the data.
|
|
|
|
{
|
|
|
|
// Check indexing
|
2019-11-12 19:24:41 +00:00
|
|
|
auto [a, b, c, d, e] = z[1];
|
|
|
|
REPORTER_ASSERT(reporter, a == 2);
|
|
|
|
REPORTER_ASSERT(reporter, b == 20.f);
|
|
|
|
REPORTER_ASSERT(reporter, c == 30);
|
|
|
|
REPORTER_ASSERT(reporter, d == 200);
|
|
|
|
REPORTER_ASSERT(reporter, e == 30);
|
2019-09-09 15:36:39 +00:00
|
|
|
|
|
|
|
// Check correct refs returned.
|
2019-11-12 19:24:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, &a == &A[1]);
|
|
|
|
REPORTER_ASSERT(reporter, &b == &B[1]);
|
|
|
|
REPORTER_ASSERT(reporter, &c == &C[1]);
|
|
|
|
REPORTER_ASSERT(reporter, &d == &D[1]);
|
|
|
|
REPORTER_ASSERT(reporter, &e == &S[1]);
|
2019-09-09 15:36:39 +00:00
|
|
|
|
|
|
|
// Check assignment
|
2019-11-12 19:24:41 +00:00
|
|
|
a = 20;
|
2019-09-09 15:36:39 +00:00
|
|
|
// std::get<1>(t) = 300.f; // is const
|
2019-11-12 19:24:41 +00:00
|
|
|
c = 300;
|
|
|
|
d = 2000;
|
|
|
|
e = 300;
|
2019-09-09 15:36:39 +00:00
|
|
|
|
|
|
|
auto t1 = z[1];
|
|
|
|
REPORTER_ASSERT(reporter, std::get<0>(t1) == 20);
|
|
|
|
REPORTER_ASSERT(reporter, std::get<1>(t1) == 20.f);
|
|
|
|
REPORTER_ASSERT(reporter, std::get<2>(t1) == 300);
|
|
|
|
REPORTER_ASSERT(reporter, std::get<3>(t1) == 2000);
|
|
|
|
REPORTER_ASSERT(reporter, std::get<4>(t1) == 300);
|
|
|
|
}
|
|
|
|
}
|
2019-09-19 15:27:43 +00:00
|
|
|
|
|
|
|
DEF_TEST(SkMakeZip, reporter) {
|
|
|
|
uint16_t A[] = {1, 2, 3, 4};
|
|
|
|
const float B[] = {10.f, 20.f, 30.f, 40.f};
|
|
|
|
const std::vector<int> C = {{20, 30, 40, 50}};
|
|
|
|
std::array<int, 4> D = {{100, 200, 300, 400}};
|
2020-05-26 16:58:59 +00:00
|
|
|
SkSpan<const int> S = SkSpan(C);
|
2019-09-19 15:27:43 +00:00
|
|
|
uint16_t* P = &A[0];
|
|
|
|
{
|
|
|
|
// Check make zip
|
|
|
|
auto zz = SkMakeZip(&A[0], B, C, D, S, P);
|
|
|
|
|
|
|
|
int i = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [a, b, c, d, s, p] : zz) {
|
2019-09-19 15:27:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
REPORTER_ASSERT(reporter, p == P[i]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check SkMakeZip in ranged for check OneSize calc of B.
|
|
|
|
int i = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [a, b, c, d, s] : SkMakeZip(&A[0], B, C, D, S)) {
|
2019-09-19 15:27:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check SkMakeZip in ranged for OneSize of C
|
|
|
|
int i = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [a, b, c, d, s] : SkMakeZip(&A[0], &B[0], C, D, S)) {
|
2019-09-19 15:27:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check SkMakeZip in ranged for OneSize for S
|
|
|
|
int i = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [s, a, b, c, d] : SkMakeZip(S, A, B, C, D)) {
|
2019-09-19 15:27:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check SkMakeZip in ranged for
|
|
|
|
int i = 0;
|
2019-11-12 19:24:41 +00:00
|
|
|
for (auto [c, s, a, b, d] : SkMakeZip(C, S, A, B, D)) {
|
2019-09-19 15:27:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, i = 4);
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:05:12 +00:00
|
|
|
{
|
|
|
|
// Check SkEnumerate and SkMakeZip in ranged for
|
|
|
|
auto zz = SkMakeZip(A, B, C, D, S);
|
2019-11-14 18:51:43 +00:00
|
|
|
for (auto [i, a, b, c, d, s] : SkMakeEnumerate(zz)) {
|
2019-07-03 22:05:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check SkEnumerate and SkMakeZip in ranged for
|
|
|
|
const auto& zz = SkMakeZip(A, B, C, D, S);
|
2019-11-14 18:51:43 +00:00
|
|
|
for (auto [i, a, b, c, d, s] : SkMakeEnumerate(zz)) {
|
2019-07-03 22:05:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check SkEnumerate and SkMakeZip in ranged for
|
2019-11-14 18:51:43 +00:00
|
|
|
for (auto [i, a, b, c, d, s] : SkMakeEnumerate(SkMakeZip(A, B, C, D, S))) {
|
2019-07-03 22:05:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, a == A[i]);
|
|
|
|
REPORTER_ASSERT(reporter, b == B[i]);
|
|
|
|
REPORTER_ASSERT(reporter, c == C[i]);
|
|
|
|
REPORTER_ASSERT(reporter, d == D[i]);
|
|
|
|
REPORTER_ASSERT(reporter, s == S[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:27:43 +00:00
|
|
|
{
|
|
|
|
std::vector<int>v;
|
|
|
|
auto z = SkMakeZip(v);
|
|
|
|
REPORTER_ASSERT(reporter, z.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
constexpr static uint16_t cA[] = {1, 2, 3, 4};
|
|
|
|
// Not constexpr in stdc++11 library.
|
|
|
|
//constexpr static std::array<int, 4> cD = {{100, 200, 300, 400}};
|
|
|
|
constexpr static const uint16_t* cP = &cA[0];
|
|
|
|
constexpr auto z = SkMakeZip(cA, cP);
|
|
|
|
REPORTER_ASSERT(reporter, !z.empty());
|
|
|
|
}
|
|
|
|
}
|