microrti-encoding 0.4.0

HLA 4 data encoding of IEEE 1516.2-2025: basic types, strings, handles, arrays, records, variant records and a FOM-driven codec.
Documentation
//! Helpers shared by the integration tests.
#![allow(dead_code)]

use std::fmt::Debug;

use microrti_encoding::{DataElement, Decode, decode_from_slice_strict, encode_to_vec};

/// Checks a typed value against its byte-exact encoding: encoding, length, strict decoding, and the same
/// through the dynamic `DataElement` view of the value.
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));
}

/// Checks a dynamic element against its byte-exact encoding. `blank` has the same datatype but other
/// values; it must equal `value` after decoding the expected octets.
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);
}

/// Small deterministic pseudo random generator (xorshift64).
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()
    }
}