aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/cmd/tools/fuzz/map_fuzz.v
blob: f1c62f2d56ac8a27c62d0de2c0e2f6a23c746d70 (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
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
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()
}