rust/compiler/rustc_index/src/vec/tests.rs
Thalia Archibald 38fad984c6 compiler: Use size_of from the prelude instead of imported
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the
prelude instead of importing or qualifying them.

These functions were added to all preludes in Rust 1.80.
2025-03-07 13:37:04 -08:00

56 lines
1.5 KiB
Rust

// Allows the macro invocation below to work
use crate as rustc_index;
crate::newtype_index! {
#[orderable]
#[max = 0xFFFF_FFFA]
struct MyIdx {}
}
#[test]
fn index_size_is_optimized() {
assert_eq!(size_of::<MyIdx>(), 4);
// Uses 0xFFFF_FFFB
assert_eq!(size_of::<Option<MyIdx>>(), 4);
// Uses 0xFFFF_FFFC
assert_eq!(size_of::<Option<Option<MyIdx>>>(), 4);
// Uses 0xFFFF_FFFD
assert_eq!(size_of::<Option<Option<Option<MyIdx>>>>(), 4);
// Uses 0xFFFF_FFFE
assert_eq!(size_of::<Option<Option<Option<Option<MyIdx>>>>>(), 4);
// Uses 0xFFFF_FFFF
assert_eq!(size_of::<Option<Option<Option<Option<Option<MyIdx>>>>>>(), 4);
// Uses a tag
assert_eq!(size_of::<Option<Option<Option<Option<Option<Option<MyIdx>>>>>>>(), 8);
}
#[test]
fn range_iterator_iterates_forwards() {
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
assert_eq!(
range.collect::<Vec<_>>(),
[MyIdx::from_u32(1), MyIdx::from_u32(2), MyIdx::from_u32(3)]
);
}
#[test]
fn range_iterator_iterates_backwards() {
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
assert_eq!(
range.rev().collect::<Vec<_>>(),
[MyIdx::from_u32(3), MyIdx::from_u32(2), MyIdx::from_u32(1)]
);
}
#[test]
fn range_count_is_correct() {
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
assert_eq!(range.count(), 3);
}
#[test]
fn range_size_hint_is_correct() {
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
assert_eq!(range.size_hint(), (3, Some(3)));
}