use ssz::{Encode, Decode, MaxVec, Compact}; use generic_array::GenericArray; use core::fmt::Debug; use typenum::*; fn t(value: T, expected: &[u8]) { let encoded = value.encode(); assert_eq!(&encoded[..], expected); let decoded = T::decode(&mut &encoded[..]).unwrap(); assert_eq!(value, decoded); } #[test] fn spec() { t(false, &[0x00]); // boolean F t(true, &[0x01]); // boolean T t(0u8, &[0x00]); // uint8 00 t(1u8, &[0x01]); // uint8 01 t(0xabu8, &[0xab]); // uint8 ab t(0x0000u16, &[0x00, 0x00]); // uint16 0000 t(0xabcdu16, &[0xcd, 0xab]); // uint16 abcd t(0x00000000u32, &[0x00, 0x00, 0x00, 0x00]); // uint32 00000000 t(0x01234567u32, &[0x67, 0x45, 0x23, 0x01]); // uint32 01234567 // uint64 0000000000000000 t(0x0000000000000000u64, &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); // uint64 0123456789abcdef t(0x0123456789abcdefu64, &[0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01]); // bitvector TTFTFTFF t(Compact(GenericArray::::from([true, true, false, true, false, true, false, false])), &[0x2b]); // bitvector FTFT t(Compact(GenericArray::::from([false, true, false, true])), &[0x0a]); // bitvector FTF t(Compact(GenericArray::::from([false, true, false])), &[0x02]); // bitvector TFTFFFTTFT t(Compact(GenericArray::::from([true, false, true, false, false, false, true, true, false, true])), &[0xc5, 0x02]); // bitvector TFTFFFTTFTFFFFTT t(Compact(GenericArray::::from([true, false, true, false, false, false, true, true, false, true, false, false, false, false, true, true])), &[0xc5, 0xc2]); // long bitvector { let mut v = GenericArray::::default(); for i in 0..512 { v[i] = true; } t(Compact(v), &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); } // long bitlist { let mut v = MaxVec::::default(); for _ in 0..512 { v.push(true); } t(Compact(v), &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]); } // longer bitlist { let mut v = MaxVec::::default(); for _ in 0..513 { v.push(true); } t(Compact(v), &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03]); } }