WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

test_types.rs 7.12 KB
Newer Older
1
2
3
4
5
6
7
extern crate mu;

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

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

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

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::new(0, MuType_::int(8));
23
24
    types.push(P(t0));
    
qinsoon's avatar
qinsoon committed
25
    let t1 = MuType::new(1, MuType_::float());
26
27
    types.push(P(t1));
    
qinsoon's avatar
qinsoon committed
28
    let t2 = MuType::new(2, MuType_::double());
29
30
    types.push(P(t2));
    
qinsoon's avatar
qinsoon committed
31
    let t3 = MuType::new(3, MuType_::muref(types[0].clone()));
32
33
    types.push(P(t3));
    
qinsoon's avatar
qinsoon committed
34
    let t4 = MuType::new(4, MuType_::iref(types[0].clone()));
35
36
    types.push(P(t4));
    
qinsoon's avatar
qinsoon committed
37
    let t5 = MuType::new(5, MuType_::weakref(types[0].clone()));
38
39
    types.push(P(t5));
    
qinsoon's avatar
qinsoon committed
40
    let t6 = MuType::new(6, MuType_::uptr(types[0].clone()));
41
42
    types.push(P(t6));
    
qinsoon's avatar
qinsoon committed
43
    let t7 = MuType::new(7, 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::new(8, MuType_::array(types[0].clone(), 5));
47
48
    types.push(P(t8));
    
qinsoon's avatar
qinsoon committed
49
    let t9 = MuType::new(9, 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::new(10, MuType_::void());
53
54
    types.push(P(t10));
    
qinsoon's avatar
qinsoon committed
55
    let t11 = MuType::new(11, MuType_::threadref());
56
57
    types.push(P(t11));
    
qinsoon's avatar
qinsoon committed
58
    let t12 = MuType::new(12, MuType_::stackref());
59
60
    types.push(P(t12));
    
qinsoon's avatar
qinsoon committed
61
    let t13 = MuType::new(13, MuType_::tagref64());
62
63
    types.push(P(t13));
    
qinsoon's avatar
qinsoon committed
64
    let t14 = MuType::new(14, MuType_::vector(types[0].clone(), 5));
65
66
    types.push(P(t14));
    
qinsoon's avatar
qinsoon committed
67
    let sig = P(MuFuncSig{id: 20, name: None, ret_tys: vec![types[10].clone()], arg_tys: vec![types[0].clone(), types[0].clone()]});
68
    
qinsoon's avatar
qinsoon committed
69
    let t15 = MuType::new(15, MuType_::funcref(sig.clone()));
70
71
    types.push(P(t15));
    
qinsoon's avatar
qinsoon committed
72
    let t16 = MuType::new(16, 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::new(0, MuType_::mustruct_empty("MyStructTag2")));
    let ref_ty = P(MuType::new(1, MuType_::muref(ty.clone())));
    let i32_ty = P(MuType::new(2, 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::new(100, 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::new(101, 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::new(102, 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::new(103, 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::new(104, 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::new(100, 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::new(101, 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::new(102, 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::new(103, 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::new(104, MuType_::hybrid(vec![types[0].clone(), types[1].clone()], types[3].clone()));
180
181
182
183
184
185
186
187
188
    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);
189
}