blob: 1dc7e0a9c895d286e71c16a7a40a68e63799477b [file] [log] [blame]
scroggo@google.com7d8013f2013-11-20 21:40:57 +00001/*
2 * Copyright 2013 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 "SkTDStackNester.h"
9
10#include "Test.h"
11#include "TestClassDef.h"
12
13/**
14 * Test SkTDStackNester<int>::push(). Pushes the current count onto the stack,
15 * and checks that the count has increased by one.
16 */
17static void test_push(skiatest::Reporter* reporter, SkTDStackNester<int>* nester) {
18 SkASSERT(nester);
19 const int count = nester->count();
20 // test_pop depends on this value.
21 nester->push(count);
22 REPORTER_ASSERT(reporter, nester->count() == count + 1);
23}
24
25/**
26 * Test SkTDStackNester<int>::pop(). Pops the top element off the stack, and
27 * checks that the new count is one smaller, and that the popped element
28 * matches the new count (as was pushed by test_push).
29 */
30static void test_pop(skiatest::Reporter* reporter, SkTDStackNester<int>* nester) {
31 SkASSERT(nester);
32 const int count = nester->count();
33 // This test should not be called with a count <= 0.
34 SkASSERT(count > 0);
35 const int top = nester->top();
36 int value = -1;
37 nester->pop(&value);
38 REPORTER_ASSERT(reporter, top == value);
39 const int newCount = nester->count();
40 REPORTER_ASSERT(reporter, newCount == count - 1);
41 // Since test_push always pushes the count prior to the push, value should
42 // always be one less than count.
43 REPORTER_ASSERT(reporter, newCount == value);
44}
45
46/**
47 * Test nest() and unnest(). nest() is called, and it is confirmed that the
48 * count is now zero. Then test_push() is called inc times, followed by a call to
49 * unnest(). After this call, check that the count has returned to the initial count, and
50 * that nestingLevel() has returned to its initial value.
51 */
52static void test_nest(skiatest::Reporter* reporter, SkTDStackNester<int>* nester, int inc) {
53 SkASSERT(nester);
54 SkASSERT(inc > 0);
55 const int initialCount = nester->count();
56 const int initialNesting = nester->nestingLevel();
57
58 nester->nest();
59 REPORTER_ASSERT(reporter, nester->count() == 0);
60 REPORTER_ASSERT(reporter, nester->nestingLevel() == initialNesting + 1);
61
62 for (int i = 0; i < inc; ++i) {
63 test_push(reporter, nester);
64 }
65
66 nester->unnest();
67 REPORTER_ASSERT(reporter, nester->count() == initialCount);
68 REPORTER_ASSERT(reporter, nester->nestingLevel() == initialNesting);
69}
70
71class SkTDStackNesterTester {
72public:
73 static int GetSlotCount() {
74 return SkTDStackNester<int>::kSlotCount;
75 }
76};
77
78static void test_stack_nester(skiatest::Reporter* reporter) {
79 SkTDStackNester<int> nester;
80 int count = nester.count();
81 REPORTER_ASSERT(reporter, 0 == count);
82 REPORTER_ASSERT(reporter, nester.nestingLevel() == 0);
83 REPORTER_ASSERT(reporter, nester.empty());
84
85 // Test nesting (with arbitrary number of pushes) from the beginning.
86 test_nest(reporter, &nester, 3);
87
88 const int slotCount = SkTDStackNesterTester::GetSlotCount();
89
90 // Test pushing beyond the boundary of the first Rec.
91 for (; count < 2 * slotCount; ++count) {
92 if (3 == count) {
93 // Test nesting (an arbitrary number of pushes) early on.
94 test_nest(reporter, &nester, 7);
95 } else if (slotCount - 4 == count) {
96 // Test nesting across the boundary of a Rec.
97 test_nest(reporter, &nester, 6);
98 }
99 test_push(reporter, &nester);
100 }
101
102 // Pop everything off the stack except for the last one, to confirm
103 // that the destructor handles a remaining object.
104 while (nester.count() > 1) {
105 test_pop(reporter, &nester);
106 }
107}
108
109DEF_TEST(TDStackNester, reporter) {
110 test_stack_nester(reporter);
111}