OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / internal / asm / f64 / stubs_test.go
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
6
7 import "testing"
8
9 func TestL1Norm(t *testing.T) {
10         var src_gd float64 = 1
11         for j, v := range []struct {
12                 want float64
13                 x    []float64
14         }{
15                 {want: 0, x: []float64{}},
16                 {want: 2, x: []float64{2}},
17                 {want: 6, x: []float64{1, 2, 3}},
18                 {want: 6, x: []float64{-1, -2, -3}},
19                 {want: nan, x: []float64{nan}},
20                 {want: 40, x: []float64{8, -8, 8, -8, 8}},
21                 {want: 5, x: []float64{0, 1, 0, -1, 0, 1, 0, -1, 0, 1}},
22         } {
23                 g_ln := 4 + j%2
24                 v.x = guardVector(v.x, src_gd, g_ln)
25                 src := v.x[g_ln : len(v.x)-g_ln]
26                 ret := L1Norm(src)
27                 if !same(ret, v.want) {
28                         t.Errorf("Test %d L1Norm error Got: %f Expected: %f", j, ret, v.want)
29                 }
30                 if !isValidGuard(v.x, src_gd, g_ln) {
31                         t.Errorf("Test %d Guard violated in src vector %v %v", j, v.x[:g_ln], v.x[len(v.x)-g_ln:])
32                 }
33         }
34 }
35
36 func TestL1NormInc(t *testing.T) {
37         var src_gd float64 = 1
38         for j, v := range []struct {
39                 inc  int
40                 want float64
41                 x    []float64
42         }{
43                 {inc: 2, want: 0, x: []float64{}},
44                 {inc: 3, want: 2, x: []float64{2}},
45                 {inc: 10, want: 6, x: []float64{1, 2, 3}},
46                 {inc: 5, want: 6, x: []float64{-1, -2, -3}},
47                 {inc: 3, want: nan, x: []float64{nan}},
48                 {inc: 15, want: 40, x: []float64{8, -8, 8, -8, 8}},
49                 {inc: 1, want: 5, x: []float64{0, 1, 0, -1, 0, 1, 0, -1, 0, 1}},
50         } {
51                 g_ln, ln := 4+j%2, len(v.x)
52                 v.x = guardIncVector(v.x, src_gd, v.inc, g_ln)
53                 src := v.x[g_ln : len(v.x)-g_ln]
54                 ret := L1NormInc(src, ln, v.inc)
55                 if !same(ret, v.want) {
56                         t.Errorf("Test %d L1NormInc error Got: %f Expected: %f", j, ret, v.want)
57                 }
58                 checkValidIncGuard(t, v.x, src_gd, v.inc, g_ln)
59         }
60 }
61
62 func TestAdd(t *testing.T) {
63         var src_gd, dst_gd float64 = 1, 0
64         for j, v := range []struct {
65                 dst, src, expect []float64
66         }{
67                 {
68                         dst:    []float64{1},
69                         src:    []float64{0},
70                         expect: []float64{1},
71                 },
72                 {
73                         dst:    []float64{1, 2, 3},
74                         src:    []float64{1},
75                         expect: []float64{2, 2, 3},
76                 },
77                 {
78                         dst:    []float64{},
79                         src:    []float64{},
80                         expect: []float64{},
81                 },
82                 {
83                         dst:    []float64{1},
84                         src:    []float64{nan},
85                         expect: []float64{nan},
86                 },
87                 {
88                         dst:    []float64{8, 8, 8, 8, 8},
89                         src:    []float64{2, 4, nan, 8, 9},
90                         expect: []float64{10, 12, nan, 16, 17},
91                 },
92                 {
93                         dst:    []float64{0, 1, 2, 3, 4},
94                         src:    []float64{-inf, 4, nan, 8, 9},
95                         expect: []float64{-inf, 5, nan, 11, 13},
96                 },
97                 {
98                         dst:    make([]float64, 50)[1:49],
99                         src:    make([]float64, 50)[1:49],
100                         expect: make([]float64, 50)[1:49],
101                 },
102         } {
103                 sg_ln, dg_ln := 4+j%2, 4+j%3
104                 v.src, v.dst = guardVector(v.src, src_gd, sg_ln), guardVector(v.dst, dst_gd, dg_ln)
105                 src, dst := v.src[sg_ln:len(v.src)-sg_ln], v.dst[dg_ln:len(v.dst)-dg_ln]
106                 Add(dst, src)
107                 for i := range v.expect {
108                         if !same(dst[i], v.expect[i]) {
109                                 t.Errorf("Test %d Add error at %d Got: %v Expected: %v", j, i, dst[i], v.expect[i])
110                         }
111                 }
112                 if !isValidGuard(v.src, src_gd, sg_ln) {
113                         t.Errorf("Test %d Guard violated in src vector %v %v", j, v.src[:sg_ln], v.src[len(v.src)-sg_ln:])
114                 }
115                 if !isValidGuard(v.dst, dst_gd, dg_ln) {
116                         t.Errorf("Test %d Guard violated in dst vector %v %v", j, v.dst[:dg_ln], v.dst[len(v.dst)-dg_ln:])
117                 }
118         }
119 }
120
121 func TestAddConst(t *testing.T) {
122         var src_gd float64 = 0
123         for j, v := range []struct {
124                 alpha       float64
125                 src, expect []float64
126         }{
127                 {
128                         alpha:  1,
129                         src:    []float64{0},
130                         expect: []float64{1},
131                 },
132                 {
133                         alpha:  5,
134                         src:    []float64{},
135                         expect: []float64{},
136                 },
137                 {
138                         alpha:  1,
139                         src:    []float64{nan},
140                         expect: []float64{nan},
141                 },
142                 {
143                         alpha:  8,
144                         src:    []float64{2, 4, nan, 8, 9},
145                         expect: []float64{10, 12, nan, 16, 17},
146                 },
147                 {
148                         alpha:  inf,
149                         src:    []float64{-inf, 4, nan, 8, 9},
150                         expect: []float64{nan, inf, nan, inf, inf},
151                 },
152         } {
153                 g_ln := 4 + j%2
154                 v.src = guardVector(v.src, src_gd, g_ln)
155                 src := v.src[g_ln : len(v.src)-g_ln]
156                 AddConst(v.alpha, src)
157                 for i := range v.expect {
158                         if !same(src[i], v.expect[i]) {
159                                 t.Errorf("Test %d AddConst error at %d Got: %v Expected: %v", j, i, src[i], v.expect[i])
160                         }
161                 }
162                 if !isValidGuard(v.src, src_gd, g_ln) {
163                         t.Errorf("Test %d Guard violated in src vector %v %v", j, v.src[:g_ln], v.src[len(v.src)-g_ln:])
164                 }
165         }
166 }
167
168 func TestCumSum(t *testing.T) {
169         var src_gd, dst_gd float64 = -1, 0
170         for j, v := range []struct {
171                 dst, src, expect []float64
172         }{
173                 {
174                         dst:    []float64{},
175                         src:    []float64{},
176                         expect: []float64{},
177                 },
178                 {
179                         dst:    []float64{0},
180                         src:    []float64{1},
181                         expect: []float64{1},
182                 },
183                 {
184                         dst:    []float64{nan},
185                         src:    []float64{nan},
186                         expect: []float64{nan},
187                 },
188                 {
189                         dst:    []float64{0, 0, 0},
190                         src:    []float64{1, 2, 3},
191                         expect: []float64{1, 3, 6},
192                 },
193                 {
194                         dst:    []float64{0, 0, 0, 0},
195                         src:    []float64{1, 2, 3},
196                         expect: []float64{1, 3, 6},
197                 },
198                 {
199                         dst:    []float64{0, 0, 0, 0},
200                         src:    []float64{1, 2, 3, 4},
201                         expect: []float64{1, 3, 6, 10},
202                 },
203                 {
204                         dst:    []float64{1, nan, nan, 1, 1},
205                         src:    []float64{1, 1, nan, 1, 1},
206                         expect: []float64{1, 2, nan, nan, nan},
207                 },
208                 {
209                         dst:    []float64{nan, 4, inf, -inf, 9},
210                         src:    []float64{inf, 4, nan, -inf, 9},
211                         expect: []float64{inf, inf, nan, nan, nan},
212                 },
213                 {
214                         dst:    make([]float64, 16),
215                         src:    []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
216                         expect: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
217                 },
218         } {
219                 g_ln := 4 + j%2
220                 v.src, v.dst = guardVector(v.src, src_gd, g_ln), guardVector(v.dst, dst_gd, g_ln)
221                 src, dst := v.src[g_ln:len(v.src)-g_ln], v.dst[g_ln:len(v.dst)-g_ln]
222                 ret := CumSum(dst, src)
223                 for i := range v.expect {
224                         if !same(ret[i], v.expect[i]) {
225                                 t.Errorf("Test %d CumSum error at %d Got: %v Expected: %v", j, i, ret[i], v.expect[i])
226                         }
227                         if !same(ret[i], dst[i]) {
228                                 t.Errorf("Test %d CumSum ret/dst mismatch %d Ret: %v Dst: %v", j, i, ret[i], dst[i])
229                         }
230                 }
231                 if !isValidGuard(v.src, src_gd, g_ln) {
232                         t.Errorf("Test %d Guard violated in src vector %v %v", j, v.src[:g_ln], v.src[len(v.src)-g_ln:])
233                 }
234                 if !isValidGuard(v.dst, dst_gd, g_ln) {
235                         t.Errorf("Test %d Guard violated in dst vector %v %v", j, v.dst[:g_ln], v.dst[len(v.dst)-g_ln:])
236                 }
237         }
238 }
239
240 func TestCumProd(t *testing.T) {
241         var src_gd, dst_gd float64 = -1, 1
242         for j, v := range []struct {
243                 dst, src, expect []float64
244         }{
245                 {
246                         dst:    []float64{},
247                         src:    []float64{},
248                         expect: []float64{},
249                 },
250                 {
251                         dst:    []float64{1},
252                         src:    []float64{1},
253                         expect: []float64{1},
254                 },
255                 {
256                         dst:    []float64{nan},
257                         src:    []float64{nan},
258                         expect: []float64{nan},
259                 },
260                 {
261                         dst:    []float64{0, 0, 0, 0},
262                         src:    []float64{1, 2, 3, 4},
263                         expect: []float64{1, 2, 6, 24},
264                 },
265                 {
266                         dst:    []float64{0, 0, 0},
267                         src:    []float64{1, 2, 3},
268                         expect: []float64{1, 2, 6},
269                 },
270                 {
271                         dst:    []float64{0, 0, 0, 0},
272                         src:    []float64{1, 2, 3},
273                         expect: []float64{1, 2, 6},
274                 },
275                 {
276                         dst:    []float64{nan, 1, nan, 1, 0},
277                         src:    []float64{1, 1, nan, 1, 1},
278                         expect: []float64{1, 1, nan, nan, nan},
279                 },
280                 {
281                         dst:    []float64{nan, 4, nan, -inf, 9},
282                         src:    []float64{inf, 4, nan, -inf, 9},
283                         expect: []float64{inf, inf, nan, nan, nan},
284                 },
285                 {
286                         dst:    make([]float64, 18),
287                         src:    []float64{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
288                         expect: []float64{2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536},
289                 },
290         } {
291                 sg_ln, dg_ln := 4+j%2, 4+j%3
292                 v.src, v.dst = guardVector(v.src, src_gd, sg_ln), guardVector(v.dst, dst_gd, dg_ln)
293                 src, dst := v.src[sg_ln:len(v.src)-sg_ln], v.dst[dg_ln:len(v.dst)-dg_ln]
294                 ret := CumProd(dst, src)
295                 for i := range v.expect {
296                         if !same(ret[i], v.expect[i]) {
297                                 t.Errorf("Test %d CumProd error at %d Got: %v Expected: %v", j, i, ret[i], v.expect[i])
298                         }
299                         if !same(ret[i], dst[i]) {
300                                 t.Errorf("Test %d CumProd ret/dst mismatch %d Ret: %v Dst: %v", j, i, ret[i], dst[i])
301                         }
302                 }
303                 if !isValidGuard(v.src, src_gd, sg_ln) {
304                         t.Errorf("Test %d Guard violated in src vector %v %v", j, v.src[:sg_ln], v.src[len(v.src)-sg_ln:])
305                 }
306                 if !isValidGuard(v.dst, dst_gd, dg_ln) {
307                         t.Errorf("Test %d Guard violated in dst vector %v %v", j, v.dst[:dg_ln], v.dst[len(v.dst)-dg_ln:])
308                 }
309         }
310 }
311
312 func TestDiv(t *testing.T) {
313         var src_gd, dst_gd float64 = -1, 0.5
314         for j, v := range []struct {
315                 dst, src, expect []float64
316         }{
317                 {
318                         dst:    []float64{1},
319                         src:    []float64{1},
320                         expect: []float64{1},
321                 },
322                 {
323                         dst:    []float64{nan},
324                         src:    []float64{nan},
325                         expect: []float64{nan},
326                 },
327                 {
328                         dst:    []float64{1, 2, 3, 4},
329                         src:    []float64{1, 2, 3, 4},
330                         expect: []float64{1, 1, 1, 1},
331                 },
332                 {
333                         dst:    []float64{1, 2, 3, 4, 2, 4, 6, 8},
334                         src:    []float64{1, 2, 3, 4, 1, 2, 3, 4},
335                         expect: []float64{1, 1, 1, 1, 2, 2, 2, 2},
336                 },
337                 {
338                         dst:    []float64{2, 4, 6},
339                         src:    []float64{1, 2, 3},
340                         expect: []float64{2, 2, 2},
341                 },
342                 {
343                         dst:    []float64{0, 0, 0, 0},
344                         src:    []float64{1, 2, 3},
345                         expect: []float64{0, 0, 0},
346                 },
347                 {
348                         dst:    []float64{nan, 1, nan, 1, 0, nan, 1, nan, 1, 0},
349                         src:    []float64{1, 1, nan, 1, 1, 1, 1, nan, 1, 1},
350                         expect: []float64{nan, 1, nan, 1, 0, nan, 1, nan, 1, 0},
351                 },
352                 {
353                         dst:    []float64{inf, 4, nan, -inf, 9, inf, 4, nan, -inf, 9},
354                         src:    []float64{inf, 4, nan, -inf, 3, inf, 4, nan, -inf, 3},
355                         expect: []float64{nan, 1, nan, nan, 3, nan, 1, nan, nan, 3},
356                 },
357         } {
358                 sg_ln, dg_ln := 4+j%2, 4+j%3
359                 v.src, v.dst = guardVector(v.src, src_gd, sg_ln), guardVector(v.dst, dst_gd, dg_ln)
360                 src, dst := v.src[sg_ln:len(v.src)-sg_ln], v.dst[dg_ln:len(v.dst)-dg_ln]
361                 Div(dst, src)
362                 for i := range v.expect {
363                         if !same(dst[i], v.expect[i]) {
364                                 t.Errorf("Test %d Div error at %d Got: %v Expected: %v", j, i, dst[i], v.expect[i])
365                         }
366                 }
367                 if !isValidGuard(v.src, src_gd, sg_ln) {
368                         t.Errorf("Test %d Guard violated in src vector %v %v", j, v.src[:sg_ln], v.src[len(v.src)-sg_ln:])
369                 }
370                 if !isValidGuard(v.dst, dst_gd, dg_ln) {
371                         t.Errorf("Test %d Guard violated in dst vector %v %v", j, v.dst[:dg_ln], v.dst[len(v.dst)-dg_ln:])
372                 }
373         }
374 }
375
376 func TestDivTo(t *testing.T) {
377         var dst_gd, x_gd, y_gd float64 = -1, 0.5, 0.25
378         for j, v := range []struct {
379                 dst, x, y, expect []float64
380         }{
381                 {
382                         dst:    []float64{1},
383                         x:      []float64{1},
384                         y:      []float64{1},
385                         expect: []float64{1},
386                 },
387                 {
388                         dst:    []float64{1},
389                         x:      []float64{nan},
390                         y:      []float64{nan},
391                         expect: []float64{nan},
392                 },
393                 {
394                         dst:    []float64{-2, -2, -2},
395                         x:      []float64{1, 2, 3},
396                         y:      []float64{1, 2, 3},
397                         expect: []float64{1, 1, 1},
398                 },
399                 {
400                         dst:    []float64{0, 0, 0},
401                         x:      []float64{2, 4, 6},
402                         y:      []float64{1, 2, 3, 4},
403                         expect: []float64{2, 2, 2},
404                 },
405                 {
406                         dst:    []float64{-1, -1, -1},
407                         x:      []float64{0, 0, 0},
408                         y:      []float64{1, 2, 3},
409                         expect: []float64{0, 0, 0},
410                 },
411                 {
412                         dst:    []float64{inf, inf, inf, inf, inf, inf, inf, inf, inf, inf},
413                         x:      []float64{nan, 1, nan, 1, 0, nan, 1, nan, 1, 0},
414                         y:      []float64{1, 1, nan, 1, 1, 1, 1, nan, 1, 1},
415                         expect: []float64{nan, 1, nan, 1, 0, nan, 1, nan, 1, 0},
416                 },
417                 {
418                         dst:    []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
419                         x:      []float64{inf, 4, nan, -inf, 9, inf, 4, nan, -inf, 9},
420                         y:      []float64{inf, 4, nan, -inf, 3, inf, 4, nan, -inf, 3},
421                         expect: []float64{nan, 1, nan, nan, 3, nan, 1, nan, nan, 3},
422                 },
423         } {
424                 xg_ln, yg_ln := 4+j%2, 4+j%3
425                 v.y, v.x = guardVector(v.y, y_gd, yg_ln), guardVector(v.x, x_gd, xg_ln)
426                 y, x := v.y[yg_ln:len(v.y)-yg_ln], v.x[xg_ln:len(v.x)-xg_ln]
427                 v.dst = guardVector(v.dst, dst_gd, xg_ln)
428                 dst := v.dst[xg_ln : len(v.dst)-xg_ln]
429                 ret := DivTo(dst, x, y)
430                 for i := range v.expect {
431                         if !same(ret[i], v.expect[i]) {
432                                 t.Errorf("Test %d DivTo error at %d Got: %v Expected: %v", j, i, ret[i], v.expect[i])
433                         }
434                         if !same(ret[i], dst[i]) {
435                                 t.Errorf("Test %d DivTo ret/dst mismatch %d Ret: %v Dst: %v", j, i, ret[i], dst[i])
436                         }
437                 }
438                 if !isValidGuard(v.y, y_gd, yg_ln) {
439                         t.Errorf("Test %d Guard violated in y vector %v %v", j, v.y[:yg_ln], v.y[len(v.y)-yg_ln:])
440                 }
441                 if !isValidGuard(v.x, x_gd, xg_ln) {
442                         t.Errorf("Test %d Guard violated in x vector %v %v", j, v.x[:xg_ln], v.x[len(v.x)-xg_ln:])
443                 }
444                 if !isValidGuard(v.dst, dst_gd, xg_ln) {
445                         t.Errorf("Test %d Guard violated in dst vector %v %v", j, v.dst[:xg_ln], v.dst[len(v.dst)-xg_ln:])
446                 }
447         }
448 }
449
450 func TestL1Dist(t *testing.T) {
451         var t_gd, s_gd float64 = -inf, inf
452         for j, v := range []struct {
453                 s, t   []float64
454                 expect float64
455         }{
456                 {
457                         s:      []float64{1},
458                         t:      []float64{1},
459                         expect: 0,
460                 },
461                 {
462                         s:      []float64{nan},
463                         t:      []float64{nan},
464                         expect: nan,
465                 },
466                 {
467                         s:      []float64{1, 2, 3, 4},
468                         t:      []float64{1, 2, 3, 4},
469                         expect: 0,
470                 },
471                 {
472                         s:      []float64{2, 4, 6},
473                         t:      []float64{1, 2, 3, 4},
474                         expect: 6,
475                 },
476                 {
477                         s:      []float64{0, 0, 0},
478                         t:      []float64{1, 2, 3},
479                         expect: 6,
480                 },
481                 {
482                         s:      []float64{0, -4, -10},
483                         t:      []float64{1, 2, 3},
484                         expect: 20,
485                 },
486                 {
487                         s:      []float64{0, 1, 0, 1, 0},
488                         t:      []float64{1, 1, inf, 1, 1},
489                         expect: inf,
490                 },
491                 {
492                         s:      []float64{inf, 4, nan, -inf, 9},
493                         t:      []float64{inf, 4, nan, -inf, 3},
494                         expect: nan,
495                 },
496         } {
497                 sg_ln, tg_ln := 4+j%2, 4+j%3
498                 v.s, v.t = guardVector(v.s, s_gd, sg_ln), guardVector(v.t, t_gd, tg_ln)
499                 s_lc, t_lc := v.s[sg_ln:len(v.s)-sg_ln], v.t[tg_ln:len(v.t)-tg_ln]
500                 ret := L1Dist(s_lc, t_lc)
501                 if !same(ret, v.expect) {
502                         t.Errorf("Test %d L1Dist error Got: %f Expected: %f", j, ret, v.expect)
503                 }
504                 if !isValidGuard(v.s, s_gd, sg_ln) {
505                         t.Errorf("Test %d Guard violated in s vector %v %v", j, v.s[:sg_ln], v.s[len(v.s)-sg_ln:])
506                 }
507                 if !isValidGuard(v.t, t_gd, tg_ln) {
508                         t.Errorf("Test %d Guard violated in t vector %v %v", j, v.t[:tg_ln], v.t[len(v.t)-tg_ln:])
509                 }
510         }
511 }
512
513 func TestLinfDist(t *testing.T) {
514         var t_gd, s_gd float64 = 0, inf
515         for j, v := range []struct {
516                 s, t   []float64
517                 expect float64
518         }{
519                 {
520                         s:      []float64{},
521                         t:      []float64{},
522                         expect: 0,
523                 },
524                 {
525                         s:      []float64{1},
526                         t:      []float64{1},
527                         expect: 0,
528                 },
529                 {
530                         s:      []float64{nan},
531                         t:      []float64{nan},
532                         expect: nan,
533                 },
534                 {
535                         s:      []float64{1, 2, 3, 4},
536                         t:      []float64{1, 2, 3, 4},
537                         expect: 0,
538                 },
539                 {
540                         s:      []float64{2, 4, 6},
541                         t:      []float64{1, 2, 3, 4},
542                         expect: 3,
543                 },
544                 {
545                         s:      []float64{0, 0, 0},
546                         t:      []float64{1, 2, 3},
547                         expect: 3,
548                 },
549                 {
550                         s:      []float64{0, 1, 0, 1, 0},
551                         t:      []float64{1, 1, inf, 1, 1},
552                         expect: inf,
553                 },
554                 {
555                         s:      []float64{inf, 4, nan, -inf, 9},
556                         t:      []float64{inf, 4, nan, -inf, 3},
557                         expect: 6,
558                 },
559         } {
560                 sg_ln, tg_ln := 4+j%2, 4+j%3
561                 v.s, v.t = guardVector(v.s, s_gd, sg_ln), guardVector(v.t, t_gd, tg_ln)
562                 s_lc, t_lc := v.s[sg_ln:len(v.s)-sg_ln], v.t[tg_ln:len(v.t)-tg_ln]
563                 ret := LinfDist(s_lc, t_lc)
564                 if !same(ret, v.expect) {
565                         t.Errorf("Test %d LinfDist error Got: %f Expected: %f", j, ret, v.expect)
566                 }
567                 if !isValidGuard(v.s, s_gd, sg_ln) {
568                         t.Errorf("Test %d Guard violated in s vector %v %v", j, v.s[:sg_ln], v.s[len(v.s)-sg_ln:])
569                 }
570                 if !isValidGuard(v.t, t_gd, tg_ln) {
571                         t.Errorf("Test %d Guard violated in t vector %v %v", j, v.t[:tg_ln], v.t[len(v.t)-tg_ln:])
572                 }
573         }
574 }