Bug 784873 - Improve ParallelArray jit-test code (r=dvander)
authorShu-yu Guo <shu@rfrn.org>
Sun, 26 Aug 2012 12:02:42 -0700
changeset 105531 0eece65be2df5d1538f9f6624e7fc9edb7dd4020
parent 105530 bf3e9e62d5dee3807b0f2800ec830ffadddfc9b5
child 105532 a5d691072fd6192d4197d67c065bae3b2455e299
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersdvander
bugs784873
milestone17.0a1
Bug 784873 - Improve ParallelArray jit-test code (r=dvander)
js/src/jit-test/lib/parallelarray-helpers.js
js/src/jit-test/tests/parallelarray/comprehension-1.js
js/src/jit-test/tests/parallelarray/comprehension-2.js
js/src/jit-test/tests/parallelarray/comprehension-scale.js
js/src/jit-test/tests/parallelarray/constructor-1.js
js/src/jit-test/tests/parallelarray/constructor-4.js
js/src/jit-test/tests/parallelarray/element-2.js
js/src/jit-test/tests/parallelarray/filter-1.js
js/src/jit-test/tests/parallelarray/filter-2.js
js/src/jit-test/tests/parallelarray/filter-3.js
js/src/jit-test/tests/parallelarray/filter-4.js
js/src/jit-test/tests/parallelarray/flatten-1.js
js/src/jit-test/tests/parallelarray/flatten-2.js
js/src/jit-test/tests/parallelarray/get-2.js
js/src/jit-test/tests/parallelarray/map-1.js
js/src/jit-test/tests/parallelarray/map-3.js
js/src/jit-test/tests/parallelarray/partition-1.js
js/src/jit-test/tests/parallelarray/scan-3.js
js/src/jit-test/tests/parallelarray/scatter-2.js
js/src/jit-test/tests/parallelarray/scatter-3.js
js/src/jit-test/tests/parallelarray/scatter-4.js
js/src/jit-test/tests/parallelarray/scatter-5.js
js/src/jit-test/tests/parallelarray/scatter-7.js
js/src/jit-test/tests/parallelarray/scatter-8.js
js/src/jit-test/tests/parallelarray/scatter-9.js
js/src/jit-test/tests/parallelarray/shape-2.js
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/lib/parallelarray-helpers.js
@@ -0,0 +1,52 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+load(libdir + "eqArrayHelper.js");
+
+function assertEqParallelArray(a, b) {
+  assertEq(a instanceof ParallelArray, true);
+  assertEq(b instanceof ParallelArray, true);
+
+  var shape = a.shape;
+  assertEqArray(shape, b.shape);
+
+  function bump(indices) {
+    var d = indices.length - 1;
+    while (d >= 0) {
+      if (++indices[d] < shape[d])
+        break;
+      indices[d] = 0;
+      d--;
+    }
+    return d >= 0;
+  }
+
+  var iv = shape.map(function () { return 0; });
+  do {
+    var e1 = a.get(iv);
+    var e2 = b.get(iv);
+    if (e1 instanceof ParallelArray && e2 instanceof ParallelArray)
+      assertEqParallelArray(e1, e2);
+    else if (e1 instanceof Array && e2 instanceof Array)
+      assertEqArray(e1, e2);
+    else
+      assertEq(e1, e2);
+  } while (bump(iv));
+}
+
+function assertParallelArrayModesCommute(modes, pa, op) {
+  var args = Array.slice(arguments, 3);
+  var acc;
+  modes.forEach(function (mode) {
+    var result = op.apply(pa, args.concat([{ mode: mode, expect: "success" }]));
+    if (acc) {
+      if (acc instanceof ParallelArray)
+        assertEqParallelArray(acc, result);
+      else
+        assertEq(acc, result);
+    } else {
+      acc = result;
+    }
+  });
+}
--- a/js/src/jit-test/tests/parallelarray/comprehension-1.js
+++ b/js/src/jit-test/tests/parallelarray/comprehension-1.js
@@ -1,9 +1,10 @@
+load(libdir + "parallelarray-helpers.js")
 
 function buildComprehension() {
-  // 1D comprehension 
+  // 1D comprehension
   var p = new ParallelArray(10, function (idx) { return idx; });
-  var a = [0,1,2,3,4,5,6,7,8,9];
-  assertEq(p.toString(), "<" + a.join(",") + ">");
+  var a = new ParallelArray([0,1,2,3,4,5,6,7,8,9]);
+  assertEqParallelArray(p, a);
 }
 
 buildComprehension();
