Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2012 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 "SkTInternalSList.h" | |
| 9 #include "Test.h" | |
| 10 | |
| 11 class SListEntry { | |
| 12 public: | |
| 13 SListEntry* next() { return getSListNext(); } | |
| 14 private: | |
| 15 SK_DECLARE_INTERNAL_SLIST_INTERFACE(SListEntry); | |
| 16 }; | |
| 17 | |
| 18 static bool verifyEmptyList(skiatest::Reporter* reporter, | |
| 19 const SkTInternalSList<SListEntry>& list, | |
| 20 const char* stage) { | |
| 21 | |
| 22 if (!list.isEmpty()) { | |
| 23 ERRORF(reporter, "%s - List not empty", stage); | |
| 24 return false; | |
| 25 } | |
| 26 if (0 != list.getCount()) { | |
| 27 ERRORF(reporter, "%s - List count is not zero, %d instead", stage, list. getCount()); | |
| 28 return false; | |
| 29 } | |
| 30 if (NULL != list.head()) { | |
| 31 ERRORF(reporter, "%s - List has elements when empty", stage); | |
| 32 return false; | |
| 33 } | |
| 34 return true; | |
| 35 } | |
| 36 | |
| 37 static bool verifyList(skiatest::Reporter* reporter, | |
| 38 const SkTInternalSList<SListEntry>& list, | |
| 39 const char* stage, | |
| 40 SListEntry* start, int count, int step = 1) { | |
| 41 SListEntry* next = list.head(); | |
| 42 if (list.getCount() != count) { | |
| 43 ERRORF(reporter, "%s - List was too short, %d instead of %d", stage, lis t.getCount(), count); | |
| 44 return false; | |
| 45 } | |
| 46 int index = 0; | |
| 47 for(SListEntry* value = start; index < count; value += step, ++index) { | |
| 48 if (NULL == next) { | |
| 49 ERRORF(reporter, "%s - List too short, should be %d", stage, count); | |
| 50 return false; | |
| 51 } | |
| 52 if (next!= value) { | |
| 53 ERRORF(reporter, "%s - List entries at index %d of %d don't match", stage, index, count); | |
| 54 return false; | |
| 55 } | |
| 56 next = next->next(); | |
| 57 } | |
| 58 if (NULL != next) { | |
| 59 ERRORF(reporter, "%s - List too long, should be %d", stage, count); | |
| 60 return false; | |
| 61 } | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 static void testTInternalSList(skiatest::Reporter* reporter) { | |
| 66 // Build a test array of data | |
| 67 static const int testArraySize = 10; | |
| 68 SListEntry testArray[testArraySize]; | |
| 69 // Basic add remove tests | |
| 70 SkTInternalSList<SListEntry> list; | |
| 71 verifyEmptyList(reporter, list, "start"); | |
| 72 // Push values in, testing on the way | |
| 73 for (int index = 0; index < testArraySize; ++index) { | |
| 74 list.push(&testArray[index]); | |
| 75 if (!verifyList(reporter, list, "push", &testArray[index], index+1, -1)) { | |
| 76 return; | |
| 77 } | |
| 78 } | |
| 79 // Now remove them again | |
| 80 for (int index = testArraySize - 1; index >= 0; --index) { | |
| 81 REPORTER_ASSERT(reporter, &testArray[index] == list.pop()); | |
| 82 if (!verifyList(reporter, list, "pop", &testArray[index-1], index, -1)) { | |
|
Tom Hudson
2014/03/14 12:02:52
This can go to index[-1], which the compiler is go
iancottrell
2014/03/14 13:14:47
No, I meant exactly what I wrote, which is totally
| |
| 83 return; | |
| 84 } | |
| 85 } | |
| 86 verifyEmptyList(reporter, list, "end"); | |
| 87 // Move between list tests | |
| 88 for (int index = 0; index < testArraySize; ++index) { | |
| 89 list.push(&testArray[index]); | |
| 90 } | |
| 91 verifyList(reporter, list, "swap", &testArray[testArraySize-1], testArraySiz e, -1); | |
| 92 SkTInternalSList<SListEntry> other; | |
| 93 // Check swap moves the list over unchanged | |
| 94 other.swap(&list); | |
| 95 verifyEmptyList(reporter, list, "swap"); | |
| 96 verifyList(reporter, other, "swap", &testArray[testArraySize-1], testArraySi ze, -1); | |
| 97 // Check pushAll optimizes to a swap when one of the is empty | |
| 98 list.pushAll(&other); | |
| 99 verifyList(reporter, list, "pushAll-empty", &testArray[testArraySize-1], tes tArraySize, -1); | |
| 100 verifyEmptyList(reporter, other, "pushAll-empty"); | |
| 101 // Check pushAll when non empty works | |
| 102 other.push(list.pop()); | |
| 103 other.pushAll(&list); | |
| 104 verifyEmptyList(reporter, list, "pushAll"); | |
| 105 verifyList(reporter, other, "pushAll", &testArray[0], testArraySize, 1); | |
| 106 } | |
| 107 | |
| 108 DEF_TEST(SList, reporter) { | |
| 109 testTInternalSList(reporter); | |
| 110 } | |
| OLD | NEW |