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_tr64.rs 7.37 KB
Newer Older
1
// Copyright 2017 The Australian National University
2
//
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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
10
11
12
13
14
15
16
17
18
19
// 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.

use mu::vm::*;
use mu::vm::handle::*;
use mu::ast::types::*;
use mu::utils::Address;

20
use std::f64;
21
use std::mem::transmute;
22

23
24
25
26
27
28
29
30
31
32
/**
 * Helper functions to test VM:: methods with literal values
 *
 * Unfortunately these need to be called as e.g. &tr64(...)
 * due to the lifetime checker.
 */

fn tr64(val: u64) -> APIHandle {
    APIHandle {
        id: 0, // arbitrary
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
33
        v: APIHandleValue::TagRef64(val),
34
35
36
37
38
39
    }
}

fn double(val: f64) -> APIHandle {
    APIHandle {
        id: 0, // arbitrary
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
40
        v: APIHandleValue::Double(val),
41
42
43
44
45
46
    }
}

fn tag(val: u64) -> APIHandle {
    APIHandle {
        id: 0, // arbitrary
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
47
        v: APIHandleValue::Int(val, 6),
48
49
50
51
52
53
    }
}

fn int52(val: u64) -> APIHandle {
    APIHandle {
        id: 0, // arbitrary
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
54
        v: APIHandleValue::Int(val, 52),
55
56
57
    }
}

58
fn ref_void(val: u64) -> APIHandle {
59
60
    APIHandle {
        id: 0, // arbitrary
61
62
        v: APIHandleValue::Ref(REF_VOID_TYPE.clone(), unsafe {
            Address::from_usize(val as usize)
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
63
        }),
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    }
}

/**
 * These tests are translated from those in the reference implementation.
 * See: https://gitlab.anu.edu.au/mu/mu-impl-ref2/blob/master/src/test/scala/uvm/refimpl/itpr/UvmTagRef64OperationSpec.scala
 *
 * TODO: These test specific values. It would be nice if we had something
 * along the lines of QuickCheck properties, e.g. tr64ToInt . intToTr64 == id,
 * and ditto for floats except for NANs.
 */
#[test]
fn test_nan_with_suffix_1_is_integer() {
    let vm = VM::new();

    assert!(vm.handle_tr64_is_int(&tr64(0x7ff0000000000001u64)));
    assert!(vm.handle_tr64_is_int(&tr64(0xfff0000000000001u64)));
    assert!(vm.handle_tr64_is_int(&tr64(0xffffffffffffffffu64)));
}

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#[test]
fn test_nan_with_suffix_10_is_ref() {
    let vm = VM::new();

    assert!(vm.handle_tr64_is_ref(&tr64(0x7ff0000000000002u64)));
    assert!(vm.handle_tr64_is_ref(&tr64(0xfff0000000000002u64)));
    assert!(vm.handle_tr64_is_ref(&tr64(0xfffffffffffffffeu64)));
}

#[test]
fn test_other_bit_pattern_is_double() {
    let vm = VM::new();

    assert!(vm.handle_tr64_is_fp(&tr64(0x0u64)));
    assert!(vm.handle_tr64_is_fp(&tr64(0x123456789abcdef0u64)));
    assert!(vm.handle_tr64_is_fp(&tr64(0x7ff123456789abccu64)));
    assert!(vm.handle_tr64_is_fp(&tr64(0xfffffffffffffffcu64)));
101
102
103
    unsafe {
        assert!(vm.handle_tr64_is_fp(&tr64(transmute(3.1415927f64))));
    }
104
105
106
107
108
}

#[test]
fn test_encode_int() {
    let vm = VM::new();
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

    assert_eq!(
        vm.handle_tr64_from_int(&int52(0x0000000000000u64))
            .v
            .as_tr64(),
        0x7ff0000000000001u64
    );
    assert_eq!(
        vm.handle_tr64_from_int(&int52(0xfffffffffffffu64))
            .v
            .as_tr64(),
        0xffffffffffffffffu64
    );
    assert_eq!(
        vm.handle_tr64_from_int(&int52(0x5555555555555u64))
            .v
            .as_tr64(),
        0x7ffaaaaaaaaaaaabu64
    );
    assert_eq!(
        vm.handle_tr64_from_int(&int52(0xaaaaaaaaaaaaau64))
            .v
            .as_tr64(),
        0xfff5555555555555u64
    );
134
135
136
137
138
}

