Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/rust/bindgen/codegen/   (Firefox Browser Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 8 kB image not shown  

Quelle  bitfield_unit_tests.rs

  Sprache: Rust
 

//! Tests for `__BindgenBitfieldUnit`.
//!
//! Note that bit-fields are allocated right to left (least to most significant
//! bits).
//!
//! From the x86 PS ABI:
//!
//! ```c
//! struct {
//!     int j : 5;
//!     int k : 6;
//!     int m : 7;
//! };
//! ```
//!
//! ```ignore
//! +------------------------------------------------------------+
//! |                     |              |            |          |
//! |       padding       |       m      |     k      |    j     |
//! |31                 18|17          11|10         5|4        0|
//! +------------------------------------------------------------+
//! ```

use super::bitfield_unit::__BindgenBitfieldUnit;

#[test]
fn bitfield_unit_get_bit() {
    let unit = __BindgenBitfieldUnit::<[u8; 2]>::new([0b10011101, 0b00011101]);

    let mut bits = vec![];
    for i in 0..16 {
        bits.push(unit.get_bit(i));
    }

    println!();
    println!("bits = {:?}", bits);
    assert_eq!(
        bits,
        &[
            // 0b10011101
            truefalsetruetruetruefalsefalsetrue,
            // 0b00011101
            truefalsetruetruetruefalsefalsefalse
        ]
    );
}

#[test]
fn bitfield_unit_set_bit() {
    let mut unit =
        __BindgenBitfieldUnit::<[u8; 2]>::new([0b00000000, 0b00000000]);

    for i in 0..16 {
        if i % 3 == 0 {
            unit.set_bit(i, true);
        }
    }

    for i in 0..16 {
        assert_eq!(unit.get_bit(i), i % 3 == 0);
    }

    let mut unit =
        __BindgenBitfieldUnit::<[u8; 2]>::new([0b11111111, 0b11111111]);

    for i in 0..16 {
        if i % 3 == 0 {
            unit.set_bit(i, false);
        }
    }

    for i in 0..16 {
        assert_eq!(unit.get_bit(i), i % 3 != 0);
    }
}

macro_rules! bitfield_unit_get {
    (
        $(
            With $storage:expr , then get($start:expr, $len:expr) is $expected:expr;
        )*
    ) => {
        #[test]
        fn bitfield_unit_get() {
            $({
                let expected = $expected;
                let unit = __BindgenBitfieldUnit::<_>::new($storage);
                let actual = unit.get($start, $len);

                println!();
                println!("expected = {:064b}", expected);
                println!("actual   = {:064b}", actual);

                assert_eq!(expected, actual);
            })*
        }
   }
}

bitfield_unit_get! {
    // Let's just exhaustively test getting the bits from a single byte, since
    // there are few enough combinations...

    With [0b11100010], then get(01) is 0;
    With [0b11100010], then get(11) is 1;
    With [0b11100010], then get(21) is 0;
    With [0b11100010], then get(31) is 0;
    With [0b11100010], then get(41) is 0;
    With [0b11100010], then get(51) is 1;
    With [0b11100010], then get(61) is 1;
    With [0b11100010], then get(71) is 1;

    With [0b11100010], then get(02) is 0b10;
    With [0b11100010], then get(12) is 0b01;
    With [0b11100010], then get(22) is 0b00;
    With [0b11100010], then get(32) is 0b00;
    With [0b11100010], then get(42) is 0b10;
    With [0b11100010], then get(52) is 0b11;
    With [0b11100010], then get(62) is 0b11;

    With [0b11100010], then get(03) is 0b010;
    With [0b11100010], then get(13) is 0b001;
    With [0b11100010], then get(23) is 0b000;
    With [0b11100010], then get(33) is 0b100;
    With [0b11100010], then get(43) is 0b110;
    With [0b11100010], then get(53) is 0b111;

    With [0b11100010], then get(04) is 0b0010;
    With [0b11100010], then get(14) is 0b0001;
    With [0b11100010], then get(24) is 0b1000;
    With [0b11100010], then get(34) is 0b1100;
    With [0b11100010], then get(44) is 0b1110;

    With [0b11100010], then get(05) is 0b00010;
    With [0b11100010], then get(15) is 0b10001;
    With [0b11100010], then get(25) is 0b11000;
    With [0b11100010], then get(35) is 0b11100;

    With [0b11100010], then get(06) is 0b100010;
    With [0b11100010], then get(16) is 0b110001;
    With [0b11100010], then get(26) is 0b111000;

    With [0b11100010], then get(07) is 0b1100010;
    With [0b11100010], then get(17) is 0b1110001;

    With [0b11100010], then get(08) is 0b11100010;

    // OK. Now let's test getting bits from across byte boundaries.

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(016) is 0b1111111101010101;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(116) is 0b0111111110101010;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(216) is 0b0011111111010101;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(316) is 0b0001111111101010;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(416) is 0b0000111111110101;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(516) is 0b0000011111111010;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(616) is 0b0000001111111101;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(716) is 0b0000000111111110;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(816) is 0b0000000011111111;
}

macro_rules! bitfield_unit_set {
    (
        $(
            set($start:expr, $len:expr, $val:expr) is $expected:expr;
        )*
    ) => {
        #[test]
        fn bitfield_unit_set() {
            $(
                let mut unit = __BindgenBitfieldUnit::<[u8; 4]>::new([0000]);
                unit.set($start, $len, $val);
                let actual = unit.get(032);

                println!();
                println!("set({}, {}, {:032b}", $start, $len, $val);
                println!("expected = {:064b}", $expected);
                println!("actual   = {:064b}", actual);

                assert_eq!($expected, actual);
            )*
        }
    }
}

bitfield_unit_set! {
    // Once again, let's exhaustively test single byte combinations.

    set(010b11111111) is 0b00000001;
    set(110b11111111) is 0b00000010;
    set(210b11111111) is 0b00000100;
    set(310b11111111) is 0b00001000;
    set(410b11111111) is 0b00010000;
    set(510b11111111) is 0b00100000;
    set(610b11111111) is 0b01000000;
    set(710b11111111) is 0b10000000;

    set(020b11111111) is 0b00000011;
    set(120b11111111) is 0b00000110;
    set(220b11111111) is 0b00001100;
    set(320b11111111) is 0b00011000;
    set(420b11111111) is 0b00110000;
    set(520b11111111) is 0b01100000;
    set(620b11111111) is 0b11000000;

    set(030b11111111) is 0b00000111;
    set(130b11111111) is 0b00001110;
    set(230b11111111) is 0b00011100;
    set(330b11111111) is 0b00111000;
    set(430b11111111) is 0b01110000;
    set(530b11111111) is 0b11100000;

    set(040b11111111) is 0b00001111;
    set(140b11111111) is 0b00011110;
    set(240b11111111) is 0b00111100;
    set(340b11111111) is 0b01111000;
    set(440b11111111) is 0b11110000;

    set(050b11111111) is 0b00011111;
    set(150b11111111) is 0b00111110;
    set(250b11111111) is 0b01111100;
    set(350b11111111) is 0b11111000;

    set(060b11111111) is 0b00111111;
    set(160b11111111) is 0b01111110;
    set(260b11111111) is 0b11111100;

    set(070b11111111) is 0b01111111;
    set(170b11111111) is 0b11111110;

    set(080b11111111) is 0b11111111;

    // And, now let's cross byte boundaries.

    set(0160b1111111111111111) is 0b00000000000000001111111111111111;
    set(1160b1111111111111111) is 0b00000000000000011111111111111110;
    set(2160b1111111111111111) is 0b00000000000000111111111111111100;
    set(3160b1111111111111111) is 0b00000000000001111111111111111000;
    set(4160b1111111111111111) is 0b00000000000011111111111111110000;
    set(5160b1111111111111111) is 0b00000000000111111111111111100000;
    set(6160b1111111111111111) is 0b00000000001111111111111111000000;
    set(7160b1111111111111111) is 0b00000000011111111111111110000000;
    set(8160b1111111111111111) is 0b00000000111111111111111100000000;
}

Messung V0.5 in Prozent
C=89 H=100 G=94

¤ Dauer der Verarbeitung: 0.13 Sekunden  (vorverarbeitet am  2026-06-24) ¤

*© 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.