1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --expose-debug-as debug
6// Test the mirror object for collection iterators.
7
8function testIteratorMirror(iter, offset, expected, opt_limit) {
9  while (offset-- > 0) iter.next();
10
11  var mirror = debug.MakeMirror(iter);
12  assertTrue(mirror.isIterator());
13
14  var preview = mirror.preview(opt_limit);
15  assertArrayEquals(expected, preview);
16
17  // Check that iterator has not changed after taking preview.
18  var values = [];
19  for (var i of iter) {
20    if (opt_limit && values.length >= opt_limit) break;
21    values.push(i);
22  }
23  assertArrayEquals(expected, values);
24}
25
26function testIteratorInternalProperties(iter, offset, kind, index, has_more) {
27  while (offset-- > 0) iter.next();
28
29  var mirror = debug.MakeMirror(iter);
30  assertTrue(mirror.isIterator());
31
32  var properties = mirror.internalProperties();
33  assertEquals(3, properties.length);
34  assertEquals("[[IteratorHasMore]]", properties[0].name());
35  assertEquals(has_more, properties[0].value().value());
36  assertEquals("[[IteratorIndex]]", properties[1].name());
37  assertEquals(index, properties[1].value().value());
38  assertEquals("[[IteratorKind]]", properties[2].name());
39  assertEquals(kind, properties[2].value().value());
40}
41
42var o1 = { foo: 1 };
43var o2 = { foo: 2 };
44
45var map = new Map();
46map.set(41, 42);
47map.set(o1, o2);
48
49testIteratorMirror(map.keys(), 0, [41, o1]);
50testIteratorMirror(map.values(), 0, [42, o2]);
51testIteratorMirror(map.entries(), 0, [[41, 42], [o1, o2]]);
52
53testIteratorMirror(map.keys(), 1, [o1]);
54testIteratorMirror(map.values(), 1, [o2]);
55testIteratorMirror(map.entries(), 1, [[o1, o2]]);
56
57testIteratorMirror(map.keys(), 2, []);
58testIteratorMirror(map.values(), 2, []);
59testIteratorMirror(map.entries(), 2, []);
60
61// Test with maximum limit.
62testIteratorMirror(map.keys(), 0, [41], 1);
63testIteratorMirror(map.values(), 0, [42], 1);
64testIteratorMirror(map.entries(), 0, [[41, 42]], 1);
65
66testIteratorInternalProperties(map.keys(), 0, "keys", 0, true);
67testIteratorInternalProperties(map.values(), 1, "values", 1, true);
68testIteratorInternalProperties(map.entries(), 2, "entries", 2, false);
69testIteratorInternalProperties(map.keys(), 3, "keys", 2, false);
70
71var set = new Set();
72set.add(41);
73set.add(42);
74set.add(o1);
75set.add(o2);
76
77testIteratorMirror(set.keys(), 0, [41, 42, o1, o2]);
78testIteratorMirror(set.values(), 0, [41, 42, o1, o2]);
79testIteratorMirror(set.entries(), 0, [[41, 41], [42, 42], [o1, o1], [o2, o2]]);
80
81testIteratorMirror(set.keys(), 1, [42, o1, o2]);
82testIteratorMirror(set.values(), 1, [42, o1, o2]);
83testIteratorMirror(set.entries(), 1, [[42, 42], [o1, o1], [o2, o2]]);
84
85testIteratorMirror(set.keys(), 3, [o2]);
86testIteratorMirror(set.values(), 3, [o2]);
87testIteratorMirror(set.entries(), 3, [[o2, o2]]);
88
89testIteratorMirror(set.keys(), 5, []);
90testIteratorMirror(set.values(), 5, []);
91testIteratorMirror(set.entries(), 5, []);
92
93// Test with maximum limit.
94testIteratorMirror(set.keys(), 1, [42, o1], 2);
95testIteratorMirror(set.values(), 1, [42, o1], 2);
96testIteratorMirror(set.entries(), 1, [[42, 42], [o1, o1]], 2);
97
98testIteratorInternalProperties(set.keys(), 0, "values", 0, true);
99testIteratorInternalProperties(set.values(), 1, "values", 1, true);
100testIteratorInternalProperties(set.entries(), 2, "entries", 2, true);
101testIteratorInternalProperties(set.keys(), 3, "values", 3, true);
102testIteratorInternalProperties(set.values(), 4, "values", 4, false);
103testIteratorInternalProperties(set.entries(), 5, "entries", 4, false);
104