summaryrefslogtreecommitdiffstats
path: root/js/src/tests/js1_7/extensions/basic-Iterator.js
blob: b35f46d03d36dc0b9716aea316828e42f0eafde8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//-----------------------------------------------------------------------------
var BUGNUMBER     = "(none)";
var summary = "Basic support for accessing iterable objects using Iterator";
var actual, expect;

printBugNumber(BUGNUMBER);
printStatus(summary);

/**************
 * BEGIN TEST *
 **************/

var failed = false;

function Array_equals(a, b)
{
  if (!(a instanceof Array) || !(b instanceof Array))
    throw new Error("Arguments not both of type Array");
  if (a.length != b.length)
    return false;
  for (var i = 0, sz = a.length; i < sz; i++)
    if (a[i] !== b[i])
      return false;
  return true;
}

var iterable = { persistedProp: 17 };

try
{
  // nothing unusual so far -- verify basic properties
  for (var i in iterable)
  {
    if (i != "persistedProp")
      throw "no persistedProp!";
    if (iterable[i] != 17)
      throw "iterable[\"persistedProp\"] == 17";
  }

  var keys = ["foo", "bar", "baz"];
  var vals = [6, 5, 14];

  iterable.__iterator__ =
    function(keysOnly)
    {
      var gen =
      function()
      {
	for (var i = 0; i < keys.length; i++)
	{
	  if (keysOnly)
	    yield keys[i];
	  else
	    yield [keys[i], vals[i]];
	}
      };
      return gen();
    };

  /* Test [key, value] Iterator */
  var index = 0;
  var lastSeen = "INITIALVALUE";
  var it = Iterator(iterable);
  try
  {
    while (true)
    {
      var nextVal = it.next();
      if (!Array_equals(nextVal, [keys[index], vals[index]]))
        throw "Iterator(iterable): wrong next result\n" +
	  "  expected: " + [keys[index], vals[index]] + "\n" +
	  "  actual:   " + nextVal;
      lastSeen = keys[index];
      index++;
    }
  }
  catch (e)
  {
    if (lastSeen !== keys[keys.length - 1])
      throw "Iterator(iterable): not everything was iterated!\n" +
	"  last iterated was: " + lastSeen + "\n" +
	"  error: " + e;
    if (e !== StopIteration)
      throw "Iterator(iterable): missing or invalid StopIteration";
  }

  if (iterable.persistedProp != 17)
    throw "iterable.persistedProp not persisted!";

  /* Test [key, value] Iterator, called with an explicit |false| parameter */
  var index = 0;
  lastSeen = "INITIALVALUE";
  it = Iterator(iterable, false);
  try
  {
    while (true)
    {
      var nextVal = it.next();
      if (!Array_equals(nextVal, [keys[index], vals[index]]))
        throw "Iterator(iterable, false): wrong next result\n" +
	  "  expected: " + [keys[index], vals[index]] + "\n" +
	  "  actual:   " + nextVal;
      lastSeen = keys[index];
      index++;
    }
  }
  catch (e)
  {
    if (lastSeen !== keys[keys.length - 1])
      throw "Iterator(iterable, false): not everything was iterated!\n" +
	"  last iterated was: " + lastSeen + "\n" +
	"  error: " + e;
    if (e !== StopIteration)
      throw "Iterator(iterable, false): missing or invalid StopIteration";
  }

  if (iterable.persistedProp != 17)
    throw "iterable.persistedProp not persisted!";

  /* Test key-only Iterator */
  index = 0;
  lastSeen = undefined;
  it = Iterator(iterable, true);
  try
  {
    while (true)
    {
      var nextVal = it.next();
      if (nextVal !== keys[index])
        throw "Iterator(iterable, true): wrong next result\n" +
	  "  expected: " + keys[index] + "\n" +
	  "  actual:   " + nextVal;
      lastSeen = keys[index];
      index++;
    }
  }
  catch (e)
  {
    if (lastSeen !== keys[keys.length - 1])
      throw "Iterator(iterable, true): not everything was iterated!\n" +
	"  last iterated was: " + lastSeen + "\n" +
	"  error: " + e;
    if (e !== StopIteration)
      throw "Iterator(iterable, true): missing or invalid StopIteration";
  }

  if (iterable.persistedProp != 17)
    throw "iterable.persistedProp not persisted!";
}
catch (e)
{
  failed = e;
}



expect = false;
actual = failed;

reportCompare(expect, actual, summary);