To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

test_types.rs 7.24 KB
Newer Older
1
2
extern crate mu;

3
use self::mu::ast::ir::MuEntityHeader;
4
5
6
7
8
use self::mu::ast::ptr::*;
use self::mu::ast::types::*;

macro_rules! assert_type (
    ($test:expr, $expect: expr) => (
9
        assert_eq!(format!("{}", $test), $expect)
10
11
12
13
14
    )
);

macro_rules! println_type (
    ($test:expr) => (
15
        println!("{}", $test)
16
17
18
    )  
);

qinsoon's avatar
qinsoon committed
19
20
/// create one of each MuType
fn create_types() -> Vec<P<MuType>> {
21
22
    let mut types = vec![];
    
qinsoon's avatar
qinsoon committed
23
    let t0 = MuType::new(0, MuType_::int(8));
24
25
    types.push(P(t0));
    
qinsoon's avatar
qinsoon committed
26
    let t1 = MuType::new(1, MuType_::float());
27
28
    types.push(P(t1));
    
qinsoon's avatar
qinsoon committed
29
    let t2 = MuType::new(2, MuType_::double());
30
31
    types.push(P(t2));
    
qinsoon's avatar
qinsoon committed
32
    let t3 = MuType::new(3, MuType_::muref(types[0].clone()));
33
34
    types.push(P(t3));
    
qinsoon's avatar
qinsoon committed
35
    let t4 = MuType::new(4, MuType_::iref(types[0].clone()));
36
37
    types.push(P(t4));
    
qinsoon's avatar
qinsoon committed
38
    let t5 = MuType::new(5, MuType_::weakref(types[0].clone()));
39
40
    types.push(P(t5));
    
qinsoon's avatar
qinsoon committed
41
    let t6 = MuType::new(6, MuType_::uptr(types[0].clone()));
42
43
    types.push(P(t6));
    
44
    let t7 = MuType::new(7, MuType_::mustruct("MyStructTag1".to_string(), vec![types[0].clone(), types[1].clone()]));
45
46
    types.push(P(t7));
    
qinsoon's avatar
qinsoon committed
47
    let t8 = MuType::new(8, MuType_::array(types[0].clone(), 5));
48
49
    types.push(P(t8));
    
qinsoon's avatar
qinsoon committed
50
    let t9 = MuType::new(9, MuType_::hybrid(vec![types[7].clone(), types[1].clone()], types[0].clone()));
51
52
    types.push(P(t9));
    
qinsoon's avatar
qinsoon committed
53
    let t10 = MuType::new(10, MuType_::void());
54
55
    types.push(P(t10));
    
qinsoon's avatar
qinsoon committed
56
    let t11 = MuType::new(11, MuType_::threadref());
57
58
    types.push(P(t11));
    
qinsoon's avatar
qinsoon committed
59
    let t12 = MuType::new(12, MuType_::stackref());
60
61
    types.push(P(t12));
    
qinsoon's avatar
qinsoon committed
62
    let t13 = MuType::new(13, MuType_::tagref64());
63
64
    types.push(P(t13));
    
qinsoon's avatar
qinsoon committed
65
    let t14 = MuType::new(14, MuType_::vector(types[0].clone(), 5));
66
67
    types.push(P(t14));
    
68
    let sig = P(MuFuncSig{hdr: MuEntityHeader::unnamed(20), ret_tys: vec![types[10].clone()], arg_tys: vec![types[0].clone(), types[0].clone()]});
69
    
qinsoon's avatar
qinsoon committed
70
    let t15 = MuType::new(15, MuType_::funcref(sig.clone()));
71
72
    types.push(P(t15));
    
qinsoon's avatar
qinsoon committed
73
    let t16 = MuType::new(16, MuType_::ufuncptr(sig.clone()));
74
75
76
77
78
79
80
81
82
83
    types.push(P(t16));
    
    types
}

#[test]
#[allow(unused_variables)]
fn test_type_constructors() {
    let types = create_types();
    
qinsoon's avatar
qinsoon committed
84
85
86
87
88
89
90
91
    assert_type!(*types[0], "int<8>");
    assert_type!(*types[1], "float");
    assert_type!(*types[2], "double");
    assert_type!(*types[3], "ref<int<8>>");
    assert_type!(*types[4], "iref<int<8>>");
    assert_type!(*types[5], "weakref<int<8>>");
    assert_type!(*types[6], "uptr<int<8>>");
    assert_type!(*types[7], "MyStructTag1(struct)");
92
93
94
    {
        let map = STRUCT_TAG_MAP.read().unwrap();
        let t7_struct_ty = map.get("MyStructTag1").unwrap();
qinsoon's avatar
qinsoon committed
95
        assert_type!(t7_struct_ty, "struct<int<8> float>");
96
    }
qinsoon's avatar
qinsoon committed
97
98
99
100
101
102
103
104
105
    assert_type!(*types[8], "array<int<8> 5>");
    assert_type!(*types[9], "hybrid<[MyStructTag1(struct), float] int<8>>");
    assert_type!(*types[10], "void");
    assert_type!(*types[11], "threadref");
    assert_type!(*types[12], "stackref");
    assert_type!(*types[13], "tagref64");
    assert_type!(*types[14], "vector<int<8> 5>");
    assert_type!(*types[15], "funcref<[void] -> [int<8>, int<8>]>");
    assert_type!(*types[16], "ufuncref<[void] -> [int<8>, int<8>]>");
106
107
108
109
110
}

