michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: // vim:cindent:ts=4:et:sw=4: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestHarness.h" michael@0: #include "nsTObserverArray.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: typedef nsTObserverArray IntArray; michael@0: michael@0: #define DO_TEST(_type, _exp, _code) \ michael@0: do { \ michael@0: ++testNum; \ michael@0: count = 0; \ michael@0: IntArray::_type iter(arr); \ michael@0: while (iter.HasMore() && count != ArrayLength(_exp)) { \ michael@0: _code \ michael@0: int next = iter.GetNext(); \ michael@0: int expected = _exp[count++]; \ michael@0: if (next != expected) { \ michael@0: fail("During test %d at position %d got %d expected %d\n", \ michael@0: testNum, count-1, next, expected); \ michael@0: rv = 1; \ michael@0: } \ michael@0: } \ michael@0: if (iter.HasMore()) { \ michael@0: fail("During test %d, iterator ran over", testNum); \ michael@0: rv = 1; \ michael@0: } \ michael@0: if (count != ArrayLength(_exp)) { \ michael@0: fail("During test %d, iterator finished too early", testNum); \ michael@0: rv = 1; \ michael@0: } \ michael@0: } while (0) michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: ScopedXPCOM xpcom("nsTObserverArrayTests"); michael@0: if (xpcom.failed()) { michael@0: return 1; michael@0: } michael@0: michael@0: int rv = 0; michael@0: michael@0: IntArray arr; michael@0: arr.AppendElement(3); michael@0: arr.AppendElement(4); michael@0: michael@0: size_t count; michael@0: int testNum = 0; michael@0: michael@0: // Basic sanity michael@0: static int test1Expected[] = { 3, 4 }; michael@0: DO_TEST(ForwardIterator, test1Expected, { /* nothing */ }); michael@0: michael@0: // Appends michael@0: static int test2Expected[] = { 3, 4, 2 }; michael@0: DO_TEST(ForwardIterator, test2Expected, michael@0: if (count == 1) arr.AppendElement(2); michael@0: ); michael@0: DO_TEST(ForwardIterator, test2Expected, { /* nothing */ }); michael@0: michael@0: DO_TEST(EndLimitedIterator, test2Expected, michael@0: if (count == 1) arr.AppendElement(5); michael@0: ); michael@0: michael@0: static int test5Expected[] = { 3, 4, 2, 5 }; michael@0: DO_TEST(ForwardIterator, test5Expected, { /* nothing */ }); michael@0: michael@0: // Removals michael@0: DO_TEST(ForwardIterator, test5Expected, michael@0: if (count == 1) arr.RemoveElementAt(0); michael@0: ); michael@0: michael@0: static int test7Expected[] = { 4, 2, 5 }; michael@0: DO_TEST(ForwardIterator, test7Expected, { /* nothing */ }); michael@0: michael@0: static int test8Expected[] = { 4, 5 }; michael@0: DO_TEST(ForwardIterator, test8Expected, michael@0: if (count == 1) arr.RemoveElementAt(1); michael@0: ); michael@0: DO_TEST(ForwardIterator, test8Expected, { /* nothing */ }); michael@0: michael@0: arr.AppendElement(2); michael@0: arr.AppendElementUnlessExists(6); michael@0: static int test10Expected[] = { 4, 5, 2, 6 }; michael@0: DO_TEST(ForwardIterator, test10Expected, { /* nothing */ }); michael@0: michael@0: arr.AppendElementUnlessExists(5); michael@0: DO_TEST(ForwardIterator, test10Expected, { /* nothing */ }); michael@0: michael@0: static int test12Expected[] = { 4, 5, 6 }; michael@0: DO_TEST(ForwardIterator, test12Expected, michael@0: if (count == 1) arr.RemoveElementAt(2); michael@0: ); michael@0: DO_TEST(ForwardIterator, test12Expected, { /* nothing */ }); michael@0: michael@0: // Removals + Appends michael@0: static int test14Expected[] = { 4, 6, 7 }; michael@0: DO_TEST(ForwardIterator, test14Expected, michael@0: if (count == 1) { michael@0: arr.RemoveElementAt(1); michael@0: arr.AppendElement(7); michael@0: } michael@0: ); michael@0: DO_TEST(ForwardIterator, test14Expected, { /* nothing */ }); michael@0: michael@0: arr.AppendElement(2); michael@0: static int test16Expected[] = { 4, 6, 7, 2 }; michael@0: DO_TEST(ForwardIterator, test16Expected, { /* nothing */ }); michael@0: michael@0: static int test17Expected[] = { 4, 7, 2 }; michael@0: DO_TEST(EndLimitedIterator, test17Expected, michael@0: if (count == 1) { michael@0: arr.RemoveElementAt(1); michael@0: arr.AppendElement(8); michael@0: } michael@0: ); michael@0: michael@0: static int test18Expected[] = { 4, 7, 2, 8 }; michael@0: DO_TEST(ForwardIterator, test18Expected, { /* nothing */ }); michael@0: michael@0: // Prepends michael@0: arr.PrependElementUnlessExists(3); michael@0: static int test19Expected[] = { 3, 4, 7, 2, 8 }; michael@0: DO_TEST(ForwardIterator, test19Expected, { /* nothing */ }); michael@0: michael@0: arr.PrependElementUnlessExists(7); michael@0: DO_TEST(ForwardIterator, test19Expected, { /* nothing */ }); michael@0: michael@0: // Commented out because it fails; bug 474369 will fix michael@0: /* DO_TEST(ForwardIterator, test19Expected, michael@0: if (count == 1) { michael@0: arr.PrependElementUnlessExists(9); michael@0: } michael@0: ); michael@0: michael@0: static int test22Expected[] = { 9, 3, 4, 7, 2, 8 }; michael@0: DO_TEST(ForwardIterator, test22Expected, { }); michael@0: */ michael@0: michael@0: return rv; michael@0: }