MCPcopy Create free account
hub / github.com/bitflags/bitflags / test_defined_names

Function test_defined_names

src/tests/iter.rs:215–298  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 4

iterMethod · 0.80
is_namedMethod · 0.80
valueMethod · 0.80
bitsMethod · 0.45

Tested by

no test coverage detected