All files / Sources/Testing testAlgorithm.js

100% Statements 57/57
100% Branches 0/0
100% Functions 13/13
100% Lines 51/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 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 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117      1x 1x 1x 1x   1x 1x 1x 3x 3x           1x   5x 15x   5x 15x   5x 15x   5x 15x       1x 1x     9x   1x   1x 1x 1x   1x 1x 1x   1x 1x   1x 1x           1x 1x           1x     1x 1x       1x         1x         1x 1x   1x 1x     1x 3x     1x 1x             1x     1x 1x           1x    
import test from 'tape';
import macro from 'vtk.js/Sources/macros';
 
const model = {};
const publicAPI = {};
const numInputs = 2;
const numOutputs = 4;
 
const inLeft = new Float32Array(3);
const inRight = new Float32Array(3);
for (let i = 0; i < inLeft.length; i++) {
  inLeft[i] = i;
  inRight[i] = i + 3;
}
 
// const daLeft = vtkDataArray.newInstance({ name: 'left', values: inLeft });
// const daRight = vtkDataArray.newInstance({ name: 'left', values: inLeft });
 
publicAPI.requestData = (inData, outData) => {
  // implement requestData
  outData[0] = inData.reduce((inArray1, inArray2) =>
    inArray2.map((d, i) => d + inArray1[i])
  );
  outData[1] = inData.reduce((inArray1, inArray2) =>
    inArray2.map((d, i) => d - inArray1[i])
  );
  outData[2] = inData.reduce((inArray1, inArray2) =>
    inArray2.map((d, i) => d * inArray1[i])
  );
  outData[3] = inData.reduce((inArray1, inArray2) =>
    inArray2.map((d, i) => d / inArray1[i])
  );
};
 
test('Macro methods algo tests', (t) => {
  macro.algo(publicAPI, model, numInputs, numOutputs);
 
  // Override shouldUpdate to prevent the need of getMTime()
  publicAPI.shouldUpdate = () => true;
 
  t.ok(publicAPI.setInputData, 'populate publicAPI');
 
  publicAPI.setInputData(inLeft, 0);
  publicAPI.setInputData(inRight, 1);
  t.equal(publicAPI.getInputData(0), inLeft, 'return input data');
 
  publicAPI.update();
  let output = publicAPI.getOutputData(0);
  t.deepEqual(output, new Float32Array([3, 5, 7]), 'Add two input arrays');
 
  output = publicAPI.getOutputData(1);
  t.deepEqual(output, new Float32Array([3, 3, 3]), 'Subtract two input arrays');
 
  output = publicAPI.getOutputData(2);
  t.deepEqual(
    output,
    new Float32Array([0, 4, 10]),
    'Multiply two input arrays'
  );
 
  const outputPort = publicAPI.getOutputPort(3);
  t.deepEqual(
    outputPort(),
    new Float32Array([Infinity, 4, 2.5]),
    'Divide two input arrays, using outputPort'
  );
 
  t.end();
});
 
test('Macro shouldUpdate returns true if output is deleted', (t) => {
  const algo = {
    publicAPI: {},
    model: {},
  };
  const input1 = {
    publicAPI: {},
    model: {},
  };
 
  const input2 = {
    publicAPI: {},
    model: {},
  };
 
  macro.obj(algo.publicAPI, algo.model);
  macro.algo(algo.publicAPI, algo.model, 1, 1);
 
  macro.obj(input1.publicAPI, input1.model);
  macro.obj(input2.publicAPI, input2.model);
 
  // trivial producer
  algo.publicAPI.requestData = (inData, outData) => {
    outData[0] = inData[0];
  };
 
  algo.publicAPI.setInputData(input1.publicAPI, 0);
  t.equal(
    input1.publicAPI,
    algo.publicAPI.getOutputData(),
    'Trivial producer outputs first input data'
  );
 
  // delete output data
  algo.publicAPI.getOutputData().delete();
 
  // set new data
  algo.publicAPI.setInputData(input2.publicAPI, 0);
  t.equal(
    input2.publicAPI,
    algo.publicAPI.getOutputData(),
    'Trivial producer outputs second input data'
  );
 
  t.end();
});