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

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

mercurial