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

test_types.rs 7.96 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright 2017 The Australian National University
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
16
extern crate mu;

17
use self::mu::ast::ir::MuEntityHeader;
18
19
20
21
22
use self::mu::ast::ptr::*;
use self::mu::ast::types::*;

macro_rules! assert_type (
    ($test:expr, $expect: expr) => (
23
        assert_eq!(format!("{}", $test), $expect)
24
25
26
27
28
    )
);

macro_rules! println_type (
    ($test:expr) => (
29
        println!("{}", $test)
30
31
32
    )  
);

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

#[test]
#[allow(unused_variables)]
fn test_type_constructors() {
    let types = create_types();
    
qinsoon's avatar
qinsoon committed
98
99
100
101
102
103
104
105
    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)");
106
107
108
    {
        let map = STRUCT_TAG_MAP.read().unwrap();
        let t7_struct_ty = map.get("MyStructTag1").unwrap();
qinsoon's avatar
qinsoon committed
109
        assert_type!(t7_struct_ty, "struct<int<8> float>");
110
    }
qinsoon's avatar
qinsoon committed
111
    assert_type!(*types[8], "array<int<8> 5>");
112
    assert_type!(*types[9], "MyHybridTag1(hybrid)");
qinsoon's avatar
qinsoon committed
113
114
115
116
117
    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
118
119
    assert_type!(*types[15], "funcref<[int<8>, int<8>] -> [void]>");
    assert_type!(*types[16], "ufuncref<[int<8>, int<8>] -> [void]>");
120
121
122
123
124
}

#[test]
fn test_cyclic_struct() {
    // .typedef @cyclic_struct_ty = struct<ref<@cyclic_struct_ty> int<32>>
125
126
127
    let ty = P(MuType::new(0, MuType_::mustruct_empty("MyStructTag2".to_string())));
    let ref_ty = P(MuType::new(1, MuType_::muref(ty.clone())));
    let i32_ty = P(MuType::new(2, MuType_::int(32)));
128
129
130
131
132
133
134
135
    
    {
        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
136
    assert_type!(struct_ty, "struct<ref<MyStructTag2(struct)> int<32>>");
137
138
139
140
141
142
}

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

#[test]
fn test_is_native_safe() {
    let types = create_types();    
    
176
177
178
179
180
181
182
183
    assert_eq!(types[0].is_native_safe(), true);
    assert_eq!(types[1].is_native_safe(), true);
    assert_eq!(types[2].is_native_safe(), true);
    assert_eq!(types[3].is_native_safe(), false);
    assert_eq!(types[4].is_native_safe(), false);
    assert_eq!(types[5].is_native_safe(), false);
    assert_eq!(types[6].is_native_safe(), true);
    assert_eq!(types[7].is_native_safe(), true);
184
    let struct3 = MuType::new(100, MuType_::mustruct("MyStructTag3".to_string(), vec![types[3].clone(), types[0].clone()]));
185
    assert_eq!(struct3.is_native_safe(), false);
186
    let struct4 = MuType::new(101, MuType_::mustruct("MyStructTag4".to_string(), vec![types[3].clone(), types[4].clone()]));
187
188
    assert_eq!(struct4.is_native_safe(), false);
    assert_eq!(types[8].is_native_safe(), true);
189
    let ref_array = MuType::new(102, MuType_::array(types[3].clone(), 5));
190
191
    assert_eq!(ref_array.is_native_safe(), false);
    assert_eq!(types[9].is_native_safe(), true);
192
    let fix_ref_hybrid = MuType::new(103, MuType_::hybrid("FixRefHybrid".to_string(), vec![types[3].clone(), types[0].clone()], types[0].clone()));
193
    assert_eq!(fix_ref_hybrid.is_native_safe(), false);
194
    let var_ref_hybrid = MuType::new(104, MuType_::hybrid("VarRefHybrid".to_string(), vec![types[0].clone(), types[1].clone()], types[3].clone()));
195
196
197
198
199
200
201
    assert_eq!(var_ref_hybrid.is_native_safe(), false);
    assert_eq!(types[10].is_native_safe(), true);
    assert_eq!(types[11].is_native_safe(), false);
    assert_eq!(types[12].is_native_safe(), false);
    assert_eq!(types[13].is_native_safe(), false);
    assert_eq!(types[14].is_native_safe(), true);
    assert_eq!(types[15].is_native_safe(), false);    // funcref is not native safe
202
                                                      // and not traced either
203
    assert_eq!(types[16].is_native_safe(), true);
204
}