aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2021-05-30 21:49:34 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2021-05-30 21:49:34 -0400
commita1bad11403e3c2da0b7563d440098b3ebdd3af83 (patch)
tree6144f9e2e7341dc561d6cce915c213a2147693a2
parentdee27d80962d92bb225e048ed6456794bdafd45e (diff)
Begin writing fuzz tester, mostly shape generation
-rw-r--r--test/fuzz.bqn59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/fuzz.bqn b/test/fuzz.bqn
new file mode 100644
index 00000000..1bc10213
--- /dev/null
+++ b/test/fuzz.bqn
@@ -0,0 +1,59 @@
+# Tester that checks primitives on random arguments
+
+# Linear congruential RNG; result in ↕2⋆16
+rr ← 2⋆16 # Random range
+_makeRand ← {st←𝕗
+ d‿m←2⋆15‿31
+ {𝕤⋄ d ⌊∘÷˜ st ↩ m|2531011+214013×st }
+}
+RandGen ← 1234 _makeRand
+Rand ← { 𝕩 | 0 rr⊸×⊸+˜´ RandGen¨↕⌈rr⋆⁼𝕩 }∘(1⊸⌈)⚇0
+_randChoose ← { Rand∘(≠𝕗)◶𝕗 }
+
+# Prime factorization
+⟨Factor⟩ ← {
+ p ← (¬∘∊/⊣)⟜(⥊×⌜˜)2↓↕m←60
+ Pr ← {m<𝕩}◶{𝕩↑p}‿{ m↩(טm)⌊2×𝕩 ⋄ p∾↩1↓/1(m⥊0<↕)⊸∧´p ⋄ Pr 𝕩 }
+ Factor ⇐ {
+ !(1=•Type𝕩)∧(𝕩=⌊𝕩)∧0<𝕩
+ ∧ 𝕩 {(0<≠∘⊢)◶⟨⥊⊣,⊢∾𝕊⟩⍟(>⟜1)˜⟜(𝕨÷×´)𝕩/˜0=𝕩|𝕨} Pr ⌈√𝕩
+ }
+}
+
+Sigmoid ← (40≤|)◶⟨1(-÷+)˜⋆,×⟩
+
+# 𝕩 is maximum bound plus 1 for both functions
+⟨RandBound,RandShape⟩ ← {
+ RandBound ⇐ ⟨
+ Rand # Uniform
+ Rand 128⊸⌊ # Small
+ -⟜1 ⌊ Rand∘(1⌈⌈)⌾((2⋆3+⊢)⁼) + ¯7+Rand∘15 # Near power of two
+ ⟩_randChoose
+
+ RandRank ← { 𝕊⊸+⍟(1=-)⟜Rand 4 }
+
+ Augment ← {
+ d ← 1+⌊𝕨÷1⌈×´𝕩 # Maximum bound that can be added, plus 1
+ C ← 10⊸+ Rand⊸< 1.2⊸√ # Decide whether to add
+ d (𝕨 𝕊 ⟨∾,∾˜⟩_randChoose⟜RandBound˜)⍟(C⊣) 𝕩
+ }
+ Combine ← ⟨
+ Rand∘≠⊸⌽ (2+Rand∘≠)⊸{×´¨𝕨↑(𝕨|↕∘≠)⊸⊔𝕩}∘⊢ # Random number of groups
+ ×´¨ (⊐·Rand⥊˜∘≠)⊸⊔∘⊢ # Distribute randomly
+ ⟩_randChoose
+ RandShape ⇐ ⊢ Augment ⟨
+ ⊢ (⊢ ⌊∘× ⊢ ≠⊸√ (Sigmoid⊸÷1⌈×´⊸÷˜)) · Rand¨ (RandRank⌈√<Rand)⊸⥊
+ ⊢ Combine⟜Factor 1⌈RandBound
+ ⟩_randChoose
+}
+
+# 𝕨 is 2⋆⁼bits in type; 𝕩 is shape
+RandArith ← {
+ •internal.Squeeze (1⊸<⊸×m÷2) -˜ Rand 𝕩⥊m←2⋆𝕨
+}
+# 𝕩 is bound
+RandMonArith ← RandArith⟜RandShape
+RandDyArith ← { 𝕨⊸RandArith¨ (Rand 2) ⌽ (Rand 1+≠)⊸(⊢≍○<↑) RandShape 𝕩 }
+
+types ← 0‿3‿4‿5‿6
+types { ! (𝕨+𝕩)≡𝕨--𝕩 }´∘RandDyArith⌜ 100⥊1e3