michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = "(none)"; michael@0: var summary = "Basic support for accessing iterable objects using Iterator"; michael@0: var actual, expect; michael@0: michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus(summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: var failed = false; michael@0: michael@0: function Array_equals(a, b) michael@0: { michael@0: if (!(a instanceof Array) || !(b instanceof Array)) michael@0: throw new Error("Arguments not both of type Array"); michael@0: if (a.length != b.length) michael@0: return false; michael@0: for (var i = 0, sz = a.length; i < sz; i++) michael@0: if (a[i] !== b[i]) michael@0: return false; michael@0: return true; michael@0: } michael@0: michael@0: var iterable = { persistedProp: 17 }; michael@0: michael@0: try michael@0: { michael@0: // nothing unusual so far -- verify basic properties michael@0: for (var i in iterable) michael@0: { michael@0: if (i != "persistedProp") michael@0: throw "no persistedProp!"; michael@0: if (iterable[i] != 17) michael@0: throw "iterable[\"persistedProp\"] == 17"; michael@0: } michael@0: michael@0: var keys = ["foo", "bar", "baz"]; michael@0: var vals = [6, 5, 14]; michael@0: michael@0: iterable.__iterator__ = michael@0: function(keysOnly) michael@0: { michael@0: var gen = michael@0: function() michael@0: { michael@0: for (var i = 0; i < keys.length; i++) michael@0: { michael@0: if (keysOnly) michael@0: yield keys[i]; michael@0: else michael@0: yield [keys[i], vals[i]]; michael@0: } michael@0: }; michael@0: return gen(); michael@0: }; michael@0: michael@0: /* Test [key, value] Iterator */ michael@0: var index = 0; michael@0: var lastSeen = "INITIALVALUE"; michael@0: var it = Iterator(iterable); michael@0: try michael@0: { michael@0: while (true) michael@0: { michael@0: var nextVal = it.next(); michael@0: if (!Array_equals(nextVal, [keys[index], vals[index]])) michael@0: throw "Iterator(iterable): wrong next result\n" + michael@0: " expected: " + [keys[index], vals[index]] + "\n" + michael@0: " actual: " + nextVal; michael@0: lastSeen = keys[index]; michael@0: index++; michael@0: } michael@0: } michael@0: catch (e) michael@0: { michael@0: if (lastSeen !== keys[keys.length - 1]) michael@0: throw "Iterator(iterable): not everything was iterated!\n" + michael@0: " last iterated was: " + lastSeen + "\n" + michael@0: " error: " + e; michael@0: if (e !== StopIteration) michael@0: throw "Iterator(iterable): missing or invalid StopIteration"; michael@0: } michael@0: michael@0: if (iterable.persistedProp != 17) michael@0: throw "iterable.persistedProp not persisted!"; michael@0: michael@0: /* Test [key, value] Iterator, called with an explicit |false| parameter */ michael@0: var index = 0; michael@0: lastSeen = "INITIALVALUE"; michael@0: it = Iterator(iterable, false); michael@0: try michael@0: { michael@0: while (true) michael@0: { michael@0: var nextVal = it.next(); michael@0: if (!Array_equals(nextVal, [keys[index], vals[index]])) michael@0: throw "Iterator(iterable, false): wrong next result\n" + michael@0: " expected: " + [keys[index], vals[index]] + "\n" + michael@0: " actual: " + nextVal; michael@0: lastSeen = keys[index]; michael@0: index++; michael@0: } michael@0: } michael@0: catch (e) michael@0: { michael@0: if (lastSeen !== keys[keys.length - 1]) michael@0: throw "Iterator(iterable, false): not everything was iterated!\n" + michael@0: " last iterated was: " + lastSeen + "\n" + michael@0: " error: " + e; michael@0: if (e !== StopIteration) michael@0: throw "Iterator(iterable, false): missing or invalid StopIteration"; michael@0: } michael@0: michael@0: if (iterable.persistedProp != 17) michael@0: throw "iterable.persistedProp not persisted!"; michael@0: michael@0: /* Test key-only Iterator */ michael@0: index = 0; michael@0: lastSeen = undefined; michael@0: it = Iterator(iterable, true); michael@0: try michael@0: { michael@0: while (true) michael@0: { michael@0: var nextVal = it.next(); michael@0: if (nextVal !== keys[index]) michael@0: throw "Iterator(iterable, true): wrong next result\n" + michael@0: " expected: " + keys[index] + "\n" + michael@0: " actual: " + nextVal; michael@0: lastSeen = keys[index]; michael@0: index++; michael@0: } michael@0: } michael@0: catch (e) michael@0: { michael@0: if (lastSeen !== keys[keys.length - 1]) michael@0: throw "Iterator(iterable, true): not everything was iterated!\n" + michael@0: " last iterated was: " + lastSeen + "\n" + michael@0: " error: " + e; michael@0: if (e !== StopIteration) michael@0: throw "Iterator(iterable, true): missing or invalid StopIteration"; michael@0: } michael@0: michael@0: if (iterable.persistedProp != 17) michael@0: throw "iterable.persistedProp not persisted!"; michael@0: } michael@0: catch (e) michael@0: { michael@0: failed = e; michael@0: } michael@0: michael@0: michael@0: michael@0: expect = false; michael@0: actual = failed; michael@0: michael@0: reportCompare(expect, actual, summary);