1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_7/extensions/basic-Iterator.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,165 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +//----------------------------------------------------------------------------- 1.10 +var BUGNUMBER = "(none)"; 1.11 +var summary = "Basic support for accessing iterable objects using Iterator"; 1.12 +var actual, expect; 1.13 + 1.14 +printBugNumber(BUGNUMBER); 1.15 +printStatus(summary); 1.16 + 1.17 +/************** 1.18 + * BEGIN TEST * 1.19 + **************/ 1.20 + 1.21 +var failed = false; 1.22 + 1.23 +function Array_equals(a, b) 1.24 +{ 1.25 + if (!(a instanceof Array) || !(b instanceof Array)) 1.26 + throw new Error("Arguments not both of type Array"); 1.27 + if (a.length != b.length) 1.28 + return false; 1.29 + for (var i = 0, sz = a.length; i < sz; i++) 1.30 + if (a[i] !== b[i]) 1.31 + return false; 1.32 + return true; 1.33 +} 1.34 + 1.35 +var iterable = { persistedProp: 17 }; 1.36 + 1.37 +try 1.38 +{ 1.39 + // nothing unusual so far -- verify basic properties 1.40 + for (var i in iterable) 1.41 + { 1.42 + if (i != "persistedProp") 1.43 + throw "no persistedProp!"; 1.44 + if (iterable[i] != 17) 1.45 + throw "iterable[\"persistedProp\"] == 17"; 1.46 + } 1.47 + 1.48 + var keys = ["foo", "bar", "baz"]; 1.49 + var vals = [6, 5, 14]; 1.50 + 1.51 + iterable.__iterator__ = 1.52 + function(keysOnly) 1.53 + { 1.54 + var gen = 1.55 + function() 1.56 + { 1.57 + for (var i = 0; i < keys.length; i++) 1.58 + { 1.59 + if (keysOnly) 1.60 + yield keys[i]; 1.61 + else 1.62 + yield [keys[i], vals[i]]; 1.63 + } 1.64 + }; 1.65 + return gen(); 1.66 + }; 1.67 + 1.68 + /* Test [key, value] Iterator */ 1.69 + var index = 0; 1.70 + var lastSeen = "INITIALVALUE"; 1.71 + var it = Iterator(iterable); 1.72 + try 1.73 + { 1.74 + while (true) 1.75 + { 1.76 + var nextVal = it.next(); 1.77 + if (!Array_equals(nextVal, [keys[index], vals[index]])) 1.78 + throw "Iterator(iterable): wrong next result\n" + 1.79 + " expected: " + [keys[index], vals[index]] + "\n" + 1.80 + " actual: " + nextVal; 1.81 + lastSeen = keys[index]; 1.82 + index++; 1.83 + } 1.84 + } 1.85 + catch (e) 1.86 + { 1.87 + if (lastSeen !== keys[keys.length - 1]) 1.88 + throw "Iterator(iterable): not everything was iterated!\n" + 1.89 + " last iterated was: " + lastSeen + "\n" + 1.90 + " error: " + e; 1.91 + if (e !== StopIteration) 1.92 + throw "Iterator(iterable): missing or invalid StopIteration"; 1.93 + } 1.94 + 1.95 + if (iterable.persistedProp != 17) 1.96 + throw "iterable.persistedProp not persisted!"; 1.97 + 1.98 + /* Test [key, value] Iterator, called with an explicit |false| parameter */ 1.99 + var index = 0; 1.100 + lastSeen = "INITIALVALUE"; 1.101 + it = Iterator(iterable, false); 1.102 + try 1.103 + { 1.104 + while (true) 1.105 + { 1.106 + var nextVal = it.next(); 1.107 + if (!Array_equals(nextVal, [keys[index], vals[index]])) 1.108 + throw "Iterator(iterable, false): wrong next result\n" + 1.109 + " expected: " + [keys[index], vals[index]] + "\n" + 1.110 + " actual: " + nextVal; 1.111 + lastSeen = keys[index]; 1.112 + index++; 1.113 + } 1.114 + } 1.115 + catch (e) 1.116 + { 1.117 + if (lastSeen !== keys[keys.length - 1]) 1.118 + throw "Iterator(iterable, false): not everything was iterated!\n" + 1.119 + " last iterated was: " + lastSeen + "\n" + 1.120 + " error: " + e; 1.121 + if (e !== StopIteration) 1.122 + throw "Iterator(iterable, false): missing or invalid StopIteration"; 1.123 + } 1.124 + 1.125 + if (iterable.persistedProp != 17) 1.126 + throw "iterable.persistedProp not persisted!"; 1.127 + 1.128 + /* Test key-only Iterator */ 1.129 + index = 0; 1.130 + lastSeen = undefined; 1.131 + it = Iterator(iterable, true); 1.132 + try 1.133 + { 1.134 + while (true) 1.135 + { 1.136 + var nextVal = it.next(); 1.137 + if (nextVal !== keys[index]) 1.138 + throw "Iterator(iterable, true): wrong next result\n" + 1.139 + " expected: " + keys[index] + "\n" + 1.140 + " actual: " + nextVal; 1.141 + lastSeen = keys[index]; 1.142 + index++; 1.143 + } 1.144 + } 1.145 + catch (e) 1.146 + { 1.147 + if (lastSeen !== keys[keys.length - 1]) 1.148 + throw "Iterator(iterable, true): not everything was iterated!\n" + 1.149 + " last iterated was: " + lastSeen + "\n" + 1.150 + " error: " + e; 1.151 + if (e !== StopIteration) 1.152 + throw "Iterator(iterable, true): missing or invalid StopIteration"; 1.153 + } 1.154 + 1.155 + if (iterable.persistedProp != 17) 1.156 + throw "iterable.persistedProp not persisted!"; 1.157 +} 1.158 +catch (e) 1.159 +{ 1.160 + failed = e; 1.161 +} 1.162 + 1.163 + 1.164 + 1.165 +expect = false; 1.166 +actual = failed; 1.167 + 1.168 +reportCompare(expect, actual, summary);