1 // Copyright ©2015 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.
13 msgVal = "%v: unexpected value at %v Got: %v Expected: %v"
14 msgGuard = "%v: Guard violated in %s vector %v %v"
17 var axpyTests = []struct {
22 wantRev []float64 // Result when x is traversed in reverse direction.
36 wantRev: []float64{-3},
43 wantRev: []float64{-1},
50 wantRev: []float64{3},
57 wantRev: []float64{-9},
63 want: []float64{3, 2},
64 wantRev: []float64{7, -2},
68 x: []float64{2, 3, 4},
69 y: []float64{-3, -2, -1},
70 want: []float64{-1, 1, 3},
71 wantRev: []float64{1, 1, 1},
75 x: []float64{0, 0, 1, 1, 2, -3, -4},
76 y: []float64{0, 1, 0, 3, -4, 5, -6},
77 want: []float64{0, 1, 0, 3, -4, 5, -6},
78 wantRev: []float64{0, 1, 0, 3, -4, 5, -6},
82 x: []float64{0, 0, 1, 1, 2, -3, -4},
83 y: []float64{0, 1, 0, 3, -4, 5, -6},
84 want: []float64{0, 1, 1, 4, -2, 2, -10},
85 wantRev: []float64{-4, -2, 2, 4, -3, 5, -6},
89 x: []float64{0, 0, 1, 1, 2, -3, -4},
90 y: []float64{0, 1, 0, 3, -4, 5, -6},
91 want: []float64{0, 1, 3, 6, 2, -4, -18},
92 wantRev: []float64{-12, -8, 6, 6, -1, 5, -6},
96 x: []float64{0, 0, 1, 1, 2, -3, -4, 0, 0, 1, 1, 2, -3, -4},
97 y: []float64{0, 1, 0, 3, -4, 5, -6, 0, 1, 0, 3, -4, 5, -6},
98 want: []float64{0, 1, -3, 0, -10, 14, 6, 0, 1, -3, 0, -10, 14, 6},
99 wantRev: []float64{12, 10, -6, 0, -7, 5, -6, 12, 10, -6, 0, -7, 5, -6},
103 x: []float64{0, 0, 1, 1, 2, -3, -4, 5, 1, 2, -3, -4, 5},
104 y: []float64{0, 1, 0, 3, -4, 5, -6, 7, 3, -4, 5, -6, 7},
105 want: []float64{0, 1, -5, -2, -14, 20, 14, -18, -2, -14, 20, 14, -18},
106 wantRev: []float64{-25, 21, 15, -7, -9, -20, 14, 22, -7, -9, 0, -6, 7},
110 func TestAxpyUnitary(t *testing.T) {
111 const xGdVal, yGdVal = -1, 0.5
112 for i, test := range axpyTests {
113 for _, align := range align2 {
114 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y)
115 xgLn, ygLn := 4+align.x, 4+align.y
116 xg, yg := guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
117 x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
118 AxpyUnitary(test.alpha, x, y)
119 for i := range test.want {
120 if !same(y[i], test.want[i]) {
121 t.Errorf(msgVal, prefix, i, y[i], test.want[i])
124 if !isValidGuard(xg, xGdVal, xgLn) {
125 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
127 if !isValidGuard(yg, yGdVal, ygLn) {
128 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
130 if !equalStrided(test.x, x, 1) {
131 t.Errorf("%v: modified read-only x argument", prefix)
137 func TestAxpyUnitaryTo(t *testing.T) {
138 const dstGdVal, xGdVal, yGdVal = 1, -1, 0.5
139 for i, test := range axpyTests {
140 for _, align := range align3 {
141 prefix := fmt.Sprintf("Test %v (x:%v y:%v dst:%v)", i, align.x, align.y, align.dst)
143 dgLn, xgLn, ygLn := 4+align.dst, 4+align.x, 4+align.y
144 dstOrig := make([]float64, len(test.x))
145 xg, yg := guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
146 dstg := guardVector(dstOrig, dstGdVal, dgLn)
147 x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
148 dst := dstg[dgLn : len(dstg)-dgLn]
150 AxpyUnitaryTo(dst, test.alpha, x, y)
151 for i := range test.want {
152 if !same(dst[i], test.want[i]) {
153 t.Errorf(msgVal, prefix, i, dst[i], test.want[i])
156 if !isValidGuard(xg, xGdVal, xgLn) {
157 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
159 if !isValidGuard(yg, yGdVal, ygLn) {
160 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
162 if !isValidGuard(dstg, dstGdVal, dgLn) {
163 t.Errorf(msgGuard, prefix, "dst", dstg[:dgLn], dstg[len(dstg)-dgLn:])
165 if !equalStrided(test.x, x, 1) {
166 t.Errorf("%v: modified read-only x argument", prefix)
168 if !equalStrided(test.y, y, 1) {
169 t.Errorf("%v: modified read-only y argument", prefix)
175 func TestAxpyInc(t *testing.T) {
176 const xGdVal, yGdVal = -1, 0.5
178 for i, test := range axpyTests {
180 for _, inc := range newIncSet(-7, -4, -3, -2, -1, 1, 2, 3, 4, 7) {
183 ix = (-n + 1) * inc.x
186 iy = (-n + 1) * inc.y
188 prefix := fmt.Sprintf("test %v, inc.x = %v, inc.y = %v", i, inc.x, inc.y)
189 xg := guardIncVector(test.x, xGdVal, inc.x, gdLn)
190 yg := guardIncVector(test.y, yGdVal, inc.y, gdLn)
191 x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
193 AxpyInc(test.alpha, x, y, uintptr(n),
194 uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
203 for i := range want {
204 if !same(y[i*inc.y], want[i]) {
205 t.Errorf(msgVal, prefix, i, y[iy+i*inc.y], want[i])
208 if !equalStrided(test.x, x, inc.x) {
209 t.Errorf("%v: modified read-only x argument", prefix)
211 checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
212 checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn)
217 func TestAxpyIncTo(t *testing.T) {
218 const dstGdVal, xGdVal, yGdVal = 1, -1, 0.5
221 for i, test := range axpyTests {
223 for _, inc := range newIncToSet(-7, -4, -3, -2, -1, 1, 2, 3, 4, 7) {
224 var ix, iy, idst uintptr
226 ix = uintptr((-n + 1) * inc.x)
229 iy = uintptr((-n + 1) * inc.y)
232 idst = uintptr((-n + 1) * inc.dst)
235 prefix := fmt.Sprintf("Test %v: (x: %v, y: %v, dst:%v)", i, inc.x, inc.y, inc.dst)
236 dstOrig := make([]float64, len(test.want))
237 xg := guardIncVector(test.x, xGdVal, inc.x, gdLn)
238 yg := guardIncVector(test.y, yGdVal, inc.y, gdLn)
239 dstg := guardIncVector(dstOrig, dstGdVal, inc.dst, gdLn)
240 x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
241 dst := dstg[gdLn : len(dstg)-gdLn]
243 AxpyIncTo(dst, uintptr(inc.dst), idst,
244 test.alpha, x, y, uintptr(n),
245 uintptr(inc.x), uintptr(inc.y), ix, iy)
250 var iW, incW int = 0, 1
251 if inc.y*inc.dst < 0 {
252 iW, incW = len(want)-1, -1
257 for i := range want {
258 if !same(dst[i*inc.dst], want[iW+i*incW]) {
259 t.Errorf(msgVal, prefix, i, dst[i*inc.dst], want[iW+i*incW])
263 checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
264 checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn)
265 checkValidIncGuard(t, dstg, dstGdVal, inc.dst, gdLn)
266 if !equalStrided(test.x, x, inc.x) {
267 t.Errorf("%v: modified read-only x argument", prefix)
269 if !equalStrided(test.y, y, inc.y) {
270 t.Errorf("%v: modified read-only y argument", prefix)