#[test]
fn test_encode_double() {
    let vm = VM::new();
139

140
    unsafe {
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
        assert_eq!(
            vm.handle_tr64_from_fp(&double(3.14_f64)).v.as_tr64(),
            transmute(3.14_f64)
        );
        assert_eq!(
            vm.handle_tr64_from_fp(&double(-3.14_f64)).v.as_tr64(),
            transmute(-3.14_f64)
        );
        assert_eq!(
            vm.handle_tr64_from_fp(&double(f64::INFINITY)).v.as_tr64(),
            0x7ff0000000000000u64
        );
        assert_eq!(
            vm.handle_tr64_from_fp(&double(transmute(0x7ff123456789abcdu64)))
                .v
                .as_tr64(),
            0x7ff0000000000008u64
        );
        assert!(
            transmute::<u64, f64>(
                (vm.handle_tr64_from_fp(&double(transmute(0x7ff123456789abcdu64)))
                     .v
                     .as_tr64())
            ).is_nan()
        );
166
    }
167
168
169
170
171
}

#[test]
fn test_encode_tagref() {
    let vm = VM::new();
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

    assert_eq!(
        vm.handle_tr64_from_ref(&ref_void(0x000000000000u64), &tag(0x00u64))
            .v
            .as_tr64(),
        0x7ff0000000000002u64
    );
    assert_eq!(
        vm.handle_tr64_from_ref(&ref_void(0x7ffffffffff8u64), &tag(0x00u64))
            .v
            .as_tr64(),
        0x7ff07ffffffffffau64
    );
    assert_eq!(
        vm.handle_tr64_from_ref(&ref_void(0xfffffffffffffff8u64), &tag(0x00u64))
            .v
            .as_tr64(),
        0xfff07ffffffffffau64
    );
    assert_eq!(
        vm.handle_tr64_from_ref(&ref_void(0x000000000000u64), &tag(0x3fu64))
            .v
            .as_tr64(),
        0x7fff800000000006u64
    );
197
198
199
200
201
}

#[test]
fn test_decode_integer() {
    let vm = VM::new();
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

    assert_eq!(
        vm.handle_tr64_to_int(&tr64(0x7ff0000000000001u64))
            .v
            .as_int(),
        0u64
    );
    assert_eq!(
        vm.handle_tr64_to_int(&tr64(0xfff0000000000001u64))
            .v
            .as_int(),
        0x8000000000000u64
    );
    assert_eq!(
        vm.handle_tr64_to_int(&tr64(0xfff5555555555555u64))
            .v
            .as_int(),
        0xaaaaaaaaaaaaau64
    );
    assert_eq!(
        vm.handle_tr64_to_int(&tr64(0x7ffaaaaaaaaaaaabu64))
            .v
            .as_int(),
        0x5555555555555u64
    );
227
228
229
230
231
}

#[test]
fn test_decode_double() {
    let vm = VM::new();
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

    assert_eq!(
        vm.handle_tr64_to_fp(&tr64(0x0000000000000000u64))
            .v
            .as_double(),
        0.0_f64
    );
    assert_eq!(
        vm.handle_tr64_to_fp(&tr64(0x8000000000000000u64))
            .v
            .as_double(),
        -0.0_f64
    );
    assert_eq!(
        vm.handle_tr64_to_fp(&tr64(0x3ff0000000000000u64))
            .v
            .as_double(),
        1.0_f64
    );
    assert!(
        vm.handle_tr64_to_fp(&tr64(0x7ff0000000000008))
            .v
            .as_double()
            .is_nan()
    );
257
258
259
260
261
262
}

#[test]
fn test_decode_tagref() {
    let vm = VM::new();

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    assert_eq!(
        vm.handle_tr64_to_ref(&tr64(0x7ff0555555555552u64))
            .v
            .as_ref()
            .1
            .as_usize() as u64,
        0x555555555550u64
    );
    assert_eq!(
        vm.handle_tr64_to_ref(&tr64(0xfff02aaaaaaaaaaau64))
            .v
            .as_ref()
            .1
            .as_usize() as u64,
        0xffffaaaaaaaaaaa8u64
    );
    assert_eq!(
        vm.handle_tr64_to_tag(&tr64(0x7ff0555555555552u64))
            .v
            .as_int(),
        0u64
    );
    assert_eq!(
        vm.handle_tr64_to_tag(&tr64(0x7fff800000000006u64))
            .v
            .as_int(),
        0x3fu64
    );
    assert_eq!(
        vm.handle_tr64_to_tag(&tr64(0x7ffa800000000002u64))
            .v
            .as_int(),
        0x2au64
    );
    assert_eq!(
        vm.handle_tr64_to_tag(&tr64(0x7ff5000000000006u64))
            .v
            .as_int(),
        0x15u64
    );
303
}