bitable 0.1.0

An all-const, compile-time checked, safe bit handling library with zero runtime overhead
Documentation
//! # Test suite
//! 

// ---------------------------------------------------------------------------
// Testing context (shared between runtime and compile-time test suites)
// ---------------------------------------------------------------------------

pub mod context;

// ---------------------------------------------------------------------------
// Core helper functions
// ---------------------------------------------------------------------------

mod core_helper_functions {
    // use super::*;
    // #[test]
    // #[should_panic]
    // fn field_mask_32() {
    //     let mask: u32 = field_mask(32);
    //     println!("*** Mask: {}", mask)
    // }
}

// ---------------------------------------------------------------------------
// Core target types
// ---------------------------------------------------------------------------

mod core_target_u8 {
    use super::context::*;

    // is_set(u8)
    #[test]
    fn u8bit_is_set() {
        let yes: u8 = 0b1 << U8_OFFSET;
        assert_eq!(true, U8_BIT.is_set(&yes));
    }
    #[test]
    fn u8bit_is_not_set() {
        let no: u8 = 0;
        assert_eq!(false, U8_BIT.is_set(&no));
    }

    // #[test]
    // #[should_panic(expected = "Bit offset too large for this set of bits")]
    // fn u16bit_is_set() {
    //     let dc: u8 = 0;
    //     U16BIT.is_set(&dc);
    // }
    // is_clr(u8)
    // #[test]
    // fn u8bit_is_clr() {
    //     let yes: u8 = 0;
    //     assert_eq!(true, U8_BIT.is_clr(&yes));
    // }
    // #[test]
    // fn u8bit_is_not_clr() {
    //     let no: u8 = 0b1 << U8_OFFSET;
    //     assert_eq!(false, U8_BIT.is_clr(&no));
    // }
    // #[test]
    // #[should_panic(expected = "Bit offset too large for this set of bits")]
    // fn u16bit_is_clr() {
    //     let dc: u8 = 0;
    //     U16BIT.is_set(&dc);
    // }
}

mod core_target_u16 {
    use super::context::*;

    // is_set(u16)
    #[test]
    fn u16bit_is_set() {
        let yes: u16 = 0b1 << U8_OFFSET;
        assert_eq!(true, U8_BIT.is_set(&yes));
    }
    #[test]
    fn u16bit_is_not_set() {
        let no: u16 = 0;
        assert_eq!(false, U8_BIT.is_set(&no));
    }
    // #[test]
    // #[should_panic(expected = "Bit offset too large for this set of bits")]
    // fn u32bit_is_set() {
    //     let dc: u16 = 0;
    //     U32BIT.is_set(&dc);
    // }
    // is_clr(u16)
    #[test]
    fn u16bit_is_clr() {
        let yes: u8 = 0;
        assert_eq!(true, U8_BIT.is_clr(&yes));
    }
    #[test]
    fn u16bit_is_not_clr() {
        let no: u8 = 0b1 << U8_OFFSET;
        assert_eq!(false, U8_BIT.is_clr(&no));
    }
    // #[test]
    // #[should_panic(expected = "Bit offset too large for this set of bits")]
    // fn u32bit_is_clr() {
    //     let dc: u8 = 0;
    //     U16BIT.is_set(&dc);
    // }
    #[test]
    fn u32field_0_32_in_u8array_read() {
        let no: [u8; 5] = [0x12, 0x34, 0x56, 0x78, 0x9a];
        let f = Field::<0, 32>::new();
        let s : u32 = f.read(&no);
        assert_eq!(0x12345678, s);
    }
    #[test]
    fn u32field_0_32_in_u8array_read_at_0() {
        let no: [u8; 5] = [0x12, 0x34, 0x56, 0x78, 0x9a];
        let f = Field::<0, 32>::new();
        let s0 : u32 = f.read_at(&no, 0);
        assert_eq!(0x12345678, s0);
    }
    #[test]
    fn u32field_0_32_in_u8array_read_at_1() {
        let no: [u8; 5] = [0x12, 0x34, 0x56, 0x78, 0x9a];
        let f = Field::<0, 32>::new();
        let s1 : u32 = f.read_at(&no, 1);
        assert_eq!(0x3456789a, s1);
    }
    #[test]
    fn u32field_8_24_in_u8array_read_at_1() {
        let no: [u8; 5] = [0x12, 0x34, 0x56, 0x78, 0x9a];
        let f = Field::<8, 24>::new();
        let s0 : u32 = f.read_at(&no, 1);
        assert_eq!(0x345678, s0);
    }
    #[test]
    fn u32field_4_20_in_u8array_read_at_1() {
        let no: [u8; 5] = [0x12, 0x34, 0x56, 0x78, 0x9a];
        let f = Field::<4, 20>::new();
        let s0 : u32 = f.read_at(&no, 1);
        assert_eq!(0x56789, s0);
    }
}

// ---------------------------------------------------------------------------
// Conversion between core types
// ---------------------------------------------------------------------------

mod basic_bit_and_bitmasks {
    use super::context::*;

    const BITMASK_U32: Mask = Mask::new(1 << U8_OFFSET);

    // #[test]
    // fn mask_from_bit() {
    //     assert_eq!( (1 << U8_OFFSET) as u32, U8_BIT.mask().into() );
    // }
    // #[test]
    // #[should_panic(expected = "Bit offset too large for this set of bits")]
    // fn bit_offset_too_big() {
    //     let bit = Bit::new(32);
    //     let _ = bit.mask();
    // }

    // #[test]
    // fn mask_from_bitfield() {
    //     assert_eq!( (((1 << LENGTH) - 1) << U8_OFFSET) as u32, BITFIELD.mask().into() );
    // }

    // #[test]
    // fn bitmask_u32_from_bit() {
    //     assert_eq!( BITMASK_U32, Mask::<u32>::from(U8_BIT) );
    // }
    // #[test]
    // fn bit_into_bitmask_u32() {
    //     assert_eq!( BITMASK_U32, U8_BIT.into() );
    // }

    #[test]
    fn u32_from_bitmask_u32() {
        assert_eq!( 1 << U8_OFFSET, u32::from(BITMASK_U32) );
    }
    // #[test]
    // fn bitmask_u32_into_u32() {
    //     assert_eq!( (1 << U8_OFFSET) as u32, BITMASK_U32.into() );
    // }
}

// ---------------------------------------------------------------------------
// core::ops implementations
// ---------------------------------------------------------------------------

mod bit_deref_tests {
    // use super::context::*;

    // fn u32_param(p: u32) -> u32 { p }
    
    // #[test]
    // fn test_deref() {
    //     assert_eq!(*U8_BIT, U8_OFFSET);
    // }
}

mod bit_add_tests {
    // use super::context::*;

    // #[test]
    // fn test_add_u32_and_bit() {
    //     let mut cnt: u32 = 1;
    //     cnt = cnt + U8_BIT;
    //     assert_eq!(cnt, 1 + U8_OFFSET as u32);

    //     // let mut cnt: u32 = 1;
    //     // cnt = (BIT + cnt).into();
    //     // assert_eq!(cnt, 0b1 << (1 + U8_OFFSET as u32));

    //     // let mut bit = Bit::new(U8_OFFSET);
    //     // let cnt: u32 = 0;
    //     // bit = bit + cnt;
    //     // assert_eq!((0b1 << U8_OFFSET) as u32, bit.into());
    // }
}