summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_index/src/vec/tests.rs
blob: cb0f0db220d97b6505b46030d497787eb6f53a81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![allow(dead_code)]

// Allows the macro invocation below to work
use crate as rustc_index;

rustc_macros::newtype_index! {
    #[max = 0xFFFF_FFFA]
    struct MyIdx {}
}

#[test]
fn index_size_is_optimized() {
    use std::mem::size_of;

    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)));
}