1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/TestObserverArray.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,146 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +// vim:cindent:ts=4:et:sw=4: 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "TestHarness.h" 1.11 +#include "nsTObserverArray.h" 1.12 + 1.13 +using namespace mozilla; 1.14 + 1.15 +typedef nsTObserverArray<int> IntArray; 1.16 + 1.17 +#define DO_TEST(_type, _exp, _code) \ 1.18 + do { \ 1.19 + ++testNum; \ 1.20 + count = 0; \ 1.21 + IntArray::_type iter(arr); \ 1.22 + while (iter.HasMore() && count != ArrayLength(_exp)) { \ 1.23 + _code \ 1.24 + int next = iter.GetNext(); \ 1.25 + int expected = _exp[count++]; \ 1.26 + if (next != expected) { \ 1.27 + fail("During test %d at position %d got %d expected %d\n", \ 1.28 + testNum, count-1, next, expected); \ 1.29 + rv = 1; \ 1.30 + } \ 1.31 + } \ 1.32 + if (iter.HasMore()) { \ 1.33 + fail("During test %d, iterator ran over", testNum); \ 1.34 + rv = 1; \ 1.35 + } \ 1.36 + if (count != ArrayLength(_exp)) { \ 1.37 + fail("During test %d, iterator finished too early", testNum); \ 1.38 + rv = 1; \ 1.39 + } \ 1.40 + } while (0) 1.41 + 1.42 +int main(int argc, char **argv) 1.43 +{ 1.44 + ScopedXPCOM xpcom("nsTObserverArrayTests"); 1.45 + if (xpcom.failed()) { 1.46 + return 1; 1.47 + } 1.48 + 1.49 + int rv = 0; 1.50 + 1.51 + IntArray arr; 1.52 + arr.AppendElement(3); 1.53 + arr.AppendElement(4); 1.54 + 1.55 + size_t count; 1.56 + int testNum = 0; 1.57 + 1.58 + // Basic sanity 1.59 + static int test1Expected[] = { 3, 4 }; 1.60 + DO_TEST(ForwardIterator, test1Expected, { /* nothing */ }); 1.61 + 1.62 + // Appends 1.63 + static int test2Expected[] = { 3, 4, 2 }; 1.64 + DO_TEST(ForwardIterator, test2Expected, 1.65 + if (count == 1) arr.AppendElement(2); 1.66 + ); 1.67 + DO_TEST(ForwardIterator, test2Expected, { /* nothing */ }); 1.68 + 1.69 + DO_TEST(EndLimitedIterator, test2Expected, 1.70 + if (count == 1) arr.AppendElement(5); 1.71 + ); 1.72 + 1.73 + static int test5Expected[] = { 3, 4, 2, 5 }; 1.74 + DO_TEST(ForwardIterator, test5Expected, { /* nothing */ }); 1.75 + 1.76 + // Removals 1.77 + DO_TEST(ForwardIterator, test5Expected, 1.78 + if (count == 1) arr.RemoveElementAt(0); 1.79 + ); 1.80 + 1.81 + static int test7Expected[] = { 4, 2, 5 }; 1.82 + DO_TEST(ForwardIterator, test7Expected, { /* nothing */ }); 1.83 + 1.84 + static int test8Expected[] = { 4, 5 }; 1.85 + DO_TEST(ForwardIterator, test8Expected, 1.86 + if (count == 1) arr.RemoveElementAt(1); 1.87 + ); 1.88 + DO_TEST(ForwardIterator, test8Expected, { /* nothing */ }); 1.89 + 1.90 + arr.AppendElement(2); 1.91 + arr.AppendElementUnlessExists(6); 1.92 + static int test10Expected[] = { 4, 5, 2, 6 }; 1.93 + DO_TEST(ForwardIterator, test10Expected, { /* nothing */ }); 1.94 + 1.95 + arr.AppendElementUnlessExists(5); 1.96 + DO_TEST(ForwardIterator, test10Expected, { /* nothing */ }); 1.97 + 1.98 + static int test12Expected[] = { 4, 5, 6 }; 1.99 + DO_TEST(ForwardIterator, test12Expected, 1.100 + if (count == 1) arr.RemoveElementAt(2); 1.101 + ); 1.102 + DO_TEST(ForwardIterator, test12Expected, { /* nothing */ }); 1.103 + 1.104 + // Removals + Appends 1.105 + static int test14Expected[] = { 4, 6, 7 }; 1.106 + DO_TEST(ForwardIterator, test14Expected, 1.107 + if (count == 1) { 1.108 + arr.RemoveElementAt(1); 1.109 + arr.AppendElement(7); 1.110 + } 1.111 + ); 1.112 + DO_TEST(ForwardIterator, test14Expected, { /* nothing */ }); 1.113 + 1.114 + arr.AppendElement(2); 1.115 + static int test16Expected[] = { 4, 6, 7, 2 }; 1.116 + DO_TEST(ForwardIterator, test16Expected, { /* nothing */ }); 1.117 + 1.118 + static int test17Expected[] = { 4, 7, 2 }; 1.119 + DO_TEST(EndLimitedIterator, test17Expected, 1.120 + if (count == 1) { 1.121 + arr.RemoveElementAt(1); 1.122 + arr.AppendElement(8); 1.123 + } 1.124 + ); 1.125 + 1.126 + static int test18Expected[] = { 4, 7, 2, 8 }; 1.127 + DO_TEST(ForwardIterator, test18Expected, { /* nothing */ }); 1.128 + 1.129 + // Prepends 1.130 + arr.PrependElementUnlessExists(3); 1.131 + static int test19Expected[] = { 3, 4, 7, 2, 8 }; 1.132 + DO_TEST(ForwardIterator, test19Expected, { /* nothing */ }); 1.133 + 1.134 + arr.PrependElementUnlessExists(7); 1.135 + DO_TEST(ForwardIterator, test19Expected, { /* nothing */ }); 1.136 + 1.137 + // Commented out because it fails; bug 474369 will fix 1.138 + /* DO_TEST(ForwardIterator, test19Expected, 1.139 + if (count == 1) { 1.140 + arr.PrependElementUnlessExists(9); 1.141 + } 1.142 + ); 1.143 + 1.144 + static int test22Expected[] = { 9, 3, 4, 7, 2, 8 }; 1.145 + DO_TEST(ForwardIterator, test22Expected, { }); 1.146 + */ 1.147 + 1.148 + return rv; 1.149 +}