oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1#[cfg(all(target_family = "wasm", not(target_os = "wasi")))]
2use wasm_bindgen_test::wasm_bindgen_test as test;
3
4#[cfg(all(target_family = "wasm", not(target_os = "wasi")))]
5wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
6
7#[test]
8fn bool() {
9 for x in &[false, true] {
10 while fastrand::bool() != *x {}
11 }
12}
13
14#[test]
15fn 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]
26fn 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]
37fn 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]
50fn 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]
64fn 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]
79fn f32() {
80 let mut r = fastrand::Rng::with_seed(0);
81 let tiny = (-24.0f32).exp2();
82 let mut count_tiny_nonzero = 0;
83 let mut count_top_half = 0;
84 for _ in 0..100_000_000 {
85 let x = r.f32();
86 assert!((0.0..1.0).contains(&x));
87 if x > 0.0 && x < tiny {
88 count_tiny_nonzero += 1;
89 } else if x > 0.5 {
90 count_top_half += 1;
91 }
92 }
93 assert!(count_top_half >= 49_000_000);
94 assert!(count_tiny_nonzero > 0);
95}
96
97#[test]
98fn f32_inclusive() {
99 let mut r = fastrand::Rng::with_seed(0);
100 let tiny = (-24.0f32).exp2();
101 let mut count_top_half = 0;
102 let mut count_tiny_nonzero = 0;
103 let mut count_one = 0;
104 for _ in 0..100_000_000 {
105 let x = r.f32_inclusive();
106 assert!((0.0..=1.0).contains(&x));
107 if x == 1.0 {
108 count_one += 1;
109 } else if x > 0.5 {
110 count_top_half += 1;
111 } else if x > 0.0 && x < tiny {
112 count_tiny_nonzero += 1;
113 }
114 }
115 assert!(count_top_half >= 49_000_000);
116 assert!(count_one > 0);
117 assert!(count_tiny_nonzero > 0);
118}
119
120#[test]
121fn f64() {
122 let mut r = fastrand::Rng::with_seed(0);
123 let mut count_top_half = 0;
124 for _ in 0..100_000_000 {
125 let x = r.f64();
126 assert!((0.0..1.0).contains(&x));
127 if x > 0.5 {
128 count_top_half += 1;
129 }
130 }
131 assert!(count_top_half >= 49_000_000);
132}
133
134#[test]
135fn f64_inclusive() {
136 let mut r = fastrand::Rng::with_seed(0);
137 let mut count_top_half = 0;
138 for _ in 0..100_000_000 {
139 let x = r.f64_inclusive();
140 assert!((0.0..=1.0).contains(&x));
141 if x > 0.5 {
142 count_top_half += 1;
143 }
144 }
145 assert!(count_top_half >= 49_000_000);
146}
147
148#[test]
149fn digit() {
150 for base in 1..36 {
151 let result = fastrand::digit(base);
152 assert!(result.is_ascii_digit() || result.is_ascii_lowercase());
153 }
154}
155
156#[test]
157fn global_rng_choice() {
158 let items = [1, 4, 9, 5, 2, 3, 6, 7, 8, 0];
159
160 for item in &items {
161 while fastrand::choice(&items).unwrap() != item {}
162 }
163}
164
165#[test]
166fn global_rng_alphabetic() {
167 for _ in 0..1000 {
168 let result = fastrand::alphabetic();
169 assert!(result.is_ascii_alphabetic())
170 }
171}
172
173#[test]
174fn global_rng_lowercase() {
175 for _ in 0..1000 {
176 let result = fastrand::lowercase();
177 assert!(result.is_ascii_lowercase())
178 }
179}
180
181#[test]
182fn global_rng_uppercase() {
183 for _ in 0..1000 {
184 let result = fastrand::uppercase();
185 assert!(result.is_ascii_uppercase())
186 }
187}
188
189#[test]
190fn fill() {
191 let mut r = fastrand::Rng::new();
192 let mut a = [0u8; 64];
193 let mut b = [0u8; 64];
194
195 r.fill(&mut a);
196 r.fill(&mut b);
197
198 assert_ne!(a, b);
199
200 let mut a = [0u8; 9];
201 let mut b = [0u8; 9];
202
203 r.fill(&mut a);
204 r.fill(&mut b);
205
206 assert_ne!(a, b);
207}
208
209#[test]
210fn global_rng_fill() {
211 let mut a = [0u8; 64];
212 let mut b = [0u8; 64];
213
214 fastrand::fill(&mut a);
215 fastrand::fill(&mut b);
216
217 assert_ne!(a, b);
218}
219
220#[test]
221fn rng() {
222 let mut r = fastrand::Rng::new();
223
224 assert_ne!(r.u64(..), r.u64(..));
225
226 r.seed(7);
227 let a = r.u64(..);
228 r.seed(7);
229 let b = r.u64(..);
230 assert_eq!(a, b);
231}
232
233#[test]
234fn rng_init() {
235 let mut a = fastrand::Rng::new();
236 let mut b = fastrand::Rng::new();
237 assert_ne!(a.u64(..), b.u64(..));
238
239 a.seed(7);
240 b.seed(7);
241 assert_eq!(a.u64(..), b.u64(..));
242}
243
244#[test]
245fn rng_digit() {
246 let mut rng = fastrand::Rng::new();
247 for base in 1..36 {
248 let result = rng.digit(base);
249 assert!(result.is_ascii_digit() || result.is_ascii_lowercase());
250 }
251}
252
253#[test]
254#[should_panic]
255fn rng_digit_panic_1() {
256 let mut rng = fastrand::Rng::new();
257 let _result = rng.digit(0);
258}
259
260#[test]
261#[should_panic]
262fn rng_digit_panic_2() {
263 let mut rng = fastrand::Rng::new();
264 let base = rng.u32(37..);
265 let _result = rng.digit(base);
266}
267
268#[test]
269fn with_seed() {
270 let mut a = fastrand::Rng::with_seed(7);
271 let mut b = fastrand::Rng::new();
272 b.seed(7);
273 assert_eq!(a.u64(..), b.u64(..));
274}
275
276#[test]
277fn choose_multiple() {
278 let mut a = fastrand::Rng::new();
279 let mut elements = (0..20).collect::<Vec<_>>();
280
281 while !elements.is_empty() {
282 let chosen = a.choose_multiple(0..20, 5);
283 for &x in &chosen {
284 elements.retain(|&y| y != x);
285 }
286 }
287
288 let empty_elements: Vec<i32> = Vec::new();
289 let empty_result = a.choose_multiple(empty_elements, 5);
290 assert!(empty_result.is_empty());
291}
292
293#[test]
294fn choice() {
295 let items = [1, 4, 9, 5, 2, 3, 6, 7, 8, 0];
296 let mut r = fastrand::Rng::new();
297
298 for item in &items {
299 while r.choice(&items).unwrap() != item {}
300 }
301}
302
303#[test]
304fn choice_empty() {
305 let mut rng = fastrand::Rng::new();
306 let data: Vec<i32> = Vec::new();
307 let result = rng.choice(data);
308 assert!(result.is_none());
309}
310
311#[test]
312fn lowercase() {
313 let mut rng = fastrand::Rng::new();
314 for _ in 0..1000 {
315 let result = rng.lowercase();
316 assert!(result.is_ascii_lowercase())
317 }
318}
319
320#[test]
321fn alphabetic() {
322 let mut rng = fastrand::Rng::new();
323 for _ in 0..1000 {
324 let result = rng.alphabetic();
325 assert!(result.is_ascii_alphabetic())
326 }
327}
328
329#[test]
330fn uppercase() {
331 let mut rng = fastrand::Rng::new();
332 for _ in 0..1000 {
333 let result = rng.uppercase();
334 assert!(result.is_ascii_uppercase())
335 }
336}
337
338#[test]
339#[should_panic]
340fn char_panic() {
341 let mut rng = fastrand::Rng::new();
342 let _result = rng.char('z'..='a');
343}
344
345#[test]
346fn char() {
347 use core::ops::Bound;
348 let mut rng = fastrand::Rng::new();
349
350 let result = rng.char(..);
351 assert!(result >= 0 as char && result <= core::char::MAX);
352
353 let result = rng.char((Bound::Excluded('0'), Bound::Excluded('9')));
354 for _ in 0..1000 {
355 assert!(result > '0' && result < '9');
356 }
357}
358