summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_data_structures/src/tagged_ptr/copy/tests.rs
blob: bfcc2e603de43cc433b7d8262e79095b0747eefb (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
use std::ptr;

use crate::stable_hasher::{HashStable, StableHasher};
use crate::tagged_ptr::{CopyTaggedPtr, Pointer, Tag, Tag2};

#[test]
fn smoke() {
    let value = 12u32;
    let reference = &value;
    let tag = Tag2::B01;

    let ptr = tag_ptr(reference, tag);

    assert_eq!(ptr.tag(), tag);
    assert_eq!(*ptr, 12);
    assert!(ptr::eq(ptr.pointer(), reference));

    let copy = ptr;

    let mut ptr = ptr;
    ptr.set_tag(Tag2::B00);
    assert_eq!(ptr.tag(), Tag2::B00);

    assert_eq!(copy.tag(), tag);
    assert_eq!(*copy, 12);
    assert!(ptr::eq(copy.pointer(), reference));
}

#[test]
fn stable_hash_hashes_as_tuple() {
    let hash_packed = {
        let mut hasher = StableHasher::new();
        tag_ptr(&12, Tag2::B11).hash_stable(&mut (), &mut hasher);

        hasher.finalize()
    };

    let hash_tupled = {
        let mut hasher = StableHasher::new();
        (&12, Tag2::B11).hash_stable(&mut (), &mut hasher);
        hasher.finalize()
    };

    assert_eq!(hash_packed, hash_tupled);
}

/// Helper to create tagged pointers without specifying `COMPARE_PACKED` if it does not matter.
fn tag_ptr<P: Pointer, T: Tag>(ptr: P, tag: T) -> CopyTaggedPtr<P, T, true> {
    CopyTaggedPtr::new(ptr, tag)
}