From 95ebd17cf4f66952862289882ee5ac00da31cb8a Mon Sep 17 00:00:00 2001 From: "bungeman@google.com" Date: Fri, 21 Mar 2014 19:39:02 +0000 Subject: Add removeShuffle to SkTArray and add SkTArray tests. R=reed@google.com Review URL: https://codereview.chromium.org/208153008 git-svn-id: http://skia.googlecode.com/svn/trunk@13895 2bbb7eff-a529-9590-31e7-b0007b416f81 --- gyp/tests.gypi | 1 + include/core/SkTArray.h | 23 ++++++++++++++++-- tests/TArrayTest.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 tests/TArrayTest.cpp diff --git a/gyp/tests.gypi b/gyp/tests.gypi index a036dbd4d8..ccfa47dfc2 100644 --- a/gyp/tests.gypi +++ b/gyp/tests.gypi @@ -154,6 +154,7 @@ '../tests/StringTest.cpp', '../tests/StrokeTest.cpp', '../tests/SurfaceTest.cpp', + '../tests/TArrayTest.cpp', '../tests/TLSTest.cpp', '../tests/TSetTest.cpp', '../tests/TestSize.cpp', diff --git a/include/core/SkTArray.h b/include/core/SkTArray.h index f7bdc5a158..31bcabae18 100644 --- a/include/core/SkTArray.h +++ b/include/core/SkTArray.h @@ -16,6 +16,10 @@ template class SkTArray; namespace SkTArrayExt { +template +inline void copy(SkTArray* self, int dst, int src) { + memcpy(&self->fItemArray[dst], &self->fItemArray[src], sizeof(T)); +} template inline void copy(SkTArray* self, const T* array) { memcpy(self->fMemArray, array, self->fCount * sizeof(T)); @@ -25,6 +29,10 @@ inline void copyAndDelete(SkTArray* self, char* newMemArray) { memcpy(newMemArray, self->fMemArray, self->fCount * sizeof(T)); } +template +inline void copy(SkTArray* self, int dst, int src) { + SkNEW_PLACEMENT_ARGS(&self->fItemArray[dst], T, (self->fItemArray[src])); +} template inline void copy(SkTArray* self, const T* array) { for (int i = 0; i < self->fCount; ++i) { @@ -140,8 +148,17 @@ public: int delta = count - fCount; this->checkRealloc(delta); fCount = count; - for (int i = 0; i < count; ++i) { - SkTArrayExt::copy(this, array); + SkTArrayExt::copy(this, array); + } + + void removeShuffle(int n) { + SkASSERT(n < fCount); + int newCount = fCount - 1; + fCount = newCount; + fItemArray[n].~T(); + if (n != newCount) { + SkTArrayExt::copy(this, n, newCount); + fItemArray[newCount].~T(); } } @@ -427,9 +444,11 @@ private: friend void* operator new(size_t, SkTArray*, int); + template friend void SkTArrayExt::copy(SkTArray* that, int dst, int src); template friend void SkTArrayExt::copy(SkTArray* that, const X*); template friend void SkTArrayExt::copyAndDelete(SkTArray* that, char*); + template friend void SkTArrayExt::copy(SkTArray* that, int dst, int src); template friend void SkTArrayExt::copy(SkTArray* that, const X*); template friend void SkTArrayExt::copyAndDelete(SkTArray* that, char*); diff --git a/tests/TArrayTest.cpp b/tests/TArrayTest.cpp new file mode 100644 index 0000000000..f86cfe40f7 --- /dev/null +++ b/tests/TArrayTest.cpp @@ -0,0 +1,64 @@ +/* + * Copyright 2014 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkTArray.h" +#include "Test.h" + +// Tests the SkTArray class template. + +template +static void TestTSet_basic(skiatest::Reporter* reporter) { + SkTArray a; + + // Starts empty. + REPORTER_ASSERT(reporter, a.empty()); + REPORTER_ASSERT(reporter, a.count() == 0); + + // { }, add a default constructed element + REPORTER_ASSERT(reporter, a.push_back()); + REPORTER_ASSERT(reporter, !a.empty()); + REPORTER_ASSERT(reporter, a.count() == 1); + + // { 0 }, removeShuffle the only element. + a.removeShuffle(0); + REPORTER_ASSERT(reporter, a.empty()); + REPORTER_ASSERT(reporter, a.count() == 0); + + // { }, add a default, add a 1, remove first + REPORTER_ASSERT(reporter, a.push_back()); + REPORTER_ASSERT(reporter, a.push_back() = 1); + a.removeShuffle(0); + REPORTER_ASSERT(reporter, !a.empty()); + REPORTER_ASSERT(reporter, a.count() == 1); + REPORTER_ASSERT(reporter, a[0] == 1); + + // { 1 }, replace with new array + int b[5] = { 0, 1, 2, 3, 4 }; + a.reset(b, SK_ARRAY_COUNT(b)); + REPORTER_ASSERT(reporter, a.count() == SK_ARRAY_COUNT(b)); + REPORTER_ASSERT(reporter, a[2] == 2); + REPORTER_ASSERT(reporter, a[4] == 4); + + // { 0, 1, 2, 3, 4 }, removeShuffle the last + a.removeShuffle(4); + REPORTER_ASSERT(reporter, a.count() == SK_ARRAY_COUNT(b) - 1); + REPORTER_ASSERT(reporter, a[3] == 3); + + // { 0, 1, 2, 3 }, remove a middle, note shuffle + a.removeShuffle(1); + REPORTER_ASSERT(reporter, a.count() == SK_ARRAY_COUNT(b) - 2); + REPORTER_ASSERT(reporter, a[0] == 0); + REPORTER_ASSERT(reporter, a[1] == 3); + REPORTER_ASSERT(reporter, a[2] == 2); + + // {0, 3, 2 } +} + +DEF_TEST(TArray, reporter) { + TestTSet_basic(reporter); + TestTSet_basic(reporter); +} -- cgit v1.2.3