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.2% of users enabled 2FA.

test_types.rs 8.53 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
// Copyright 2017 The Australian National University
qinsoon's avatar
qinsoon committed
2
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
3 4 5
// 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
qinsoon's avatar
qinsoon committed
6
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
7
//     http://www.apache.org/licenses/LICENSE-2.0
qinsoon's avatar
qinsoon committed
8
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
9 10 11 12 13 14
// 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)
qinsoon's avatar
qinsoon committed
30
    )
31 32
);

qinsoon's avatar
qinsoon committed
33 34
/// create one of each MuType
fn create_types() -> Vec<P<MuType>> {
35
    let mut types = vec![];
qinsoon's avatar
qinsoon committed
36

37
    let t0 = MuType::new(0, MuType_::int(8));
38
    types.push(P(t0));
qinsoon's avatar
qinsoon committed
39

40
    let t1 = MuType::new(1, MuType_::float());
41
    types.push(P(t1));
qinsoon's avatar
qinsoon committed
42

43
    let t2 = MuType::new(2, MuType_::double());
44
    types.push(P(t2));
qinsoon's avatar
qinsoon committed
45

46
    let t3 = MuType::new(3, MuType_::muref(types[0].clone()));
47
    types.push(P(t3));
qinsoon's avatar
qinsoon committed
48

49
    let t4 = MuType::new(4, MuType_::iref(types[0].clone()));
50
    types.push(P(t4));
qinsoon's avatar
qinsoon committed
51

52
    let t5 = MuType::new(5, MuType_::weakref(types[0].clone()));
53
    types.push(P(t5));
qinsoon's avatar
qinsoon committed
54

55
    let t6 = MuType::new(6, MuType_::uptr(types[0].clone()));
56
    types.push(P(t6));
qinsoon's avatar
qinsoon committed
57 58 59 60 61 62 63 64

    let t7 = MuType::new(
        7,
        MuType_::mustruct(
            "MyStructTag1".to_string(),
            vec![types[0].clone(), types[1].clone()],
        ),
    );
65
    types.push(P(t7));
qinsoon's avatar
qinsoon committed
66

67
    let t8 = MuType::new(8, MuType_::array(types[0].clone(), 5));
68
    types.push(P(t8));
qinsoon's avatar
qinsoon committed
69 70 71 72 73 74 75 76 77

    let t9 = MuType::new(
        9,
        MuType_::hybrid(
            "MyHybridTag1".to_string(),
            vec![types[7].clone(), types[1].clone()],
            types[0].clone(),
        ),
    );
78
    types.push(P(t9));
qinsoon's avatar
qinsoon committed
79

80
    let t10 = MuType::new(10, MuType_::void());
81
    types.push(P(t10));
qinsoon's avatar
qinsoon committed
82

83
    let t11 = MuType::new(11, MuType_::threadref());
84
    types.push(P(t11));
qinsoon's avatar
qinsoon committed
85

86
    let t12 = MuType::new(12, MuType_::stackref());
87
    types.push(P(t12));
qinsoon's avatar
qinsoon committed
88

89
    let t13 = MuType::new(13, MuType_::tagref64());
90
    types.push(P(t13));
qinsoon's avatar
qinsoon committed
91

92
    let t14 = MuType::new(14, MuType_::vector(types[0].clone(), 5));
93
    types.push(P(t14));
qinsoon's avatar
qinsoon committed
94 95 96 97 98 99 100

    let sig = P(MuFuncSig {
        hdr: MuEntityHeader::unnamed(20),
        ret_tys: vec![types[10].clone()],
        arg_tys: vec![types[0].clone(), types[0].clone()],
    });

101
    let t15 = MuType::new(15, MuType_::funcref(sig.clone()));
102
    types.push(P(t15));
qinsoon's avatar
qinsoon committed
103

104
    let t16 = MuType::new(16, MuType_::ufuncptr(sig.clone()));
105
    types.push(P(t16));
qinsoon's avatar
qinsoon committed
106

107 108 109 110 111 112 113
    types
}

#[test]
#[allow(unused_variables)]
fn test_type_constructors() {
    let types = create_types();
qinsoon's avatar
qinsoon committed
114

qinsoon's avatar
qinsoon committed
115 116 117 118 119 120 121 122
    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)");
123 124 125
    {
        let map = STRUCT_TAG_MAP.read().unwrap();
        let t7_struct_ty = map.get("MyStructTag1").unwrap();
qinsoon's avatar
qinsoon committed
126
        assert_type!(t7_struct_ty, "struct<int<8> float>");
127
    }
qinsoon's avatar
qinsoon committed
128
    assert_type!(*types[8], "array<int<8> 5>");
129
    assert_type!(*types[9], "MyHybridTag1(hybrid)");
qinsoon's avatar
qinsoon committed
130 131 132 133 134
    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
135 136
    assert_type!(*types[15], "funcref<[int<8>, int<8>] -> [void]>");
    assert_type!(*types[16], "ufuncref<[int<8>, int<8>] -> [void]>");
137 138 139 140 141
}

