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