blob: 675aa33bf86e2b43287b8d2d194fd82dbddce4ba [file] [log] [blame]
bungeman@google.com95ebd172014-03-21 19:39:02 +00001/*
2 * Copyright 2014 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 */
7
8#include "SkTArray.h"
9#include "Test.h"
10
11// Tests the SkTArray<T> class template.
12
13template <bool MEM_COPY>
14static void TestTSet_basic(skiatest::Reporter* reporter) {
15 SkTArray<int, MEM_COPY> a;
16
17 // Starts empty.
18 REPORTER_ASSERT(reporter, a.empty());
19 REPORTER_ASSERT(reporter, a.count() == 0);
20
21 // { }, add a default constructed element
bungeman@google.com34ce63c2014-03-21 20:14:05 +000022 a.push_back() = 0;
bungeman@google.com95ebd172014-03-21 19:39:02 +000023 REPORTER_ASSERT(reporter, !a.empty());
24 REPORTER_ASSERT(reporter, a.count() == 1);
25
26 // { 0 }, removeShuffle the only element.
27 a.removeShuffle(0);
28 REPORTER_ASSERT(reporter, a.empty());
29 REPORTER_ASSERT(reporter, a.count() == 0);
30
31 // { }, add a default, add a 1, remove first
bungeman@google.com34ce63c2014-03-21 20:14:05 +000032 a.push_back() = 0;
bungeman@google.com95ebd172014-03-21 19:39:02 +000033 REPORTER_ASSERT(reporter, a.push_back() = 1);
34 a.removeShuffle(0);
35 REPORTER_ASSERT(reporter, !a.empty());
36 REPORTER_ASSERT(reporter, a.count() == 1);
37 REPORTER_ASSERT(reporter, a[0] == 1);
38
39 // { 1 }, replace with new array
40 int b[5] = { 0, 1, 2, 3, 4 };
41 a.reset(b, SK_ARRAY_COUNT(b));
42 REPORTER_ASSERT(reporter, a.count() == SK_ARRAY_COUNT(b));
43 REPORTER_ASSERT(reporter, a[2] == 2);
44 REPORTER_ASSERT(reporter, a[4] == 4);
45
46 // { 0, 1, 2, 3, 4 }, removeShuffle the last
47 a.removeShuffle(4);
48 REPORTER_ASSERT(reporter, a.count() == SK_ARRAY_COUNT(b) - 1);
49 REPORTER_ASSERT(reporter, a[3] == 3);
50
51 // { 0, 1, 2, 3 }, remove a middle, note shuffle
52 a.removeShuffle(1);
53 REPORTER_ASSERT(reporter, a.count() == SK_ARRAY_COUNT(b) - 2);
54 REPORTER_ASSERT(reporter, a[0] == 0);
55 REPORTER_ASSERT(reporter, a[1] == 3);
56 REPORTER_ASSERT(reporter, a[2] == 2);
57
58 // {0, 3, 2 }
59}
60
bungeman0d9e9be2016-04-20 10:22:20 -070061template <typename T> static void test_swap(skiatest::Reporter* reporter,
62 SkTArray<T>* (&arrays)[4],
63 int (&sizes)[7])
64{
mtklein86211002016-03-03 09:48:53 -080065 for (auto a : arrays) {
66 for (auto b : arrays) {
67 if (a == b) {
68 continue;
bsalomon3632f842015-02-10 19:46:58 -080069 }
mtklein86211002016-03-03 09:48:53 -080070
71 for (auto sizeA : sizes) {
72 for (auto sizeB : sizes) {
73 a->reset();
74 b->reset();
75
76 int curr = 0;
77 for (int i = 0; i < sizeA; i++) { a->push_back(curr++); }
78 for (int i = 0; i < sizeB; i++) { b->push_back(curr++); }
79
80 a->swap(b);
81 REPORTER_ASSERT(reporter, b->count() == sizeA);
82 REPORTER_ASSERT(reporter, a->count() == sizeB);
83
84 curr = 0;
bungeman0d9e9be2016-04-20 10:22:20 -070085 for (auto&& x : *b) { REPORTER_ASSERT(reporter, x == curr++); }
86 for (auto&& x : *a) { REPORTER_ASSERT(reporter, x == curr++); }
mtklein86211002016-03-03 09:48:53 -080087
88 a->swap(a);
89 curr = sizeA;
bungeman0d9e9be2016-04-20 10:22:20 -070090 for (auto&& x : *a) { REPORTER_ASSERT(reporter, x == curr++); }
mtklein86211002016-03-03 09:48:53 -080091 }}
92 }}
bsalomon3632f842015-02-10 19:46:58 -080093}
94
bungeman0d9e9be2016-04-20 10:22:20 -070095static void test_swap(skiatest::Reporter* reporter) {
96 int sizes[] = {0, 1, 5, 10, 15, 20, 25};
97
98 SkTArray<int> arr;
99 SkSTArray< 5, int> arr5;
100 SkSTArray<10, int> arr10;
101 SkSTArray<20, int> arr20;
102 SkTArray<int>* arrays[] = { &arr, &arr5, &arr10, &arr20 };
103 test_swap(reporter, arrays, sizes);
104
105 struct MoveOnlyInt {
106 MoveOnlyInt(int i) : fInt(i) {}
107 MoveOnlyInt(MoveOnlyInt&& that) : fInt(that.fInt) {}
108 bool operator==(int i) { return fInt == i; }
109 int fInt;
110 };
111
112 SkTArray<MoveOnlyInt> moi;
113 SkSTArray< 5, MoveOnlyInt> moi5;
114 SkSTArray<10, MoveOnlyInt> moi10;
115 SkSTArray<20, MoveOnlyInt> moi20;
116 SkTArray<MoveOnlyInt>* arraysMoi[] = { &moi, &moi5, &moi10, &moi20 };
117 test_swap(reporter, arraysMoi, sizes);
118}
119
bungeman@google.com95ebd172014-03-21 19:39:02 +0000120DEF_TEST(TArray, reporter) {
121 TestTSet_basic<true>(reporter);
122 TestTSet_basic<false>(reporter);
bsalomon3632f842015-02-10 19:46:58 -0800123 test_swap(reporter);
bungeman@google.com95ebd172014-03-21 19:39:02 +0000124}