# Tester that checks primitives on random arguments opts ← { help ← 1↓" Fuzz testing. Options: -h, --help: Print this message and exit -b: Maximum bound (1e3) -n: Number of iterations (100) -t: Element type (0 3 4 5 6) -p: Primitives: both | mon%dy | mon%dy%both Any number of types or bounds can be given; all combinations are tested." o ← "-h"‿"--help"‿"-b"‿"-n"‿"-t"‿"-p" oo ← (≠o) = oi ← o ⊐ a←•args •Exit∘•Out∘help⍟(∨´2⊸>) oi opts ← 2↓o≠⊸↑ a ⊔˜ (¬-˜⊢× oi⊏˜ ↕∘≠⌈`∘׬)oo bounds‿num‿types‿prims ⇐ •BQN¨¨⌾(¯1⊸↓) opts "Only one iteration number can be given" ! 1≥≠num num ↩ ≠◶⟨100,+´⟩ num types ↩ 0‿3‿4‿5‿6⍟(0=≠) types bounds ↩ ⟨1e3⟩⍟(0=≠) bounds prims ↩ ((⊢-˜¬×+`+2×·¬∨´)'%'⊸=)¨⊸(⊔○∾) prims "At most three %-separated primitive groups allowed" ! 3≥≠prims arith ← ∾`"+-×÷⋆√⌊⌈¬|"‿"∧∨≤<>≥=≠" Pr ← { 𝕩 ↩ 2 (↑((¬∘∊/⊣)∾⊢)¨⊏) 3 ↑ 𝕩 "Only arithmetic primitives supported" ! ∧´∾𝕩∊¨arith 𝕩 } monArith‿dyArith ⇐ •BQN∘⥊¨¨ prims ↩ (0<≠)◶arith‿Pr prims } Squeeze‿ListVariations‿Variation‿ClearRefs ← •internal Range ← (•MakeRand 2).Range Rand ← {𝕨 Range 1⌈𝕩} _randChoose ← { Rand∘(≠𝕗)◶𝕗 } _randUnbounded ← { 𝕊⊸+⍟(1=-)⟜Rand 𝕗 } RandRank ← 4 _randUnbounded # 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(-÷+)˜⋆,×⟩ # Generate 𝕨 positive (not zero) integers summing to 𝕩 RandPart ← {k←𝕨 ⋄ n←𝕩 RU ← Range∘0 # Random float in [0,1] # Vitter, "An Efficient Algorithm for Sequential Random Sampling" vPrime ← @ # Add or reject each possibility individually MethodA ← {k←𝕩 v ← RU@ 1 { (𝕨×-⟜k⊸÷n-𝕩) (⊣𝕊1+⊢)⍟(v<⊣) 𝕩 } 0 } # Compute number of skipped possibilities MethodD ← {k←𝕩 RvP ← k√RU # Generate new vPrime kn ← k-1 # k for next iteration qu1 ← n-kn Sk ← {vP←𝕩 s ← ⌊ x ← { 𝕊∘RvP⍟(qu1⊸≤) n׬𝕩 } vP y1 ← kn √ (n ÷ qu1) × RU@ vPrime ↩ y1 × (1-x÷n) × qu1 (⊣÷-) s Cont ← { y2 ← ×´ kn (((n-1)-↕∘⌊) (⊣÷-) ⌈) 𝕩 (n(⊣÷-)x) < y1 × kn√y2 } Cont◶⟨{ vPrime↩@ ⋄ 𝕩 }, 𝕊 RvP⟩⍟(1 (5⌊´≠¨)⊸((Rand⟜≠⊏⊢)¨) ListVariations¨ a←𝕨≍○<𝕩 (ClearRefs@) ⊢ (∧´ ⊏ Match¨ 1⊸↓) (𝕨 (𝔽⊑∘⊢)⊘(𝔽´⊢) Variation¨⟜a)¨ v } FlatMatch ← ≡◶⟨∧´∘⥊=∨∧○(≠˜),1⟩ TestMonArith ← opts.monArith{ (0<≠f←𝕗) ⊑ 1‿{ _t ← { ! 𝕏 _testConsistent_ FlatMatch 𝕗 } (𝕨 RandArith RandShape 𝕩)_t¨ f } } RandDyShape ← { Prefix ← (∨`⌾⌽ 𝕩 ≥ ×`)⊸/ (Rand 1+≠)⊸↑ (Rand 2) ⌽ ≍○<⟜Prefix RandShape 𝕩 } TestDyArith ← opts.dyArith{ (0<≠f←𝕗) ⊑ 1‿{ sh ← RandDyShape 𝕩 _t ← { ! 𝕏 _testConsistent_ FlatMatch´ 𝕗 } (𝕨⊸RandArith¨ sh)_t¨ f { k←𝕩 p‿m‿n ← (∊/⊣)⟜f¨ ⟨+⟩‿⟨-⟩‿⟨¬⟩ rca ← ⟨RandChar,RandArith⟩ Fit ← -⟜(@+1-˜17×2⋆16)⌈-⟜@⌊⊢ { (⌽⍟(Rand 2) -∘Fit⟜-` rca{k𝕎𝕩}¨sh)_t 𝕩 }¨ p { s←Rand 2⋄(Fit`⍟s(⊣`⍟(¬s)rca){k𝕎𝕩}¨sh)_t 𝕩 }¨ m { s←Rand 2⋄((1+Fit)`⍟s(⊣`⍟(¬s)rca){k𝕎𝕩}¨sh)_t 𝕩 }¨ n }⍟(0⊸<∧≤⟜5) 𝕨 } } t←opts.types ⋄ b←opts.num/opts.bounds t TestMonArith⌜ b t TestDyArith ⌜ b