#[test]
fn test_cyclic_struct() {
    // .typedef @cyclic_struct_ty = struct<ref<@cyclic_struct_ty> int<32>>
qinsoon's avatar
qinsoon committed
142 143 144 145
    let ty = P(MuType::new(
        0,
        MuType_::mustruct_empty("MyStructTag2".to_string()),
    ));
146 147
    let ref_ty = P(MuType::new(1, MuType_::muref(ty.clone())));
    let i32_ty = P(MuType::new(2, MuType_::int(32)));
qinsoon's avatar
qinsoon committed
148

149
    {
qinsoon's avatar
qinsoon committed
150 151 152 153 154 155
        STRUCT_TAG_MAP
            .write()
            .unwrap()
            .get_mut("MyStructTag2")
            .unwrap()
            .set_tys(vec![ref_ty.clone(), i32_ty.clone()]);
156
    }
qinsoon's avatar
qinsoon committed
157

158 159
    let map = STRUCT_TAG_MAP.read().unwrap();
    let struct_ty = map.get("MyStructTag2").unwrap();
qinsoon's avatar
qinsoon committed
160
    assert_type!(struct_ty, "struct<ref<MyStructTag2(struct)> int<32>>");
161 162 163 164 165
}

#[test]
fn test_is_traced() {
    let types = create_types();
qinsoon's avatar
qinsoon committed
166

167 168 169 170 171 172 173 174
    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);
qinsoon's avatar
qinsoon committed
175 176 177 178 179 180 181
    let struct3 = MuType::new(
        100,
        MuType_::mustruct(
            "MyStructTag3".to_string(),
            vec![types[3].clone(), types[0].clone()],
        ),
    );
182
    assert_eq!(struct3.is_traced(), true);
qinsoon's avatar
qinsoon committed
183 184 185 186 187 188 189
    let struct4 = MuType::new(
        101,
        MuType_::mustruct(
            "MyStructTag4".to_string(),
            vec![types[3].clone(), types[4].clone()],
        ),
    );
190 191
    assert_eq!(struct4.is_traced(), true);
    assert_eq!(types[8].is_traced(), false);
192
    let ref_array = MuType::new(102, MuType_::array(types[3].clone(), 5));
193 194
    assert_eq!(ref_array.is_traced(), true);
    assert_eq!(types[9].is_traced(), false);
qinsoon's avatar
qinsoon committed
195 196 197 198 199 200 201 202
    let fix_ref_hybrid = MuType::new(
        103,
        MuType_::hybrid(
            "FixRefHybrid".to_string(),
            vec![types[3].clone(), types[0].clone()],
            types[0].clone(),
        ),
    );
203
    assert_eq!(fix_ref_hybrid.is_traced(), true);
qinsoon's avatar
qinsoon committed
204 205 206 207 208 209 210 211
    let var_ref_hybrid = MuType::new(
        104,
        MuType_::hybrid(
            "VarRefHybrid".to_string(),
            vec![types[0].clone(), types[1].clone()],
            types[3].clone(),
        ),
    );
212 213 214 215 216 217 218 219
    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);
220 221 222 223
}

#[test]
fn test_is_native_safe() {
qinsoon's avatar
qinsoon committed
224 225
    let types = create_types();

226 227 228 229 230 231 232 233
    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);
qinsoon's avatar
qinsoon committed
234 235 236 237 238 239 240
    let struct3 = MuType::new(
        100,
        MuType_::mustruct(
            "MyStructTag3".to_string(),
            vec![types[3].clone(), types[0].clone()],
        ),
    );
241
    assert_eq!(struct3.is_native_safe(), false);
qinsoon's avatar
qinsoon committed
242 243 244 245 246 247 248
    let struct4 = MuType::new(
        101,
        MuType_::mustruct(
            "MyStructTag4".to_string(),
            vec![types[3].clone(), types[4].clone()],
        ),
    );
249 250
    assert_eq!(struct4.is_native_safe(), false);
    assert_eq!(types[8].is_native_safe(), true);
251
    let ref_array = MuType::new(102, MuType_::array(types[3].clone(), 5));
252 253
    assert_eq!(ref_array.is_native_safe(), false);
    assert_eq!(types[9].is_native_safe(), true);
qinsoon's avatar
qinsoon committed
254 255 256 257 258 259 260 261
    let fix_ref_hybrid = MuType::new(
        103,
        MuType_::hybrid(
            "FixRefHybrid".to_string(),
            vec![types[3].clone(), types[0].clone()],
            types[0].clone(),
        ),
    );
262
    assert_eq!(fix_ref_hybrid.is_native_safe(), false);
qinsoon's avatar
qinsoon committed
263 264 265 266 267 268 269 270
    let var_ref_hybrid = MuType::new(
        104,
        MuType_::hybrid(
            "VarRefHybrid".to_string(),
            vec![types[0].clone(), types[1].clone()],
            types[3].clone(),
        ),
    );
271 272 273 274 275 276
    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);
qinsoon's avatar
qinsoon committed
277 278
    assert_eq!(types[15].is_native_safe(), false); // funcref is not native safe
    // and not traced either
279
    assert_eq!(types[16].is_native_safe(), true);
280
}