bn.js icon indicating copy to clipboard operation
bn.js copied to clipboard

toArray/toString: add tests

Open dcousens opened this issue 9 years ago • 14 comments

This PR adds tests for toArray and toString to enforce consistency. The tests as of right now are failing because I am not aware of the intended output format of toArray vs toString.

toString() seems to output decimal. Tests pass. toString(16) seems to output unsigned hex string with a - sign appended to the front. toArray() seems to output unsigned binary, but not in twos complement (the standard)

@indutny I'm happy to amend these tests if you could shed some light on what is expected :).

dcousens avatar Feb 12 '15 04:02 dcousens

The returned hex from toString also seems to be incomplete, in that hex.length % 2 === 0 doesn't always hold true. In fact, 0x01 will be returned as 0x1. Is it intentional that the hex string is not grouped by bytes?

dcousens avatar Feb 12 '15 04:02 dcousens

ping @indutny

dcousens avatar Apr 08 '15 01:04 dcousens

I don't think that we need invalid section, at least not now.

indutny avatar Apr 08 '15 08:04 indutny

Otherwise LGTM, thank you! ;)

indutny avatar Apr 08 '15 08:04 indutny

Rebased.

Ok @indutny I addressed most of the nits.

Waiting on your answer about whether it should have a the default zero padding of 1 (which IMHO I think is odd).

Also curious if after the discussion about regex's whether I keep/remove the invalid section.

dcousens avatar Apr 09 '15 03:04 dcousens

@dcousens looking very good now, except positive-only hex and a failing tests... Speaking of this, I'm not a big fan of having failing tests in repo's master. Would you mind separating them into an another PR, where we'll figure out the best way to make it work properly?

indutny avatar Apr 10 '15 18:04 indutny

Sure, did you have a branch I could target? On 11 Apr 2015 04:29, "Fedor Indutny" [email protected] wrote:

@dcousens https://github.com/dcousens looking very good now, except positive-only hex and a failing tests... Speaking of this, I'm not a big fan of having failing tests in repo's master. Would you mind separating them into an another PR, where we'll figure out the best way to make it work properly?

— Reply to this email directly or view it on GitHub https://github.com/indutny/bn.js/pull/36#issuecomment-91645821.

dcousens avatar Apr 10 '15 21:04 dcousens

@dcousens oh right! Added you as a collaborator to this repo, I hope you don't mind :)

indutny avatar Apr 10 '15 21:04 indutny

@dcousens do you think it would make sense revisiting this PR?

axic avatar Feb 13 '16 14:02 axic

@axic I sure hope so.

Garbage sounds like a real easy way to have people encounter issues without knowing it. Especially given this library is most often used for crypto. I think it is likely people will just accept the garbage result as what it should be at face value, because the underlying implementation they have no understanding of.

I couldn't have said it any better @dcousens!

jprichardson avatar Feb 14 '16 03:02 jprichardson

Some of the things you've mentioned could be addressed/discussed as part of #129:

toString(16) seems to output unsigned hex string with a - sign appended to the front. toArray() seems to output unsigned binary, but not in twos complement (the standard)

axic avatar Feb 14 '16 17:02 axic

Added this back into my work queue :+1:

edit 29/09/2016: soon...

edit 30/11/2017: :disappointed:

dcousens avatar Jul 08 '16 01:07 dcousens

re-adding to work queue attempt 3

dcousens avatar Nov 29 '17 22:11 dcousens

Note: right now the toString() function doesn't zero pad hex (or anything > base 10), which results in strings like "a" instead of "0a" being output as valid hex strings. Perhaps this is causing tests to fail. If people are relying on the current behavior, might need to make "padding" an option.

earonesty avatar Aug 08 '18 18:08 earonesty