Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/bitflags/src/tests/iter.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / bitflags / src / tests / iter.rs` 299 lines ยท 8582 bytes Raw Blame History 1 use super::*; 2 3 use crate::Flags; 4 5 #[test] 6 #[cfg(not(miri))] // Very slow in miri 7 fn roundtrip() { 8 for a in 0u8..=255 { 9 for b in 0u8..=255 { 10 let f = TestFlags::from_bits_retain(a | b); 11 12 assert_eq!(f, f.iter().collect::()); 13 assert_eq!( 14 TestFlags::from_bits_truncate(f.bits()), 15 f.iter_names().map(|(_, f)| f).collect::() 16 ); 17 18 let f = TestExternal::from_bits_retain(a | b); 19 20 assert_eq!(f, f.iter().collect::()); 21 } 22 } 23 } 24 25 mod collect { 26 use super::*; 27 28 #[test] 29 fn cases() { 30 assert_eq!(0, [].into_iter().collect::().bits()); 31 32 assert_eq!(1, [TestFlags::A,].into_iter().collect::().bits()); 33 34 assert_eq!( 35 1 | 1 << 1 | 1 << 2, 36 [TestFlags::A, TestFlags::B | TestFlags::C,] 37 .into_iter() 38 .collect::() 39 .bits() 40 ); 41 42 assert_eq!( 43 1 | 1 << 3, 44 [ 45 TestFlags::from_bits_retain(1 << 3), 46 TestFlags::empty(), 47 TestFlags::A, 48 ] 49 .into_iter() 50 .collect::() 51 .bits() 52 ); 53 54 assert_eq!( 55 1 << 5 | 1 << 7, 56 [ 57 TestExternal::empty(), 58 TestExternal::from_bits_retain(1 << 5), 59 TestExternal::from_bits_retain(1 << 7), 60 ] 61 .into_iter() 62 .collect::() 63 .bits() 64 ); 65 } 66 } 67 68 mod iter { 69 use super::*; 70 71 #[test] 72 fn cases() { 73 case(&[], TestFlags::empty(), TestFlags::iter); 74 75 case(&[1], TestFlags::A, TestFlags::iter); 76 case(&[1, 1 << 1], TestFlags::A | TestFlags::B, TestFlags::iter); 77 case( 78 &[1, 1 << 1, 1 << 3], 79 TestFlags::A | TestFlags::B | TestFlags::from_bits_retain(1 << 3), 80 TestFlags::iter, 81 ); 82 83 case(&[1, 1 << 1, 1 << 2], TestFlags::ABC, TestFlags::iter); 84 case( 85 &[1, 1 << 1, 1 << 2, 1 << 3], 86 TestFlags::ABC | TestFlags::from_bits_retain(1 << 3), 87 TestFlags::iter, 88 ); 89 90 case( 91 &[1 | 1 << 1 | 1 << 2], 92 TestFlagsInvert::ABC, 93 TestFlagsInvert::iter, 94 ); 95 96 case(&[], TestZero::ZERO, TestZero::iter); 97 98 case( 99 &[1, 1 << 1, 1 << 2, 0b1111_1000], 100 TestExternal::all(), 101 TestExternal::iter, 102 ); 103 } 104 105 #[track_caller] 106 fn case + Copy>( 107 expected: &[T::Bits], 108 value: T, 109 inherent: impl FnOnce(&T) -> crate::iter::Iter, 110 ) where 111 T::Bits: std::fmt::Debug + PartialEq, 112 { 113 assert_eq!( 114 expected, 115 inherent(&value).map(|f| f.bits()).collect::>(), 116 "{:?}.iter()", 117 value 118 ); 119 assert_eq!( 120 expected, 121 Flags::iter(&value).map(|f| f.bits()).collect::>(), 122 "Flags::iter({:?})", 123 value 124 ); 125 assert_eq!( 126 expected, 127 value.into_iter().map(|f| f.bits()).collect::>(), 128 "{:?}.into_iter()", 129 value 130 ); 131 } 132 } 133 134 mod iter_names { 135 use super::*; 136 137 #[test] 138 fn cases() { 139 case(&[], TestFlags::empty(), TestFlags::iter_names); 140 141 case(&[("A", 1)], TestFlags::A, TestFlags::iter_names); 142 case( 143 &[("A", 1), ("B", 1 << 1)], 144 TestFlags::A | TestFlags::B, 145 TestFlags::iter_names, 146 ); 147 case( 148 &[("A", 1), ("B", 1 << 1)], 149 TestFlags::A | TestFlags::B | TestFlags::from_bits_retain(1 << 3), 150 TestFlags::iter_names, 151 ); 152 153 case( 154 &[("A", 1), ("B", 1 << 1), ("C", 1 << 2)], 155 TestFlags::ABC, 156 TestFlags::iter_names, 157 ); 158 case( 159 &[("A", 1), ("B", 1 << 1), ("C", 1 << 2)], 160 TestFlags::ABC | TestFlags::from_bits_retain(1 << 3), 161 TestFlags::iter_names, 162 ); 163 164 case( 165 &[("ABC", 1 | 1 << 1 | 1 << 2)], 166 TestFlagsInvert::ABC, 167 TestFlagsInvert::iter_names, 168 ); 169 170 case(&[], TestZero::ZERO, TestZero::iter_names); 171 172 case( 173 &[("A", 1)], 174 TestOverlappingFull::A, 175 TestOverlappingFull::iter_names, 176 ); 177 case( 178 &[("A", 1), ("D", 1 << 1)], 179 TestOverlappingFull::A | TestOverlappingFull::D, 180 TestOverlappingFull::iter_names, 181 ); 182 } 183 184 #[track_caller] 185 fn case( 186 expected: &[(&'static str, T::Bits)], 187 value: T, 188 inherent: impl FnOnce(&T) -> crate::iter::IterNames, 189 ) where 190 T::Bits: std::fmt::Debug + PartialEq, 191 { 192 assert_eq!( 193 expected, 194 inherent(&value) 195 .map(|(n, f)| (n, f.bits())) 196 .collect::>(), 197 "{:?}.iter_names()", 198 value 199 ); 200 assert_eq!( 201 expected, 202 Flags::iter_names(&value) 203 .map(|(n, f)| (n, f.bits())) 204 .collect::>(), 205 "Flags::iter_names({:?})", 206 value 207 ); 208 } 209 } 210 211 mod iter_defined_names { 212 use crate::Flags; 213 214 #[test] 215 fn test_defined_names() { 216 bitflags! { 217 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 218 struct TestFlags: u32 { 219 const A = `0b00000001`; 220 const ZERO = 0; 221 const B = `0b00000010`; 222 const C = `0b00000100`; 223 const CC = Self::C.bits(); 224 const D = `0b10000100`; 225 const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits(); 226 const AB = Self::A.bits() | Self::B.bits(); 227 const AC = Self::A.bits() | Self::C.bits(); 228 const CB = Self::B.bits() | Self::C.bits(); 229 } 230 } 231 232 // Test all named flags produced by the iterator 233 let all_named: Vec<(&'static str, TestFlags)> = TestFlags::iter_defined_names().collect(); 234 235 // Verify all named flags are included 236 let expected_flags = vec![ 237 ("A", TestFlags::A), 238 ("ZERO", TestFlags::ZERO), 239 ("B", TestFlags::B), 240 ("C", TestFlags::C), 241 // Note: CC and C have the same bit value, but both are named flags 242 ("CC", TestFlags::CC), 243 ("D", TestFlags::D), 244 ("ABC", TestFlags::ABC), 245 ("AB", TestFlags::AB), 246 ("AC", TestFlags::AC), 247 ("CB", TestFlags::CB), 248 ]; 249 250 assert_eq!( 251 all_named.len(), 252 expected_flags.len(), 253 "Should have 10 named flags" 254 ); 255 256 // Verify each expected flag is in the result 257 for expected_flag in &expected_flags { 258 assert!( 259 all_named.contains(expected_flag), 260 "Missing flag: {:?}", 261 expected_flag 262 ); 263 } 264 265 // Test if iterator order is consistent with definition order 266 let flags_in_order: Vec<(&'static str, TestFlags)> = 267 TestFlags::iter_defined_names().collect(); 268 assert_eq!( 269 flags_in_order, expected_flags, 270 "Flag order should match definition order" 271 ); 272 273 // Test that iterator can be used multiple times 274 let first_iteration: Vec<(&'static str, TestFlags)> = 275 TestFlags::iter_defined_names().collect(); 276 let second_iteration: Vec<(&'static str, TestFlags)> = 277 TestFlags::iter_defined_names().collect(); 278 assert_eq!( 279 first_iteration, second_iteration, 280 "Multiple iterations should produce the same result" 281 ); 282 283 // Test consistency with FLAGS constant 284 let flags_from_iter: std::collections::HashSet = TestFlags::iter_defined_names() 285 .map(|(_, f)| f.bits()) 286 .collect(); 287 288 let flags_from_const: std::collections::HashSet = TestFlags::FLAGS 289 .iter() 290 .filter(|f| f.is_named()) 291 .map(|f| f.value().bits()) 292 .collect(); 293 294 assert_eq!( 295 flags_from_iter, flags_from_const, 296 "iter_defined_names() should be consistent with named flags in FLAGS" 297 ); 298 } 299 }