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
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
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
61DEF_TEST(TArray, reporter) {
62    TestTSet_basic<true>(reporter);
63    TestTSet_basic<false>(reporter);
64}
65