bungeman@google.com | 95ebd17 | 2014-03-21 19:39:02 +0000 | [diff] [blame] | 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> |
| 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 |
bungeman@google.com | 34ce63c | 2014-03-21 20:14:05 +0000 | [diff] [blame] | 22 | a.push_back() = 0; |
bungeman@google.com | 95ebd17 | 2014-03-21 19:39:02 +0000 | [diff] [blame] | 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 |
bungeman@google.com | 34ce63c | 2014-03-21 20:14:05 +0000 | [diff] [blame] | 32 | a.push_back() = 0; |
bungeman@google.com | 95ebd17 | 2014-03-21 19:39:02 +0000 | [diff] [blame] | 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 | |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 61 | template <typename T> static void test_swap(skiatest::Reporter* reporter, |
| 62 | SkTArray<T>* (&arrays)[4], |
| 63 | int (&sizes)[7]) |
| 64 | { |
mtklein | 8621100 | 2016-03-03 09:48:53 -0800 | [diff] [blame] | 65 | for (auto a : arrays) { |
| 66 | for (auto b : arrays) { |
| 67 | if (a == b) { |
| 68 | continue; |
bsalomon | 3632f84 | 2015-02-10 19:46:58 -0800 | [diff] [blame] | 69 | } |
mtklein | 8621100 | 2016-03-03 09:48:53 -0800 | [diff] [blame] | 70 | |
| 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; |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 85 | for (auto&& x : *b) { REPORTER_ASSERT(reporter, x == curr++); } |
| 86 | for (auto&& x : *a) { REPORTER_ASSERT(reporter, x == curr++); } |
mtklein | 8621100 | 2016-03-03 09:48:53 -0800 | [diff] [blame] | 87 | |
| 88 | a->swap(a); |
| 89 | curr = sizeA; |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 90 | for (auto&& x : *a) { REPORTER_ASSERT(reporter, x == curr++); } |
mtklein | 8621100 | 2016-03-03 09:48:53 -0800 | [diff] [blame] | 91 | }} |
| 92 | }} |
bsalomon | 3632f84 | 2015-02-10 19:46:58 -0800 | [diff] [blame] | 93 | } |
| 94 | |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 95 | static 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.com | 95ebd17 | 2014-03-21 19:39:02 +0000 | [diff] [blame] | 120 | DEF_TEST(TArray, reporter) { |
| 121 | TestTSet_basic<true>(reporter); |
| 122 | TestTSet_basic<false>(reporter); |
bsalomon | 3632f84 | 2015-02-10 19:46:58 -0800 | [diff] [blame] | 123 | test_swap(reporter); |
bungeman@google.com | 95ebd17 | 2014-03-21 19:39:02 +0000 | [diff] [blame] | 124 | } |