2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkTDStackNester.h"
13 * Test SkTDStackNester<int>::push(). Pushes the current count onto the stack,
14 * and checks that the count has increased by one.
16 static void test_push(skiatest::Reporter* reporter, SkTDStackNester<int>* nester) {
18 const int count = nester->count();
19 // test_pop depends on this value.
21 REPORTER_ASSERT(reporter, nester->count() == count + 1);
25 * Test SkTDStackNester<int>::pop(). Pops the top element off the stack, and
26 * checks that the new count is one smaller, and that the popped element
27 * matches the new count (as was pushed by test_push).
29 static void test_pop(skiatest::Reporter* reporter, SkTDStackNester<int>* nester) {
31 const int count = nester->count();
32 // This test should not be called with a count <= 0.
34 const int top = nester->top();
37 REPORTER_ASSERT(reporter, top == value);
38 const int newCount = nester->count();
39 REPORTER_ASSERT(reporter, newCount == count - 1);
40 // Since test_push always pushes the count prior to the push, value should
41 // always be one less than count.
42 REPORTER_ASSERT(reporter, newCount == value);
46 * Test nest() and unnest(). nest() is called, and it is confirmed that the
47 * count is now zero. Then test_push() is called inc times, followed by a call to
48 * unnest(). After this call, check that the count has returned to the initial count, and
49 * that nestingLevel() has returned to its initial value.
51 static void test_nest(skiatest::Reporter* reporter, SkTDStackNester<int>* nester, int inc) {
54 const int initialCount = nester->count();
55 const int initialNesting = nester->nestingLevel();
58 REPORTER_ASSERT(reporter, nester->count() == 0);
59 REPORTER_ASSERT(reporter, nester->nestingLevel() == initialNesting + 1);
61 for (int i = 0; i < inc; ++i) {
62 test_push(reporter, nester);
66 REPORTER_ASSERT(reporter, nester->count() == initialCount);
67 REPORTER_ASSERT(reporter, nester->nestingLevel() == initialNesting);
70 class SkTDStackNesterTester {
72 static int GetSlotCount() {
73 return SkTDStackNester<int>::kSlotCount;
77 static void test_stack_nester(skiatest::Reporter* reporter) {
78 SkTDStackNester<int> nester;
79 int count = nester.count();
80 REPORTER_ASSERT(reporter, 0 == count);
81 REPORTER_ASSERT(reporter, nester.nestingLevel() == 0);
82 REPORTER_ASSERT(reporter, nester.empty());
84 // Test nesting (with arbitrary number of pushes) from the beginning.
85 test_nest(reporter, &nester, 3);
87 const int slotCount = SkTDStackNesterTester::GetSlotCount();
89 // Test pushing beyond the boundary of the first Rec.
90 for (; count < 2 * slotCount; ++count) {
92 // Test nesting (an arbitrary number of pushes) early on.
93 test_nest(reporter, &nester, 7);
94 } else if (slotCount - 4 == count) {
95 // Test nesting across the boundary of a Rec.
96 test_nest(reporter, &nester, 6);
98 test_push(reporter, &nester);
101 // Pop everything off the stack except for the last one, to confirm
102 // that the destructor handles a remaining object.
103 while (nester.count() > 1) {
104 test_pop(reporter, &nester);
108 DEF_TEST(TDStackNester, reporter) {
109 test_stack_nester(reporter);