Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | // vim:cindent:ts=4:et:sw=4: |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "TestHarness.h" |
michael@0 | 8 | #include "nsTObserverArray.h" |
michael@0 | 9 | |
michael@0 | 10 | using namespace mozilla; |
michael@0 | 11 | |
michael@0 | 12 | typedef nsTObserverArray<int> IntArray; |
michael@0 | 13 | |
michael@0 | 14 | #define DO_TEST(_type, _exp, _code) \ |
michael@0 | 15 | do { \ |
michael@0 | 16 | ++testNum; \ |
michael@0 | 17 | count = 0; \ |
michael@0 | 18 | IntArray::_type iter(arr); \ |
michael@0 | 19 | while (iter.HasMore() && count != ArrayLength(_exp)) { \ |
michael@0 | 20 | _code \ |
michael@0 | 21 | int next = iter.GetNext(); \ |
michael@0 | 22 | int expected = _exp[count++]; \ |
michael@0 | 23 | if (next != expected) { \ |
michael@0 | 24 | fail("During test %d at position %d got %d expected %d\n", \ |
michael@0 | 25 | testNum, count-1, next, expected); \ |
michael@0 | 26 | rv = 1; \ |
michael@0 | 27 | } \ |
michael@0 | 28 | } \ |
michael@0 | 29 | if (iter.HasMore()) { \ |
michael@0 | 30 | fail("During test %d, iterator ran over", testNum); \ |
michael@0 | 31 | rv = 1; \ |
michael@0 | 32 | } \ |
michael@0 | 33 | if (count != ArrayLength(_exp)) { \ |
michael@0 | 34 | fail("During test %d, iterator finished too early", testNum); \ |
michael@0 | 35 | rv = 1; \ |
michael@0 | 36 | } \ |
michael@0 | 37 | } while (0) |
michael@0 | 38 | |
michael@0 | 39 | int main(int argc, char **argv) |
michael@0 | 40 | { |
michael@0 | 41 | ScopedXPCOM xpcom("nsTObserverArrayTests"); |
michael@0 | 42 | if (xpcom.failed()) { |
michael@0 | 43 | return 1; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | int rv = 0; |
michael@0 | 47 | |
michael@0 | 48 | IntArray arr; |
michael@0 | 49 | arr.AppendElement(3); |
michael@0 | 50 | arr.AppendElement(4); |
michael@0 | 51 | |
michael@0 | 52 | size_t count; |
michael@0 | 53 | int testNum = 0; |
michael@0 | 54 | |
michael@0 | 55 | // Basic sanity |
michael@0 | 56 | static int test1Expected[] = { 3, 4 }; |
michael@0 | 57 | DO_TEST(ForwardIterator, test1Expected, { /* nothing */ }); |
michael@0 | 58 | |
michael@0 | 59 | // Appends |
michael@0 | 60 | static int test2Expected[] = { 3, 4, 2 }; |
michael@0 | 61 | DO_TEST(ForwardIterator, test2Expected, |
michael@0 | 62 | if (count == 1) arr.AppendElement(2); |
michael@0 | 63 | ); |
michael@0 | 64 | DO_TEST(ForwardIterator, test2Expected, { /* nothing */ }); |
michael@0 | 65 | |
michael@0 | 66 | DO_TEST(EndLimitedIterator, test2Expected, |
michael@0 | 67 | if (count == 1) arr.AppendElement(5); |
michael@0 | 68 | ); |
michael@0 | 69 | |
michael@0 | 70 | static int test5Expected[] = { 3, 4, 2, 5 }; |
michael@0 | 71 | DO_TEST(ForwardIterator, test5Expected, { /* nothing */ }); |
michael@0 | 72 | |
michael@0 | 73 | // Removals |
michael@0 | 74 | DO_TEST(ForwardIterator, test5Expected, |
michael@0 | 75 | if (count == 1) arr.RemoveElementAt(0); |
michael@0 | 76 | ); |
michael@0 | 77 | |
michael@0 | 78 | static int test7Expected[] = { 4, 2, 5 }; |
michael@0 | 79 | DO_TEST(ForwardIterator, test7Expected, { /* nothing */ }); |
michael@0 | 80 | |
michael@0 | 81 | static int test8Expected[] = { 4, 5 }; |
michael@0 | 82 | DO_TEST(ForwardIterator, test8Expected, |
michael@0 | 83 | if (count == 1) arr.RemoveElementAt(1); |
michael@0 | 84 | ); |
michael@0 | 85 | DO_TEST(ForwardIterator, test8Expected, { /* nothing */ }); |
michael@0 | 86 | |
michael@0 | 87 | arr.AppendElement(2); |
michael@0 | 88 | arr.AppendElementUnlessExists(6); |
michael@0 | 89 | static int test10Expected[] = { 4, 5, 2, 6 }; |
michael@0 | 90 | DO_TEST(ForwardIterator, test10Expected, { /* nothing */ }); |
michael@0 | 91 | |
michael@0 | 92 | arr.AppendElementUnlessExists(5); |
michael@0 | 93 | DO_TEST(ForwardIterator, test10Expected, { /* nothing */ }); |
michael@0 | 94 | |
michael@0 | 95 | static int test12Expected[] = { 4, 5, 6 }; |
michael@0 | 96 | DO_TEST(ForwardIterator, test12Expected, |
michael@0 | 97 | if (count == 1) arr.RemoveElementAt(2); |
michael@0 | 98 | ); |
michael@0 | 99 | DO_TEST(ForwardIterator, test12Expected, { /* nothing */ }); |
michael@0 | 100 | |
michael@0 | 101 | // Removals + Appends |
michael@0 | 102 | static int test14Expected[] = { 4, 6, 7 }; |
michael@0 | 103 | DO_TEST(ForwardIterator, test14Expected, |
michael@0 | 104 | if (count == 1) { |
michael@0 | 105 | arr.RemoveElementAt(1); |
michael@0 | 106 | arr.AppendElement(7); |
michael@0 | 107 | } |
michael@0 | 108 | ); |
michael@0 | 109 | DO_TEST(ForwardIterator, test14Expected, { /* nothing */ }); |
michael@0 | 110 | |
michael@0 | 111 | arr.AppendElement(2); |
michael@0 | 112 | static int test16Expected[] = { 4, 6, 7, 2 }; |
michael@0 | 113 | DO_TEST(ForwardIterator, test16Expected, { /* nothing */ }); |
michael@0 | 114 | |
michael@0 | 115 | static int test17Expected[] = { 4, 7, 2 }; |
michael@0 | 116 | DO_TEST(EndLimitedIterator, test17Expected, |
michael@0 | 117 | if (count == 1) { |
michael@0 | 118 | arr.RemoveElementAt(1); |
michael@0 | 119 | arr.AppendElement(8); |
michael@0 | 120 | } |
michael@0 | 121 | ); |
michael@0 | 122 | |
michael@0 | 123 | static int test18Expected[] = { 4, 7, 2, 8 }; |
michael@0 | 124 | DO_TEST(ForwardIterator, test18Expected, { /* nothing */ }); |
michael@0 | 125 | |
michael@0 | 126 | // Prepends |
michael@0 | 127 | arr.PrependElementUnlessExists(3); |
michael@0 | 128 | static int test19Expected[] = { 3, 4, 7, 2, 8 }; |
michael@0 | 129 | DO_TEST(ForwardIterator, test19Expected, { /* nothing */ }); |
michael@0 | 130 | |
michael@0 | 131 | arr.PrependElementUnlessExists(7); |
michael@0 | 132 | DO_TEST(ForwardIterator, test19Expected, { /* nothing */ }); |
michael@0 | 133 | |
michael@0 | 134 | // Commented out because it fails; bug 474369 will fix |
michael@0 | 135 | /* DO_TEST(ForwardIterator, test19Expected, |
michael@0 | 136 | if (count == 1) { |
michael@0 | 137 | arr.PrependElementUnlessExists(9); |
michael@0 | 138 | } |
michael@0 | 139 | ); |
michael@0 | 140 | |
michael@0 | 141 | static int test22Expected[] = { 9, 3, 4, 7, 2, 8 }; |
michael@0 | 142 | DO_TEST(ForwardIterator, test22Expected, { }); |
michael@0 | 143 | */ |
michael@0 | 144 | |
michael@0 | 145 | return rv; |
michael@0 | 146 | } |