epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkDeque.h" |
| 10 | |
| 11 | static void assert_count(skiatest::Reporter* reporter, const SkDeque& deq, int count) { |
| 12 | if (0 == count) { |
| 13 | REPORTER_ASSERT(reporter, deq.empty()); |
| 14 | REPORTER_ASSERT(reporter, 0 == deq.count()); |
| 15 | REPORTER_ASSERT(reporter, sizeof(int) == deq.elemSize()); |
| 16 | REPORTER_ASSERT(reporter, NULL == deq.front()); |
| 17 | REPORTER_ASSERT(reporter, NULL == deq.back()); |
| 18 | } else { |
| 19 | REPORTER_ASSERT(reporter, !deq.empty()); |
| 20 | REPORTER_ASSERT(reporter, count == deq.count()); |
| 21 | REPORTER_ASSERT(reporter, sizeof(int) == deq.elemSize()); |
| 22 | REPORTER_ASSERT(reporter, NULL != deq.front()); |
| 23 | REPORTER_ASSERT(reporter, NULL != deq.back()); |
| 24 | if (1 == count) { |
| 25 | REPORTER_ASSERT(reporter, deq.back() == deq.front()); |
| 26 | } else { |
| 27 | REPORTER_ASSERT(reporter, deq.back() != deq.front()); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | static void assert_f2biter(skiatest::Reporter* reporter, const SkDeque& deq, |
| 33 | int max, int min) { |
| 34 | SkDeque::F2BIter iter(deq); |
| 35 | void* ptr; |
| 36 | |
| 37 | int value = max; |
| 38 | while ((ptr = iter.next()) != NULL) { |
| 39 | REPORTER_ASSERT(reporter, value == *(int*)ptr); |
| 40 | value -= 1; |
| 41 | } |
| 42 | REPORTER_ASSERT(reporter, value+1 == min); |
| 43 | } |
| 44 | |
| 45 | static void TestDeque(skiatest::Reporter* reporter) { |
| 46 | SkDeque deq(sizeof(int)); |
| 47 | int i; |
| 48 | |
reed@google.com | f9e7132 | 2011-02-22 19:56:18 +0000 | [diff] [blame] | 49 | // test pushing on the front |
| 50 | |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 51 | assert_count(reporter, deq, 0); |
| 52 | for (i = 1; i <= 10; i++) { |
| 53 | *(int*)deq.push_front() = i; |
| 54 | } |
| 55 | assert_count(reporter, deq, 10); |
| 56 | assert_f2biter(reporter, deq, 10, 1); |
| 57 | |
| 58 | for (i = 0; i < 5; i++) { |
| 59 | deq.pop_front(); |
| 60 | } |
| 61 | assert_count(reporter, deq, 5); |
| 62 | assert_f2biter(reporter, deq, 5, 1); |
| 63 | |
| 64 | for (i = 0; i < 5; i++) { |
| 65 | deq.pop_front(); |
| 66 | } |
| 67 | assert_count(reporter, deq, 0); |
reed@google.com | f9e7132 | 2011-02-22 19:56:18 +0000 | [diff] [blame] | 68 | |
| 69 | // now test pushing on the back |
| 70 | |
| 71 | for (i = 10; i >= 1; --i) { |
| 72 | *(int*)deq.push_back() = i; |
| 73 | } |
| 74 | assert_count(reporter, deq, 10); |
| 75 | assert_f2biter(reporter, deq, 10, 1); |
| 76 | |
| 77 | for (i = 0; i < 5; i++) { |
| 78 | deq.pop_back(); |
| 79 | } |
| 80 | assert_count(reporter, deq, 5); |
| 81 | assert_f2biter(reporter, deq, 10, 6); |
| 82 | |
| 83 | for (i = 0; i < 5; i++) { |
| 84 | deq.pop_back(); |
| 85 | } |
| 86 | assert_count(reporter, deq, 0); |
| 87 | |
| 88 | // now tests pushing/poping on both ends |
| 89 | |
| 90 | *(int*)deq.push_front() = 5; |
| 91 | *(int*)deq.push_back() = 4; |
| 92 | *(int*)deq.push_front() = 6; |
| 93 | *(int*)deq.push_back() = 3; |
| 94 | *(int*)deq.push_front() = 7; |
| 95 | *(int*)deq.push_back() = 2; |
| 96 | *(int*)deq.push_front() = 8; |
| 97 | *(int*)deq.push_back() = 1; |
| 98 | assert_count(reporter, deq, 8); |
| 99 | assert_f2biter(reporter, deq, 8, 1); |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | #include "TestClassDef.h" |
| 103 | DEFINE_TESTCLASS("Deque", TestDequeClass, TestDeque) |