Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/rust/bitflags/src/tests/   (Firefox Browser Version 153.0.1©)  Datei vom 27.6.2026 mit Größe 2 kB image not shown  

Quelle  bitflags_match.rs

  Sprache: Rust
 

bitflags! {
    #[derive(PartialEq)]
    struct Flags: u8 {
        const A = 1 << 0;
        const B = 1 << 1;
        const C = 1 << 2;
        const D = 1 << 3;
    }
}

fn flag_to_string(flag: Flags) -> String {
    bitflags_match!(flag, {
        Flags::A => "A".to_string(),
        Flags::B => { "B".to_string() }
        Flags::C => "C".to_string(),
        Flags::D => "D".to_string(),
        Flags::A | Flags::B => "A or B".to_string(),
        Flags::A & Flags::B => { "A and B | empty".to_string() },
        Flags::A ^ Flags::B => "A xor B".to_string(),
        Flags::A | Flags::B | Flags::C => "A or B or C".to_string(),
        Flags::A & Flags::B & Flags::C => "A and B and C".to_string(),
        Flags::A ^ Flags::B ^ Flags::C => "A xor B xor C".to_string(),
        Flags::A | Flags::B | Flags::C | Flags::D => "All flags".to_string(),
        _ => "Unknown combination".to_string()
    })
}

#[test]
fn test_single_flags() {
    assert_eq!(flag_to_string(Flags::A), "A");
    assert_eq!(flag_to_string(Flags::B), "B");
    assert_eq!(flag_to_string(Flags::C), "C");
    assert_eq!(flag_to_string(Flags::D), "D");
}

#[test]
fn test_or_operations() {
    assert_eq!(flag_to_string(Flags::A | Flags::B), "A or B");
    assert_eq!(
        flag_to_string(Flags::A | Flags::B | Flags::C),
        "A or B or C"
    );
    assert_eq!(
        flag_to_string(Flags::A | Flags::B | Flags::C | Flags::D),
        "All flags"
    );
}

#[test]
fn test_and_operations() {
    assert_eq!(flag_to_string(Flags::A & Flags::A), "A");
    assert_eq!(flag_to_string(Flags::A & Flags::B), "A and B | empty");
    assert_eq!(
        flag_to_string(Flags::A & Flags::B & Flags::C),
        "A and B | empty"
    ); // Since A, B, and C are mutually exclusive, the result of A & B & C is 0 ==> A & B & C = 0000 (i.e., empty).
       // However, in the bitflags_match! statement (actually is if {..} else if {..} .. else {..}),
       // the "A & B = 0000" condition is listed first, so 0000 will match "A & B" first,
       // resulting in the output of the "A and B | empty" branch.
    assert_eq!(
        flag_to_string(Flags::A & Flags::B & Flags::C & Flags::D),
        "A and B | empty"
    );
}

#[test]
fn test_xor_operations() {
    assert_eq!(flag_to_string(Flags::A ^ Flags::B), "A or B"); // A | B = A ^ B == 0011
    assert_eq!(flag_to_string(Flags::A ^ Flags::A), "A and B | empty");
    assert_eq!(
        flag_to_string(Flags::A ^ Flags::B ^ Flags::C),
        "A or B or C"
    );
}

#[test]
fn test_complex_operations() {
    assert_eq!(flag_to_string(Flags::A | (Flags::B & Flags::C)), "A");
    assert_eq!(
        flag_to_string((Flags::A | Flags::B) & (Flags::B | Flags::C)),
        "B"
    );
    assert_eq!(
        flag_to_string(Flags::A ^ (Flags::B | Flags::C)),
        "A or B or C"
    );
}

#[test]
fn test_empty_and_full_flags() {
    assert_eq!(flag_to_string(Flags::empty()), "A and B | empty");
    assert_eq!(flag_to_string(Flags::all()), "All flags");
}

Messung V0.5 in Prozent
C=91 H=95 G=92

¤ Dauer der Verarbeitung: 0.11 Sekunden  (vorverarbeitet am  2026-08-25) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

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.