Skip to content
This repository has been archived by the owner on May 30, 2019. It is now read-only.

Add assertEqual #457

Merged
merged 2 commits into from
Mar 31, 2018
Merged

Add assertEqual #457

merged 2 commits into from
Mar 31, 2018

Conversation

dangerdak
Copy link
Contributor

@dangerdak dangerdak commented Mar 29, 2018

I attempted to address #423 , adding assertEqual to util.ts and sharing its code with tensor_util.ts and jasmine_shim.ts
Any guidance or feedback would be great @ry @piscisaureus

@dangerdak dangerdak changed the title [WIPAssert Fixes assertEqual Mar 29, 2018
@dangerdak dangerdak changed the title Fixes assertEqual [WIP] Fixes assertEqual Mar 29, 2018
Copy link
Contributor

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work! Thank you!
Looks good modulo some minor comments.
@piscisaureus please review this due to the sensitive nature of the patch.

assertAllEqual,
assertEqual,
assertShapesEqual
}
from "./tensor_util";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: Please have the closing curly brace and from "./tensor_util"; on the same line.

src/dl/util.ts Outdated
@@ -17,6 +17,7 @@ import { DataType, DataTypeMap, NDArray } from "./math/ndarray";
* =============================================================================
*/

export { assertEqual } from "../util";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this might have been violated elsewhere, the intention is that src/dl is standalone and imports should not reach outside of it. Please remove.

try {
assertEqual(value, expected);
return true;
} catch (e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the try catch needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it there because assertEqual throws if its arguments aren't equal, whereas toEqual returns a boolean

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah makes sense - In that case can you add another routine to util.ts called equal which returns boolean, and use that here and in assertEqual, so the try block can be avoided?

return true;
}
if (a && typeof a === "object" && b && typeof b === "object") {
if (seen.get(a) === b) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file there is a method called objectsEqual - can that be reused here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks. Am I right in thinking that assertObjectsEqual (https://github.com/propelml/propel/blob/master/src/util.ts#L140) isn't needed anymore as assertEqual deals with objects?

Copy link
Contributor

@ry ry Mar 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Please replace assertObjectsEqual calls with asserEqual

Copy link
Contributor Author

@dangerdak dangerdak Mar 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using objectsEqual but it doesnt work with nested objects so I kept equal as it is here (and replaced objectsEqual with equal)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great - thank you!

src/util.ts Outdated
export function assertEqual(actual: any, expected: any, msg = null) {
if (!msg) { msg = `actual: ${actual} expected: ${expected}`; }
if (!equal(actual, expected)) {
throw new Error(msg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For debugging on web, in the case where the actual and expected are objects - it would be helpful to have an additional output here. Something like this:

console.error("assertEqual failed. actual = ", actual, "expected =", expected)

(This way the nice inspector interface can be used to inspect deep objects)

src/dl/util.ts Outdated
return false;
}
seen.set(a, b);
return true;
Copy link
Contributor

@ry ry Mar 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard for me to tell if this algorithm is correct. Could you add some tests to util_test.ts ?
(You can copy some examples from tools/jasmine_shim_test)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure no problem. Just to check first, is this already covered by the tests in jasmine_shim_test.ts? As toEqual is tested in jasmine_shim.ts and it just returns the value from this function. Shall i move the tests from jasmine_shim_test.ts to util_test.ts or copy them?
Thanks for your help, hope you don't mind all the questions!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, kind of. The jasmine_shim_test.ts is covering the jasmine wrapper around assertEqual (called toEqual)... but we also want to unit test assertEqual by itself. So just duplicating the tests is fine. No, thank you! :)

src/dl/util.ts Outdated
}
}

function equal(c: any, d: any): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in src/util.ts not src/dl/util.ts

src/dl/util.ts Outdated
@@ -73,6 +73,52 @@ export function assert(expr: boolean, msg: string) {
}
}

export function assertEqual(actual: any, expected: any, msg = null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in src/util.ts not src/dl/util.ts

@@ -222,7 +222,7 @@ export class NDArray<D extends DataType = DataType, R extends Rank = Rank> {

asScalar(): Scalar<D> {
this.throwIfDisposed();
util.assert(this.size === 1, "The array must have only 1 element.");
util.assertEqual(this.size, 1, "The array must have only 1 element.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files in src/dl should not be changed.

@dangerdak dangerdak force-pushed the assert branch 2 times, most recently from f144375 to b4b026b Compare March 30, 2018 18:31
Works with objects. jasmine_shim.ts and tensor_util.ts use
equal/assertEqual from util.ts
Relates propelml#423
as assertEqual and equal work with objects (including
nested objects).
Relates propelml#423
@dangerdak dangerdak changed the title [WIP] Fixes assertEqual Add assertEqual Mar 31, 2018
Copy link
Contributor

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Elegantly done - thank you!

@ry ry merged commit f5e2560 into propelml:master Mar 31, 2018
@dangerdak dangerdak deleted the assert branch March 31, 2018 10:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants