Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encoding.binary: add unit tests and module documentation (#15000)
- Loading branch information
1 parent
d4b0ebf
commit 8ee614d
Showing
4 changed files
with
145 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,3 +105,6 @@ thirdparty/stdatomic/nix/cpp/*.h | |
|
||
# ignore VLS log | ||
vls.log | ||
|
||
# ignore v2go tmperror files | ||
*.tmperr |
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,23 @@ | ||
## Description | ||
|
||
`encoding.binary` contains utility functions for converting between an array of bytes (`[]u8`) | ||
and unsigned integers of various widths (`u16`, `u32`, and `u64`). | ||
|
||
There are two ways in which bytes can be encoded: | ||
|
||
1. Little endian: The least significant bytes are stored first, followed by the most | ||
significant bytes. | ||
2. Big endian: The most significant bytes are stored first, opposite to the little endian | ||
convention. | ||
|
||
For example, let us take the number `0x12345678`. In little endian, the bytes are extracted as | ||
`0x78`, `0x56`, `0x34`, and `0x12`. In big endian, the bytes are `0x12`, `0x34`, `0x56`, | ||
and `0x78`. | ||
|
||
We follow a similar procedure when we want to go the other way around. Consider the second | ||
sequence of bytes in the previous example: `0x12`, `0x34`, `0x56`, and `0x78`. If we encode | ||
this sequence in little endian format, we get the integer `0x78563412`. If we encode this | ||
sequence in big endian, we get `0x12345678`. | ||
|
||
**NOTE:** The functions in this module assume appropriately sized u8 arrays. If the sizes | ||
are not valid, the functions will panic. |
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,107 @@ | ||
module binary | ||
|
||
// Little Endian Tests | ||
|
||
fn test_little_endian_u16() { | ||
assert little_endian_u16([u8(0), 0]) == u16(0) | ||
assert little_endian_u16([u8(5), 4]) == u16(0x0405) | ||
assert little_endian_u16([u8(0x35), 0x57]) == u16(0x5735) | ||
assert little_endian_u16([u8(0x35), 0x57]) != u16(0x3557) | ||
} | ||
|
||
fn test_little_endian_put_u16() { | ||
mut buf := []u8{len: 2} | ||
little_endian_put_u16(mut buf, 0x8725) | ||
assert buf == [u8(0x25), 0x87] | ||
little_endian_put_u16(mut buf, 0) | ||
assert buf == [u8(0), 0] | ||
little_endian_put_u16(mut buf, 0xfdff) | ||
assert buf == [u8(0xff), 0xfd] | ||
} | ||
|
||
fn test_little_endian_u32() { | ||
assert little_endian_u32([u8(0), 0, 0, 0]) == u32(0) | ||
assert little_endian_u32([u8(5), 4, 9, 1]) == u32(0x01090405) | ||
assert little_endian_u32([u8(0xf8), 0xa2, 0x9e, 0x21]) == u32(0x219ea2f8) | ||
assert little_endian_u32([u8(0xf8), 0xa2, 0x9e, 0x21]) != u32(0xf8a29e21) | ||
} | ||
|
||
fn test_little_endian_put_u32() { | ||
mut buf := []u8{len: 4} | ||
little_endian_put_u32(mut buf, 0x872fea95) | ||
assert buf == [u8(0x95), 0xea, 0x2f, 0x87] | ||
little_endian_put_u32(mut buf, 0) | ||
assert buf == [u8(0), 0, 0, 0] | ||
little_endian_put_u32(mut buf, 0xfdf2e68f) | ||
assert buf == [u8(0x8f), 0xe6, 0xf2, 0xfd] | ||
} | ||
|
||
fn test_little_endian_u64() { | ||
assert little_endian_u64([u8(0), 0, 0, 0, 0, 0, 0, 0]) == u64(0) | ||
assert little_endian_u64([u8(5), 4, 9, 1, 7, 3, 6, 8]) == u64(0x0806030701090405) | ||
assert little_endian_u64([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]) == u64(0x8f8e9f7f219ea2f8) | ||
assert little_endian_u64([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]) != u64(0xf8a29e217f9f8e8f) | ||
} | ||
|
||
fn test_little_endian_put_u64() { | ||
mut buf := []u8{len: 8} | ||
little_endian_put_u64(mut buf, 0x872fea95fdf2e68f) | ||
assert buf == [u8(0x8f), 0xe6, 0xf2, 0xfd, 0x95, 0xea, 0x2f, 0x87] | ||
little_endian_put_u64(mut buf, 0) | ||
assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 0] | ||
little_endian_put_u64(mut buf, 0xfdf2e68f8e9f7f21) | ||
assert buf == [u8(0x21), 0x7f, 0x9f, 0x8e, 0x8f, 0xe6, 0xf2, 0xfd] | ||
} | ||
|
||
// Big Endian Tests | ||
|
||
fn test_big_endian_u16() { | ||
assert big_endian_u16([u8(0), 0]) == u16(0) | ||
assert big_endian_u16([u8(5), 4]) == u16(0x0504) | ||
assert big_endian_u16([u8(0x35), 0x57]) == u16(0x3557) | ||
assert big_endian_u16([u8(0x35), 0x57]) != u16(0x5735) | ||
} | ||
|
||
fn test_big_endian_put_u16() { | ||
mut buf := []u8{len: 2} | ||
big_endian_put_u16(mut buf, 0x8725) | ||
assert buf == [u8(0x87), 0x25] | ||
big_endian_put_u16(mut buf, 0) | ||
assert buf == [u8(0), 0] | ||
big_endian_put_u16(mut buf, 0xfdff) | ||
assert buf == [u8(0xfd), 0xff] | ||
} | ||
|
||
fn test_big_endian_u32() { | ||
assert big_endian_u32([u8(0), 0, 0, 0]) == u32(0) | ||
assert big_endian_u32([u8(5), 4, 9, 1]) == u32(0x05040901) | ||
assert big_endian_u32([u8(0xf8), 0xa2, 0x9e, 0x21]) == u32(0xf8a29e21) | ||
assert big_endian_u32([u8(0xf8), 0xa2, 0x9e, 0x21]) != u32(0x2192a2f8) | ||
} | ||
|
||
fn test_big_endian_put_u32() { | ||
mut buf := []u8{len: 4} | ||
big_endian_put_u32(mut buf, 0x872fea95) | ||
assert buf == [u8(0x87), 0x2f, 0xea, 0x95] | ||
big_endian_put_u32(mut buf, 0) | ||
assert buf == [u8(0), 0, 0, 0] | ||
big_endian_put_u32(mut buf, 0xfdf2e68f) | ||
assert buf == [u8(0xfd), 0xf2, 0xe6, 0x8f] | ||
} | ||
|
||
fn test_big_endian_u64() { | ||
assert big_endian_u64([u8(0), 0, 0, 0, 0, 0, 0, 0]) == u64(0) | ||
assert big_endian_u64([u8(5), 4, 9, 1, 7, 3, 6, 8]) == u64(0x0504090107030608) | ||
assert big_endian_u64([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]) == u64(0xf8a29e217f9f8e8f) | ||
assert big_endian_u64([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]) != u64(0x8f8e9f7f219ea2f8) | ||
} | ||
|
||
fn test_big_endian_put_u64() { | ||
mut buf := []u8{len: 8} | ||
big_endian_put_u64(mut buf, 0x872fea95fdf2e68f) | ||
assert buf == [u8(0x87), 0x2f, 0xea, 0x95, 0xfd, 0xf2, 0xe6, 0x8f] | ||
big_endian_put_u64(mut buf, 0) | ||
assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 0] | ||
big_endian_put_u64(mut buf, 0xfdf2e68f8e9f7f21) | ||
assert buf == [u8(0xfd), 0xf2, 0xe6, 0x8f, 0x8e, 0x9f, 0x7f, 0x21] | ||
} |