diff options
author | bsalomon <bsalomon@google.com> | 2015-02-10 19:46:58 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-10 19:46:58 -0800 |
commit | 3632f8473a7842b6f1351694d9b4071a66529224 (patch) | |
tree | 0a9eb331c06bcdcff8aa39457df673338ba1654d | |
parent | d160192fd9bab3c81d2dc40dbed41cdd7d660988 (diff) |
Add tests for STArray swap
Review URL: https://codereview.chromium.org/909583003
-rw-r--r-- | include/core/SkTArray.h | 3 | ||||
-rw-r--r-- | tests/TArrayTest.cpp | 54 |
2 files changed, 57 insertions, 0 deletions
diff --git a/include/core/SkTArray.h b/include/core/SkTArray.h index 9d410c12ea..dea0e38fec 100644 --- a/include/core/SkTArray.h +++ b/include/core/SkTArray.h @@ -273,6 +273,9 @@ public: /** Swaps the contents of this array with that array. Does a pointer swap if possible, otherwise copies the T values. */ void swap(SkTArray* that) { + if (this == that) { + return; + } if (this->fPreAllocMemArray != this->fItemArray && that->fPreAllocMemArray != that->fItemArray) { // If neither is using a preallocated array then just swap. diff --git a/tests/TArrayTest.cpp b/tests/TArrayTest.cpp index b85efcac07..8c1e8e4532 100644 --- a/tests/TArrayTest.cpp +++ b/tests/TArrayTest.cpp @@ -58,7 +58,61 @@ static void TestTSet_basic(skiatest::Reporter* reporter) { // {0, 3, 2 } } +namespace { +SkTArray<int>* make() { + typedef SkTArray<int> IntArray; + return SkNEW(IntArray); +} + +template <int N> SkTArray<int>* make_s() { + typedef SkSTArray<N, int> IntArray; + return SkNEW(IntArray); +} +} + +static void test_swap(skiatest::Reporter* reporter) { + typedef SkTArray<int>* (*ArrayMaker)(); + ArrayMaker arrayMakers[] = {make, make_s<5>, make_s<10>, make_s<20>}; + static int kSizes[] = {0, 1, 5, 10, 15, 20, 25}; + for (size_t arrayA = 0; arrayA < SK_ARRAY_COUNT(arrayMakers); ++arrayA) { + for (size_t arrayB = arrayA; arrayB < SK_ARRAY_COUNT(arrayMakers); ++arrayB) { + for (size_t dataSizeA = 0; dataSizeA < SK_ARRAY_COUNT(kSizes); ++dataSizeA) { + for (size_t dataSizeB = 0; dataSizeB < SK_ARRAY_COUNT(kSizes); ++dataSizeB) { + int curr = 0; + SkTArray<int>* a = arrayMakers[arrayA](); + SkTArray<int>* b = arrayMakers[arrayB](); + for (int i = 0; i < kSizes[dataSizeA]; ++i) { + a->push_back(curr++); + } + for (int i = 0; i < kSizes[dataSizeB]; ++i) { + b->push_back(curr++); + } + a->swap(b); + REPORTER_ASSERT(reporter, kSizes[dataSizeA] == b->count()); + REPORTER_ASSERT(reporter, kSizes[dataSizeB] == a->count()); + curr = 0; + for (int i = 0; i < kSizes[dataSizeA]; ++i) { + REPORTER_ASSERT(reporter, curr++ == (*b)[i]); + } + for (int i = 0; i < kSizes[dataSizeB]; ++i) { + REPORTER_ASSERT(reporter, curr++ == (*a)[i]); + } + SkDELETE(b); + + a->swap(a); + curr = kSizes[dataSizeA]; + for (int i = 0; i < kSizes[dataSizeB]; ++i) { + REPORTER_ASSERT(reporter, curr++ == (*a)[i]); + } + SkDELETE(a); + } + } + } + } +} + DEF_TEST(TArray, reporter) { TestTSet_basic<true>(reporter); TestTSet_basic<false>(reporter); + test_swap(reporter); } |