|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 //----------------------------------------------------------------------------- |
|
7 var BUGNUMBER = "(none)"; |
|
8 var summary = "Basic support for accessing iterable objects using Iterator"; |
|
9 var actual, expect; |
|
10 |
|
11 printBugNumber(BUGNUMBER); |
|
12 printStatus(summary); |
|
13 |
|
14 /************** |
|
15 * BEGIN TEST * |
|
16 **************/ |
|
17 |
|
18 var failed = false; |
|
19 |
|
20 function Array_equals(a, b) |
|
21 { |
|
22 if (!(a instanceof Array) || !(b instanceof Array)) |
|
23 throw new Error("Arguments not both of type Array"); |
|
24 if (a.length != b.length) |
|
25 return false; |
|
26 for (var i = 0, sz = a.length; i < sz; i++) |
|
27 if (a[i] !== b[i]) |
|
28 return false; |
|
29 return true; |
|
30 } |
|
31 |
|
32 var iterable = { persistedProp: 17 }; |
|
33 |
|
34 try |
|
35 { |
|
36 // nothing unusual so far -- verify basic properties |
|
37 for (var i in iterable) |
|
38 { |
|
39 if (i != "persistedProp") |
|
40 throw "no persistedProp!"; |
|
41 if (iterable[i] != 17) |
|
42 throw "iterable[\"persistedProp\"] == 17"; |
|
43 } |
|
44 |
|
45 var keys = ["foo", "bar", "baz"]; |
|
46 var vals = [6, 5, 14]; |
|
47 |
|
48 iterable.__iterator__ = |
|
49 function(keysOnly) |
|
50 { |
|
51 var gen = |
|
52 function() |
|
53 { |
|
54 for (var i = 0; i < keys.length; i++) |
|
55 { |
|
56 if (keysOnly) |
|
57 yield keys[i]; |
|
58 else |
|
59 yield [keys[i], vals[i]]; |
|
60 } |
|
61 }; |
|
62 return gen(); |
|
63 }; |
|
64 |
|
65 /* Test [key, value] Iterator */ |
|
66 var index = 0; |
|
67 var lastSeen = "INITIALVALUE"; |
|
68 var it = Iterator(iterable); |
|
69 try |
|
70 { |
|
71 while (true) |
|
72 { |
|
73 var nextVal = it.next(); |
|
74 if (!Array_equals(nextVal, [keys[index], vals[index]])) |
|
75 throw "Iterator(iterable): wrong next result\n" + |
|
76 " expected: " + [keys[index], vals[index]] + "\n" + |
|
77 " actual: " + nextVal; |
|
78 lastSeen = keys[index]; |
|
79 index++; |
|
80 } |
|
81 } |
|
82 catch (e) |
|
83 { |
|
84 if (lastSeen !== keys[keys.length - 1]) |
|
85 throw "Iterator(iterable): not everything was iterated!\n" + |
|
86 " last iterated was: " + lastSeen + "\n" + |
|
87 " error: " + e; |
|
88 if (e !== StopIteration) |
|
89 throw "Iterator(iterable): missing or invalid StopIteration"; |
|
90 } |
|
91 |
|
92 if (iterable.persistedProp != 17) |
|
93 throw "iterable.persistedProp not persisted!"; |
|
94 |
|
95 /* Test [key, value] Iterator, called with an explicit |false| parameter */ |
|
96 var index = 0; |
|
97 lastSeen = "INITIALVALUE"; |
|
98 it = Iterator(iterable, false); |
|
99 try |
|
100 { |
|
101 while (true) |
|
102 { |
|
103 var nextVal = it.next(); |
|
104 if (!Array_equals(nextVal, [keys[index], vals[index]])) |
|
105 throw "Iterator(iterable, false): wrong next result\n" + |
|
106 " expected: " + [keys[index], vals[index]] + "\n" + |
|
107 " actual: " + nextVal; |
|
108 lastSeen = keys[index]; |
|
109 index++; |
|
110 } |
|
111 } |
|
112 catch (e) |
|
113 { |
|
114 if (lastSeen !== keys[keys.length - 1]) |
|
115 throw "Iterator(iterable, false): not everything was iterated!\n" + |
|
116 " last iterated was: " + lastSeen + "\n" + |
|
117 " error: " + e; |
|
118 if (e !== StopIteration) |
|
119 throw "Iterator(iterable, false): missing or invalid StopIteration"; |
|
120 } |
|
121 |
|
122 if (iterable.persistedProp != 17) |
|
123 throw "iterable.persistedProp not persisted!"; |
|
124 |
|
125 /* Test key-only Iterator */ |
|
126 index = 0; |
|
127 lastSeen = undefined; |
|
128 it = Iterator(iterable, true); |
|
129 try |
|
130 { |
|
131 while (true) |
|
132 { |
|
133 var nextVal = it.next(); |
|
134 if (nextVal !== keys[index]) |
|
135 throw "Iterator(iterable, true): wrong next result\n" + |
|
136 " expected: " + keys[index] + "\n" + |
|
137 " actual: " + nextVal; |
|
138 lastSeen = keys[index]; |
|
139 index++; |
|
140 } |
|
141 } |
|
142 catch (e) |
|
143 { |
|
144 if (lastSeen !== keys[keys.length - 1]) |
|
145 throw "Iterator(iterable, true): not everything was iterated!\n" + |
|
146 " last iterated was: " + lastSeen + "\n" + |
|
147 " error: " + e; |
|
148 if (e !== StopIteration) |
|
149 throw "Iterator(iterable, true): missing or invalid StopIteration"; |
|
150 } |
|
151 |
|
152 if (iterable.persistedProp != 17) |
|
153 throw "iterable.persistedProp not persisted!"; |
|
154 } |
|
155 catch (e) |
|
156 { |
|
157 failed = e; |
|
158 } |
|
159 |
|
160 |
|
161 |
|
162 expect = false; |
|
163 actual = failed; |
|
164 |
|
165 reportCompare(expect, actual, summary); |