X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fbyte_utils.rs;h=8ae3649780634cd6a6b2c3243222cc9f40b1e782;hb=99e2283aee3fb7ab512ad5688d2eb5e926d6d9f9;hp=44445666f776644947320cacac39fd0c9b57c982;hpb=126b514168ff8294f6ee7b9573797c6759512b9c;p=rust-lightning diff --git a/lightning/src/util/byte_utils.rs b/lightning/src/util/byte_utils.rs index 44445666..8ae36497 100644 --- a/lightning/src/util/byte_utils.rs +++ b/lightning/src/util/byte_utils.rs @@ -1,3 +1,12 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + #[inline] pub fn slice_to_be16(v: &[u8]) -> u16 { ((v[0] as u16) << 8*1) | @@ -104,3 +113,23 @@ pub fn le64_to_array(u: u64) -> [u8; 8] { v[7] = ((u >> 8*7) & 0xff) as u8; v } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_all() { + assert_eq!(slice_to_be16(&[0xde, 0xad]), 0xdead); + assert_eq!(slice_to_be32(&[0xde, 0xad, 0xbe, 0xef]), 0xdeadbeef); + assert_eq!(slice_to_le32(&[0xef, 0xbe, 0xad, 0xde]), 0xdeadbeef); + assert_eq!(slice_to_be48(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]), 0xdeadbeef1bad); + assert_eq!(slice_to_be64(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]), 0xdeadbeef1bad1dea); + assert_eq!(be16_to_array(0xdead), [0xde, 0xad]); + assert_eq!(be32_to_array(0xdeadbeef), [0xde, 0xad, 0xbe, 0xef]); + assert_eq!(le32_to_array(0xdeadbeef), [0xef, 0xbe, 0xad, 0xde]); + assert_eq!(be48_to_array(0xdeadbeef1bad), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]); + assert_eq!(be64_to_array(0xdeadbeef1bad1dea), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]); + assert_eq!(le64_to_array(0xdeadbeef1bad1dea), [0xea, 0x1d, 0xad, 0x1b, 0xef, 0xbe, 0xad, 0xde]); + } +}