summary refs log tree commit diff
path: root/bit_field/tests/test_enum.rs
blob: d926d9cc73b12e1f2e7da1894b96f9c400308752 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
extern crate bit_field;

use bit_field::*;

#[bitfield]
#[derive(Debug, PartialEq)]
enum TwoBits {
    Zero = 0b00,
    One = 0b01,
    Two = 0b10,
    Three = 0b11,
}

#[bitfield]
struct Struct {
    prefix: BitField1,
    two_bits: TwoBits,
    suffix: BitField5,
}

#[test]
fn test_enum() {
    let mut s = Struct::new();
    assert_eq!(s.get(0, 8), 0b_0000_0000);
    assert_eq!(s.get_two_bits(), TwoBits::Zero);

    s.set_two_bits(TwoBits::Three);
    assert_eq!(s.get(0, 8), 0b_0000_0110);
    assert_eq!(s.get_two_bits(), TwoBits::Three);

    s.set(0, 8, 0b_1010_1010);
    //                   ^^ TwoBits
    assert_eq!(s.get_two_bits(), TwoBits::One);
}