#![allow(dead_code)]
use std::fmt::Debug;
use microrti_encoding::{DataElement, Decode, decode_from_slice_strict, encode_to_vec};
pub fn check<T>(value: T, expected: &[u8])
where
T: Decode + Clone + PartialEq + Debug + Send + Sync + 'static,
{
assert_eq!(encode_to_vec(&value), expected, "encoding of {value:?}");
assert_eq!(value.encoded_len(), expected.len(), "encoded_len of {value:?}");
assert_eq!(decode_from_slice_strict::<T>(expected).as_ref(), Ok(&value), "decoding of {value:?}");
assert_eq!(value.octet_boundary(), T::OCTET_BOUNDARY);
assert_eq!(value.encoded_length(), Ok(expected.len()));
assert_eq!(value.to_bytes().as_deref(), Ok(expected));
}
pub fn check_dynamic<E>(value: &E, mut blank: E, expected: &[u8])
where
E: DataElement + PartialEq + Debug,
{
assert!(value.is_same_type_as(&blank), "{blank:?} is not a blank of {value:?}");
assert_eq!(value.to_bytes().as_deref(), Ok(expected), "encoding of {value:?}");
assert_eq!(value.encoded_length(), Ok(expected.len()));
blank.decode_slice_strict(expected).unwrap();
assert_eq!(&blank, value);
}
pub struct XorShift(pub u64);
impl XorShift {
pub fn next(&mut self) -> u64 {
let mut x = self.0;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
self.0 = x;
x
}
pub fn below(&mut self, bound: usize) -> usize {
(self.next() % bound as u64) as usize
}
pub fn bytes(&mut self, len: usize) -> Vec<u8> {
(0..len).map(|_| self.next() as u8).collect()
}
}