aboutsummaryrefslogtreecommitdiff
path: root/test/fuzz.bqn
blob: 749448498c2c6f81b30be3fc9163043d540835eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Tester that checks primitives on random arguments

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(-Γ·+)Λœβ‹†,Γ—βŸ©

# 𝕩 is maximum bound plus 1 for both functions
⟨RandBound,RandShape⟩ ← {
  RandBound ⇐ ⟨
    Rand                                         # Uniform
    Rand 128⊸⌊                                   # Small
    (0⌈-⟜1) ⌊ Rand∘(1⌈⌈)⌾((2⋆3+⊒)⁼) + Β―7+Rand∘15 # Near power of two
  ⟩_randChoose

  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⟩ ← {
  RandInt ← { (1⊸<βŠΈΓ—mΓ·2) -˜ 𝕩 Rand m←2⋆2⋆𝕨 }

  floats ← ⟨2⋆¯1074,2⋆¯1022,(2-2⋆¯52)Γ—2⋆1023⟩
  RandFloat ← ⟨
    (floats∾2⋆0β€Ώ8β€Ώ32β€Ώ100)_randChoose Γ— Range⟜0 - 2÷˜Rand∘3
    ⊒ (RandβŸœβ‰ βŠβŠ’) (Rand(⊣β‰₯⌈´⊸⌊)3+Rand∘+)Λœβˆ˜β‰ βŠΈ/∘(∾⟜-0∾floats∾∞)
  ⟩_randChoose

  RandTyped ← =⟜6β—ΆβŸ¨RandInt,RandFloat⊣⟩

  RN ← (0⌈-⟜1) ⌊ 1β€ΏRandRankβ€ΏRandBound _randChoose
  RandSplit ← ⌽⍟(Rand∘2) (-β‰βŠ’)⟜RN
  Combine ← (⍋RandΛœβˆ˜β‰ )⊸⊏⍟(Rand∘2) ∾

  RL ← {𝕨RandList𝕩}
  RandList ⇐ ⟨
    RandTyped                               # Random
    βˆ§β€Ώβˆ¨_randChoose RandTyped                # Sort
    ⊒ β₯Š RL⟜(1⌈RN)                           # Repeat
    Combine Randβ€ΏβŠ’_randChoose⊸RL¨⟜RandSplit # Partition
  ⟩{ 8βŠΈβ‰€β—ΆβŸ¨0,Rand∘(≠𝕗)βŸ©β—Άπ•— }
  RandArith ⇐ β€’internal.Squeeze ⊒ β₯Š RandList⟜(Γ—Β΄)
}

# 𝕩 is bound
RandMonArith ← RandArith⟜RandShape
RandDyArith ← {
  Prefix ← (∨`⌾⌽ 𝕩 β‰₯ Γ—`)⊸/ (Rand 1+β‰ )βŠΈβ†‘
  π•¨βŠΈRandArithΒ¨ (Rand 2) ⌽ ≍○<⟜Prefix RandShape 𝕩
}

types ← 0β€Ώ3β€Ώ4β€Ώ5β€Ώ6
types { ! (𝕨+𝕩)β‰‘β—ΆβŸ¨βˆ§Β΄βˆ˜β₯Š=βˆ¨βˆ§β—‹(β‰ Λœ),1βŸ©π•¨--𝕩 }´∘RandDyArith⌜ 100β₯Š1e3