-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix encode signed numbers. #6
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6baf239
Fix encode signed numbers.
ramonalobont 10afc93
Fix encode signed number function for positive numbers.
ramonalobont 63f84d3
Fix encode signed number + add some test vectors
ramonalobont 9cec5e5
Fixes after reviews.
ramonalobont 03506dd
Readability fixes.
ramonalobont 7262515
Readability fixes.
ramonalobont File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from multiversx_sdk.core import codec | ||
|
||
test_vectors_1 = [ | ||
[-1, 0xFF], | ||
[1, 0x01], | ||
[2, 0x02], | ||
[-2, 0xFE], | ||
[3, 0x03], | ||
[-3, 0xFD], | ||
[4, 0x04], | ||
[-4, 0xFC], | ||
] | ||
|
||
test_vectors_2 = [ | ||
[-1, 0xFF], | ||
[1, 0x01], | ||
[2, 0x02], | ||
[-2, 0xFE], | ||
[3, 0x03], | ||
[-3, 0xFD], | ||
[4, 0x04], | ||
[-4, 0xFC], | ||
[5, 0x05], | ||
[-5, 0xFB], | ||
[6, 0x06], | ||
[-6, 0xFA], | ||
] | ||
|
||
test_vectors_3 = [ | ||
[125, 0x7D], | ||
[-125, 0x83], | ||
[126, 0x7E], | ||
[-126, 0x82], | ||
[127, 0x7F], | ||
[-127, 0x81], | ||
[-128, 0x80], | ||
] | ||
|
||
test_vectors_4 = [ | ||
[128, [0x00, 0x80]], | ||
[129, [0x00, 0x81]], | ||
[-129, [0xFF, 0x7F]], | ||
[130, [0x00, 0x82]], | ||
[-130, [0xFF, 0x7E]], | ||
[253, [0x00, 0xFD]], | ||
[256, [0x01, 0x00]], | ||
[-256, [0xFF, 0x00]], | ||
[-257, [0xFE, 0xFF]], | ||
[258, [0x01, 0x02]], | ||
] | ||
|
||
|
||
def test_encode_signed_number(): | ||
assert codec.encode_signed_number(-256) == bytes([0xFF, 0x00]) | ||
assert codec.encode_signed_number(-0x11) == bytes([0xEF]) | ||
assert codec.encode_signed_number(-128) == bytes([0x80]) | ||
assert codec.encode_signed_number(-1) == bytes([0xFF]) | ||
assert codec.encode_signed_number(0) == bytes([]) | ||
assert codec.encode_signed_number(1) == bytes([0x01]) | ||
assert codec.encode_signed_number(256) == bytes([0x01, 0x00]) | ||
assert codec.encode_signed_number(127) == bytes([0x7F]) | ||
assert codec.encode_signed_number(0x11) == bytes([0x11]) | ||
assert codec.encode_signed_number(255) == bytes([0x00, 0xFF]) | ||
|
||
for [input_data, expected_data] in test_vectors_1: | ||
assert codec.encode_signed_number(input_data) == bytes([expected_data]) | ||
|
||
for [input_data, expected_data] in test_vectors_2: | ||
assert codec.encode_signed_number(input_data) == bytes([expected_data]) | ||
|
||
for [input_data, expected_data] in test_vectors_3: | ||
assert codec.encode_signed_number(input_data) == bytes([expected_data]) | ||
|
||
for [input_data, [expected_data_1, expected_data_2]] in test_vectors_4: | ||
assert codec.encode_signed_number(input_data) == bytes([expected_data_1, expected_data_2]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for readability, maybe add an empty line between the last
assert
statement and the firstfor
statement, but also between eachfor
staments.