1 /*
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
13 template <bool MEM_COPY>
TestTSet_basic(skiatest::Reporter * reporter)14 static 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
22 a.push_back() = 0;
23 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
32 a.push_back() = 0;
33 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
61 namespace {
make()62 SkTArray<int>* make() {
63 typedef SkTArray<int> IntArray;
64 return new IntArray;
65 }
66
make_s()67 template <int N> SkTArray<int>* make_s() {
68 typedef SkSTArray<N, int> IntArray;
69 return new IntArray;
70 }
71 }
72
test_swap(skiatest::Reporter * reporter)73 static void test_swap(skiatest::Reporter* reporter) {
74 typedef SkTArray<int>* (*ArrayMaker)();
75 ArrayMaker arrayMakers[] = {make, make_s<5>, make_s<10>, make_s<20>};
76 static int kSizes[] = {0, 1, 5, 10, 15, 20, 25};
77 for (size_t arrayA = 0; arrayA < SK_ARRAY_COUNT(arrayMakers); ++arrayA) {
78 for (size_t arrayB = arrayA; arrayB < SK_ARRAY_COUNT(arrayMakers); ++arrayB) {
79 for (size_t dataSizeA = 0; dataSizeA < SK_ARRAY_COUNT(kSizes); ++dataSizeA) {
80 for (size_t dataSizeB = 0; dataSizeB < SK_ARRAY_COUNT(kSizes); ++dataSizeB) {
81 int curr = 0;
82 SkTArray<int>* a = arrayMakers[arrayA]();
83 SkTArray<int>* b = arrayMakers[arrayB]();
84 for (int i = 0; i < kSizes[dataSizeA]; ++i) {
85 a->push_back(curr++);
86 }
87 for (int i = 0; i < kSizes[dataSizeB]; ++i) {
88 b->push_back(curr++);
89 }
90 a->swap(b);
91 REPORTER_ASSERT(reporter, kSizes[dataSizeA] == b->count());
92 REPORTER_ASSERT(reporter, kSizes[dataSizeB] == a->count());
93 curr = 0;
94 for (int i = 0; i < kSizes[dataSizeA]; ++i) {
95 REPORTER_ASSERT(reporter, curr++ == (*b)[i]);
96 }
97 for (int i = 0; i < kSizes[dataSizeB]; ++i) {
98 REPORTER_ASSERT(reporter, curr++ == (*a)[i]);
99 }
100 delete b;
101
102 a->swap(a);
103 curr = kSizes[dataSizeA];
104 for (int i = 0; i < kSizes[dataSizeB]; ++i) {
105 REPORTER_ASSERT(reporter, curr++ == (*a)[i]);
106 }
107 delete a;
108 }
109 }
110 }
111 }
112 }
113
DEF_TEST(TArray,reporter)114 DEF_TEST(TArray, reporter) {
115 TestTSet_basic<true>(reporter);
116 TestTSet_basic<false>(reporter);
117 test_swap(reporter);
118 }
119