--- a/js/src/jit-test/tests/parallelarray/comprehension-2.js
+++ b/js/src/jit-test/tests/parallelarray/comprehension-2.js
@@ -1,9 +1,12 @@
+load(libdir + "parallelarray-helpers.js");
+load(libdir + "eqArrayHelper.js");
 
 function buildMultidim() {
   // 2D comprehension
   var p = new ParallelArray([2,2], function (i,j) { return i + j; });
-  assertEq(p.shape.toString(), [2,2].toString());
-  assertEq(p.toString(), "<<0,1>,<1,2>>");
+  var a = new ParallelArray([0,1,1,2]).partition(2);
+  assertEqArray(p.shape, [2,2]);
+  assertEqParallelArray(p, a);
 }
 
 buildMultidim();
--- a/js/src/jit-test/tests/parallelarray/comprehension-scale.js
+++ b/js/src/jit-test/tests/parallelarray/comprehension-scale.js
@@ -1,31 +1,21 @@
+load(libdir + "parallelarray-helpers.js");
 
 function buildComprehension() {
   var H = 96;
   var W = 96;
   var d = 4;
   // 3D 96x96x4 texture-like PA
   var p = new ParallelArray([H,W,d], function (i,j,k) { return i + j + k; });
-  var a = "<";
+  var a = [];
   for (var i = 0; i < H; i++) {
-    a += "<";
     for (var j = 0; j < W; j++) {
-      a += "<";
       for (var k = 0; k < d; k++) {
-        a += i+j+k;
-        if (k !== d - 1)
-          a += ",";
+        a.push(i+j+k);
       }
-      a += ">";
-      if (j !== W - 1)
-        a += ","
     }
-    a += ">";
-    if (i !== H - 1)
-      a += ","
   }
-  a += ">"
-
-  assertEq(p.toString(), a);
+  var p2 = new ParallelArray(a).partition(d).partition(H);
+  assertEqParallelArray(p, p2);
 }
 
 buildComprehension();
--- a/js/src/jit-test/tests/parallelarray/constructor-1.js
+++ b/js/src/jit-test/tests/parallelarray/constructor-1.js
@@ -1,16 +1,14 @@
-function bracket(s) {
-  return "<" + s + ">";
-}
+load(libdir + "eqArrayHelper.js");
 
 function buildSimple() {
   // Simple constructor
   var a = [1,2,3,4,5];
   var p = new ParallelArray(a);
-  var e = a.join(",");
-  assertEq(p.toString(), bracket(e));
+  assertEqArray(p, a);
+  var a2 = a.slice();
   a[0] = 9;
   // No sharing
-  assertEq(p.toString(), bracket(e));
+  assertEqArray(p, a2);
 }
 
 buildSimple();
--- a/js/src/jit-test/tests/parallelarray/constructor-4.js
+++ b/js/src/jit-test/tests/parallelarray/constructor-4.js
@@ -1,12 +1,13 @@
+load(libdir + "parallelarray-helpers.js");
 
 function buildPA() {
   // Construct copying from PA
   var p1 = new ParallelArray([1,2,3,4]);
   var p2 = new ParallelArray(p1);
-  assertEq(p1.toString(), p2.toString());
+  assertEqParallelArray(p1, p2);
   var p1d = new ParallelArray([2,2], function(i,j) { return i + j; });
   var p2d = new ParallelArray(p1d);
   assertEq(p1d.toString(), p2d.toString());
 }
 
 buildPA();
