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

test_types.rs 6.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
extern crate mu;

use self::mu::ast::ptr::*;
use self::mu::ast::types::*;

macro_rules! assert_type (
    ($test:expr, $expect: expr) => (
        assert_eq!(format!("{:?}", $test), $expect)
    )
);

macro_rules! println_type (
    ($test:expr) => (
        println!("{:?}", $test)
    )  
);

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

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

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

#[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
136
    let struct3 = MuType::mustruct("MyStructTag3", vec![types[3].clone(), types[0].clone()]);
137
    assert_eq!(is_traced(&struct3), true);
qinsoon's avatar
qinsoon committed
138
    let struct4 = MuType::mustruct("MyStructTag4", vec![types[3].clone(), types[4].clone()]);
139
140
    assert_eq!(is_traced(&struct4), true);
    assert_eq!(is_traced(&types[8]), false);
qinsoon's avatar
qinsoon committed
141
    let ref_array = MuType::array(types[3].clone(), 5);
142
143
    assert_eq!(is_traced(&ref_array), true);
    assert_eq!(is_traced(&types[9]), false);
qinsoon's avatar
qinsoon committed
144
    let fix_ref_hybrid = MuType::hybrid(vec![types[3].clone(), types[0].clone()], types[0].clone());
145
    assert_eq!(is_traced(&fix_ref_hybrid), true);
qinsoon's avatar
qinsoon committed
146
    let var_ref_hybrid = MuType::hybrid(vec![types[0].clone(), types[1].clone()], types[3].clone());
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    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
169
    let struct3 = MuType::mustruct("MyStructTag3", vec![types[3].clone(), types[0].clone()]);
170
    assert_eq!(is_native_safe(&struct3), false);
qinsoon's avatar
qinsoon committed
171
    let struct4 = MuType::mustruct("MyStructTag4", vec![types[3].clone(), types[4].clone()]);
172
173
    assert_eq!(is_native_safe(&struct4), false);
    assert_eq!(is_native_safe(&types[8]), true);
qinsoon's avatar
qinsoon committed
174
    let ref_array = MuType::array(types[3].clone(), 5);
175
176
    assert_eq!(is_native_safe(&ref_array), false);
    assert_eq!(is_native_safe(&types[9]), true);
qinsoon's avatar
qinsoon committed
177
    let fix_ref_hybrid = MuType::hybrid(vec![types[3].clone(), types[0].clone()], types[0].clone());
178
    assert_eq!(is_native_safe(&fix_ref_hybrid), false);
qinsoon's avatar
qinsoon committed
179
    let var_ref_hybrid = MuType::hybrid(vec![types[0].clone(), types[1].clone()], types[3].clone());
180
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);
}