aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / bitflags / src / tests / bitflags_match.rs
| 1 | bitflags! { |
| 2 | #[derive(PartialEq)] |
| 3 | struct Flags: u8 { |
| 4 | const A = 1 << 0; |
| 5 | const B = 1 << 1; |
| 6 | const C = 1 << 2; |
| 7 | const D = 1 << 3; |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | fn flag_to_string(flag: Flags) -> String { |
| 12 | bitflags_match!(flag, { |
| 13 | Flags::A => "A".to_string(), |
| 14 | Flags::B => { "B".to_string() } |
| 15 | Flags::C => "C".to_string(), |
| 16 | Flags::D => "D".to_string(), |
| 17 | Flags::A | Flags::B => "A or B".to_string(), |
| 18 | Flags::A & Flags::B => { "A and B | empty".to_string() }, |
| 19 | Flags::A ^ Flags::B => "A xor B".to_string(), |
| 20 | Flags::A | Flags::B | Flags::C => "A or B or C".to_string(), |
| 21 | Flags::A & Flags::B & Flags::C => "A and B and C".to_string(), |
| 22 | Flags::A ^ Flags::B ^ Flags::C => "A xor B xor C".to_string(), |
| 23 | Flags::A | Flags::B | Flags::C | Flags::D => "All flags".to_string(), |
| 24 | _ => "Unknown combination".to_string() |
| 25 | }) |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn test_single_flags() { |
| 30 | assert_eq!(flag_to_string(Flags::A), "A"); |
| 31 | assert_eq!(flag_to_string(Flags::B), "B"); |
| 32 | assert_eq!(flag_to_string(Flags::C), "C"); |
| 33 | assert_eq!(flag_to_string(Flags::D), "D"); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn test_or_operations() { |
| 38 | assert_eq!(flag_to_string(Flags::A | Flags::B), "A or B"); |
| 39 | assert_eq!( |
| 40 | flag_to_string(Flags::A | Flags::B | Flags::C), |
| 41 | "A or B or C" |
| 42 | ); |
| 43 | assert_eq!( |
| 44 | flag_to_string(Flags::A | Flags::B | Flags::C | Flags::D), |
| 45 | "All flags" |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn test_and_operations() { |
| 51 | assert_eq!(flag_to_string(Flags::A & Flags::A), "A"); |
| 52 | assert_eq!(flag_to_string(Flags::A & Flags::B), "A and B | empty"); |
| 53 | assert_eq!( |
| 54 | flag_to_string(Flags::A & Flags::B & Flags::C), |
| 55 | "A and B | empty" |
| 56 | ); // Since A, B, and C are mutually exclusive, the result of A & B & C is 0 ==> A & B & C = 0000 (i.e., empty). |
| 57 | // However, in the bitflags_match! statement (actually is if {..} else if {..} .. else {..}), |
| 58 | // the "A & B = 0000" condition is listed first, so 0000 will match "A & B" first, |
| 59 | // resulting in the output of the "A and B | empty" branch. |
| 60 | assert_eq!( |
| 61 | flag_to_string(Flags::A & Flags::B & Flags::C & Flags::D), |
| 62 | "A and B | empty" |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn test_xor_operations() { |
| 68 | assert_eq!(flag_to_string(Flags::A ^ Flags::B), "A or B"); // A | B = A ^ B == 0011 |
| 69 | assert_eq!(flag_to_string(Flags::A ^ Flags::A), "A and B | empty"); |
| 70 | assert_eq!( |
| 71 | flag_to_string(Flags::A ^ Flags::B ^ Flags::C), |
| 72 | "A or B or C" |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn test_complex_operations() { |
| 78 | assert_eq!(flag_to_string(Flags::A | (Flags::B & Flags::C)), "A"); |
| 79 | assert_eq!( |
| 80 | flag_to_string((Flags::A | Flags::B) & (Flags::B | Flags::C)), |
| 81 | "B" |
| 82 | ); |
| 83 | assert_eq!( |
| 84 | flag_to_string(Flags::A ^ (Flags::B | Flags::C)), |
| 85 | "A or B or C" |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn test_empty_and_full_flags() { |
| 91 | assert_eq!(flag_to_string(Flags::empty()), "A and B | empty"); |
| 92 | assert_eq!(flag_to_string(Flags::all()), "All flags"); |
| 93 | } |