|
| 1 | +extern crate packed_struct; |
| 2 | +#[macro_use] |
| 3 | +extern crate packed_struct_codegen; |
| 4 | + |
| 5 | +use packed_struct::prelude::*; |
| 6 | + |
| 7 | +macro_rules! test_int_14 { |
| 8 | + ($f: ident, $fi: tt) => { |
| 9 | + #[test] |
| 10 | + fn $f() { |
| 11 | + |
| 12 | + #[derive(PackedStruct, Debug, Default, Copy, Clone, PartialEq)] |
| 13 | + #[packed_struct(bit_numbering="msb0", endian="msb")] |
| 14 | + pub struct Test { |
| 15 | + #[packed_field(bits= $fi )] |
| 16 | + pub int1: Integer<u16, packed_bits::Bits14> |
| 17 | + } |
| 18 | + |
| 19 | + let roundtrip = |x: u16| { |
| 20 | + let mut t: Test = Default::default(); |
| 21 | + t.int1 = x.into(); |
| 22 | + let packed = t.pack(); |
| 23 | + |
| 24 | + let unpacked = Test::unpack(&packed).unwrap(); |
| 25 | + assert_eq!(unpacked, t); |
| 26 | + }; |
| 27 | + |
| 28 | + roundtrip(0b00_101010_10101010); |
| 29 | + roundtrip(0b00_010101_01010101); |
| 30 | + roundtrip(0b00_111111_11111111); |
| 31 | + roundtrip(0b00_111111_01111111); |
| 32 | + roundtrip(0b00_111110_11111111); |
| 33 | + roundtrip(0b00_111110_01111111); |
| 34 | + roundtrip(0b00_100000_00000001); |
| 35 | + } |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +test_int_14!(test_10_0, "0.."); |
| 41 | +test_int_14!(test_10_1, "1.."); |
| 42 | +test_int_14!(test_10_2, "2.."); |
| 43 | +test_int_14!(test_10_3, "3.."); |
| 44 | +test_int_14!(test_10_4, "4.."); |
| 45 | +test_int_14!(test_10_5, "5.."); |
| 46 | +test_int_14!(test_10_6, "6.."); |
| 47 | +test_int_14!(test_10_7, "7.."); |
| 48 | +test_int_14!(test_10_8, "8.."); |
| 49 | +test_int_14!(test_10_9, "9.."); |
| 50 | +test_int_14!(test_10_10, "10.."); |
| 51 | +test_int_14!(test_10_11, "11.."); |
| 52 | +test_int_14!(test_10_12, "12.."); |
| 53 | +test_int_14!(test_10_13, "13.."); |
| 54 | +test_int_14!(test_10_14, "14.."); |
| 55 | +test_int_14!(test_10_15, "15.."); |
| 56 | +test_int_14!(test_10_16, "16.."); |
| 57 | +test_int_14!(test_10_17, "17.."); |
| 58 | +test_int_14!(test_10_18, "18.."); |
| 59 | +test_int_14!(test_10_19, "19.."); |
| 60 | + |
| 61 | + |
| 62 | +test_int_14!(test_10_100, "100.."); |
0 commit comments