Skip to content

Commit

Permalink
Test Coverage: Reorganize test files (tensorflow#542)
Browse files Browse the repository at this point in the history
* Rename: element_wise_arithmetic_test -> arithmetic_test
* Compare Operators: Tests moved to separate comparison file
* Rename: logical_ops_test -> logicalop_test
  • Loading branch information
manrajgrover authored and dsmilkov committed Jan 14, 2018
1 parent b57aedc commit b88cd0a
Show file tree
Hide file tree
Showing 3 changed files with 607 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import * as test_util from '../test_util';
import {MathTests} from '../test_util';
import * as util from '../util';
import {Array1D, Array2D, Array3D, Scalar} from './ndarray';

// divide
Expand Down Expand Up @@ -776,133 +775,3 @@ import {Array1D, Array2D, Array3D, Scalar} from './ndarray';
{'WEBGL_FLOAT_TEXTURE_ENABLED': false, 'WEBGL_VERSION': 1}
]);
}

// element-wise equal
{
const tests: MathTests = it => {
it('propagates NaNs', math => {
const a = Array1D.new([2, 5, NaN]);
const b = Array1D.new([4, 5, -1]);

const res = math.equal(a, b);
expect(res.dtype).toBe('bool');
test_util.expectArraysEqual(res, [0, 1, util.NAN_BOOL]);
});

it('strict version throws when x and y are different shape', math => {
const a = Array1D.new([2]);
const b = Array1D.new([4, 2, -1]);

expect(() => math.equalStrict(a, b)).toThrowError();
expect(() => math.equalStrict(b, a)).toThrowError();
});

it('2D and scalar broadcast', math => {
const a = Array2D.new([2, 3], [1, 2, 3, 2, 5, 6]);
const b = Scalar.new(2);
const res = math.equal(a, b);
expect(res.dtype).toBe('bool');
expect(res.shape).toEqual([2, 3]);
test_util.expectArraysEqual(res, [0, 1, 0, 1, 0, 0]);
});

it('scalar and 1D broadcast', math => {
const a = Scalar.new(2);
const b = Array1D.new([1, 2, 3, 4, 5, 2]);
const res = math.equal(a, b);
expect(res.dtype).toBe('bool');
expect(res.shape).toEqual([6]);
test_util.expectArraysEqual(res, [0, 1, 0, 0, 0, 1]);
});

it('2D and 2D broadcast each with 1 dim', math => {
const a = Array2D.new([1, 3], [1, 2, 5]);
const b = Array2D.new([2, 1], [5, 1]);
const res = math.equal(a, b);
expect(res.dtype).toBe('bool');
expect(res.shape).toEqual([2, 3]);
test_util.expectArraysEqual(res, [0, 0, 1, 1, 0, 0]);
});

it('3D and scalar', math => {
const a = Array3D.new([2, 3, 1], [1, 2, 3, 4, 5, -1]);
const b = Scalar.new(-1);
const res = math.equal(a, b);
expect(res.dtype).toBe('bool');
expect(res.shape).toEqual([2, 3, 1]);
test_util.expectArraysEqual(res, [0, 0, 0, 0, 0, 1]);
});
};

test_util.describeMathCPU('equal', [tests]);
test_util.describeMathGPU('equal', [tests], [
{'WEBGL_FLOAT_TEXTURE_ENABLED': true, 'WEBGL_VERSION': 1},
{'WEBGL_FLOAT_TEXTURE_ENABLED': true, 'WEBGL_VERSION': 2},
{'WEBGL_FLOAT_TEXTURE_ENABLED': false, 'WEBGL_VERSION': 1}
]);
}

// element-wise not equal
{
const tests: MathTests = it => {
it('propagates NaNs', math => {
const a = Array1D.new([2, 5, NaN]);
const b = Array1D.new([4, 5, -1]);

const res = math.notEqual(a, b);
expect(res.dtype).toBe('bool');
test_util.expectArraysEqual(res, [1, 0, util.NAN_BOOL]);
});

it('strict version throws when x and y are different shape', math => {
const a = Array1D.new([2]);
const b = Array1D.new([4, 2, -1]);

expect(() => math.notEqualStrict(a, b)).toThrowError();
expect(() => math.notEqualStrict(b, a)).toThrowError();
});

it('2D and scalar broadcast', math => {
const a = Array2D.new([2, 3], [1, 2, 3, 2, 5, 6]);
const b = Scalar.new(2);
const res = math.notEqual(a, b);
expect(res.dtype).toBe('bool');
expect(res.shape).toEqual([2, 3]);
test_util.expectArraysEqual(res, [1, 0, 1, 0, 1, 1]);
});

it('scalar and 1D broadcast', math => {
const a = Scalar.new(2);
const b = Array1D.new([1, 2, 3, 4, 5, 2]);
const res = math.notEqual(a, b);
expect(res.dtype).toBe('bool');
expect(res.shape).toEqual([6]);
test_util.expectArraysEqual(res, [1, 0, 1, 1, 1, 0]);
});

it('2D and 2D broadcast each with 1 dim', math => {
const a = Array2D.new([1, 3], [1, 2, 5]);
const b = Array2D.new([2, 1], [5, 1]);
const res = math.notEqual(a, b);
expect(res.dtype).toBe('bool');
expect(res.shape).toEqual([2, 3]);
test_util.expectArraysEqual(res, [1, 1, 0, 0, 1, 1]);
});

it('3D and scalar', math => {
const a = Array3D.new([2, 3, 1], [1, 2, 3, 4, 5, -1]);
const b = Scalar.new(-1);
const res = math.notEqual(a, b);
expect(res.dtype).toBe('bool');
expect(res.shape).toEqual([2, 3, 1]);
test_util.expectArraysEqual(res, [1, 1, 1, 1, 1, 0]);
});
};

test_util.describeMathCPU('notEqual', [tests]);
test_util.describeMathGPU('notEqual', [tests], [
{'WEBGL_FLOAT_TEXTURE_ENABLED': true, 'WEBGL_VERSION': 1},
{'WEBGL_FLOAT_TEXTURE_ENABLED': true, 'WEBGL_VERSION': 2},
{'WEBGL_FLOAT_TEXTURE_ENABLED': false, 'WEBGL_VERSION': 1}
]);
}
Loading

0 comments on commit b88cd0a

Please sign in to comment.