github.com/wzzhu/tensor@v0.9.24/genlib2/dense_cons_tests.go (about)

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"text/template"
     6  )
     7  
     8  const onesTestsRaw = `var onesTests = []struct {
     9  	of Dtype
    10  	shape Shape
    11  	correct interface{}
    12  }{
    13  	{{range .Kinds -}}
    14  	{{if isNumber . -}}
    15  	{ {{asType . | title | strip}},  ScalarShape(), {{asType .}}(1)},
    16  	{ {{asType . | title | strip}},  Shape{2,2}, []{{asType .}}{1,1,1,1}},
    17  	{{end -}}
    18  	{{end -}}
    19  	{Bool, ScalarShape(), true},
    20  	{Bool, Shape{2,2}, []bool{true, true, true, true}},
    21  }
    22  
    23  func TestOnes(t *testing.T){
    24  	assert := assert.New(t)
    25  	for _, ot := range onesTests{
    26  		T := Ones(ot.of, ot.shape...)
    27  		assert.True(ot.shape.Eq(T.Shape()))
    28  		assert.Equal(ot.correct, T.Data())
    29  	}
    30  }
    31  `
    32  
    33  const eyeTestsRaw = `// yes, it's a pun on eye tests, stop asking and go see your optometrist
    34  var eyeTests = []struct{
    35  	E Dtype
    36  	R, C, K int
    37  
    38  
    39  	correct interface{}
    40  }{
    41  	{{range .Kinds -}}
    42  	{{if isNumber . -}}
    43  	{ {{asType . | title | strip}}, 4,4, 0, []{{asType .}}{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}},
    44  	{ {{asType . | title | strip}}, 4,4, 1, []{{asType .}}{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0}},
    45  	{ {{asType . | title | strip}}, 4,4, 2, []{{asType .}}{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}},
    46  	{ {{asType . | title | strip}}, 4,4, 3, []{{asType .}}{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
    47  	{ {{asType . | title | strip}}, 4,4, 4, []{{asType .}}{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
    48  	{ {{asType . | title | strip}}, 4,4, -1, []{{asType .}}{0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}},
    49  	{ {{asType . | title | strip}}, 4,4, -2, []{{asType .}}{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0}},
    50  	{ {{asType . | title | strip}}, 4,4, -3, []{{asType .}}{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}},
    51  	{ {{asType . | title | strip}}, 4,4, -4, []{{asType .}}{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
    52  	{ {{asType . | title | strip}}, 4,5, 0, []{{asType .}}{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0}},
    53  	{ {{asType . | title | strip}}, 4,5, 1, []{{asType .}}{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1}},
    54  	{ {{asType . | title | strip}}, 4,5, -1, []{{asType .}}{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}},
    55  	{{end -}}
    56  	{{end -}}
    57  }
    58  
    59  func TestI(t *testing.T){
    60  	assert := assert.New(t)
    61  	var T Tensor
    62  
    63  	for i, it := range eyeTests {
    64  		T = I(it.E, it.R, it.C, it.K)
    65  		assert.True(Shape{it.R, it.C}.Eq(T.Shape()))
    66  		assert.Equal(it.correct, T.Data(), "Test %d-R: %d, C: %d K: %d", i, it.R, it.C, it.K)
    67  	}
    68  
    69  }
    70  `
    71  
    72  var (
    73  	onesTests *template.Template
    74  	eyeTests  *template.Template
    75  )
    76  
    77  func init() {
    78  	onesTests = template.Must(template.New("onesTest").Funcs(funcs).Parse(onesTestsRaw))
    79  	eyeTests = template.Must(template.New("eyeTest").Funcs(funcs).Parse(eyeTestsRaw))
    80  }
    81  
    82  func generateDenseConsTests(f io.Writer, generic Kinds) {
    83  	onesTests.Execute(f, generic)
    84  	eyeTests.Execute(f, generic)
    85  }