]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - tests/stress/array-from-with-iterable.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / array-from-with-iterable.js
... / ...
CommitLineData
1function shouldBe(actual, expected) {
2 if (actual !== expected)
3 throw new Error('bad value: ' + actual);
4}
5
6// Set is iterable.
7var set = new Set([0, 1, 2, 3, 4, 5]);
8var array = Array.from(set);
9
10shouldBe(array.length, set.size);
11for (var i = 0; i < array.length; ++i) {
12 shouldBe(set.has(array[i]), true);
13}
14
15// Map is iterable.
16var map = new Map([
17 [0, 0],
18 [1, 0],
19 [2, 0],
20 [3, 0],
21 [4, 0],
22 [5, 0]
23]);
24var array = Array.from(map);
25
26shouldBe(array.length, map.size);
27for (var i = 0; i < array.length; ++i) {
28 shouldBe(array[i][1], 0);
29 shouldBe(map.has(array[i][0]), true);
30 shouldBe(map.get(array[i][0]), 0);
31}
32
33// String is iterable
34var string = "Cocoa Cappuccino";
35var array = Array.from(string);
36shouldBe(array.length, string.length);
37for (var i = 0; i < array.length; ++i) {
38 shouldBe(array[i], string[i]);
39}
40
41// Arguments is iterable
42var argumentsGenerators = [
43 function () {
44 return arguments;
45 },
46
47 function () {
48 'use strict';
49 return arguments;
50 },
51
52 function (a, b, c) {
53 return arguments;
54 },
55
56 function (a, b, c) {
57 'use strict';
58 return arguments;
59 }
60];
61
62for (var gen of argumentsGenerators) {
63 var args = gen(1, 2, 3, 4, 5);
64 var array = Array.from(args);
65 shouldBe(array.length, args.length);
66 for (var i = 0; i < array.length; ++i) {
67 shouldBe(array[i], args[i]);
68 }
69}