-
Notifications
You must be signed in to change notification settings - Fork 12
/
barbucha.red
188 lines (171 loc) · 4.54 KB
/
barbucha.red
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Red[
Title: "Barbucha - collection of tools for fuzzy testing"
Author: "Boleslav Březovsky"
]
comment {
Dialect specs:
opt [integer!] ; repeat following type X times
opt RANDOM ; return random value
<datatype> [word!] ; value type
opt <options> ;
Supported options:
integer!:
NEGATIVE ; return negative integer
string!:
LENGTH integer! ; set string length
}
type-templates: [
datatype! [
reduce [
datatype!
first random collect [
foreach word words-of system/words [
if datatype? get/any word [keep word]
]
]
]
]
unset! [[<TODO>] [<TODO>]]
none! [none]
logic! [[true] [first random [true false]]]
block! [[[foo #bar "baz"]] [collect [loop length [keep random-string length]]]]
paren! [[quote (foo #bar "baz")] [<TODO:paren! random>]]
string! [["foo"] [random-string length]]
file! [[%foo.bar]]
url! [[http://foo.bar]]
char! [[#"x"] [random 1FFFFFh]]
integer! [[0] [random 2147483647]] ; TODO: also negative integers and switch in dialect for it
float! [[0.0] [random 1.797693134862315e308]]
word! [['foo] [to word! random-string length]]
set-word! [[quote foo:] [to set-word! random-string length]]
lit-word! [[quote 'foo] [to lit-word! random-string length]]
get-word! [[quote :foo] [to get-word! random-string length]]
refinement! [[/foo] [to refinement! random-string length]]
issue! [[#foo] [to issue! random-string length]]
native! [<TODO:native!>]
action! [<TODO:action!>]
op! [<TODO:op!>]
function! [<TODO:function!>]
path! [[quote foo/bar/baz]]
lit-path! [[quote 'foo/bar/baz]]
set-path! [[quote foo/bar/baz:]]
get-path! [quote :foo/bar/baz]
routine! [<TODO:routine!>]
bitset! [[charset "bar"] [charset random-string length]]
point! [<TODO:point!>]
object! [<TODO:object!>]
typeset! [<TODO:typeset!>]
error! [<TODO:error!>]
vector! [[make vector! [integer! 8 10]]]
hash! [[make hash! [foo bar baz]]]
pair! [[0x0] [random 2147483647x2147483647]]
percent! [[0%] [random 1.797693134862315e308%]]
tuple! [[0.0.0] [random 255.255.255]] ; TODO: support different length
map! [[#(foo: bar)]]
binary! [[#{deadcafe}]]
time! [[11:22:33]]
tag! [[<foo>]]
email! [[foo@bar.baz]]
handle! [<TODO:handle!>]
date! [[27-2-2011]]
]
random-string: func [
"Return random string"
length
; TODO: support description dialect
][
unless length [length: 8]
collect/into [loop length [keep #"`" + random 26]] copy {}
]
random-map: func [
"Return random map"
size
/depth
level
/local
make-map map maps out key
][
; currently creates random map with words as keys and strings as values.
make-map: func [size][
to map! make-type collect [
loop size [
keep compose [
random word!
random string! length (random 1000)
]
]
]
]
either level [
maps: collect [
loop level [
keep make-map size
]
]
map: out: take maps
until [
key: to word! random-string 8
map/:key: take maps
map: map/:key
empty? maps
]
out
][
make-map size
]
]
context [
action: none
length: 8
rules: [
float! integer! pair! percent! [['negative (action: [negate value])]]
string! word! set-word! get-word! lit-word! [[
'length set value integer! (pre-action: compose [length: (value)])
]]
]
set 'make-type func [
"Return default value of given type"
type [datatype! block!] "Type of value or dialect specs"
/random "Return random value of given type"
/local
species values results
repetition
][
if datatype? type [type: reduce [type]]
species: 1 ; 1 - default, 2 - random
length: 8 ; default length for random strings
repetition: 1
values: copy [] ; internal "dialect": [type random? options]
results: copy []
parse type [
some [
(species: repetition: 1)
(pre-action: action: none)
(length: 8)
opt [set repetition integer!]
opt ['random (species: 2)]
set type skip
(opt-rule: switch to word! type rules)
opt opt-rule
(
loop repetition [
repend values [to word! type species pre-action action]
]
)
]
]
foreach [type species pre-act act] values [
; NOTE: This is bit crazy, but when binding length directly,
; code not using length somehow stops working
length: 8
do pre-act
value: select type-templates type
value: any [pick value species first value]
value: func [length] value
value: value length
if action [value: do action]
append/only results value
]
results
]
]