aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/cmd/tools/fuzz
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/cmd/tools/fuzz
downloadcli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/cmd/tools/fuzz')
-rw-r--r--v_windows/v/cmd/tools/fuzz/fuzz.sh18
-rw-r--r--v_windows/v/cmd/tools/fuzz/map_fuzz.v144
2 files changed, 162 insertions, 0 deletions
diff --git a/v_windows/v/cmd/tools/fuzz/fuzz.sh b/v_windows/v/cmd/tools/fuzz/fuzz.sh
new file mode 100644
index 0000000..cbba7d7
--- /dev/null
+++ b/v_windows/v/cmd/tools/fuzz/fuzz.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+cores=$(nproc --all)
+
+echo Number of cores: $cores
+echo Compiling...
+./v -cc clang -o cmd/tools/fuzz/map_fuzz cmd/tools/fuzz/map_fuzz.v -prod -cflags "-fsanitize=memory"
+
+echo Fuzzing:
+while true
+do
+ for ((i=1;i<=cores;++i))
+ do
+ sleep 0.001
+ ./cmd/tools/fuzz/map_fuzz &
+ done
+ wait
+done
diff --git a/v_windows/v/cmd/tools/fuzz/map_fuzz.v b/v_windows/v/cmd/tools/fuzz/map_fuzz.v
new file mode 100644
index 0000000..f1c62f2
--- /dev/null
+++ b/v_windows/v/cmd/tools/fuzz/map_fuzz.v
@@ -0,0 +1,144 @@
+import rand
+import time
+
+fn generate_strings(str_len int, arr_len int) []string {
+ mut arr := []string{len: arr_len}
+ for i in 0 .. arr_len {
+ arr[i] = rand.string(str_len)
+ }
+ return arr
+}
+
+fn fuzz1() {
+ amount := 200000 - rand.intn(100000)
+ amount2 := 200000 - rand.intn(100000)
+ len := 25 - rand.intn(10)
+ arr := generate_strings(len, amount)
+ arr2 := generate_strings(len, amount2)
+ mut m := map[string]int{}
+ for i in 0 .. amount {
+ m[arr[i]] = i
+ assert i == m[arr[i]]
+ }
+ for i in 0 .. amount {
+ assert i == m[arr[i]]
+ }
+ for i in 0 .. amount2 {
+ assert 0 == m[arr2[i]]
+ }
+ unsafe {
+ m.free()
+ arr.free()
+ }
+}
+
+fn fuzz2() {
+ mut m := map[string]int{}
+ amount := rand.intn(500000) + 1
+ len := 25 - rand.intn(10)
+ arr := generate_strings(len, amount)
+ for i, x in arr {
+ m[x] = i
+ }
+ mut i := 0
+ for key, val in m {
+ assert key == arr[i]
+ assert val == i
+ i++
+ }
+ unsafe {
+ m.free()
+ arr.free()
+ }
+}
+
+fn fuzz3() {
+ mut m := map[string]int{}
+ amount := rand.intn(500000) + 1
+ len := 25 - rand.intn(10)
+ arr := generate_strings(len, amount)
+ for i, x in arr {
+ if (i % 10000) == 0 {
+ keys := m.keys()
+ assert keys.len == i
+ assert keys == arr[0..i]
+ }
+ m[x] = i
+ }
+ assert m.keys() == arr
+ assert m.keys().len == amount
+ unsafe {
+ m.free()
+ arr.free()
+ }
+}
+
+fn fuzz4() {
+ amount := rand.intn(500000)
+ len := 25 - rand.intn(10)
+ arr := generate_strings(len, amount)
+ mut m := map[string]int{}
+ for i in 0 .. amount {
+ m[arr[i]] = i
+ }
+ for i in 0 .. amount {
+ m.delete(arr[i])
+ assert m[arr[i]] == 0
+ }
+ assert m.len == 0
+ unsafe {
+ m.free()
+ arr.free()
+ }
+}
+
+fn fuzz5() {
+ amount := rand.intn(500000) + 1
+ arr := generate_strings(20, amount)
+ mut m := map[string]int{}
+ for i in 0 .. amount {
+ m[arr[i]] = i
+ assert (arr[i] in m) == true
+ }
+ for i in 0 .. amount {
+ m.delete(arr[i])
+ assert (arr[i] !in m) == true
+ assert m.len == amount - i - 1
+ }
+ assert m.len == 0
+ unsafe {
+ m.free()
+ arr.free()
+ }
+}
+
+fn fuzz6() {
+ mut m := map[string]int{}
+ amount := rand.intn(500000) + 1
+ len := 25 - rand.intn(10)
+ arr := generate_strings(len, amount)
+ for i, x in arr {
+ m[x]++
+ m[x] += i
+ assert m[x] == i + 1
+ }
+ for i, x in arr {
+ assert m[x] == i + 1
+ }
+ unsafe {
+ m.free()
+ arr.free()
+ }
+}
+
+fn main() {
+ seed := u32(time.ticks())
+ println('seed: $seed.hex()')
+ rand.seed([seed, seed])
+ fuzz1()
+ fuzz2()
+ fuzz3()
+ fuzz4()
+ fuzz5()
+ fuzz6()
+}