#[test]
fn test_cyclic_struct() {
    // .typedef @cyclic_struct_ty = struct<ref<@cyclic_struct_ty> int<32>>
111
    let ty = P(MuType::new(0, MuType_::mustruct_empty("MyStructTag2".to_string())));
qinsoon's avatar
qinsoon committed
112
113
    let ref_ty = P(MuType::new(1, MuType_::muref(ty.clone())));
    let i32_ty = P(MuType::new(2, MuType_::int(32)));
114
115
116
117
118
119
120
121
    
    {
        STRUCT_TAG_MAP.write().unwrap().
            get_mut("MyStructTag2").unwrap().set_tys(vec![ref_ty.clone(), i32_ty.clone()]);
    }
    
    let map = STRUCT_TAG_MAP.read().unwrap();
    let struct_ty = map.get("MyStructTag2").unwrap();
qinsoon's avatar
qinsoon committed
122
    assert_type!(struct_ty, "struct<ref<MyStructTag2(struct)> int<32>>");
123
124
125
126
127
128
129
130
131
132
133
134
135
136
}

#[test]
fn test_is_traced() {
    let types = create_types();
    
    assert_eq!(is_traced(&types[0]), false);
    assert_eq!(is_traced(&types[1]), false);
    assert_eq!(is_traced(&types[2]), false);
    assert_eq!(is_traced(&types[3]), true);
    assert_eq!(is_traced(&types[4]), true);
    assert_eq!(is_traced(&types[5]), true);
    assert_eq!(is_traced(&types[6]), false);
    assert_eq!(is_traced(&types[7]), false);
137
    let struct3 = MuType::new(100, MuType_::mustruct("MyStructTag3".to_string(), vec![types[3].clone(), types[0].clone()]));
138
    assert_eq!(is_traced(&struct3), true);
139
    let struct4 = MuType::new(101, MuType_::mustruct("MyStructTag4".to_string(), vec![types[3].clone(), types[4].clone()]));
140
141
    assert_eq!(is_traced(&struct4), true);
    assert_eq!(is_traced(&types[8]), false);
qinsoon's avatar
qinsoon committed
142
    let ref_array = MuType::new(102, MuType_::array(types[3].clone(), 5));
143
144
    assert_eq!(is_traced(&ref_array), true);
    assert_eq!(is_traced(&types[9]), false);
qinsoon's avatar
qinsoon committed
145
    let fix_ref_hybrid = MuType::new(103, MuType_::hybrid(vec![types[3].clone(), types[0].clone()], types[0].clone()));
146
    assert_eq!(is_traced(&fix_ref_hybrid), true);
qinsoon's avatar
qinsoon committed
147
    let var_ref_hybrid = MuType::new(104, MuType_::hybrid(vec![types[0].clone(), types[1].clone()], types[3].clone()));
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    assert_eq!(is_traced(&var_ref_hybrid), true);
    assert_eq!(is_traced(&types[10]), false);
    assert_eq!(is_traced(&types[11]), true);
    assert_eq!(is_traced(&types[12]), true);
    assert_eq!(is_traced(&types[13]), true);
    assert_eq!(is_traced(&types[14]), false);
    assert_eq!(is_traced(&types[15]), false);
    assert_eq!(is_traced(&types[16]), false);
}

#[test]
fn test_is_native_safe() {
    let types = create_types();    
    
    assert_eq!(is_native_safe(&types[0]), true);
    assert_eq!(is_native_safe(&types[1]), true);
    assert_eq!(is_native_safe(&types[2]), true);
    assert_eq!(is_native_safe(&types[3]), false);
    assert_eq!(is_native_safe(&types[4]), false);
    assert_eq!(is_native_safe(&types[5]), false);
    assert_eq!(is_native_safe(&types[6]), true);
    assert_eq!(is_native_safe(&types[7]), true);
170
    let struct3 = MuType::new(100, MuType_::mustruct("MyStructTag3".to_string(), vec![types[3].clone(), types[0].clone()]));
171
    assert_eq!(is_native_safe(&struct3), false);
172
    let struct4 = MuType::new(101, MuType_::mustruct("MyStructTag4".to_string(), vec![types[3].clone(), types[4].clone()]));
173
174
    assert_eq!(is_native_safe(&struct4), false);
    assert_eq!(is_native_safe(&types[8]), true);
qinsoon's avatar
qinsoon committed
175
    let ref_array = MuType::new(102, MuType_::array(types[3].clone(), 5));
176
177
    assert_eq!(is_native_safe(&ref_array), false);
    assert_eq!(is_native_safe(&types[9]), true);
qinsoon's avatar
qinsoon committed
178
    let fix_ref_hybrid = MuType::new(103, MuType_::hybrid(vec![types[3].clone(), types[0].clone()], types[0].clone()));
179
    assert_eq!(is_native_safe(&fix_ref_hybrid), false);
qinsoon's avatar
qinsoon committed
180
    let var_ref_hybrid = MuType::new(104, MuType_::hybrid(vec![types[0].clone(), types[1].clone()], types[3].clone()));
181
182
183
184
185
186
187
188
189
    assert_eq!(is_native_safe(&var_ref_hybrid), false);
    assert_eq!(is_native_safe(&types[10]), true);
    assert_eq!(is_native_safe(&types[11]), false);
    assert_eq!(is_native_safe(&types[12]), false);
    assert_eq!(is_native_safe(&types[13]), false);
    assert_eq!(is_native_safe(&types[14]), true);
    assert_eq!(is_native_safe(&types[15]), false);    // funcref is not native safe
                                                      // and not traced either
    assert_eq!(is_native_safe(&types[16]), true);
190
}