• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15var methods = ['entries', 'keys', 'values', Symbol.iterator];
16
17methods.forEach(function (method) {
18  try {
19    Set.prototype[method].call(5);
20    assert(false);
21  } catch (e) {
22    assert(e instanceof TypeError);
23  }
24});
25
26methods.forEach(function (method) {
27  try {
28    Set.prototype[method].call({});
29    assert(false);
30  } catch (e) {
31    assert(e instanceof TypeError);
32  }
33});
34
35var s = new Set([0, 1, 2, 3, 4, 5, 6]);
36
37methods.forEach(function(method) {
38  assert(s[method]().toString() === '[object Set Iterator]');
39});
40
41methods.forEach(function (method) {
42  try {
43    s[method].next.call(5);
44    assert(false);
45  } catch (e) {
46    assert(e instanceof TypeError);
47  }
48});
49
50methods.forEach(function (method) {
51  try {
52    s[method].next.call({});
53    assert(false);
54  } catch (e) {
55    assert(e instanceof TypeError);
56  }
57});
58
59var setFromSet = new Set(s);
60assert(setFromSet.size === 7);
61
62var iterators = [s.keys(), s.values(), s[Symbol.iterator]()];
63var entryIterator = s.entries();
64var elementCount = s.size;
65
66for (var i = 0; i < elementCount; i++) {
67  iterators.forEach(function(element) {
68    var next = element.next();
69    assert(next.done === false);
70    assert(next.value === i);
71  });
72
73  var next = entryIterator.next();
74  assert(next.done === false);
75  assert(next.value[0] === i);
76  assert(next.value[1] === i);
77}
78
79iterators.forEach(function(element) {
80    var next = element.next();
81    assert(next.done === true);
82    assert(next.value === undefined);
83  });
84
85var next = entryIterator.next();
86assert(next.done === true);
87assert(next.value === undefined);
88
89
90iterators = [s.keys(), s.values(), s[Symbol.iterator]()];
91entryIterator = s.entries();
92var elementCount = s.size;
93
94for (var i = 0; i < elementCount; i++) {
95  iterators.forEach(function(element) {
96    var next = element.next();
97    assert(next.done === false);
98    assert(next.value === i);
99  });
100
101  var next = entryIterator.next();
102  assert(next.done === false);
103  assert(next.value[0] === i);
104  assert(next.value[1] === i);
105  s.delete(i);
106}
107
108assert(s.size === 0);
109
110s = new Set ([0, 1]);
111var expected = [0, 1, 2, 4, 5, 6, 3];
112var loopCount = 0;
113
114s.forEach(function(element) {
115  if (loopCount === 0) {
116    for (var i = 0; i < expected.length ; i++) {
117      s.add(i);
118    }
119    s.delete(3);
120    s.add(3);
121  }
122  assert(element === expected[loopCount++]);
123});
124
125assert(loopCount === expected.length);
126
127s = new Set([0, 1, 2, 3, 4, 5, 6]);
128expected = [0, 1];
129loopCount = 0;
130
131for (var value of s) {
132  if (loopCount === 0) {
133    s.clear();
134    s.add(1);
135  }
136
137  assert(value === expected[loopCount++]);
138}
139
140s = new Set([0])
141expected = [0, 1];
142loopCount = 0;
143
144for (var value of s) {
145  if (loopCount === 0) {
146    s.add(2);
147    s.delete(2);
148    s.add(3);
149    s.delete(3);
150    s.add(1);
151  }
152
153  assert(value === expected[loopCount++]);
154}
155