js/src/tests/ecma_6/Array/for_of_1.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // Test corner cases of for-of iteration over Arrays.
michael@0 2 // The current spidermonky JSOP_SPREAD implementation for function calls
michael@0 3 // with '...rest' arguments uses a ForOfIterator to extract values from
michael@0 4 // the array, so we use that mechanism to test ForOfIterator here.
michael@0 5
michael@0 6
michael@0 7 // Test the properties and prototype of a generator object.
michael@0 8 function TestManySmallArrays() {
michael@0 9 function doIter(f, arr) {
michael@0 10 return f(...arr)
michael@0 11 }
michael@0 12
michael@0 13 function fun(a, b, c) {
michael@0 14 var result = 0;
michael@0 15 for (var i = 0; i < arguments.length; i++)
michael@0 16 result += arguments[i];
michael@0 17 return result;
michael@0 18 }
michael@0 19
michael@0 20
michael@0 21 var TRUE_SUM = 0;
michael@0 22 var N = 100;
michael@0 23 var M = 3;
michael@0 24 var sum = 0;
michael@0 25 for (var i = 0; i < N; i++) {
michael@0 26 var arr = new Array(M);
michael@0 27 for (var j = 0; j < M; j++) {
michael@0 28 arr[j] = j;
michael@0 29 TRUE_SUM += j;
michael@0 30 }
michael@0 31 sum += doIter(fun, arr);
michael@0 32 }
michael@0 33 assertEq(sum, TRUE_SUM);
michael@0 34 }
michael@0 35 TestManySmallArrays();
michael@0 36
michael@0 37 // Test the properties and prototype of a generator object.
michael@0 38 function TestSingleSmallArray() {
michael@0 39 function doIter(f, arr) {
michael@0 40 return f(...arr)
michael@0 41 }
michael@0 42
michael@0 43 function fun(a, b, c) {
michael@0 44 var result = 0;
michael@0 45 for (var i = 0; i < arguments.length; i++)
michael@0 46 result += arguments[i];
michael@0 47 return result;
michael@0 48 }
michael@0 49
michael@0 50
michael@0 51 var TRUE_SUM = 0;
michael@0 52 var N = 100;
michael@0 53 var M = 3;
michael@0 54 var arr = new Array(M);
michael@0 55 for (var j = 0; j < M; j++) {
michael@0 56 arr[j] = j;
michael@0 57 TRUE_SUM += j;
michael@0 58 }
michael@0 59 TRUE_SUM *= N;
michael@0 60
michael@0 61 var sum = 0;
michael@0 62 for (var i = 0; i < N; i++) {
michael@0 63 sum += doIter(fun, arr);
michael@0 64 }
michael@0 65 assertEq(sum, TRUE_SUM);
michael@0 66 }
michael@0 67 TestSingleSmallArray();
michael@0 68
michael@0 69
michael@0 70 function TestChangeArrayPrototype() {
michael@0 71 function doIter(f, arr) {
michael@0 72 return f(...arr)
michael@0 73 }
michael@0 74
michael@0 75 function fun(a, b, c) {
michael@0 76 var result = 0;
michael@0 77 for (var i = 0; i < arguments.length; i++)
michael@0 78 result += arguments[i];
michael@0 79 return result;
michael@0 80 }
michael@0 81
michael@0 82 var Proto1 = Object.create(Array.prototype);
michael@0 83
michael@0 84 var TRUE_SUM = 0;
michael@0 85 var N = 100;
michael@0 86 var MID = N/2;
michael@0 87 var M = 3;
michael@0 88 var arr = new Array(M);
michael@0 89 var ARR_SUM = 0;
michael@0 90 for (var j = 0; j < M; j++) {
michael@0 91 arr[j] = j;
michael@0 92 ARR_SUM += j;
michael@0 93 }
michael@0 94
michael@0 95 var sum = 0;
michael@0 96 for (var i = 0; i < N; i++) {
michael@0 97 sum += doIter(fun, arr);
michael@0 98 if (i == MID)
michael@0 99 arr.__proto__ = Proto1;
michael@0 100 TRUE_SUM += ARR_SUM;
michael@0 101 }
michael@0 102 assertEq(sum, TRUE_SUM);
michael@0 103 }
michael@0 104 TestChangeArrayPrototype();
michael@0 105
michael@0 106
michael@0 107 function TestChangeManyArrayShape() {
michael@0 108 function doIter(f, arr) {
michael@0 109 return f(...arr)
michael@0 110 }
michael@0 111
michael@0 112 function fun(a, b, c) {
michael@0 113 var result = 0;
michael@0 114 for (var i = 0; i < arguments.length; i++)
michael@0 115 result += arguments[i];
michael@0 116 return result;
michael@0 117 }
michael@0 118
michael@0 119 var TRUE_SUM = 0;
michael@0 120 var N = 100;
michael@0 121 var MID = N/2;
michael@0 122 var M = 3;
michael@0 123 var sum = 0;
michael@0 124 for (var i = 0; i < N; i++) {
michael@0 125 var arr = new Array(M);
michael@0 126 var ARR_SUM = 0;
michael@0 127 for (var j = 0; j < M; j++) {
michael@0 128 arr[j] = j;
michael@0 129 ARR_SUM += j;
michael@0 130 }
michael@0 131 arr['v_' + i] = i;
michael@0 132 sum += doIter(fun, arr);
michael@0 133 TRUE_SUM += ARR_SUM;
michael@0 134 }
michael@0 135 assertEq(sum, TRUE_SUM);
michael@0 136 }
michael@0 137 TestChangeManyArrayShape();
michael@0 138
michael@0 139 if (typeof reportCompare === "function")
michael@0 140 reportCompare(true, true);

mercurial