Skip to content

Commit 9e2fa6e

Browse files
OlegStotskyJasonShin
authored andcommitted
assertArrayAlmostEqual -> getAlmostEqualElemsCount; add assertArrayAlmostEqual
1 parent c811dc1 commit 9e2fa6e

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/lib/utils/Errors.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// NOTE: Below custom errors are hack because Jest has a bug with asserting error types
22

3+
/**
4+
* The error is used to indicate that some assertion failed
5+
* @ignore
6+
*/
7+
export const AssertionError = function(message) {
8+
Error.captureStackTrace(this, this.constructor);
9+
this.name = this.constructor.name;
10+
this.message = message;
11+
};
12+
313
/**
414
* The error is used for class initiation failures due to invalid arguments.
515
* @ignore

test/util_testing.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
1-
import * as _ from 'lodash';
1+
import { AssertionError } from '../src/lib/utils/Errors';
22

33
/**
4-
* Raises an AssertionError if two objects are not equal up to desired precision.
4+
* Raises an AssertionError if two arrays are not equal up to desired precision.
55
* @param desired
66
* @param actual
77
* @param precision
88
*/
9-
const assertArrayAlmostEqual = (desired: number[], actual: number[], precision: number = 6): number => {
9+
const getAlmostEqualElemsCount = (desired: number[], actual: number[], precision: number = 6): number => {
1010
const results = [];
1111
for (let i = 0; i < desired.length; i++) {
1212
const d = desired[i];
1313
const a = actual[i];
14-
const calc = Math.abs(d - a) < 1.5 * Math.pow(10, -precision);
15-
results.push(calc);
14+
if (Number.isNaN(a) && Number.isNaN(d)) {
15+
results.push(true);
16+
} else {
17+
const calc = Math.abs(d - a) < 1.5 * Math.pow(10, -precision);
18+
results.push(calc);
19+
}
1620
}
1721
const numTrues = results.reduce((sum, cur) => (cur ? sum + 1 : sum), 0);
18-
return numTrues / results.length * 100;
22+
23+
return numTrues;
24+
};
25+
26+
const assertArrayAlmostEqual = (desired: number[], actual: number[], precision: number = 6) => {
27+
if (desired.length !== actual.length) {
28+
throw new AssertionError('Desired and actual arrays should have the same length');
29+
}
30+
31+
const numAlmostEqualElems = getAlmostEqualElemsCount(desired, actual, precision);
32+
33+
if (numAlmostEqualElems < desired.length) {
34+
throw new AssertionError(`Expected ${desired.length} almost equal numbers, got ${numAlmostEqualElems}`);
35+
}
1936
};
2037

2138
const matchExceptionWithSnapshot = (method: (...x) => any, args: any[]): void => {
@@ -26,4 +43,4 @@ const matchExceptionWithSnapshot = (method: (...x) => any, args: any[]): void =>
2643
}
2744
};
2845

29-
export { assertArrayAlmostEqual, matchExceptionWithSnapshot };
46+
export { assertArrayAlmostEqual, getAlmostEqualElemsCount, matchExceptionWithSnapshot };

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy