Skip to content

Commit

Permalink
addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceDai committed Dec 11, 2024
1 parent 47e10ea commit acbcd64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
27 changes: 17 additions & 10 deletions test/unary_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import * as unaryFunctions from '../src/unary.js';
import * as utils from './utils.js';

describe('test unary', function() {
function testUnary(op, input, expected, shape, atolTolerance) {
function testUnary(op, input, expected, shape, atol) {
const x = new Tensor(shape, input);
const y = unaryFunctions[op](x);
utils.checkShape(y, shape);
if (atolTolerance !== undefined) {
utils.checkValueByATOL(y, expected, atolTolerance);
if (atol !== undefined) {
utils.checkValueByATOL(y, expected, atol);
} else {
utils.checkValue(y, expected);
}
Expand Down Expand Up @@ -384,7 +384,8 @@ describe('test unary', function() {
-1.0692689659512902,
0.04121219038394666,
],
[3], 1 / 1024);
[3],
1 / 1024);
testUnary(
'log',
[
Expand All @@ -406,7 +407,8 @@ describe('test unary', function() {
0.34275879190200165,
-2.753918343538326,
],
[3, 4], 1 / 1024);
[3, 4],
1 / 1024);
testUnary(
'log',
[
Expand Down Expand Up @@ -485,7 +487,8 @@ describe('test unary', function() {
0.31843664006339245,
-0.8416115978644251,
],
[3, 4, 5], 1 / 1024);
[3, 4, 5],
1 / 1024);
testUnary(
'log',
[
Expand Down Expand Up @@ -564,7 +567,8 @@ describe('test unary', function() {
0.31843664006339245,
-0.8416115978644251,
],
[3, 2, 2, 5], 1 / 1024);
[3, 2, 2, 5],
1 / 1024);
});

it('neg', function() {
Expand Down Expand Up @@ -618,7 +622,8 @@ describe('test unary', function() {
0.21339342411295945,
-0.6558230571220807,
],
[3, 4], 1 / 1024);
[3, 4],
1 / 1024);
testUnary(
'sin',
[
Expand All @@ -643,7 +648,8 @@ describe('test unary', function() {
0.21339342411295945,
-0.6558230571220807,
],
[3, 2, 2], 1 / 1024);
[3, 2, 2],
1 / 1024);
testUnary(
'sin',
[
Expand Down Expand Up @@ -674,7 +680,8 @@ describe('test unary', function() {
0.21339342411295945,
-0.6558230571220807,
],
[3, 2, 2, 1], 1 / 1024);
[3, 2, 2, 1],
1 / 1024);
});

it('tan', function() {
Expand Down
11 changes: 6 additions & 5 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ export function checkValue(tensor, expected, nulp = 0) {
}
}

export function checkValueByATOL(tensor, expected, tolerance) {
assert.isTrue(tensor.size === expected.length);
export function checkValueByATOL(actual, expected, tolerance) {
assert.isTrue(actual.size === expected.length);
for (let i = 0; i < expected.length; ++i) {
const actual = tensor.getValueByIndex(i);
assert.isTrue(actual >= expected[i] - tolerance && actual <= expected[i] + tolerance,
`${actual} is almost equal to ${expected[i]} by ${tolerance} tolerance of ATOL metrics`);
const actualValue = actual.getValueByIndex(i);
assert.isTrue(actualValue >= expected[i] - tolerance && actualValue <= expected[i] + tolerance,
`${actualValue} is almost equal to ${expected[i]} by ${tolerance} tolerance of ATOL` +
'metrics');
}
}

Expand Down

0 comments on commit acbcd64

Please sign in to comment.