aboutsummaryrefslogtreecommitdiff
path: root/tests/fuzz/vector/pds.go
blob: d4dfea78294a9af3632fb6f505af7e11bc0237f1 (plain) (blame)
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
package pds

import (
	"os"
	"testing"
	"testing/internal/testdeps"
)



type opType uint8
const (
	opType_NewVectorEmpty opType = iota
	opType_NewVector
	opType_NewVectorOfEmpty
	opType_NewVectorOf
	opType_Len
	opType_Get
	opType_Set
	opType_Append
	opType_Prepend
	opType_Slice
)

var (
	opTable = []opType{
		opType_NewVectorEmpty,
		opType_NewVector,
		opType_NewVectorOfEmpty,
		opType_NewVectorOf,
		opType_Len,
		opType_Get,
		opType_Set,
		opType_Append,
		opType_Prepend,
		opType_Slice,
	}
)



func asOp(n uint8) opType {
	idx := n % uint8(len(opTable))
	return opTable[idx]
}

func fn(f *testing.F) {
	f.Fuzz(func(
		t *testing.T,
		operations []uint8,
		i int,
		j int,
		a1 byte,
		a2 []byte,
	) {
		builtin    := []byte{}
		persistent := NewVector[byte]()
		for _, operation := range operations {
			switch asOp(operation) {
				case opType_NewVectorEmpty:
					builtin    = []byte{}
					persistent = NewVector[byte]()
				case opType_NewVector:
					builtin    = []byte{}
					builtin    = append(builtin, a2...)
					persistent = NewVector(a2...)
				case opType_NewVectorOfEmpty:
					builtin    = []byte{}
					persistent = NewVectorOf([]byte{})
				case opType_NewVectorOf:
					builtin    = []byte{}
					builtin    = append(builtin, a2...)
					persistent = NewVectorOf(a2)
				case opType_Len:
					if len(builtin) != persistent.Len() {
						t.Errorf(
							"bad length: %v != %v",
							len(builtin),
							persistent.Len(),
						)
					}
				case opType_Get:
					if i >= len(builtin) || i < 0 {
						break
					}
					lhs := builtin[i]
					rhs := persistent.Get(i)
					if lhs != rhs {
						t.Errorf(
							"bad Get: %v != %v",
							lhs,
							rhs,
						)
					}
				case opType_Set:
					if i >= len(builtin) || i < 0 {
						break
					}
					builtin[i] = a1
					persistent = persistent.Set(i, a1)
				case opType_Append:
					builtin    = append(builtin, a1)
					persistent = persistent.Append(a1)
				case opType_Prepend:
					pre := []byte{a1}
					builtin    = append(pre, builtin...)
					persistent = persistent.Prepend(a1)
				case opType_Slice:
					if i > j {
						break
					}
					if i >= len(builtin) || i < 0 {
						break
					}
					if j >= len(builtin) || j < 0 {
						break
					}
					builtin    = builtin[i:j]
					persistent = persistent.Slice(i, j)
				default:
					panic("bad op")
			}
		}
	})
}



func MainTest() {
	fuzzTargets := []testing.InternalFuzzTarget{
		{ "vector model", fn },
	}

	deps := testdeps.TestDeps{}
	tests      := []testing.InternalTest     {}
	benchmarks := []testing.InternalBenchmark{}
	examples   := []testing.InternalExample  {}
	m := testing.MainStart(deps, tests, benchmarks, fuzzTargets, examples)
	os.Exit(m.Run())
}