js/src/tests/js1_5/extensions/regress-350531.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // |reftest| skip -- slow
michael@0 2 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 //-----------------------------------------------------------------------------
michael@0 8 var BUGNUMBER = 350531;
michael@0 9 var summary = 'exhaustively test parenthesization of binary operator subsets';
michael@0 10 var actual = '';
michael@0 11 var expect = '';
michael@0 12
michael@0 13
michael@0 14 //-----------------------------------------------------------------------------
michael@0 15 test();
michael@0 16 //-----------------------------------------------------------------------------
michael@0 17
michael@0 18 function test()
michael@0 19 {
michael@0 20 enterFunc ('test');
michael@0 21 printBugNumber(BUGNUMBER);
michael@0 22 printStatus (summary);
michael@0 23
michael@0 24 // Translated from permcomb.py, found at
michael@0 25 // http://biotech.embl-ebi.ac.uk:8400/sw/common/share/python/examples/dstruct/classics/permcomb.py
michael@0 26 // by searching for "permcomb.py".
michael@0 27 //
michael@0 28 // This shows bugs, gaps, and verbosities in JS compared to Python:
michael@0 29 // 1. Lack of range([start, ] end[, step]).
michael@0 30 // 2. ![] => false, indeed !<any-object> => false.
michael@0 31 // 3. Missing append or push for strings (if append, then we'd want append for
michael@0 32 // arrays too).
michael@0 33 // 4. Missing slice operator syntax s[i:j].
michael@0 34 // 5. Lack of + for array concatenation.
michael@0 35
michael@0 36 String.prototype.push = function (str) { return this + str; };
michael@0 37
michael@0 38 function permute(list) {
michael@0 39 if (!list.length) // shuffle any sequence
michael@0 40 return [list]; // empty sequence
michael@0 41 var res = [];
michael@0 42 for (var i = 0, n = list.length; i < n; i++) { // delete current node
michael@0 43 var rest = list.slice(0, i).concat(list.slice(i+1));
michael@0 44 for each (var x in permute(rest)) // permute the others
michael@0 45 res.push(list.slice(i, i+1).concat(x)); // add node at front
michael@0 46 }
michael@0 47 return res;
michael@0 48 }
michael@0 49
michael@0 50 function subset(list, size) {
michael@0 51 if (size == 0 || !list.length) // order matters here
michael@0 52 return [list.slice(0, 0)]; // an empty sequence
michael@0 53 var result = [];
michael@0 54 for (var i = 0, n = list.length; i < n; i++) {
michael@0 55 var pick = list.slice(i, i+1); // sequence slice
michael@0 56 var rest = list.slice(0, i).concat(list.slice(i+1)); // keep [:i] part
michael@0 57 for each (var x in subset(rest, size-1))
michael@0 58 result.push(pick.concat(x));
michael@0 59 }
michael@0 60 return result;
michael@0 61 }
michael@0 62
michael@0 63 function combo(list, size) {
michael@0 64 if (size == 0 || !list.length) // order doesn't matter
michael@0 65 return [list.slice(0, 0)]; // xyz == yzx
michael@0 66 var result = [];
michael@0 67 for (var i = 0, n = (list.length - size) + 1; i < n; i++) {
michael@0 68 // iff enough left
michael@0 69 var pick = list.slice(i, i+1);
michael@0 70 var rest = list.slice(i+1); // drop [:i] part
michael@0 71 for each (var x in combo(rest, size - 1))
michael@0 72 result.push(pick.concat(x));
michael@0 73 }
michael@0 74 return result;
michael@0 75 }
michael@0 76
michael@0 77
michael@0 78 // Generate all subsets of distinct binary operators and join them from left
michael@0 79 // to right, parenthesizing minimally. Decompile, recompile, compress spaces
michael@0 80 // and compare to test correct parenthesization.
michael@0 81
michael@0 82 // load("permcomb.js");
michael@0 83
michael@0 84 var bops = [
michael@0 85 ["=", "|=", "^=", "&=", "<<=", ">>=", ">>>=", "+=", "-=", "*=", "/=", "%="],
michael@0 86 ["||"],
michael@0 87 ["&&"],
michael@0 88 ["|"],
michael@0 89 ["^"],
michael@0 90 ["&"],
michael@0 91 ["==", "!=", "===", "!=="],
michael@0 92 ["<", "<=", ">=", ">", "in", "instanceof"],
michael@0 93 ["<<", ">>", ">>>"],
michael@0 94 ["+", "-"],
michael@0 95 ["*", "/", "%"],
michael@0 96 ];
michael@0 97
michael@0 98 var prec = {};
michael@0 99 var aops = [];
michael@0 100
michael@0 101 for (var i = 0; i < bops.length; i++) {
michael@0 102 for (var j = 0; j < bops[i].length; j++) {
michael@0 103 var k = bops[i][j];
michael@0 104 prec[k] = i;
michael@0 105 aops.push(k);
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 // Theoretically all subsets of size 2 should be enough to test, but in case
michael@0 110 // there's some large-scale bug, try up to 5 (or higher? The cost in memory is
michael@0 111 // factorially explosive).
michael@0 112 next_subset:
michael@0 113 for (i = 2; i < 5; i++) {
michael@0 114 var sets = subset(aops, i);
michael@0 115 gc();
michael@0 116
michael@0 117 for each (var set in sets) {
michael@0 118 //print('for each set in sets: ' + (uneval(set)) );
michael@0 119 var src = "(function () {";
michael@0 120 for (j in set) {
michael@0 121 var op = set[j], op2 = set[j-1];
michael@0 122
michael@0 123 // Precedence 0 is for assignment ops, which are right-
michael@0 124 // associative, so don't force left associativity using
michael@0 125 // parentheses.
michael@0 126 if (prec[op] && prec[op] < prec[op2])
michael@0 127 src += "(";
michael@0 128 }
michael@0 129 src += "x ";
michael@0 130 for (j in set) {
michael@0 131 var op = set[j], op2 = set[j+1];
michael@0 132
michael@0 133 // Parenthesize only if not right-associative (precedence 0) and
michael@0 134 // the next op is higher precedence than current.
michael@0 135 var term = (prec[op] && prec[op] < prec[op2]) ? " x)" : " x";
michael@0 136
michael@0 137 src += op + term;
michael@0 138 if (j < set.length - 1)
michael@0 139 src += " ";
michael@0 140 }
michael@0 141 src += ";})";
michael@0 142 try {
michael@0 143 var ref = uneval(eval(src)).replace(/\s+/g, ' ');
michael@0 144 if (ref != src) {
michael@0 145 actual += "BROKEN! input: " + src + " output: " + ref + " ";
michael@0 146 print("BROKEN! input: " + src + " output: " + ref);
michael@0 147 break next_subset;
michael@0 148 }
michael@0 149 } catch (e) {}
michael@0 150 }
michael@0 151 }
michael@0 152
michael@0 153 reportCompare(expect, actual, summary);
michael@0 154
michael@0 155 exitFunc ('test');
michael@0 156 }

mercurial