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

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

qinsoon's avatar
qinsoon committed
3
use self::mu::ast::ir::*;
4
use self::mu::ast::ir::MuEntityHeader;
5
6
7
8
9
use self::mu::ast::ptr::*;
use self::mu::ast::types::*;

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

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

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

#[test]
#[allow(unused_variables)]
fn test_type_constructors() {
    let types = create_types();
    
qinsoon's avatar
qinsoon committed
85
86
87
88
89
90
91
92
    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)");
93
94
95
    {
        let map = STRUCT_TAG_MAP.read().unwrap();
        let t7_struct_ty = map.get("MyStructTag1").unwrap();
qinsoon's avatar
qinsoon committed
96
        assert_type!(t7_struct_ty, "struct<int<8> float>");
97
    }
qinsoon's avatar
qinsoon committed
98
    assert_type!(*types[8], "array<int<8> 5>");
99
    assert_type!(*types[9], "MyHybridTag1(hybrid)");
qinsoon's avatar
qinsoon committed
100
101
102
103
104
    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>");
qinsoon's avatar
qinsoon committed
105
106
    assert_type!(*types[15], "funcref<[int<8>, int<8>] -> [void]>");
    assert_type!(*types[16], "ufuncref<[int<8>, int<8>] -> [void]>");
107
108
109
110
111
}

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

#[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);
qinsoon's avatar
qinsoon committed
138
    let struct3 = MuType::new(MuID(100), MuType_::mustruct("MyStructTag3".to_string(), vec![types[3].clone(), types[0].clone()]));
139
    assert_eq!(is_traced(&struct3), true);
qinsoon's avatar
qinsoon committed
140
    let struct4 = MuType::new(MuID(101), MuType_::mustruct("MyStructTag4".to_string(), vec![types[3].clone(), types[4].clone()]));
141
142
    assert_eq!(is_traced(&struct4), true);
    assert_eq!(is_traced(&types[8]), false);
qinsoon's avatar
qinsoon committed
143
    let ref_array = MuType::new(MuID(102), MuType_::array(types[3].clone(), 5));
144
145
    assert_eq!(is_traced(&ref_array), true);
    assert_eq!(is_traced(&types[9]), false);
qinsoon's avatar
qinsoon committed
146
    let fix_ref_hybrid = MuType::new(MuID(103), MuType_::hybrid("FixRefHybrid".to_string(), vec![types[3].clone(), types[0].clone()], types[0].clone()));
147
    assert_eq!(is_traced(&fix_ref_hybrid), true);
qinsoon's avatar
qinsoon committed
148
    let var_ref_hybrid = MuType::new(MuID(104), MuType_::hybrid("VarRefHybrid".to_string(), vec![types[0].clone(), types[1].clone()], types[3].clone()));
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    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);
qinsoon's avatar
qinsoon committed
171
172

    let struct3 = MuType::new(MuID(100), MuType_::mustruct("MyStructTag3".to_string(), vec![types[3].clone(), types[0].clone()]));
173
    assert_eq!(is_native_safe(&struct3), false);
qinsoon's avatar
qinsoon committed
174
175

    let struct4 = MuType::new(MuID(101), MuType_::mustruct("MyStructTag4".to_string(), vec![types[3].clone(), types[4].clone()]));
176
177
    assert_eq!(is_native_safe(&struct4), false);
    assert_eq!(is_native_safe(&types[8]), true);
qinsoon's avatar
qinsoon committed
178
179

    let ref_array = MuType::new(MuID(102), MuType_::array(types[3].clone(), 5));
180
181
    assert_eq!(is_native_safe(&ref_array), false);
    assert_eq!(is_native_safe(&types[9]), true);
qinsoon's avatar
qinsoon committed
182
183

    let fix_ref_hybrid = MuType::new(MuID(103), MuType_::hybrid("FixRefHybrid".to_string(), vec![types[3].clone(), types[0].clone()], types[0].clone()));
184
    assert_eq!(is_native_safe(&fix_ref_hybrid), false);
qinsoon's avatar
qinsoon committed
185
186

    let var_ref_hybrid = MuType::new(MuID(104), MuType_::hybrid("VarRefHybrid".to_string(), vec![types[0].clone(), types[1].clone()], types[3].clone()));
187
188
189
190
191
192
193
194
195
    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);
196
}