aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/SListTest.cpp
blob: 72a82814f9b8834a04d1143ae964e73e3447f0ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkTInternalSList.h"
#include "Test.h"

class SListEntry {
public:
    SListEntry* next() { return getSListNext(); }
private:
    SK_DECLARE_INTERNAL_SLIST_INTERFACE(SListEntry);
};

static bool verifyEmptyList(skiatest::Reporter* reporter,
                            const SkTInternalSList<SListEntry>& list,
                            const char* stage) {

    if (!list.isEmpty()) {
        ERRORF(reporter, "%s - List not empty", stage);
        return false;
    }
    if (0 != list.getCount()) {
        ERRORF(reporter, "%s - List count is not zero, %d instead", stage, list.getCount());
        return false;
    }
    if (NULL != list.head()) {
        ERRORF(reporter, "%s - List has elements when empty", stage);
        return false;
    }
    return true;
}

static bool verifyList(skiatest::Reporter* reporter,
                       const SkTInternalSList<SListEntry>& list,
                       const char* stage,
                       SListEntry* start, int count, int step = 1) {
    SListEntry* next = list.head();
    if (list.getCount() != count) {
        ERRORF(reporter, "%s - List was too short, %d instead of %d", stage, list.getCount(), count);
        return false;
    }
    int index = 0;
    for(SListEntry* value = start; index < count; value += step, ++index) {
        if (NULL == next) {
            ERRORF(reporter, "%s - List too short, should be %d", stage, count);
            return false;
        }
        if (next!= value) {
            ERRORF(reporter, "%s - List entries at index %d of %d don't match", stage, index, count);
            return false;
        }
        next = next->next();
    }
    if (NULL != next) {
        ERRORF(reporter, "%s - List too long, should be %d", stage, count);
        return false;
    }
    return true;
}

static void testTInternalSList(skiatest::Reporter* reporter) {
    // Build a test array of data
    static const int testArraySize = 10;
    SListEntry testArray[testArraySize];
    // Basic add remove tests
    SkTInternalSList<SListEntry> list;
    verifyEmptyList(reporter, list, "start");
    // Push values in, testing on the way
    for (int index = 0; index < testArraySize; ++index) {
        list.push(&testArray[index]);
        if (!verifyList(reporter, list, "push", &testArray[index], index+1, -1)) {
            return;
        }
    }
    // Now remove them again
    for (int index = testArraySize - 1; index >= 0; --index) {
        REPORTER_ASSERT(reporter, &testArray[index] == list.pop());
        if ((index != 0) &&
            !verifyList(reporter, list, "pop", &testArray[index-1], index, -1)) {
            return;
        }
    }
    verifyEmptyList(reporter, list, "end");
    // Move between list tests
    for (int index = 0; index < testArraySize; ++index) {
        list.push(&testArray[index]);
    }
    verifyList(reporter, list, "swap", &testArray[testArraySize-1], testArraySize, -1);
    SkTInternalSList<SListEntry> other;
    // Check swap moves the list over unchanged
    other.swap(&list);
    verifyEmptyList(reporter, list, "swap");
    verifyList(reporter, other, "swap", &testArray[testArraySize-1], testArraySize, -1);
    // Check pushAll optimizes to a swap when one of the is empty
    list.pushAll(&other);
    verifyList(reporter, list, "pushAll-empty", &testArray[testArraySize-1], testArraySize, -1);
    verifyEmptyList(reporter, other, "pushAll-empty");
    // Check pushAll when non empty works
    other.push(list.pop());
    other.pushAll(&list);
    verifyEmptyList(reporter, list, "pushAll");
    verifyList(reporter, other, "pushAll", &testArray[0], testArraySize, 1);
}

DEF_TEST(SList, reporter) {
    testTInternalSList(reporter);
}