• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
next()13     SListEntry* next() { return getSListNext(); }
14 private:
15     SK_DECLARE_INTERNAL_SLIST_INTERFACE(SListEntry);
16 };
17 
verifyEmptyList(skiatest::Reporter * reporter,const SkTInternalSList<SListEntry> & list,const char * stage)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 
verifyList(skiatest::Reporter * reporter,const SkTInternalSList<SListEntry> & list,const char * stage,SListEntry * start,int count,int step=1)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, list.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 
testTInternalSList(skiatest::Reporter * reporter)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 ((index != 0) &&
83             !verifyList(reporter, list, "pop", &testArray[index-1], index, -1)) {
84             return;
85         }
86     }
87     verifyEmptyList(reporter, list, "end");
88     // Move between list tests
89     for (int index = 0; index < testArraySize; ++index) {
90         list.push(&testArray[index]);
91     }
92     verifyList(reporter, list, "swap", &testArray[testArraySize-1], testArraySize, -1);
93     SkTInternalSList<SListEntry> other;
94     // Check swap moves the list over unchanged
95     other.swap(&list);
96     verifyEmptyList(reporter, list, "swap");
97     verifyList(reporter, other, "swap", &testArray[testArraySize-1], testArraySize, -1);
98     // Check pushAll optimizes to a swap when one of the is empty
99     list.pushAll(&other);
100     verifyList(reporter, list, "pushAll-empty", &testArray[testArraySize-1], testArraySize, -1);
101     verifyEmptyList(reporter, other, "pushAll-empty");
102     // Check pushAll when non empty works
103     other.push(list.pop());
104     other.pushAll(&list);
105     verifyEmptyList(reporter, list, "pushAll");
106     verifyList(reporter, other, "pushAll", &testArray[0], testArraySize, 1);
107 }
108 
DEF_TEST(SList,reporter)109 DEF_TEST(SList, reporter) {
110     testTInternalSList(reporter);
111 }
112