OSDN Git Service

test (#52)
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / internal / asm / f64 / axpy_test.go
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.
4
5 package f64
6
7 import (
8         "fmt"
9         "testing"
10 )
11
12 const (
13         msgVal   = "%v: unexpected value at %v Got: %v Expected: %v"
14         msgGuard = "%v: Guard violated in %s vector %v %v"
15 )
16
17 var axpyTests = []struct {
18         alpha   float64
19         x       []float64
20         y       []float64
21         want    []float64
22         wantRev []float64 // Result when x is traversed in reverse direction.
23 }{
24         {
25                 alpha:   0,
26                 x:       []float64{},
27                 y:       []float64{},
28                 want:    []float64{},
29                 wantRev: []float64{},
30         },
31         {
32                 alpha:   0,
33                 x:       []float64{2},
34                 y:       []float64{-3},
35                 want:    []float64{-3},
36                 wantRev: []float64{-3},
37         },
38         {
39                 alpha:   1,
40                 x:       []float64{2},
41                 y:       []float64{-3},
42                 want:    []float64{-1},
43                 wantRev: []float64{-1},
44         },
45         {
46                 alpha:   3,
47                 x:       []float64{2},
48                 y:       []float64{-3},
49                 want:    []float64{3},
50                 wantRev: []float64{3},
51         },
52         {
53                 alpha:   -3,
54                 x:       []float64{2},
55                 y:       []float64{-3},
56                 want:    []float64{-9},
57                 wantRev: []float64{-9},
58         },
59         {
60                 alpha:   1,
61                 x:       []float64{1, 5},
62                 y:       []float64{2, -3},
63                 want:    []float64{3, 2},
64                 wantRev: []float64{7, -2},
65         },
66         {
67                 alpha:   1,
68                 x:       []float64{2, 3, 4},
69                 y:       []float64{-3, -2, -1},
70                 want:    []float64{-1, 1, 3},
71                 wantRev: []float64{1, 1, 1},
72         },
73         {
74                 alpha:   0,
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},
79         },
80         {
81                 alpha:   1,
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},
86         },
87         {
88                 alpha:   3,
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},
93         },
94         {
95                 alpha:   -3,
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},
100         },
101         {
102                 alpha:   -5,
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},
107         },
108 }
109
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])
122                                 }
123                         }
124                         if !isValidGuard(xg, xGdVal, xgLn) {
125                                 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
126                         }
127                         if !isValidGuard(yg, yGdVal, ygLn) {
128                                 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
129                         }
130                         if !equalStrided(test.x, x, 1) {
131                                 t.Errorf("%v: modified read-only x argument", prefix)
132                         }
133                 }
134         }
135 }
136
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)
142
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]
149
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])
154                                 }
155                         }
156                         if !isValidGuard(xg, xGdVal, xgLn) {
157                                 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
158                         }
159                         if !isValidGuard(yg, yGdVal, ygLn) {
160                                 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
161                         }
162                         if !isValidGuard(dstg, dstGdVal, dgLn) {
163                                 t.Errorf(msgGuard, prefix, "dst", dstg[:dgLn], dstg[len(dstg)-dgLn:])
164                         }
165                         if !equalStrided(test.x, x, 1) {
166                                 t.Errorf("%v: modified read-only x argument", prefix)
167                         }
168                         if !equalStrided(test.y, y, 1) {
169                                 t.Errorf("%v: modified read-only y argument", prefix)
170                         }
171                 }
172         }
173 }
174
175 func TestAxpyInc(t *testing.T) {
176         const xGdVal, yGdVal = -1, 0.5
177         gdLn := 4
178         for i, test := range axpyTests {
179                 n := len(test.x)
180                 for _, inc := range newIncSet(-7, -4, -3, -2, -1, 1, 2, 3, 4, 7) {
181                         var ix, iy int
182                         if inc.x < 0 {
183                                 ix = (-n + 1) * inc.x
184                         }
185                         if inc.y < 0 {
186                                 iy = (-n + 1) * inc.y
187                         }
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]
192
193                         AxpyInc(test.alpha, x, y, uintptr(n),
194                                 uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
195
196                         want := test.want
197                         if inc.x*inc.y < 0 {
198                                 want = test.wantRev
199                         }
200                         if inc.y < 0 {
201                                 inc.y = -inc.y
202                         }
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])
206                                 }
207                         }
208                         if !equalStrided(test.x, x, inc.x) {
209                                 t.Errorf("%v: modified read-only x argument", prefix)
210                         }
211                         checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
212                         checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn)
213                 }
214         }
215 }
216
217 func TestAxpyIncTo(t *testing.T) {
218         const dstGdVal, xGdVal, yGdVal = 1, -1, 0.5
219         var want []float64
220         gdLn := 4
221         for i, test := range axpyTests {
222                 n := len(test.x)
223                 for _, inc := range newIncToSet(-7, -4, -3, -2, -1, 1, 2, 3, 4, 7) {
224                         var ix, iy, idst uintptr
225                         if inc.x < 0 {
226                                 ix = uintptr((-n + 1) * inc.x)
227                         }
228                         if inc.y < 0 {
229                                 iy = uintptr((-n + 1) * inc.y)
230                         }
231                         if inc.dst < 0 {
232                                 idst = uintptr((-n + 1) * inc.dst)
233                         }
234
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]
242
243                         AxpyIncTo(dst, uintptr(inc.dst), idst,
244                                 test.alpha, x, y, uintptr(n),
245                                 uintptr(inc.x), uintptr(inc.y), ix, iy)
246                         want = test.want
247                         if inc.x*inc.y < 0 {
248                                 want = test.wantRev
249                         }
250                         var iW, incW int = 0, 1
251                         if inc.y*inc.dst < 0 {
252                                 iW, incW = len(want)-1, -1
253                         }
254                         if inc.dst < 0 {
255                                 inc.dst = -inc.dst
256                         }
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])
260                                 }
261                         }
262
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)
268                         }
269                         if !equalStrided(test.y, y, inc.y) {
270                                 t.Errorf("%v: modified read-only y argument", prefix)
271                         }
272                 }
273         }
274 }