|
| 1 | +var cwise = require("../cwise.js") |
| 2 | + , ndarray = require("ndarray") |
| 3 | + , test = require("tap").test |
| 4 | + |
| 5 | +function DumbStorage(n) { |
| 6 | + this.data = new Int32Array(n) |
| 7 | + this.length = n |
| 8 | +} |
| 9 | +DumbStorage.prototype.get = function(i) { return this.data[i] } |
| 10 | +DumbStorage.prototype.set = function(i, v) { return this.data[i]=v } |
| 11 | + |
| 12 | + |
| 13 | +test("offset", function(t) { |
| 14 | + |
| 15 | + var binary = cwise({ |
| 16 | + args: ["array", "array", {offset:[1], array:1}, "scalar", "shape", "index"], |
| 17 | + body: function(a,b,c,t,s,idx) { |
| 18 | + t.equals(a, 0, "idx:"+idx+", shape:"+s) |
| 19 | + a = c + b + 1000 |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + function testBinary1D(P, Q) { |
| 24 | + console.log(P.shape.toString(), Q.shape.toString()) |
| 25 | + for(var i=0; i<P.shape[0]; ++i) { |
| 26 | + Q.set(i, i) |
| 27 | + P.set(i, 0) |
| 28 | + } |
| 29 | + Q.set(P.shape[0], P.shape[0]) |
| 30 | + binary(P, Q, t) |
| 31 | + for(var i=0; i<P.shape[0]; ++i) { |
| 32 | + t.equals(P.get(i), 2*i+1001) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + var A = ndarray(new Int32Array(128)) |
| 37 | + var B = ndarray(new Int32Array(129)) |
| 38 | + |
| 39 | + testBinary1D(ndarray(new Int32Array(0)), ndarray(new Int32Array(1))) |
| 40 | + testBinary1D(ndarray(new Int32Array(1)), ndarray(new Int32Array(2))) |
| 41 | + testBinary1D(A, B) |
| 42 | + testBinary1D(A.lo(32), B) |
| 43 | + testBinary1D(A.step(-1), B) |
| 44 | + testBinary1D(A, B.step(-1)) |
| 45 | + |
| 46 | + A = ndarray(new DumbStorage(128)) |
| 47 | + B = ndarray(new DumbStorage(129)) |
| 48 | + testBinary1D(ndarray(new DumbStorage(0)), ndarray(new DumbStorage(1))) |
| 49 | + testBinary1D(ndarray(new DumbStorage(1)), ndarray(new DumbStorage(2))) |
| 50 | + testBinary1D(A, B) |
| 51 | + testBinary1D(A.lo(32), B) |
| 52 | + testBinary1D(A.step(-1), B) |
| 53 | + testBinary1D(A, B.step(-1)) |
| 54 | + |
| 55 | + t.end() |
| 56 | +}) |
0 commit comments