--- a/js/src/jit-test/tests/parallelarray/element-2.js
+++ b/js/src/jit-test/tests/parallelarray/element-2.js
@@ -1,11 +1,14 @@
+load(libdir + "parallelarray-helpers.js");
+
 function testElement() {
   // Test getting element from higher dimension
   var p = new ParallelArray([2,2,2], function () { return 0; });
-  assertEq(p[0].toString(), "<<0,0>,<0,0>>");
+  var p0 = new ParallelArray([2,2], function () { return 0; });
+  assertEqParallelArray(p[0], p0);
   // Should create new wrapper
   assertEq(p[0] !== p[0], true);
   // Test out of bounds
   assertEq(p[42], undefined);
 }
 
 testElement();
--- a/js/src/jit-test/tests/parallelarray/filter-1.js
+++ b/js/src/jit-test/tests/parallelarray/filter-1.js
@@ -1,13 +1,14 @@
+load(libdir + "parallelarray-helpers.js");
 
 function testFilterAll() {
   // Test filtering everything (leaving everything in)
   var p = new ParallelArray([0,1,2,3,4]);
   var all = p.map(function (i) { return true; });
   var r = p.filter(all);
-  assertEq(r.toString(), "<0,1,2,3,4>");
+  assertEqParallelArray(r, p);
   var p = new ParallelArray([5,2], function(i,j) { return i+j; });
   var r = p.filter(all);
-  assertEq(r.toString(), "<<0,1>,<1,2>,<2,3>,<3,4>,<4,5>>");
+  assertEqParallelArray(r, new ParallelArray(p));
 }
 
 testFilterAll();
--- a/js/src/jit-test/tests/parallelarray/filter-2.js
+++ b/js/src/jit-test/tests/parallelarray/filter-2.js
@@ -1,12 +1,14 @@
+load(libdir + "parallelarray-helpers.js");
+
 function testFilterNone() {
   // Test filtering (removing everything)
   var p = new ParallelArray([0,1,2,3,4]);
   var none = p.map(function () { return false; });
   var r = p.filter(none);
-  assertEq(r.toString(), "<>");
+  assertEqParallelArray(r, new ParallelArray);
   var p = new ParallelArray([5,2], function(i,j) { return i+j; });
   var r = p.filter(none);
-  assertEq(r.toString(), "<>");
+  assertEqParallelArray(r, new ParallelArray);
 }
 
 testFilterNone();
--- a/js/src/jit-test/tests/parallelarray/filter-3.js
+++ b/js/src/jit-test/tests/parallelarray/filter-3.js
@@ -1,16 +1,14 @@
-function bracket(s) {
-  return "<" + s + ">";
-}
+load(libdir + "parallelarray-helpers.js");
 
 function testFilterSome() {
   var p = new ParallelArray([0,1,2,3,4]);
   var evenBelowThree = p.map(function (i) { return ((i%2) === 0) && (i < 3); });
   var r = p.filter(evenBelowThree);
-  assertEq(r.toString(), bracket([0,2].join(",")));
+  assertEqParallelArray(r, new ParallelArray([0,2]));
   var p = new ParallelArray([5,2], function (i,j) { return i; });
   var evenBelowThree = p.map(function (i) { return ((i[0]%2) === 0) && (i[0] < 3); });
   var r = p.filter(evenBelowThree);
-  assertEq(r.toString(), bracket(["<0,0>","<2,2>"].join(",")));
+  assertEqParallelArray(r, new ParallelArray([p[0], p[2]]));
 }
 
 testFilterSome();
