v8/test/mjsunit/compiler/nary-binary-ops.js
Leszek Swirski 27b1c47351 [ignition] Support n-ary logical ops
Following up on adding n-ary nodes, this extends the parser and
interpreter to support n-ary logical operations.

Bug: v8:6964
Bug: chromium:731861
Change-Id: Ife2141c389b9abccd917ab2aaddf399c436ef777
Reviewed-on: https://chromium-review.googlesource.com/735497
Reviewed-by: Adam Klein <adamk@chromium.org>
Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49029}
2017-10-30 11:20:43 +00:00

151 lines
3.6 KiB
JavaScript

// 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
// binary op calls. Also test binop chains inside an if condition return
// the same branch.
// 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);
}
// 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);
}
const BINOPS = [
",",
"||",
"&&",
"|",
"^",
"&",
"<<",
">>",
">>>",
"+",
"-",
"*",
"/",
"%",
];
// Test each binop to see if the chained version is equivalent to the non-
// chained one.
for (let op of BINOPS) {
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);
// 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);
// 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 + " ") + ")");
}
}