-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathlib.rs
More file actions
55 lines (44 loc) · 1.4 KB
/
lib.rs
File metadata and controls
55 lines (44 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2018 Developers of the Rand project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Crate to test WASM with the `wasm-bindgen` lib.
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png")]
extern crate getrandom;
extern crate wasm_bindgen;
extern crate wasm_bindgen_test;
use std::slice;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use getrandom::getrandom;
#[wasm_bindgen]
pub fn test_gen() -> i32 {
let mut int: i32 = 0;
unsafe {
let ptr = &mut int as *mut i32 as *mut u8;
let slice = slice::from_raw_parts_mut(ptr, 4);
getrandom(slice).unwrap();
}
int
}
#[wasm_bindgen_test]
fn test_call() {
let mut buf = [0u8; 0];
getrandom(&mut buf).unwrap();
}
#[wasm_bindgen_test]
fn test_diff() {
let mut v1 = [0u8; 1000];
getrandom(&mut v1).unwrap();
let mut v2 = [0u8; 1000];
getrandom(&mut v2).unwrap();
let mut n_diff_bits = 0;
for i in 0..v1.len() {
n_diff_bits += (v1[i] ^ v2[i]).count_ones();
}
// Check at least 1 bit per byte differs. p(failure) < 1e-1000 with random input.
assert!(n_diff_bits >= v1.len() as u32);
}