blob: abf1075bc5be94a21741ae2f5e4c9f22071fb0bc [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 -080061static void test_swap(skiatest::Reporter* reporter) {
mtklein86211002016-03-03 09:48:53 -080062 SkTArray<int> arr;
63 SkSTArray< 5, int> arr5;
64 SkSTArray<10, int> arr10;
65 SkSTArray<20, int> arr20;
bsalomon3632f842015-02-10 19:46:58 -080066
mtklein86211002016-03-03 09:48:53 -080067 SkTArray<int>* arrays[] = { &arr, &arr5, &arr10, &arr20 };
68 int sizes[] = {0, 1, 5, 10, 15, 20, 25};
69
70 for (auto a : arrays) {
71 for (auto b : arrays) {
72 if (a == b) {
73 continue;
bsalomon3632f842015-02-10 19:46:58 -080074 }
mtklein86211002016-03-03 09:48:53 -080075
76 for (auto sizeA : sizes) {
77 for (auto sizeB : sizes) {
78 a->reset();
79 b->reset();
80
81 int curr = 0;
82 for (int i = 0; i < sizeA; i++) { a->push_back(curr++); }
83 for (int i = 0; i < sizeB; i++) { b->push_back(curr++); }
84
85 a->swap(b);
86 REPORTER_ASSERT(reporter, b->count() == sizeA);
87 REPORTER_ASSERT(reporter, a->count() == sizeB);
88
89 curr = 0;
90 for (int x : *b) { REPORTER_ASSERT(reporter, x == curr++); }
91 for (int x : *a) { REPORTER_ASSERT(reporter, x == curr++); }
92
93 a->swap(a);
94 curr = sizeA;
95 for (int x : *a) { REPORTER_ASSERT(reporter, x == curr++); }
96 }}
97 }}
bsalomon3632f842015-02-10 19:46:58 -080098}
99
bungeman@google.com95ebd172014-03-21 19:39:02 +0000100DEF_TEST(TArray, reporter) {
101 TestTSet_basic<true>(reporter);
102 TestTSet_basic<false>(reporter);
bsalomon3632f842015-02-10 19:46:58 -0800103 test_swap(reporter);
bungeman@google.com95ebd172014-03-21 19:39:02 +0000104}