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
|
package main
import (
"fmt"
"io"
"text/template"
)
const unaryTestBodyRaw = `invFn := func(q *Dense) bool {
a := q.Clone().(*Dense)
{{template "funcoptdecl" -}}
correct := a.Clone().(*Dense)
{{template "funcoptcorrect" -}}
we, willFailEq := willerr(a, {{.TypeClassName}}, {{.EqFailTypeClassName}})
_, ok := q.Engine().({{interfaceName .Name}}); we = we || !ok
ret, err := {{.Name}}(a {{template "funcoptuse"}})
if err, retEarly := qcErrCheck(t, "{{.Name}}", a, nil, we, err); retEarly{
if err != nil {
return false
}
return true
}
{{if ne .InvTypeClass "" -}}
if err := typeclassCheck(a.Dtype(), {{.InvTypeClass}}); err != nil {
return true // uninvertible due to type class implementation issues
}
{{end -}}
{{if eq .FuncOpt "incr" -}}
if ret, err = Sub(ret, identityVal(100, a.Dtype()), UseUnsafe()) ; err != nil {
t.Errorf("err while subtracting incr: %v", err)
return false
}
{{end -}}
{{.Inv}}(ret, UseUnsafe())
if !qcEqCheck(t, a.Dtype(), willFailEq, correct.Data(), ret.Data()) {
return false
}
{{template "funcoptcheck" -}}
return true
}
if err := quick.Check(invFn, &quick.Config{Rand:newRand(), MaxCount: quickchecks}); err != nil{
t.Errorf("Inv tests for {{.Name}} failed: %v", err)
}
`
type unaryTest struct {
unaryOp
FuncOpt string
EqFailTypeClassName string
InvTypeClass string
}
func (fn *unaryTest) Name() string {
if fn.unaryOp.Name() == "Eq" || fn.unaryOp.Name() == "Ne" {
return "El" + fn.unaryOp.Name()
}
return fn.unaryOp.Name()
}
func (fn *unaryTest) Signature() *Signature {
name := fmt.Sprintf("Test%s", fn.unaryOp.Name())
if fn.FuncOpt != "" {
name += "_" + fn.FuncOpt
}
return &Signature{
Name: name,
NameTemplate: plainName,
ParamNames: []string{"t"},
ParamTemplates: []*template.Template{testingType},
}
}
func (fn *unaryTest) WriteBody(w io.Writer) {
t := template.Must(template.New("unary test body").Funcs(funcs).Parse(unaryTestBodyRaw))
template.Must(t.New("funcoptdecl").Parse(funcOptDecl[fn.FuncOpt]))
template.Must(t.New("funcoptcorrect").Parse(funcOptCorrect[fn.FuncOpt]))
template.Must(t.New("funcoptuse").Parse(funcOptUse[fn.FuncOpt]))
template.Must(t.New("funcoptcheck").Parse(funcOptCheck[fn.FuncOpt]))
t.Execute(w, fn)
}
func (fn *unaryTest) canWrite() bool { return fn.Inv != "" }
func (fn *unaryTest) Write(w io.Writer) {
sig := fn.Signature()
w.Write([]byte("func "))
sig.Write(w)
w.Write([]byte("{\n"))
fn.WriteBody(w)
w.Write([]byte("}\n"))
}
func generateAPIUnaryTests(f io.Writer, ak Kinds) {
var tests []*unaryTest
for _, op := range conditionalUnaries {
t := &unaryTest{
unaryOp: op,
EqFailTypeClassName: "nil",
}
tests = append(tests, t)
}
for _, op := range unconditionalUnaries {
t := &unaryTest{
unaryOp: op,
EqFailTypeClassName: "nil",
}
switch op.name {
case "Square":
t.InvTypeClass = "floatcmplxTypes"
case "Cube":
t.InvTypeClass = "floatTypes"
}
tests = append(tests, t)
}
for _, fn := range tests {
if fn.canWrite() {
fn.Write(f)
}
fn.FuncOpt = "unsafe"
}
for _, fn := range tests {
if fn.canWrite() {
fn.Write(f)
}
fn.FuncOpt = "reuse"
}
for _, fn := range tests {
if fn.canWrite() {
fn.Write(f)
}
fn.FuncOpt = "incr"
}
// for now incr cannot be quickchecked
for _, fn := range tests {
if fn.canWrite() {
fn.Write(f)
}
}
}
|