From 0d9e9bee17aa2901582c5461ae60f7241fc0cd59 Mon Sep 17 00:00:00 2001 From: bungeman Date: Wed, 20 Apr 2016 10:22:20 -0700 Subject: SkTArray movable and swap for move only elements. SkTArray cannot currently contain move only elements because its swap currently requires the SkTArray to be copyable. This makes SkTArray movable and makes its swap move instead of copy. Review URL: https://codereview.chromium.org/1904663004 --- tests/TArrayTest.cpp | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) (limited to 'tests/TArrayTest.cpp') diff --git a/tests/TArrayTest.cpp b/tests/TArrayTest.cpp index abf1075bc5..675aa33bf8 100644 --- a/tests/TArrayTest.cpp +++ b/tests/TArrayTest.cpp @@ -58,15 +58,10 @@ static void TestTSet_basic(skiatest::Reporter* reporter) { // {0, 3, 2 } } -static void test_swap(skiatest::Reporter* reporter) { - SkTArray arr; - SkSTArray< 5, int> arr5; - SkSTArray<10, int> arr10; - SkSTArray<20, int> arr20; - - SkTArray* arrays[] = { &arr, &arr5, &arr10, &arr20 }; - int sizes[] = {0, 1, 5, 10, 15, 20, 25}; - +template static void test_swap(skiatest::Reporter* reporter, + SkTArray* (&arrays)[4], + int (&sizes)[7]) +{ for (auto a : arrays) { for (auto b : arrays) { if (a == b) { @@ -87,16 +82,41 @@ static void test_swap(skiatest::Reporter* reporter) { REPORTER_ASSERT(reporter, a->count() == sizeB); curr = 0; - for (int x : *b) { REPORTER_ASSERT(reporter, x == curr++); } - for (int x : *a) { REPORTER_ASSERT(reporter, x == curr++); } + for (auto&& x : *b) { REPORTER_ASSERT(reporter, x == curr++); } + for (auto&& x : *a) { REPORTER_ASSERT(reporter, x == curr++); } a->swap(a); curr = sizeA; - for (int x : *a) { REPORTER_ASSERT(reporter, x == curr++); } + for (auto&& x : *a) { REPORTER_ASSERT(reporter, x == curr++); } }} }} } +static void test_swap(skiatest::Reporter* reporter) { + int sizes[] = {0, 1, 5, 10, 15, 20, 25}; + + SkTArray arr; + SkSTArray< 5, int> arr5; + SkSTArray<10, int> arr10; + SkSTArray<20, int> arr20; + SkTArray* arrays[] = { &arr, &arr5, &arr10, &arr20 }; + test_swap(reporter, arrays, sizes); + + struct MoveOnlyInt { + MoveOnlyInt(int i) : fInt(i) {} + MoveOnlyInt(MoveOnlyInt&& that) : fInt(that.fInt) {} + bool operator==(int i) { return fInt == i; } + int fInt; + }; + + SkTArray moi; + SkSTArray< 5, MoveOnlyInt> moi5; + SkSTArray<10, MoveOnlyInt> moi10; + SkSTArray<20, MoveOnlyInt> moi20; + SkTArray* arraysMoi[] = { &moi, &moi5, &moi10, &moi20 }; + test_swap(reporter, arraysMoi, sizes); +} + DEF_TEST(TArray, reporter) { TestTSet_basic(reporter); TestTSet_basic(reporter); -- cgit v1.2.3