Skip to content

Commit 985b0bc

Browse files
committed
Increase test coverage of Int{64,128}::from_{be,le}_bytes
1 parent f503569 commit 985b0bc

File tree

2 files changed

+105
-11
lines changed

2 files changed

+105
-11
lines changed

packages/std/src/math/int128.rs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,16 +521,64 @@ mod tests {
521521

522522
#[test]
523523
fn int128_from_be_bytes_works() {
524-
let num = Int128::from_be_bytes([1; 16]);
524+
// zero
525+
let original = [0; 16];
526+
let num = Int128::from_be_bytes(original);
527+
assert!(num.is_zero());
528+
529+
// one
530+
let original = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
531+
let num = Int128::from_be_bytes(original);
532+
assert_eq!(num.i128(), 1);
533+
534+
// 258
535+
let original = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2];
536+
let num = Int128::from_be_bytes(original);
537+
assert_eq!(num.i128(), 258);
538+
539+
// 2x roundtrip
540+
let original = [1; 16];
541+
let num = Int128::from_be_bytes(original);
525542
let a: [u8; 16] = num.to_be_bytes();
526-
assert_eq!(a, [1; 16]);
543+
assert_eq!(a, original);
527544

528-
let be_bytes = [
545+
let original = [
529546
0u8, 222u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8,
530547
];
531-
let num = Int128::from_be_bytes(be_bytes);
548+
let num = Int128::from_be_bytes(original);
532549
let resulting_bytes: [u8; 16] = num.to_be_bytes();
533-
assert_eq!(be_bytes, resulting_bytes);
550+
assert_eq!(resulting_bytes, original);
551+
}
552+
553+
#[test]
554+
fn int128_from_le_bytes_works() {
555+
// zero
556+
let original = [0; 16];
557+
let num = Int128::from_le_bytes(original);
558+
assert!(num.is_zero());
559+
560+
// one
561+
let original = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
562+
let num = Int128::from_le_bytes(original);
563+
assert_eq!(num.i128(), 1);
564+
565+
// 258
566+
let original = [2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
567+
let num = Int128::from_le_bytes(original);
568+
assert_eq!(num.i128(), 258);
569+
570+
// 2x roundtrip
571+
let original = [1; 16];
572+
let num = Int128::from_le_bytes(original);
573+
let a: [u8; 16] = num.to_le_bytes();
574+
assert_eq!(a, original);
575+
576+
let original = [
577+
0u8, 222u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8,
578+
];
579+
let num = Int128::from_le_bytes(original);
580+
let resulting_bytes: [u8; 16] = num.to_le_bytes();
581+
assert_eq!(resulting_bytes, original);
534582
}
535583

536584
#[test]

packages/std/src/math/int64.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,60 @@ mod tests {
511511

512512
#[test]
513513
fn int64_from_be_bytes_works() {
514-
let num = Int64::from_be_bytes([1; 8]);
514+
// zero
515+
let original = [0; 8];
516+
let num = Int64::from_be_bytes(original);
517+
assert!(num.is_zero());
518+
519+
// one
520+
let original = [0, 0, 0, 0, 0, 0, 0, 1];
521+
let num = Int64::from_be_bytes(original);
522+
assert_eq!(num.i64(), 1);
523+
524+
// 258
525+
let original = [0, 0, 0, 0, 0, 0, 1, 2];
526+
let num = Int64::from_be_bytes(original);
527+
assert_eq!(num.i64(), 258);
528+
529+
// 2x roundtrip
530+
let original = [1; 8];
531+
let num = Int64::from_be_bytes(original);
515532
let a: [u8; 8] = num.to_be_bytes();
516-
assert_eq!(a, [1; 8]);
533+
assert_eq!(a, original);
517534

518-
let be_bytes = [0u8, 222u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8];
519-
let num = Int64::from_be_bytes(be_bytes);
520-
let resulting_bytes: [u8; 8] = num.to_be_bytes();
521-
assert_eq!(be_bytes, resulting_bytes);
535+
let original = [0u8, 222u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8];
536+
let num = Int64::from_be_bytes(original);
537+
let a: [u8; 8] = num.to_be_bytes();
538+
assert_eq!(a, original);
539+
}
540+
541+
#[test]
542+
fn int64_from_le_bytes_works() {
543+
// zero
544+
let original = [0; 8];
545+
let num = Int64::from_le_bytes(original);
546+
assert!(num.is_zero());
547+
548+
// one
549+
let original = [1, 0, 0, 0, 0, 0, 0, 0];
550+
let num = Int64::from_le_bytes(original);
551+
assert_eq!(num.i64(), 1);
552+
553+
// 258
554+
let original = [2, 1, 0, 0, 0, 0, 0, 0];
555+
let num = Int64::from_le_bytes(original);
556+
assert_eq!(num.i64(), 258);
557+
558+
// 2x roundtrip
559+
let original = [1; 8];
560+
let num = Int64::from_le_bytes(original);
561+
let a: [u8; 8] = num.to_le_bytes();
562+
assert_eq!(a, original);
563+
564+
let original = [0u8, 222u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8];
565+
let num = Int64::from_le_bytes(original);
566+
let a: [u8; 8] = num.to_le_bytes();
567+
assert_eq!(a, original);
522568
}
523569

524570
#[test]

0 commit comments

Comments
 (0)