#[test] fn test_defined_names() {
bitflags! { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] struct TestFlags: u32 { const A = 0b00000001; const ZERO = 0; const B = 0b00000010; const C = 0b00000100; const CC = Self::C.bits(); const D = 0b10000100; const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits(); const AB = Self::A.bits() | Self::B.bits(); const AC = Self::A.bits() | Self::C.bits(); const CB = Self::B.bits() | Self::C.bits();
}
}
// Test all named flags produced by the iterator let all_named: Vec<(&'static str, TestFlags)> = TestFlags::iter_defined_names().collect();
// Verify all named flags are included let expected_flags = vec![
("A", TestFlags::A),
("ZERO", TestFlags::ZERO),
("B", TestFlags::B),
("C", TestFlags::C), // Note: CC and C have the same bit value, but both are named flags
("CC", TestFlags::CC),
("D", TestFlags::D),
("ABC", TestFlags::ABC),
("AB", TestFlags::AB),
("AC", TestFlags::AC),
("CB", TestFlags::CB),
];
assert_eq!(
all_named.len(),
expected_flags.len(), "Should have 10 named flags"
);
// Verify each expected flag is in the result for expected_flag in &expected_flags {
assert!(
all_named.contains(expected_flag), "Missing flag: {:?}",
expected_flag
);
}
// Test if iterator order is consistent with definition order let flags_in_order: Vec<(&'static str, TestFlags)> =
TestFlags::iter_defined_names().collect();
assert_eq!(
flags_in_order, expected_flags, "Flag order should match definition order"
);
// Test that iterator can be used multiple times let first_iteration: Vec<(&'static str, TestFlags)> =
TestFlags::iter_defined_names().collect(); let second_iteration: Vec<(&'static str, TestFlags)> =
TestFlags::iter_defined_names().collect();
assert_eq!(
first_iteration, second_iteration, "Multiple iterations should produce the same result"
);
// Test consistency with FLAGS constant let flags_from_iter: std::collections::HashSet<u32> = TestFlags::iter_defined_names()
.map(|(_, f)| f.bits())
.collect();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.