--- a/js/src/jit-test/tests/parallelarray/filter-4.js
+++ b/js/src/jit-test/tests/parallelarray/filter-4.js
@@ -1,18 +1,16 @@
-function bracket(s) {
-  return "<" + s + ">";
-}
+load(libdir + "parallelarray-helpers.js");
 
 function testFilterMisc() {
   var p = new ParallelArray([0,1,2]);
   // Test array
   var r = p.filter([true, false, true]);
-  assertEq(r.toString(), bracket([0,2].join(",")));
+  assertEqParallelArray(r, new ParallelArray([p[0], p[2]]));
   // Test array-like
   var r = p.filter({ 0: true, 1: false, 2: true, length: 3 });
-  assertEq(r.toString(), bracket([0,2].join(",")));
+  assertEqParallelArray(r, new ParallelArray([p[0], p[2]]));
   // Test truthy
   var r = p.filter([1, "", {}]);
-  assertEq(r.toString(), bracket([0,2].join(",")));
+  assertEqParallelArray(r, new ParallelArray([p[0], p[2]]));
 }
 
 testFilterMisc();
--- a/js/src/jit-test/tests/parallelarray/flatten-1.js
+++ b/js/src/jit-test/tests/parallelarray/flatten-1.js
@@ -1,11 +1,13 @@
+load(libdir + "eqArrayHelper.js");
+
 function testFlatten() {
   var shape = [5];
   for (var i = 0; i < 7; i++) {
     shape.push(i+1);
     var p = new ParallelArray(shape, function(i,j) { return i+j; });
     var flatShape = ([shape[0] * shape[1]]).concat(shape.slice(2));
-    assertEq(p.flatten().shape.toString(), flatShape.toString());
+    assertEqArray(p.flatten().shape, flatShape);
   }
 }
 
 testFlatten();
--- a/js/src/jit-test/tests/parallelarray/flatten-2.js
+++ b/js/src/jit-test/tests/parallelarray/flatten-2.js
@@ -1,7 +1,9 @@
+load(libdir + "parallelarray-helpers.js");
+
 function testFlatten() {
   var p = new ParallelArray([2,2], function(i,j) { return i+j; });
   var p2 = new ParallelArray([0,1,1,2]);
-  assertEq(p.flatten().toString(), p2.toString());
+  assertEqParallelArray(p.flatten(), p2);
 }
 
 testFlatten();
--- a/js/src/jit-test/tests/parallelarray/get-2.js
+++ b/js/src/jit-test/tests/parallelarray/get-2.js
@@ -1,10 +1,12 @@
+load(libdir + "parallelarray-helpers.js");
+
 function testGet() {
   var p = new ParallelArray([2,2,2], function(i,j,k) { return i+j+k; });
   assertEq(p.get([1,1,1]), 1+1+1);
   var p2 = new ParallelArray([2], function(i) { return 1+1+i; });
-  assertEq(p.get([1,1]).toString(), p2.toString());
+  assertEqParallelArray(p.get([1,1]), p2);
   var p3 = new ParallelArray([2,2], function(i,j) { return 1+i+j; });
-  assertEq(p.get([1]).toString(), p3.toString());
+  assertEqParallelArray(p.get([1]), p3);
 }
 
 testGet();
--- a/js/src/jit-test/tests/parallelarray/map-1.js
+++ b/js/src/jit-test/tests/parallelarray/map-1.js
@@ -1,11 +1,10 @@
-function bracket(s) {
-  return "<" + s + ">";
-}
+load(libdir + "parallelarray-helpers.js");
 
 function testMap() {
-    var p = new ParallelArray([0,1,2,3,4]);
-    var m = p.map(function (v) { return v+1; });
-    assertEq(m.toString(), bracket([1,2,3,4,5].join(",")));
+  var p = new ParallelArray([0,1,2,3,4]);
+  var m = p.map(function (v) { return v+1; });
+  var p2 = new ParallelArray([1,2,3,4,5]);
+  assertEqParallelArray(m, p2);
 }
 
 testMap();
