blob: 6a9b5f5a8189cd77cbb8b39ed0bd4c44bbb93677 [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
bsalomon3632f842015-02-10 19:46:58 -080061namespace {
62SkTArray<int>* make() {
63 typedef SkTArray<int> IntArray;
halcanary385fe4d2015-08-26 13:07:48 -070064 return new IntArray;
bsalomon3632f842015-02-10 19:46:58 -080065}
66
67template <int N> SkTArray<int>* make_s() {
68 typedef SkSTArray<N, int> IntArray;
halcanary385fe4d2015-08-26 13:07:48 -070069 return new IntArray;
bsalomon3632f842015-02-10 19:46:58 -080070}
71}
72
73static 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 }
halcanary385fe4d2015-08-26 13:07:48 -0700100 delete b;
bsalomon3632f842015-02-10 19:46:58 -0800101
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 }
halcanary385fe4d2015-08-26 13:07:48 -0700107 delete a;
bsalomon3632f842015-02-10 19:46:58 -0800108 }
109 }
110 }
111 }
112}
113
bungeman@google.com95ebd172014-03-21 19:39:02 +0000114DEF_TEST(TArray, reporter) {
115 TestTSet_basic<true>(reporter);
116 TestTSet_basic<false>(reporter);
bsalomon3632f842015-02-10 19:46:58 -0800117 test_swap(reporter);
bungeman@google.com95ebd172014-03-21 19:39:02 +0000118}