| 1 | #[cfg (all(target_family = "wasm" , not(target_os = "wasi" )))] |
| 2 | use wasm_bindgen_test::wasm_bindgen_test as test; |
| 3 | |
| 4 | #[cfg (all(target_family = "wasm" , not(target_os = "wasi" )))] |
| 5 | wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); |
| 6 | |
| 7 | #[test] |
| 8 | fn bool() { |
| 9 | for x in &[false, true] { |
| 10 | while fastrand::bool() != *x {} |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | #[test] |
| 15 | fn u8() { |
| 16 | for x in 0..10 { |
| 17 | while fastrand::u8(..10) != x {} |
| 18 | } |
| 19 | |
| 20 | for x in 200..=u8::MAX { |
| 21 | while fastrand::u8(200..) != x {} |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | #[test] |
| 26 | fn i8() { |
| 27 | for x in -128..-120 { |
| 28 | while fastrand::i8(..-120) != x {} |
| 29 | } |
| 30 | |
| 31 | for x in 120..=127 { |
| 32 | while fastrand::i8(120..) != x {} |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn u32() { |
| 38 | for n in 1u32..10_000 { |
| 39 | let n = n.wrapping_mul(n); |
| 40 | let n = n.wrapping_mul(n); |
| 41 | if n != 0 { |
| 42 | for _ in 0..1000 { |
| 43 | assert!(fastrand::u32(..n) < n); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn u64() { |
| 51 | for n in 1u64..10_000 { |
| 52 | let n = n.wrapping_mul(n); |
| 53 | let n = n.wrapping_mul(n); |
| 54 | let n = n.wrapping_mul(n); |
| 55 | if n != 0 { |
| 56 | for _ in 0..1000 { |
| 57 | assert!(fastrand::u64(..n) < n); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn u128() { |
| 65 | for n in 1u128..10_000 { |
| 66 | let n = n.wrapping_mul(n); |
| 67 | let n = n.wrapping_mul(n); |
| 68 | let n = n.wrapping_mul(n); |
| 69 | let n = n.wrapping_mul(n); |
| 70 | if n != 0 { |
| 71 | for _ in 0..1000 { |
| 72 | assert!(fastrand::u128(..n) < n); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn fill() { |
| 80 | let mut r = fastrand::Rng::new(); |
| 81 | let mut a = [0u8; 64]; |
| 82 | let mut b = [0u8; 64]; |
| 83 | |
| 84 | r.fill(&mut a); |
| 85 | r.fill(&mut b); |
| 86 | |
| 87 | assert_ne!(a, b); |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn rng() { |
| 92 | let mut r = fastrand::Rng::new(); |
| 93 | |
| 94 | assert_ne!(r.u64(..), r.u64(..)); |
| 95 | |
| 96 | r.seed(7); |
| 97 | let a = r.u64(..); |
| 98 | r.seed(7); |
| 99 | let b = r.u64(..); |
| 100 | assert_eq!(a, b); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn rng_init() { |
| 105 | let mut a = fastrand::Rng::new(); |
| 106 | let mut b = fastrand::Rng::new(); |
| 107 | assert_ne!(a.u64(..), b.u64(..)); |
| 108 | |
| 109 | a.seed(7); |
| 110 | b.seed(7); |
| 111 | assert_eq!(a.u64(..), b.u64(..)); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn with_seed() { |
| 116 | let mut a = fastrand::Rng::with_seed(7); |
| 117 | let mut b = fastrand::Rng::new(); |
| 118 | b.seed(7); |
| 119 | assert_eq!(a.u64(..), b.u64(..)); |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn choose_multiple() { |
| 124 | let mut a = fastrand::Rng::new(); |
| 125 | let mut elements = (0..20).collect::<Vec<_>>(); |
| 126 | |
| 127 | while !elements.is_empty() { |
| 128 | let chosen = a.choose_multiple(0..20, 5); |
| 129 | for &x in &chosen { |
| 130 | elements.retain(|&y| y != x); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn choice() { |
| 137 | let items = [1, 4, 9, 5, 2, 3, 6, 7, 8, 0]; |
| 138 | let mut r = fastrand::Rng::new(); |
| 139 | |
| 140 | for item in &items { |
| 141 | while r.choice(&items).unwrap() != item {} |
| 142 | } |
| 143 | } |
| 144 | |