gonum.org/v1/gonum@v0.14.0/internal/asm/f64/scal_test.go (about) 1 // Copyright ©2016 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package f64_test 6 7 import ( 8 "fmt" 9 "testing" 10 11 "golang.org/x/exp/rand" 12 13 "gonum.org/v1/gonum/floats/scalar" 14 . "gonum.org/v1/gonum/internal/asm/f64" 15 ) 16 17 var scalTests = []struct { 18 alpha float64 19 x []float64 20 want []float64 21 }{ 22 { 23 alpha: 0, 24 x: []float64{}, 25 want: []float64{}, 26 }, 27 { 28 alpha: 0, 29 x: []float64{1}, 30 want: []float64{0}, 31 }, 32 { 33 alpha: 1, 34 x: []float64{1}, 35 want: []float64{1}, 36 }, 37 { 38 alpha: 2, 39 x: []float64{1, -2}, 40 want: []float64{2, -4}, 41 }, 42 { 43 alpha: 2, 44 x: []float64{1, -2, 3}, 45 want: []float64{2, -4, 6}, 46 }, 47 { 48 alpha: 2, 49 x: []float64{1, -2, 3, 4}, 50 want: []float64{2, -4, 6, 8}, 51 }, 52 { 53 alpha: 2, 54 x: []float64{1, -2, 3, 4, -5}, 55 want: []float64{2, -4, 6, 8, -10}, 56 }, 57 { 58 alpha: 2, 59 x: []float64{0, 1, -2, 3, 4, -5, 6, -7}, 60 want: []float64{0, 2, -4, 6, 8, -10, 12, -14}, 61 }, 62 { 63 alpha: 2, 64 x: []float64{0, 1, -2, 3, 4, -5, 6, -7, 8}, 65 want: []float64{0, 2, -4, 6, 8, -10, 12, -14, 16}, 66 }, 67 { 68 alpha: 2, 69 x: []float64{0, 1, -2, 3, 4, -5, 6, -7, 8, 9}, 70 want: []float64{0, 2, -4, 6, 8, -10, 12, -14, 16, 18}, 71 }, 72 { 73 alpha: 3, 74 x: []float64{0, 1, -2, 3, 4, -5, 6, -7, 8, 9, 12}, 75 want: []float64{0, 3, -6, 9, 12, -15, 18, -21, 24, 27, 36}, 76 }, 77 } 78 79 func TestScalUnitary(t *testing.T) { 80 const xGdVal = -0.5 81 for i, test := range scalTests { 82 for _, align := range align1 { 83 prefix := fmt.Sprintf("Test %v (x:%v)", i, align) 84 xgLn := 4 + align 85 xg := guardVector(test.x, xGdVal, xgLn) 86 x := xg[xgLn : len(xg)-xgLn] 87 88 ScalUnitary(test.alpha, x) 89 90 for i := range test.want { 91 if !scalar.Same(x[i], test.want[i]) { 92 t.Errorf(msgVal, prefix, i, x[i], test.want[i]) 93 } 94 } 95 if !isValidGuard(xg, xGdVal, xgLn) { 96 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:]) 97 } 98 } 99 } 100 } 101 102 func TestScalUnitaryTo(t *testing.T) { 103 const xGdVal, dstGdVal = -1, 0.5 104 rng := rand.New(rand.NewSource(42)) 105 for i, test := range scalTests { 106 n := len(test.x) 107 for _, align := range align2 { 108 prefix := fmt.Sprintf("Test %v (x:%v dst:%v)", i, align.x, align.y) 109 xgLn, dgLn := 4+align.x, 4+align.y 110 xg := guardVector(test.x, xGdVal, xgLn) 111 dg := guardVector(randSlice(n, 1, rng), dstGdVal, dgLn) 112 x, dst := xg[xgLn:len(xg)-xgLn], dg[dgLn:len(dg)-dgLn] 113 114 ScalUnitaryTo(dst, test.alpha, x) 115 116 for i := range test.want { 117 if !scalar.Same(dst[i], test.want[i]) { 118 t.Errorf(msgVal, prefix, i, dst[i], test.want[i]) 119 } 120 } 121 if !isValidGuard(xg, xGdVal, xgLn) { 122 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:]) 123 } 124 if !isValidGuard(dg, dstGdVal, dgLn) { 125 t.Errorf(msgGuard, prefix, "y", dg[:dgLn], dg[len(dg)-dgLn:]) 126 } 127 if !equalStrided(test.x, x, 1) { 128 t.Errorf(msgReadOnly, prefix, "x") 129 } 130 } 131 } 132 } 133 134 func TestScalInc(t *testing.T) { 135 const xGdVal = -0.5 136 gdLn := 4 137 for i, test := range scalTests { 138 n := len(test.x) 139 for _, incX := range []int{1, 2, 3, 4, 7, 10} { 140 prefix := fmt.Sprintf("Test %v (x:%v)", i, incX) 141 xg := guardIncVector(test.x, xGdVal, incX, gdLn) 142 x := xg[gdLn : len(xg)-gdLn] 143 144 ScalInc(test.alpha, x, uintptr(n), uintptr(incX)) 145 146 for i := range test.want { 147 if !scalar.Same(x[i*incX], test.want[i]) { 148 t.Errorf(msgVal, prefix, i, x[i*incX], test.want[i]) 149 } 150 } 151 checkValidIncGuard(t, xg, xGdVal, incX, gdLn) 152 } 153 } 154 } 155 156 func TestScalIncTo(t *testing.T) { 157 const xGdVal, dstGdVal = -1, 0.5 158 gdLn := 4 159 rng := rand.New(rand.NewSource(42)) 160 for i, test := range scalTests { 161 n := len(test.x) 162 for _, inc := range newIncSet(1, 2, 3, 4, 7, 10) { 163 prefix := fmt.Sprintf("test %v (x:%v dst:%v)", i, inc.x, inc.y) 164 xg := guardIncVector(test.x, xGdVal, inc.x, gdLn) 165 dg := guardIncVector(randSlice(n, 1, rng), dstGdVal, inc.y, gdLn) 166 x, dst := xg[gdLn:len(xg)-gdLn], dg[gdLn:len(dg)-gdLn] 167 168 ScalIncTo(dst, uintptr(inc.y), test.alpha, x, uintptr(n), uintptr(inc.x)) 169 170 for i := range test.want { 171 if !scalar.Same(dst[i*inc.y], test.want[i]) { 172 t.Errorf(msgVal, prefix, i, dst[i*inc.y], test.want[i]) 173 } 174 } 175 checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn) 176 checkValidIncGuard(t, dg, dstGdVal, inc.y, gdLn) 177 if !equalStrided(test.x, x, inc.x) { 178 t.Errorf("%v: modified read-only x argument", prefix) 179 } 180 181 } 182 } 183 }