Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
google
GitHub Repository: google/crosvm
Path: blob/main/bit_field/tests/test_enum.rs
5392 views
1
// Copyright 2019 The ChromiumOS Authors
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
use bit_field::*;
6
7
#[bitfield]
8
#[derive(Debug, PartialEq, Eq)]
9
enum TwoBits {
10
Zero = 0b00,
11
One = 0b01,
12
Two = 0b10,
13
Three = 0b11,
14
}
15
16
#[bitfield]
17
#[bits = 3]
18
#[derive(Debug, PartialEq, Eq)]
19
enum ThreeBits {
20
Zero = 0b00,
21
One = 0b01,
22
Two = 0b10,
23
Three = 0b111,
24
}
25
26
#[bitfield]
27
struct Struct {
28
prefix: BitField1,
29
two_bits: TwoBits,
30
three_bits: ThreeBits,
31
suffix: BitField2,
32
}
33
34
#[test]
35
fn test_enum() {
36
let mut s = Struct::new();
37
assert_eq!(s.get(0, 8), 0b_0000_0000);
38
assert_eq!(s.get_two_bits(), TwoBits::Zero);
39
40
s.set_two_bits(TwoBits::Three);
41
assert_eq!(s.get(0, 8), 0b_0000_0110);
42
assert_eq!(s.get_two_bits(), TwoBits::Three);
43
44
s.set(0, 8, 0b_1010_1010);
45
// ^^ TwoBits
46
// ^^_^ Three Bits.
47
assert_eq!(s.get_two_bits(), TwoBits::One);
48
assert_eq!(s.get_three_bits().unwrap_err().raw_val(), 0b101);
49
50
s.set_three_bits(ThreeBits::Two);
51
assert_eq!(s.get(0, 8), 0b_1001_0010);
52
}
53
54