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 7.36 KB
Newer Older
1 2
extern crate mu;

3
use self::mu::ast::ir::MuEntityHeader;
4 5 6 7 8
use self::mu::ast::ptr::*;
use self::mu::ast::types::*;

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

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

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

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

#[test]
fn test_cyclic_struct() {
    // .typedef @cyclic_struct_ty = struct<ref<@cyclic_struct_ty> int<32>>
111 112 113
    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)));
114 115 116 117 118 119 120 121
    
    {
        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
122
    assert_type!(struct_ty, "struct<ref<MyStructTag2(struct)> int<32>>");
123 124 125 126 127 128
}

#[test]
fn test_is_traced() {
    let types = create_types();
    
129 130 131 132 133 134 135 136
    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);
137
    let struct3 = MuType::new(100, MuType_::mustruct("MyStructTag3".to_string(), vec![types[3].clone(), types[0].clone()]));
138
    assert_eq!(struct3.is_traced(), true);
139
    let struct4 = MuType::new(101, MuType_::mustruct("MyStructTag4".to_string(), vec![types[3].clone(), types[4].clone()]));
140 141
    assert_eq!(struct4.is_traced(), true);
    assert_eq!(types[8].is_traced(), false);
142
    let ref_array = MuType::new(102, MuType_::array(types[3].clone(), 5));
143 144
    assert_eq!(ref_array.is_traced(), true);
    assert_eq!(types[9].is_traced(), false);
145
    let fix_ref_hybrid = MuType::new(103, MuType_::hybrid("FixRefHybrid".to_string(), vec![types[3].clone(), types[0].clone()], types[0].clone()));
146
    assert_eq!(fix_ref_hybrid.is_traced(), true);
147
    let var_ref_hybrid = MuType::new(104, MuType_::hybrid("VarRefHybrid".to_string(), vec![types[0].clone(), types[1].clone()], types[3].clone()));
148 149 150 151 152 153 154 155
    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);
156 157 158 159 160 161
}

#[test]
fn test_is_native_safe() {
    let types = create_types();    
    
162 163 164 165 166 167 168 169
    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);
170
    let struct3 = MuType::new(100, MuType_::mustruct("MyStructTag3".to_string(), vec![types[3].clone(), types[0].clone()]));
171
    assert_eq!(struct3.is_native_safe(), false);
172
    let struct4 = MuType::new(101, MuType_::mustruct("MyStructTag4".to_string(), vec![types[3].clone(), types[4].clone()]));
173 174
    assert_eq!(struct4.is_native_safe(), false);
    assert_eq!(types[8].is_native_safe(), true);
175
    let ref_array = MuType::new(102, MuType_::array(types[3].clone(), 5));
176 177
    assert_eq!(ref_array.is_native_safe(), false);
    assert_eq!(types[9].is_native_safe(), true);
178
    let fix_ref_hybrid = MuType::new(103, MuType_::hybrid("FixRefHybrid".to_string(), vec![types[3].clone(), types[0].clone()], types[0].clone()));
179
    assert_eq!(fix_ref_hybrid.is_native_safe(), false);
180
    let var_ref_hybrid = MuType::new(104, MuType_::hybrid("VarRefHybrid".to_string(), vec![types[0].clone(), types[1].clone()], types[3].clone()));
181 182 183 184 185 186 187
    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
188
                                                      // and not traced either
189
    assert_eq!(types[16].is_native_safe(), true);
190
}