Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

pub fn integers() {
    println!("-----------------------Testing Integers---------------------");
    let my_i32 = 256_i32; // This is 0x0000_0100

    assert_eq!(my_i32, 256_i32);
    assert_eq!(my_i32.isqrt(), 16_i32);
    assert_eq!(my_i32.pow(2), 256_i32 * 256_i32);
    assert_eq!(my_i32.swap_bytes(), 0x00010000_i32);
    assert_eq!(my_i32.reverse_bits(), 0x0080_0000_i32);
    assert_eq!(my_i32.rotate_left(1), 0x0000_0200_i32);

    assert_eq!(my_i32.checked_rem(16i32), Some(0));
    assert_eq!(0x80_u8.checked_rem(0x00_u8), None);
    // assert_eq!((-128i8).checked_rem(0xff_i8), None); This line will not compile. Rust first
    // attempts to fit 128 into an i8 which do not.
    assert_eq!(i8::MIN.checked_rem(-1), None);
    assert_eq!(my_i32.to_string(), "256");
    assert_eq!(my_i32.max(257_i32), 257_i32);
    assert_eq!(my_i32.count_ones() + my_i32.count_zeros(), 32_u32);
    assert_eq!(i32::MIN, -2 * 2i32.pow(30));
    assert_eq!(my_i32.leading_ones(), 0);
    assert_eq!(my_i32.leading_ones(), 0);
    assert_eq!(i32::MIN.to_string().parse(), Ok(i32::MIN));

    println!("All assesrtions PASSED");
}

fn main() {
    integers();
}