]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_index/src/vec/tests.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / compiler / rustc_index / src / vec / tests.rs
1 #![allow(dead_code)]
2
3 // Allows the macro invocation below to work
4 use crate as rustc_index;
5
6 rustc_macros::newtype_index! {
7     #[max = 0xFFFF_FFFA]
8     struct MyIdx {}
9 }
10
11 #[test]
12 fn index_size_is_optimized() {
13     use std::mem::size_of;
14
15     assert_eq!(size_of::<MyIdx>(), 4);
16     // Uses 0xFFFF_FFFB
17     assert_eq!(size_of::<Option<MyIdx>>(), 4);
18     // Uses 0xFFFF_FFFC
19     assert_eq!(size_of::<Option<Option<MyIdx>>>(), 4);
20     // Uses 0xFFFF_FFFD
21     assert_eq!(size_of::<Option<Option<Option<MyIdx>>>>(), 4);
22     // Uses 0xFFFF_FFFE
23     assert_eq!(size_of::<Option<Option<Option<Option<MyIdx>>>>>(), 4);
24     // Uses 0xFFFF_FFFF
25     assert_eq!(size_of::<Option<Option<Option<Option<Option<MyIdx>>>>>>(), 4);
26     // Uses a tag
27     assert_eq!(size_of::<Option<Option<Option<Option<Option<Option<MyIdx>>>>>>>(), 8);
28 }
29
30 #[test]
31 fn range_iterator_iterates_forwards() {
32     let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
33     assert_eq!(
34         range.collect::<Vec<_>>(),
35         [MyIdx::from_u32(1), MyIdx::from_u32(2), MyIdx::from_u32(3)]
36     );
37 }
38
39 #[test]
40 fn range_iterator_iterates_backwards() {
41     let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
42     assert_eq!(
43         range.rev().collect::<Vec<_>>(),
44         [MyIdx::from_u32(3), MyIdx::from_u32(2), MyIdx::from_u32(1)]
45     );
46 }
47
48 #[test]
49 fn range_count_is_correct() {
50     let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
51     assert_eq!(range.count(), 3);
52 }
53
54 #[test]
55 fn range_size_hint_is_correct() {
56     let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
57     assert_eq!(range.size_hint(), (3, Some(3)));
58 }