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

     1  // Code generated by genlib2. DO NOT EDIT.
     2  
     3  package tensor
     4  
     5  import (
     6  	"reflect"
     7  	"testing"
     8  	"testing/quick"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  var denseSetGetTests = []struct {
    14  	of   Dtype
    15  	data interface{}
    16  	set  interface{}
    17  
    18  	correct []interface{}
    19  }{
    20  	{Bool, []bool{true, false, true, false, true, false}, false, []interface{}{bool(true), bool(false), bool(true), bool(false), bool(true), bool(false)}},
    21  	{Int, []int{0, 1, 2, 3, 4, 5}, 45, []interface{}{int(0), int(1), int(2), int(3), int(4), int(5)}},
    22  	{Int8, []int8{0, 1, 2, 3, 4, 5}, 45, []interface{}{int8(0), int8(1), int8(2), int8(3), int8(4), int8(5)}},
    23  	{Int16, []int16{0, 1, 2, 3, 4, 5}, 45, []interface{}{int16(0), int16(1), int16(2), int16(3), int16(4), int16(5)}},
    24  	{Int32, []int32{0, 1, 2, 3, 4, 5}, 45, []interface{}{int32(0), int32(1), int32(2), int32(3), int32(4), int32(5)}},
    25  	{Int64, []int64{0, 1, 2, 3, 4, 5}, 45, []interface{}{int64(0), int64(1), int64(2), int64(3), int64(4), int64(5)}},
    26  	{Uint, []uint{0, 1, 2, 3, 4, 5}, 45, []interface{}{uint(0), uint(1), uint(2), uint(3), uint(4), uint(5)}},
    27  	{Uint8, []uint8{0, 1, 2, 3, 4, 5}, 45, []interface{}{uint8(0), uint8(1), uint8(2), uint8(3), uint8(4), uint8(5)}},
    28  	{Uint16, []uint16{0, 1, 2, 3, 4, 5}, 45, []interface{}{uint16(0), uint16(1), uint16(2), uint16(3), uint16(4), uint16(5)}},
    29  	{Uint32, []uint32{0, 1, 2, 3, 4, 5}, 45, []interface{}{uint32(0), uint32(1), uint32(2), uint32(3), uint32(4), uint32(5)}},
    30  	{Uint64, []uint64{0, 1, 2, 3, 4, 5}, 45, []interface{}{uint64(0), uint64(1), uint64(2), uint64(3), uint64(4), uint64(5)}},
    31  	{Float32, []float32{0, 1, 2, 3, 4, 5}, 45, []interface{}{float32(0), float32(1), float32(2), float32(3), float32(4), float32(5)}},
    32  	{Float64, []float64{0, 1, 2, 3, 4, 5}, 45, []interface{}{float64(0), float64(1), float64(2), float64(3), float64(4), float64(5)}},
    33  	{Complex64, []complex64{0, 1, 2, 3, 4, 5}, 45, []interface{}{complex64(0), complex64(1), complex64(2), complex64(3), complex64(4), complex64(5)}},
    34  	{Complex128, []complex128{0, 1, 2, 3, 4, 5}, 45, []interface{}{complex128(0), complex128(1), complex128(2), complex128(3), complex128(4), complex128(5)}},
    35  	{String, []string{"zero", "one", "two", "three", "four", "five"}, "HELLO WORLD", []interface{}{string("zero"), string("one"), string("two"), string("three"), string("four"), string("five")}},
    36  }
    37  
    38  func TestDense_setget(t *testing.T) {
    39  	assert := assert.New(t)
    40  	for _, gts := range denseSetGetTests {
    41  		T := New(Of(gts.of), WithShape(len(gts.correct)))
    42  		for i, v := range gts.correct {
    43  			T.Set(i, v)
    44  			got := T.Get(i)
    45  			assert.Equal(v, got)
    46  		}
    47  	}
    48  }
    49  
    50  var denseMemsetTests = []struct {
    51  	of    Dtype
    52  	data  interface{}
    53  	val   interface{}
    54  	shape Shape
    55  
    56  	correct interface{}
    57  }{
    58  	{Bool, []bool{true, false, true, false, true, false}, bool(false), Shape{2, 3}, []bool{false, false, false, false, false, false}},
    59  	{Int, []int{0, 1, 2, 3, 4, 5}, int(45), Shape{2, 3}, []int{45, 45, 45, 45, 45, 45}},
    60  	{Int8, []int8{0, 1, 2, 3, 4, 5}, int8(45), Shape{2, 3}, []int8{45, 45, 45, 45, 45, 45}},
    61  	{Int16, []int16{0, 1, 2, 3, 4, 5}, int16(45), Shape{2, 3}, []int16{45, 45, 45, 45, 45, 45}},
    62  	{Int32, []int32{0, 1, 2, 3, 4, 5}, int32(45), Shape{2, 3}, []int32{45, 45, 45, 45, 45, 45}},
    63  	{Int64, []int64{0, 1, 2, 3, 4, 5}, int64(45), Shape{2, 3}, []int64{45, 45, 45, 45, 45, 45}},
    64  	{Uint, []uint{0, 1, 2, 3, 4, 5}, uint(45), Shape{2, 3}, []uint{45, 45, 45, 45, 45, 45}},
    65  	{Uint8, []uint8{0, 1, 2, 3, 4, 5}, uint8(45), Shape{2, 3}, []uint8{45, 45, 45, 45, 45, 45}},
    66  	{Uint16, []uint16{0, 1, 2, 3, 4, 5}, uint16(45), Shape{2, 3}, []uint16{45, 45, 45, 45, 45, 45}},
    67  	{Uint32, []uint32{0, 1, 2, 3, 4, 5}, uint32(45), Shape{2, 3}, []uint32{45, 45, 45, 45, 45, 45}},
    68  	{Uint64, []uint64{0, 1, 2, 3, 4, 5}, uint64(45), Shape{2, 3}, []uint64{45, 45, 45, 45, 45, 45}},
    69  	{Float32, []float32{0, 1, 2, 3, 4, 5}, float32(45), Shape{2, 3}, []float32{45, 45, 45, 45, 45, 45}},
    70  	{Float64, []float64{0, 1, 2, 3, 4, 5}, float64(45), Shape{2, 3}, []float64{45, 45, 45, 45, 45, 45}},
    71  	{Complex64, []complex64{0, 1, 2, 3, 4, 5}, complex64(45), Shape{2, 3}, []complex64{45, 45, 45, 45, 45, 45}},
    72  	{Complex128, []complex128{0, 1, 2, 3, 4, 5}, complex128(45), Shape{2, 3}, []complex128{45, 45, 45, 45, 45, 45}},
    73  	{String, []string{"zero", "one", "two", "three", "four", "five"}, string("HELLO WORLD"), Shape{2, 3}, []string{"HELLO WORLD", "HELLO WORLD", "HELLO WORLD", "HELLO WORLD", "HELLO WORLD", "HELLO WORLD"}},
    74  }
    75  
    76  func TestDense_memset(t *testing.T) {
    77  	assert := assert.New(t)
    78  	for _, mts := range denseMemsetTests {
    79  		T := New(Of(mts.of), WithShape(mts.shape...))
    80  		T.Memset(mts.val)
    81  		assert.Equal(mts.correct, T.Data())
    82  
    83  		T = New(Of(mts.of), WithShape(mts.shape...), WithBacking(mts.data))
    84  		T2, _ := T.Slice(nil)
    85  		T2.Memset(mts.val)
    86  		assert.Equal(mts.correct, T2.Data())
    87  	}
    88  }
    89  
    90  var denseZeroTests = []struct {
    91  	of   Dtype
    92  	data interface{}
    93  
    94  	correct interface{}
    95  }{
    96  	{Bool, []bool{true, false, true, false, true, false}, []bool{false, false, false, false, false, false}},
    97  	{Int, []int{0, 1, 2, 3, 4, 5}, []int{0, 0, 0, 0, 0, 0}},
    98  	{Int8, []int8{0, 1, 2, 3, 4, 5}, []int8{0, 0, 0, 0, 0, 0}},
    99  	{Int16, []int16{0, 1, 2, 3, 4, 5}, []int16{0, 0, 0, 0, 0, 0}},
   100  	{Int32, []int32{0, 1, 2, 3, 4, 5}, []int32{0, 0, 0, 0, 0, 0}},
   101  	{Int64, []int64{0, 1, 2, 3, 4, 5}, []int64{0, 0, 0, 0, 0, 0}},
   102  	{Uint, []uint{0, 1, 2, 3, 4, 5}, []uint{0, 0, 0, 0, 0, 0}},
   103  	{Uint8, []uint8{0, 1, 2, 3, 4, 5}, []uint8{0, 0, 0, 0, 0, 0}},
   104  	{Uint16, []uint16{0, 1, 2, 3, 4, 5}, []uint16{0, 0, 0, 0, 0, 0}},
   105  	{Uint32, []uint32{0, 1, 2, 3, 4, 5}, []uint32{0, 0, 0, 0, 0, 0}},
   106  	{Uint64, []uint64{0, 1, 2, 3, 4, 5}, []uint64{0, 0, 0, 0, 0, 0}},
   107  	{Float32, []float32{0, 1, 2, 3, 4, 5}, []float32{0, 0, 0, 0, 0, 0}},
   108  	{Float64, []float64{0, 1, 2, 3, 4, 5}, []float64{0, 0, 0, 0, 0, 0}},
   109  	{Complex64, []complex64{0, 1, 2, 3, 4, 5}, []complex64{0, 0, 0, 0, 0, 0}},
   110  	{Complex128, []complex128{0, 1, 2, 3, 4, 5}, []complex128{0, 0, 0, 0, 0, 0}},
   111  	{String, []string{"zero", "one", "two", "three", "four", "five"}, []string{"", "", "", "", "", ""}},
   112  }
   113  
   114  func TestDense_Zero(t *testing.T) {
   115  	assert := assert.New(t)
   116  	for _, mts := range denseZeroTests {
   117  
   118  		typ := reflect.TypeOf(mts.data)
   119  		val := reflect.ValueOf(mts.data)
   120  		data := reflect.MakeSlice(typ, val.Len(), val.Cap())
   121  		reflect.Copy(data, val)
   122  
   123  		T := New(Of(mts.of), WithBacking(data.Interface()))
   124  		T.Zero()
   125  		assert.Equal(mts.correct, T.Data())
   126  
   127  		T = New(Of(mts.of), WithBacking(mts.data))
   128  		T2, _ := T.Slice(nil)
   129  		T2.Zero()
   130  		assert.Equal(mts.correct, T2.Data())
   131  	}
   132  }
   133  
   134  func TestDense_Eq(t *testing.T) {
   135  	eqFn := func(q *Dense) bool {
   136  		a := q.Clone().(*Dense)
   137  		if !q.Eq(a) {
   138  			t.Error("Expected a clone to be exactly equal")
   139  			return false
   140  		}
   141  		a.Zero()
   142  
   143  		// Bools are excluded because the probability of having an array of all false is very high
   144  		if q.Eq(a) && a.len() > 3 && a.Dtype() != Bool {
   145  			t.Errorf("a %v", a.Data())
   146  			t.Errorf("q %v", q.Data())
   147  			t.Error("Expected *Dense to be not equal")
   148  			return false
   149  		}
   150  		return true
   151  	}
   152  	if err := quick.Check(eqFn, &quick.Config{Rand: newRand(), MaxCount: quickchecks}); err != nil {
   153  		t.Errorf("Failed to perform equality checks")
   154  	}
   155  }