MCPcopy
hub / github.com/tensorflow/tfjs / expectArraysPredicate

Function expectArraysPredicate

tfjs-core/src/test_util.ts:41–99  ·  view source on GitHub ↗
(
    actual: TensorLike, expected: TensorLike,
    predicate: (a: {}, b: {}) => boolean)

Source from the content-addressed store, hash-verified

39}
40
41function expectArraysPredicate(
42 actual: TensorLike, expected: TensorLike,
43 predicate: (a: {}, b: {}) => boolean) {
44 let checkClassType = true;
45 if (isTypedArray(actual) || isTypedArray(expected)) {
46 checkClassType = false;
47 }
48 if (isTypedArray(actual) && isTypedArray(expected)) {
49 checkClassType = true;
50 }
51 if (checkClassType) {
52 const aType = actual.constructor.name;
53 const bType = expected.constructor.name;
54
55 if (aType !== bType) {
56 throw new Error(
57 `Arrays are of different type. Actual: ${aType}. ` +
58 `Expected: ${bType}`);
59 }
60 }
61
62 if (Array.isArray(actual) && Array.isArray(expected)) {
63 const actualShape = inferShape(actual);
64 const expectedShape = inferShape(expected);
65 if (!arraysEqual(actualShape, expectedShape)) {
66 throw new Error(
67 `Arrays have different shapes. ` +
68 `Actual: [${actualShape}]. Expected: [${expectedShape}]`);
69 }
70 }
71
72 const actualFlat =
73 isTypedArray(actual) ? actual : flatten(actual as RecursiveArray<number>);
74 const expectedFlat = isTypedArray(expected) ?
75 expected :
76 flatten(expected as RecursiveArray<number>);
77
78 if (actualFlat.length !== expectedFlat.length) {
79 throw new Error(
80 `Arrays have different lengths actual: ${actualFlat.length} vs ` +
81 `expected: ${expectedFlat.length}.\n` +
82 `Actual: ${actualFlat}.\n` +
83 `Expected: ${expectedFlat}.`);
84 }
85 for (let i = 0; i < expectedFlat.length; ++i) {
86 const a = actualFlat[i];
87 const e = expectedFlat[i];
88
89 if (!predicate(a, e)) {
90 throw new Error(
91 `Arrays differ: actual[${i}] = ${a}, expected[${i}] = ${e}.\n` +
92 `Actual: ${actualFlat}.\n` +
93 `Expected: ${expectedFlat}.`);
94 }
95 }
96 if (typeof expect !== 'undefined') {
97 expect().nothing();
98 }

Callers 2

expectArraysCloseFunction · 0.85
expectArraysEqualFunction · 0.85

Calls 4

isTypedArrayFunction · 0.90
inferShapeFunction · 0.90
arraysEqualFunction · 0.90
flattenFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…