--- a/js/src/jit-test/tests/parallelarray/map-3.js
+++ b/js/src/jit-test/tests/parallelarray/map-3.js
@@ -1,9 +1,12 @@
+load(libdir + "parallelarray-helpers.js");
+
 function testMap() {
   // Test mapping higher dimensional
   var p = new ParallelArray([2,2], function (i,j) { return i+j; });
-  var m = p.map(function(x) { return x.toString(); });
-  assertEq(m.toString(), "<<0,1>,<1,2>>");
+  var m = p.map(function(x) { return x; });
+  var p2 = new ParallelArray(p);
+  assertEqParallelArray(m, p2);
 }
 
 testMap();
 
--- a/js/src/jit-test/tests/parallelarray/partition-1.js
+++ b/js/src/jit-test/tests/parallelarray/partition-1.js
@@ -1,13 +1,15 @@
+load(libdir + "eqArrayHelper.js");
+
 function testPartition() {
   var p = new ParallelArray([1,2,3,4,5,6,7,8]);
   var pp = p.partition(2);
   var ppp = pp.partition(2);
   var ppShape = [p.shape[0] / 2, 2].concat(p.shape.slice(1));
   var pppShape = [pp.shape[0] / 2, 2].concat(pp.shape.slice(1));
-  assertEq(pp.shape.toString(), ppShape.toString())
+  assertEqArray(pp.shape, ppShape);
   assertEq(pp.toString(), "<<1,2>,<3,4>,<5,6>,<7,8>>");
-  assertEq(ppp.shape.toString(), pppShape.toString())
+  assertEqArray(ppp.shape, pppShape);
   assertEq(ppp.toString(), "<<<1,2>,<3,4>>,<<5,6>,<7,8>>>");
 }
 
 testPartition();
--- a/js/src/jit-test/tests/parallelarray/scan-3.js
+++ b/js/src/jit-test/tests/parallelarray/scan-3.js
@@ -5,14 +5,14 @@ function testScan() {
   function f(a, b) { return a; }
   var shape = [2];
   for (var i = 0; i < 7; i++) {
     shape.push(i+1);
     var p = new ParallelArray(shape, function () { return i+1; });
     var r = p.reduce(f);
     var s = p.scan(f)
     for (var j = 0; j < s.length; j++)
-      assertEq(s[0].shape.length, i + 1);
+      assertEq(s[j].shape.length, i + 1);
     assertEq(r.shape.length, i + 1);
   }
 }
 
 testScan();
--- a/js/src/jit-test/tests/parallelarray/scatter-2.js
+++ b/js/src/jit-test/tests/parallelarray/scatter-2.js
@@ -1,9 +1,10 @@
+load(libdir + "parallelarray-helpers.js");
 
 function testScatterIdentity() {
   var p = new ParallelArray([1,2,3,4,5]);
   var r = p.scatter([0,1,2,3,4]);
-  assertEq(p.toString(), r.toString());
+  assertEqParallelArray(p, r);
 }
 
 testScatterIdentity();
 
--- a/js/src/jit-test/tests/parallelarray/scatter-3.js
+++ b/js/src/jit-test/tests/parallelarray/scatter-3.js
@@ -1,10 +1,11 @@
+load(libdir + "parallelarray-helpers.js");
 
 function testScatter() {
   var p = new ParallelArray([1,2,3,4,5]);
   var r = p.scatter([1,0,3,2,4]);
   var p2 = new ParallelArray([2,1,4,3,5]);
-  assertEq(r.toString(), p2.toString());
+  assertEqParallelArray(r, p2);
 }
 
 testScatter();
 
--- a/js/src/jit-test/tests/parallelarray/scatter-4.js
+++ b/js/src/jit-test/tests/parallelarray/scatter-4.js
@@ -1,12 +1,10 @@
-function bracket(s) {
-  return "<" + s + ">";
-}
+load(libdir + "parallelarray-helpers.js");
 
 function testScatterDefault() {
   var p = new ParallelArray([1,2,3,4,5]);
   var r = p.scatter([0,2,4], 9);
-  assertEq(r.toString(), bracket([1,9,2,9,3].join(",")));
+  assertEqParallelArray(r, new ParallelArray([1,9,2,9,3]));
 }
 
 testScatterDefault();
 
--- a/js/src/jit-test/tests/parallelarray/scatter-5.js
+++ b/js/src/jit-test/tests/parallelarray/scatter-5.js
@@ -1,11 +1,9 @@
-function bracket(s) {
-  return "<" + s + ">";
-}
+load(libdir + "parallelarray-helpers.js");
 
 function testScatterConflict() {
-    var p = new ParallelArray([1,2,3,4,5]);
-    var r = p.scatter([0,1,0,3,4], 9, function (a,b) { return a+b; });
-    assertEq(r.toString(), bracket([4,2,9,4,5].join(",")));
+  var p = new ParallelArray([1,2,3,4,5]);
+  var r = p.scatter([0,1,0,3,4], 9, function (a,b) { return a+b; });
+  assertEqParallelArray(r, new ParallelArray([4,2,9,4,5]));
 }
 
 testScatterConflict();
--- a/js/src/jit-test/tests/parallelarray/scatter-7.js
+++ b/js/src/jit-test/tests/parallelarray/scatter-7.js
@@ -1,13 +1,15 @@
+load(libdir + "parallelarray-helpers.js");
 
 function testScatterIdentity() {
   var shape = [5];
   for (var i = 0; i < 7; i++) {
     shape.push(i+1);
     var p = new ParallelArray(shape, function(k) { return k; });
     var r = p.scatter([0,1,2,3,4]);
-    assertEq(p.toString(), r.toString());
+    var p2 = new ParallelArray([p[0], p[1], p[2], p[3], p[4]]);
+    assertEqParallelArray(p2, r);
   }
 }
 
 testScatterIdentity();
 
--- a/js/src/jit-test/tests/parallelarray/scatter-8.js
+++ b/js/src/jit-test/tests/parallelarray/scatter-8.js
@@ -1,14 +1,15 @@
+load(libdir + "parallelarray-helpers.js");
 
 function testScatter() {
   var shape = [5];
   for (var i = 0; i < 7; i++) {
     shape.push(i+1);
     var p = new ParallelArray(shape, function(k) { return k; });
     var r = p.scatter([1,0,3,2,4]);
     var p2 = new ParallelArray([p[1], p[0], p[3], p[2], p[4]]);
-    assertEq(p2.toString(), r.toString());
+    assertEqParallelArray(p2, r);
   }
 }
 
 testScatter();
 
--- a/js/src/jit-test/tests/parallelarray/scatter-9.js
+++ b/js/src/jit-test/tests/parallelarray/scatter-9.js
@@ -1,7 +1,9 @@
+load(libdir + "parallelarray-helpers.js");
+
 function testScatter() {
   // Ignore the rest of the scatter vector if longer than source
   var p = new ParallelArray([1,2,3,4,5]);
   var r = p.scatter([1,0,3,2,4,1,2,3]);
   var p2 = new ParallelArray([2,1,4,3,5]);
-  assertEq(r.toString(), p2.toString());
+  assertEqParallelArray(r, p2);
 }
--- a/js/src/jit-test/tests/parallelarray/shape-2.js
+++ b/js/src/jit-test/tests/parallelarray/shape-2.js
@@ -1,14 +1,16 @@
+load(libdir + "eqArrayHelper.js");
+
 function testShape() {
   // Test higher dimension shape up to 8D
   var shape = [];
   for (var i = 0; i < 8; i++) {
     shape.push(i+1);
     var p = new ParallelArray(shape, function () { return 0; });
     // Test shape identity and shape
     assertEq(p.shape, p.shape);
     assertEq(p.shape !== shape, true);
-    assertEq(p.shape.toString(), shape.toString());
+    assertEqArray(p.shape, shape);
   }
 }
 
 testShape();