OLD | NEW |
| 1 // Copyright 2015 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 |
1 // Generates a comparison table test case. | 5 // Generates a comparison table test case. |
2 // Usage: d8 compare-table-gen.js -- lt|lteq|gt|gteq|eq|ne|eq|sne | 6 // Usage: d8 compare-table-gen.js -- lt|lteq|gt|gteq|eq|ne|eq|sne|min|max |
3 | 7 |
4 var strings = ["true", "false", "null", "void 0", "0", "0.0", "-0", "\"\"", "-1"
, "-1.25", "1", "1.25", "-2147483648", "2147483648", "Infinity", "-Infinity", "N
aN"]; | 8 var strings = ["true", "false", "null", "void 0", "0", "0.0", "-0", "\"\"", "-1"
, "-1.25", "1", "1.25", "-2147483648", "2147483648", "Infinity", "-Infinity", "N
aN"]; |
5 var values = new Array(strings.length); | 9 var values = new Array(strings.length); |
6 for (var i = 0; i < strings.length; i++) { | 10 for (var i = 0; i < strings.length; i++) { |
7 values[i] = eval(strings[i]); | 11 values[i] = eval(strings[i]); |
8 } | 12 } |
9 | 13 |
10 function test() { | 14 function test() { |
11 for (var i = 0; i < values.length; i++) { | 15 for (var i = 0; i < values.length; i++) { |
12 for (var j = 0; j < values.length; j++) { | 16 for (var j = 0; j < values.length; j++) { |
13 var a = values[i]; | 17 var a = values[i]; |
14 var b = values[j]; | 18 var b = values[j]; |
15 var x = expected[i][j]; | 19 var x = expected[i][j]; |
16 assertEquals(x, func(a,b)); | 20 assertEquals(x, func(a,b)); |
17 assertEquals(x, left_funcs[i](b)); | 21 assertEquals(x, left_funcs[i](b)); |
18 assertEquals(x, right_funcs[j](a)); | 22 assertEquals(x, right_funcs[j](a)); |
19 } | 23 } |
20 } | 24 } |
21 | 25 |
22 var result = matrix(); | 26 var result = matrix(); |
23 for (var i = 0; i < values.length; i++) { | 27 for (var i = 0; i < values.length; i++) { |
24 for (var j = 0; j < values.length; j++) { | 28 for (var j = 0; j < values.length; j++) { |
25 assertEquals(expected[i][j], result[i][j]); | 29 assertEquals(expected[i][j], result[i][j]); |
26 } | 30 } |
27 } | 31 } |
28 } | 32 } |
29 | 33 |
30 function gen(name, cmp) { | 34 function expr(infix, a, cmp, b) { |
| 35 return infix ? a + " " + cmp + " " + b : cmp + "(" + a + ", " + b + ")"; |
| 36 } |
| 37 |
| 38 function SpecialToString(x) { |
| 39 if ((1 / x) == -Infinity) return "-0"; |
| 40 return "" + x; |
| 41 } |
| 42 |
| 43 function gen(name, cmp, infix) { |
31 | 44 |
32 print("// Copyright 2015 the V8 project authors. All rights reserved."); | 45 print("// Copyright 2015 the V8 project authors. All rights reserved."); |
33 print("// Use of this source code is governed by a BSD-style license that can
be"); | 46 print("// Use of this source code is governed by a BSD-style license that can
be"); |
34 print("// found in the LICENSE file."); | 47 print("// found in the LICENSE file."); |
35 print(); | 48 print(); |
36 print("var values = [" + strings + "];"); | 49 print("var values = [" + strings + "];"); |
37 | 50 |
38 var body = "(function " + name + "(a,b) { return a " + cmp + " b; })"; | 51 var body = "(function " + name + "(a,b) { return " + expr(infix, "a", cmp, "b"
) + "; })"; |
39 var func = eval(body); | 52 var func = eval(body); |
40 | 53 |
41 print("var expected = ["); | 54 print("var expected = ["); |
42 | 55 |
43 for (var i = 0; i < values.length; i++) { | 56 for (var i = 0; i < values.length; i++) { |
44 var line = " ["; | 57 var line = " ["; |
45 for (var j = 0; j < values.length; j++) { | 58 for (var j = 0; j < values.length; j++) { |
46 if (j > 0) line += ","; | 59 if (j > 0) line += ","; |
47 line += func(values[i], values[j]) ? "true " : "false"; | 60 line += SpecialToString(func(values[i], values[j])); |
48 } | 61 } |
49 line += "]"; | 62 line += "]"; |
50 if (i < (values.length - 1)) line += ","; | 63 if (i < (values.length - 1)) line += ","; |
51 print(line); | 64 print(line); |
52 } | 65 } |
53 print("];"); | 66 print("];"); |
54 | 67 |
55 print("var func = " + body + ";"); | 68 print("var func = " + body + ";"); |
56 print("var left_funcs = ["); | 69 print("var left_funcs = ["); |
57 | 70 |
58 for (var i = 0; i < values.length; i++) { | 71 for (var i = 0; i < values.length; i++) { |
59 var value = strings[i]; | 72 var value = strings[i]; |
60 var body = "(function " + name + "_L" + i + "(b) { return " + value + " " +
cmp + " b; })"; | 73 var body = "(function " + name + "_L" + i + "(b) { return " + expr(infix, va
lue, cmp, "b") + "; })"; |
61 var end = i < (values.length - 1) ? "," : ""; | 74 var end = i < (values.length - 1) ? "," : ""; |
62 print(" " + body + end); | 75 print(" " + body + end); |
63 } | 76 } |
64 print("];"); | 77 print("];"); |
65 | 78 |
66 print("var right_funcs = ["); | 79 print("var right_funcs = ["); |
67 for (var i = 0; i < values.length; i++) { | 80 for (var i = 0; i < values.length; i++) { |
68 var value = strings[i]; | 81 var value = strings[i]; |
69 var body = "(function " + name + "_R" + i + "(a) { return a " + cmp + " " +
value + "; })"; | 82 var body = "(function " + name + "_R" + i + "(a) { return " + expr(infix, "a
", cmp, value) + "; })"; |
70 var end = i < (values.length - 1) ? "," : ""; | 83 var end = i < (values.length - 1) ? "," : ""; |
71 print(" " + body + end); | 84 print(" " + body + end); |
72 } | 85 } |
73 print("];"); | 86 print("];"); |
74 | 87 |
75 print("function matrix() {"); | 88 print("function matrix() {"); |
76 print(" return ["); | 89 print(" return ["); |
77 for (var i = 0; i < values.length; i++) { | 90 for (var i = 0; i < values.length; i++) { |
78 var line = " ["; | 91 var line = " ["; |
79 for (var j = 0; j < values.length; j++) { | 92 for (var j = 0; j < values.length; j++) { |
80 if (j > 0) line += ","; | 93 if (j > 0) line += ","; |
81 line += strings[i] + " " + cmp + " " + strings[j]; | 94 line += expr(infix, strings[i], cmp, strings[j]); |
82 } | 95 } |
83 line += "]"; | 96 line += "]"; |
84 if (i < (values.length - 1)) line += ","; | 97 if (i < (values.length - 1)) line += ","; |
85 print(line); | 98 print(line); |
86 } | 99 } |
87 print(" ];"); | 100 print(" ];"); |
88 print("}"); | 101 print("}"); |
89 | 102 |
90 | 103 |
91 print(test.toString()); | 104 print(test.toString()); |
92 print("test();"); | 105 print("test();"); |
93 print("test();"); | 106 print("test();"); |
94 | |
95 print(); | |
96 print(); | |
97 } | 107 } |
98 | 108 |
99 switch (arguments[0]) { | 109 switch (arguments[0]) { |
100 case "lt": gen("lt", "<"); break; | 110 case "lt": gen("lt", "<", true); break; |
101 case "lteq": gen("lteq", "<="); break; | 111 case "lteq": gen("lteq", "<=", true); break; |
102 case "gt": gen("gt", ">"); break; | 112 case "gt": gen("gt", ">", true); break; |
103 case "gteq": gen("gteq", ">="); break; | 113 case "gteq": gen("gteq", ">=", true); break; |
104 case "eq": gen("eq", "=="); break; | 114 case "eq": gen("eq", "==", true); break; |
105 case "ne": gen("ne", "!="); break; | 115 case "ne": gen("ne", "!=", true); break; |
106 case "seq": gen("seq", "==="); break; | 116 case "seq": gen("seq", "===", true); break; |
107 case "sne": gen("sne", "!=="); break; | 117 case "sne": gen("sne", "!==", true); break; |
| 118 case "min": gen("min", "Math.min", false); break; |
| 119 case "max": gen("max", "Math.max", false); break; |
108 } | 120 } |
OLD | NEW |