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.
11 "golang.org/x/exp/rand"
14 var scalTests = []struct {
37 want: []float64{2, -4},
41 x: []float64{1, -2, 3},
42 want: []float64{2, -4, 6},
46 x: []float64{1, -2, 3, 4},
47 want: []float64{2, -4, 6, 8},
51 x: []float64{1, -2, 3, 4, -5},
52 want: []float64{2, -4, 6, 8, -10},
56 x: []float64{0, 1, -2, 3, 4, -5, 6, -7},
57 want: []float64{0, 2, -4, 6, 8, -10, 12, -14},
61 x: []float64{0, 1, -2, 3, 4, -5, 6, -7, 8},
62 want: []float64{0, 2, -4, 6, 8, -10, 12, -14, 16},
66 x: []float64{0, 1, -2, 3, 4, -5, 6, -7, 8, 9},
67 want: []float64{0, 2, -4, 6, 8, -10, 12, -14, 16, 18},
71 x: []float64{0, 1, -2, 3, 4, -5, 6, -7, 8, 9, 12},
72 want: []float64{0, 3, -6, 9, 12, -15, 18, -21, 24, 27, 36},
76 func TestScalUnitary(t *testing.T) {
78 for i, test := range scalTests {
79 for _, align := range align1 {
80 prefix := fmt.Sprintf("Test %v (x:%v)", i, align)
82 xg := guardVector(test.x, xGdVal, xgLn)
83 x := xg[xgLn : len(xg)-xgLn]
85 ScalUnitary(test.alpha, x)
87 for i := range test.want {
88 if !same(x[i], test.want[i]) {
89 t.Errorf(msgVal, prefix, i, x[i], test.want[i])
92 if !isValidGuard(xg, xGdVal, xgLn) {
93 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
99 func TestScalUnitaryTo(t *testing.T) {
100 const xGdVal, dstGdVal = -1, 0.5
101 rng := rand.New(rand.NewSource(42))
102 for i, test := range scalTests {
104 for _, align := range align2 {
105 prefix := fmt.Sprintf("Test %v (x:%v dst:%v)", i, align.x, align.y)
106 xgLn, dgLn := 4+align.x, 4+align.y
107 xg := guardVector(test.x, xGdVal, xgLn)
108 dg := guardVector(randSlice(n, 1, rng), dstGdVal, dgLn)
109 x, dst := xg[xgLn:len(xg)-xgLn], dg[dgLn:len(dg)-dgLn]
111 ScalUnitaryTo(dst, test.alpha, x)
113 for i := range test.want {
114 if !same(dst[i], test.want[i]) {
115 t.Errorf(msgVal, prefix, i, dst[i], test.want[i])
118 if !isValidGuard(xg, xGdVal, xgLn) {
119 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
121 if !isValidGuard(dg, dstGdVal, dgLn) {
122 t.Errorf(msgGuard, prefix, "y", dg[:dgLn], dg[len(dg)-dgLn:])
124 if !equalStrided(test.x, x, 1) {
125 t.Errorf("%v: modified read-only x argument", prefix)
131 func TestScalInc(t *testing.T) {
134 for i, test := range scalTests {
136 for _, incX := range []int{1, 2, 3, 4, 7, 10} {
137 prefix := fmt.Sprintf("Test %v (x:%v)", i, incX)
138 xg := guardIncVector(test.x, xGdVal, incX, gdLn)
139 x := xg[gdLn : len(xg)-gdLn]
141 ScalInc(test.alpha, x, uintptr(n), uintptr(incX))
143 for i := range test.want {
144 if !same(x[i*incX], test.want[i]) {
145 t.Errorf(msgVal, prefix, i, x[i*incX], test.want[i])
148 checkValidIncGuard(t, xg, xGdVal, incX, gdLn)
153 func TestScalIncTo(t *testing.T) {
154 const xGdVal, dstGdVal = -1, 0.5
156 rng := rand.New(rand.NewSource(42))
157 for i, test := range scalTests {
159 for _, inc := range newIncSet(1, 2, 3, 4, 7, 10) {
160 prefix := fmt.Sprintf("test %v (x:%v dst:%v)", i, inc.x, inc.y)
161 xg := guardIncVector(test.x, xGdVal, inc.x, gdLn)
162 dg := guardIncVector(randSlice(n, 1, rng), dstGdVal, inc.y, gdLn)
163 x, dst := xg[gdLn:len(xg)-gdLn], dg[gdLn:len(dg)-gdLn]
165 ScalIncTo(dst, uintptr(inc.y), test.alpha, x, uintptr(n), uintptr(inc.x))
167 for i := range test.want {
168 if !same(dst[i*inc.y], test.want[i]) {
169 t.Errorf(msgVal, prefix, i, dst[i*inc.y], test.want[i])
172 checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
173 checkValidIncGuard(t, dg, dstGdVal, inc.y, gdLn)
174 if !equalStrided(test.x, x, inc.x) {
175 t.Errorf("%v: modified read-only x argument", prefix)