File: dense_getset_tests.go

package info (click to toggle)
golang-github-gorgonia-tensor 0.9.24-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,696 kB
  • sloc: sh: 18; asm: 18; makefile: 8
file content (238 lines) | stat: -rw-r--r-- 5,598 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main

import (
	"fmt"
	"io"
	"reflect"
	"text/template"
)

type testData struct {
	Kind      reflect.Kind
	TestData0 []interface{}
	Set       interface{}
	Correct   []interface{}
}

func makeTests(generic Kinds) []testData {
	retVal := make([]testData, 0)
	for _, k := range generic.Kinds {
		if isParameterized(k) {
			continue
		}

		td := testData{Kind: k}

		data := make([]interface{}, 6)
		correct := make([]interface{}, 6)

		switch {
		case isRangeable(k):
			raw := []int{0, 1, 2, 3, 4, 5}
			for i := range data {
				data[i] = raw[i]
				correct[i] = 45
			}
			td.Set = 45
		case k == reflect.Bool:
			raw := []bool{true, false, true, false, true, false}
			for i := range data {
				data[i] = raw[i]
				correct[i] = false
			}
			td.Set = false
		case k == reflect.String:
			raw := []string{"\"zero\"", "\"one\"", "\"two\"", "\"three\"", "\"four\"", "\"five\""}
			for i := range data {
				data[i] = raw[i]
				correct[i] = "\"HELLO WORLD\""
			}
			td.Set = "\"HELLO WORLD\""
		default:
			continue
		}
		td.TestData0 = data
		td.Correct = correct
		retVal = append(retVal, td)

	}
	return retVal
}

func makeZeroTests(generic Kinds) []testData {
	retVal := make([]testData, 0)
	for _, k := range generic.Kinds {
		if isParameterized(k) {
			continue
		}

		td := testData{Kind: k}

		data := make([]interface{}, 6)
		correct := make([]interface{}, 6)

		switch {
		case isRangeable(k):
			raw := []int{0, 1, 2, 3, 4, 5}
			for i := range data {
				data[i] = raw[i]
				correct[i] = 0
			}
		case k == reflect.Bool:
			raw := []bool{true, false, true, false, true, false}
			for i := range data {
				data[i] = raw[i]
				correct[i] = false
			}
		case k == reflect.String:
			raw := []string{"\"zero\"", "\"one\"", "\"two\"", "\"three\"", "\"four\"", "\"five\""}
			for i := range data {
				data[i] = raw[i]
				correct[i] = "\"\""
			}
		default:
			continue
		}
		td.TestData0 = data
		td.Correct = correct
		retVal = append(retVal, td)

	}
	return retVal
}

const getTestRaw = `var denseSetGetTests = []struct {
	of Dtype
	data interface{} 
	set interface{}

	correct []interface{}
}{
	{{range . -}}
	{{$k := .Kind -}}
	{ {{title .Kind.String | strip}}, []{{.Kind.String | clean}}{ {{range .TestData0 -}}{{printf "%v" .}}, {{end -}} }, {{printf "%v" .Set}}, []interface{}{ {{range .TestData0 -}} {{$k}}({{printf "%v" .}}), {{end -}} }},
	{{end -}}
}

func TestDense_setget(t *testing.T) {
	assert := assert.New(t)
	for _, gts := range denseSetGetTests {
		T := New(Of(gts.of), WithShape(len(gts.correct)))
		for i, v := range gts.correct {
			T.Set(i, v)
			got := T.Get(i)
			assert.Equal(v, got)
		}
	}
}

`

const memsetTestRaw = `var denseMemsetTests = []struct{
	of Dtype
	data interface{}
	val interface{}
	shape Shape

	correct interface{}
}{
	{{range . -}}
	{{$val := .Set -}}
	{{$k := .Kind -}}
	{ {{title .Kind.String | strip}}, []{{asType .Kind}}{ {{range .TestData0 -}}{{printf "%v" .}}, {{end -}} }, {{asType .Kind}}({{$val}}), Shape{2,3}, []{{asType .Kind}}{ {{range .Correct}} {{printf "%v" .}}, {{end -}} } }, 
	{{end -}}
}

func TestDense_memset(t *testing.T){
	assert := assert.New(t)
	for _, mts := range denseMemsetTests {
		T := New(Of(mts.of), WithShape(mts.shape...))
		T.Memset(mts.val)
		assert.Equal(mts.correct, T.Data())

		T = New(Of(mts.of), WithShape(mts.shape...), WithBacking(mts.data))
		T2, _ := T.Slice(nil)
		T2.Memset(mts.val)
		assert.Equal(mts.correct, T2.Data())
	}
}
`

const zeroTestRaw = `var denseZeroTests = []struct{
	of Dtype
	data interface{}

	correct interface{}
}{
	{{range . -}}
	{{$val := .Set -}}
	{{$k := .Kind -}}
	{ {{title .Kind.String | strip}}, []{{asType .Kind}}{ {{range .TestData0 -}}{{printf "%v" .}}, {{end -}} }, []{{asType .Kind}}{ {{range .Correct}} {{printf "%v" .}}, {{end -}} } }, 
	{{end -}}
}

func TestDense_Zero(t *testing.T) {
	assert := assert.New(t)
	for _, mts := range denseZeroTests {
		
		typ := reflect.TypeOf(mts.data)
		val := reflect.ValueOf(mts.data)
		data := reflect.MakeSlice(typ, val.Len(), val.Cap())
		reflect.Copy(data, val)	

		T := New(Of(mts.of), WithBacking(data.Interface()))
		T.Zero()
		assert.Equal(mts.correct, T.Data())

		T = New(Of(mts.of),  WithBacking(mts.data))
		T2, _ := T.Slice(nil)
		T2.Zero()
		assert.Equal(mts.correct, T2.Data())
	}	
}
`

const denseEqTestRaw = `func TestDense_Eq(t *testing.T) {
	eqFn := func(q *Dense) bool{
		a := q.Clone().(*Dense)
		if !q.Eq(a) {
			t.Error("Expected a clone to be exactly equal")
			return false
		}
		a.Zero()

		// Bools are excluded because the probability of having an array of all false is very high
		if q.Eq(a)  && a.len() > 3 && a.Dtype() != Bool {
			t.Errorf("a %v", a.Data())
			t.Errorf("q %v", q.Data())
			t.Error("Expected *Dense to be not equal")
			return false
		}
		return true
	}
	if err := quick.Check(eqFn, &quick.Config{Rand: newRand(), MaxCount: quickchecks}); err != nil {
		t.Errorf("Failed to perform equality checks")
	}
}`

var (
	GetTest    *template.Template
	MemsetTest *template.Template
	ZeroTest   *template.Template
)

func init() {
	GetTest = template.Must(template.New("GetTest").Funcs(funcs).Parse(getTestRaw))
	MemsetTest = template.Must(template.New("MemsetTest").Funcs(funcs).Parse(memsetTestRaw))
	ZeroTest = template.Must(template.New("ZeroTest").Funcs(funcs).Parse(zeroTestRaw))
}

func generateDenseGetSetTests(f io.Writer, generic Kinds) {
	tests := makeTests(generic)
	GetTest.Execute(f, tests)
	fmt.Fprintf(f, "\n\n")
	MemsetTest.Execute(f, tests)
	fmt.Fprintf(f, "\n\n")
	ZeroTest.Execute(f, makeZeroTests(generic))
	fmt.Fprintf(f, "\n%v\n", denseEqTestRaw)
}