clone url: git://git.m455.casa/fa
esperbuild/espersrc/fennel-0.7.0/test/generate.fnl
1 | ;; A general-purpose function for generating random values. |
2 |
|
3 | (var generate nil) |
4 |
|
5 | (local random-char |
6 | (fn [] |
7 | (if (> (math.random) 0.9) |
8 | (string.char (+ 48 (math.random 10))) |
9 | (> (math.random) 0.5) |
10 | (string.char (+ 97 (math.random 26))) |
11 | (> (math.random) 0.5) |
12 | (string.char (+ 65 (math.random 26))) |
13 | (> (math.random) 0.5) |
14 | (string.char (+ 32 (math.random 11))) |
15 | (> (math.random) 0.5) |
16 | (string.char (+ 45 (math.random 2))) |
17 | :else |
18 | (string.char (+ 58 (math.random 5)))))) |
19 |
|
20 | (local generators {:number (fn [] ; weighted towards mid-range integers |
21 | (if (> (math.random) 0.9) |
22 | (let [x (math.random 2147483647)] |
23 | (math.floor (- x (/ x 2)))) |
24 | (> (math.random) 0.2) |
25 | (math.floor (math.random 2048)) |
26 | :else (math.random))) |
27 | :string (fn [] |
28 | (var s "") |
29 | (for [_ 1 (math.random 16)] |
30 | (set s (.. s (random-char)))) |
31 | s) |
32 | :table (fn [table-chance] |
33 | (let [t {}] |
34 | (var k nil) |
35 | (for [_ 1 (math.random 16)] |
36 | ;; no nans plz |
37 | (set k (generate 0.9)) |
38 | (while (not= k k) (set k (generate 0.9))) |
39 | (tset t k (generate (* table-chance 1.5)))) |
40 | t)) |
41 | :boolean (fn [] (> (math.random) 0.5))}) |
42 |
|
43 | (set generate |
44 | (fn [table-chance] |
45 | "Generate a random piece of data." |
46 | (local table-chance (or table-chance 0.5)) |
47 | (if (> (math.random) 0.5) (generators.number) |
48 | (> (math.random) 0.5) (generators.string) |
49 | (> (math.random) table-chance) (generators.table table-chance) |
50 | :else (generators.boolean)))) |
51 |
|
52 | generate |