nary-binary-ops.js 3.64 KB
Newer Older
1 2 3 4 5
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Test that n-ary chains of binary ops give an equal result to individual
6 7
// binary op calls. Also test binop chains inside an if condition return
// the same branch.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

// Generate a function of the form
//
// function(init,a0,...,aN) {
//   return init + a0 + ... + aN;
// }
//
// where + can be any binary operation.
function generate_chained_op(op, num_ops) {
    let str = "(function(init";
    for (let i = 0; i < num_ops; i++) {
        str += ",a"+i;
    }
    str += "){return (init";
    for (let i = 0; i < num_ops; i++) {
        str += op+"a"+i;
    }
    str += ");})";
    return eval(str);
}

// Generate a function of the form
//
// function(init,a0,...,aN) {
//   var tmp = init;
//   tmp = tmp + a0;
//   ...
//   tmp = tmp + aN;
//   return tmp;
// }
//
// where + can be any binary operation.
function generate_nonchained_op(op, num_ops) {
    let str = "(function(init";
    for (let i = 0; i < num_ops; i++) {
        str += ",a"+i;
    }
    str += "){ var tmp=init; ";
    for (let i = 0; i < num_ops; i++) {
        str += "tmp=(tmp"+op+"a"+i+");";
    }
    str += "return tmp;})";
    return eval(str);
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
// Generate a function of the form
//
// function(init,a0,...,aN) {
//   if(init + a0 + ... + aN) return 1;
//   else return 0;
// }
//
// where + can be any binary operation.
function generate_chained_op_test(op, num_ops) {
    let str = "(function(init";
    for (let i = 0; i < num_ops; i++) {
        str += ",a"+i;
    }
    str += "){ if(init";
    for (let i = 0; i < num_ops; i++) {
        str += op+"a"+i;
    }
    str += ")return 1;else return 0;})";
    return eval(str);
}

// Generate a function of the form
//
// function(init,a0,...,aN) {
//   var tmp = init;
//   tmp = tmp + a0;
//   ...
//   tmp = tmp + aN;
//   if(tmp) return 1
//  else return 0;
// }
//
// where + can be any binary operation.
function generate_nonchained_op_test(op, num_ops) {
    let str = "(function(init";
    for (let i = 0; i < num_ops; i++) {
        str += ",a"+i;
    }
    str += "){ var tmp=init; ";
    for (let i = 0; i < num_ops; i++) {
        str += "tmp=(tmp"+op+"a"+i+");";
    }
    str += "if(tmp)return 1;else return 0;})";
    return eval(str);
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
const BINOPS = [
    ",",
    "||",
    "&&",
    "|",
    "^",
    "&",
    "<<",
    ">>",
    ">>>",
    "+",
    "-",
    "*",
    "/",
    "%",
];

// Test each binop to see if the chained version is equivalent to the non-
// chained one.
for (let op of BINOPS) {
119 120 121 122
    let chained = generate_chained_op(op, 4);
    let nonchained = generate_nonchained_op(op, 4);
    let chained_test = generate_chained_op_test(op, 4);
    let nonchained_test = generate_nonchained_op_test(op, 4);
123 124 125 126 127 128 129 130 131 132 133 134

    // With numbers.
    assertEquals(
        nonchained(1,2,3,4,5),
        chained(1,2,3,4,5),
        "numeric " + op);

    // With numbers and strings.
    assertEquals(
        nonchained(1,"2",3,"4",5),
        chained(1,"2",3,"4",5),
        "numeric and string " + op);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    // Iterate over all possible combinations of 5 numbers that evaluate
    // to boolean true or false (for testing logical ops).
    for (var i = 0; i < 32; i++) {
        var booleanArray = [i & 1, i & 2, i & 4, i & 8, i & 16];
        assertEquals(
            nonchained.apply(this, booleanArray),
            chained.apply(this, booleanArray),
            booleanArray.join(" " + op + " "));

        assertEquals(
            nonchained_test.apply(this, booleanArray),
            chained_test.apply(this, booleanArray),
            "if (" + booleanArray.join(" " + op + " ") + ")");
    }
150
}