OLD | NEW |
(Empty) | |
| 1 // Generates a comparison table test case. |
| 2 // Usage: d8 compare-table-gen.js -- lt|lteq|gt|gteq|eq|ne|eq|sne |
| 3 |
| 4 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); |
| 6 for (var i = 0; i < strings.length; i++) { |
| 7 values[i] = eval(strings[i]); |
| 8 } |
| 9 |
| 10 function test() { |
| 11 for (var i = 0; i < values.length; i++) { |
| 12 for (var j = 0; j < values.length; j++) { |
| 13 var a = values[i]; |
| 14 var b = values[j]; |
| 15 var x = expected[i][j]; |
| 16 assertEquals(x, func(a,b)); |
| 17 assertEquals(x, left_funcs[i](b)); |
| 18 assertEquals(x, right_funcs[j](a)); |
| 19 } |
| 20 } |
| 21 |
| 22 var result = matrix(); |
| 23 for (var i = 0; i < values.length; i++) { |
| 24 for (var j = 0; j < values.length; j++) { |
| 25 assertEquals(expected[i][j], result[i][j]); |
| 26 } |
| 27 } |
| 28 } |
| 29 |
| 30 function gen(name, cmp) { |
| 31 |
| 32 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"); |
| 34 print("// found in the LICENSE file."); |
| 35 print(); |
| 36 print("var values = [" + strings + "];"); |
| 37 |
| 38 var body = "(function " + name + "(a,b) { return a " + cmp + " b; })"; |
| 39 var func = eval(body); |
| 40 |
| 41 print("var expected = ["); |
| 42 |
| 43 for (var i = 0; i < values.length; i++) { |
| 44 var line = " ["; |
| 45 for (var j = 0; j < values.length; j++) { |
| 46 if (j > 0) line += ","; |
| 47 line += func(values[i], values[j]) ? "true " : "false"; |
| 48 } |
| 49 line += "]"; |
| 50 if (i < (values.length - 1)) line += ","; |
| 51 print(line); |
| 52 } |
| 53 print("];"); |
| 54 |
| 55 print("var func = " + body + ";"); |
| 56 print("var left_funcs = ["); |
| 57 |
| 58 for (var i = 0; i < values.length; i++) { |
| 59 var value = strings[i]; |
| 60 var body = "(function " + name + "_L" + i + "(b) { return " + value + " " +
cmp + " b; })"; |
| 61 var end = i < (values.length - 1) ? "," : ""; |
| 62 print(" " + body + end); |
| 63 } |
| 64 print("];"); |
| 65 |
| 66 print("var right_funcs = ["); |
| 67 for (var i = 0; i < values.length; i++) { |
| 68 var value = strings[i]; |
| 69 var body = "(function " + name + "_R" + i + "(a) { return a " + cmp + " " +
value + "; })"; |
| 70 var end = i < (values.length - 1) ? "," : ""; |
| 71 print(" " + body + end); |
| 72 } |
| 73 print("];"); |
| 74 |
| 75 print("function matrix() {"); |
| 76 print(" return ["); |
| 77 for (var i = 0; i < values.length; i++) { |
| 78 var line = " ["; |
| 79 for (var j = 0; j < values.length; j++) { |
| 80 if (j > 0) line += ","; |
| 81 line += strings[i] + " " + cmp + " " + strings[j]; |
| 82 } |
| 83 line += "]"; |
| 84 if (i < (values.length - 1)) line += ","; |
| 85 print(line); |
| 86 } |
| 87 print(" ];"); |
| 88 print("}"); |
| 89 |
| 90 |
| 91 print(test.toString()); |
| 92 print("test();"); |
| 93 print("test();"); |
| 94 |
| 95 print(); |
| 96 print(); |
| 97 } |
| 98 |
| 99 switch (arguments[0]) { |
| 100 case "lt": gen("lt", "<"); break; |
| 101 case "lteq": gen("lteq", "<="); break; |
| 102 case "gt": gen("gt", ">"); break; |
| 103 case "gteq": gen("gteq", ">="); break; |
| 104 case "eq": gen("eq", "=="); break; |
| 105 case "ne": gen("ne", "!="); break; |
| 106 case "seq": gen("seq", "==="); break; |
| 107 case "sne": gen("sne", "!=="); break; |
| 108 } |
OLD | NEW |