OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / internal / asm / f64 / scal_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 (
8         "fmt"
9         "testing"
10
11         "golang.org/x/exp/rand"
12 )
13
14 var scalTests = []struct {
15         alpha float64
16         x     []float64
17         want  []float64
18 }{
19         {
20                 alpha: 0,
21                 x:     []float64{},
22                 want:  []float64{},
23         },
24         {
25                 alpha: 0,
26                 x:     []float64{1},
27                 want:  []float64{0},
28         },
29         {
30                 alpha: 1,
31                 x:     []float64{1},
32                 want:  []float64{1},
33         },
34         {
35                 alpha: 2,
36                 x:     []float64{1, -2},
37                 want:  []float64{2, -4},
38         },
39         {
40                 alpha: 2,
41                 x:     []float64{1, -2, 3},
42                 want:  []float64{2, -4, 6},
43         },
44         {
45                 alpha: 2,
46                 x:     []float64{1, -2, 3, 4},
47                 want:  []float64{2, -4, 6, 8},
48         },
49         {
50                 alpha: 2,
51                 x:     []float64{1, -2, 3, 4, -5},
52                 want:  []float64{2, -4, 6, 8, -10},
53         },
54         {
55                 alpha: 2,
56                 x:     []float64{0, 1, -2, 3, 4, -5, 6, -7},
57                 want:  []float64{0, 2, -4, 6, 8, -10, 12, -14},
58         },
59         {
60                 alpha: 2,
61                 x:     []float64{0, 1, -2, 3, 4, -5, 6, -7, 8},
62                 want:  []float64{0, 2, -4, 6, 8, -10, 12, -14, 16},
63         },
64         {
65                 alpha: 2,
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},
68         },
69         {
70                 alpha: 3,
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},
73         },
74 }
75
76 func TestScalUnitary(t *testing.T) {
77         const xGdVal = -0.5
78         for i, test := range scalTests {
79                 for _, align := range align1 {
80                         prefix := fmt.Sprintf("Test %v (x:%v)", i, align)
81                         xgLn := 4 + align
82                         xg := guardVector(test.x, xGdVal, xgLn)
83                         x := xg[xgLn : len(xg)-xgLn]
84
85                         ScalUnitary(test.alpha, x)
86
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])
90                                 }
91                         }
92                         if !isValidGuard(xg, xGdVal, xgLn) {
93                                 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
94                         }
95                 }
96         }
97 }
98
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 {
103                 n := len(test.x)
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]
110
111                         ScalUnitaryTo(dst, test.alpha, x)
112
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])
116                                 }
117                         }
118                         if !isValidGuard(xg, xGdVal, xgLn) {
119                                 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
120                         }
121                         if !isValidGuard(dg, dstGdVal, dgLn) {
122                                 t.Errorf(msgGuard, prefix, "y", dg[:dgLn], dg[len(dg)-dgLn:])
123                         }
124                         if !equalStrided(test.x, x, 1) {
125                                 t.Errorf("%v: modified read-only x argument", prefix)
126                         }
127                 }
128         }
129 }
130
131 func TestScalInc(t *testing.T) {
132         const xGdVal = -0.5
133         gdLn := 4
134         for i, test := range scalTests {
135                 n := len(test.x)
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]
140
141                         ScalInc(test.alpha, x, uintptr(n), uintptr(incX))
142
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])
146                                 }
147                         }
148                         checkValidIncGuard(t, xg, xGdVal, incX, gdLn)
149                 }
150         }
151 }
152
153 func TestScalIncTo(t *testing.T) {
154         const xGdVal, dstGdVal = -1, 0.5
155         gdLn := 4
156         rng := rand.New(rand.NewSource(42))
157         for i, test := range scalTests {
158                 n := len(test.x)
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]
164
165                         ScalIncTo(dst, uintptr(inc.y), test.alpha, x, uintptr(n), uintptr(inc.x))
166
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])
170                                 }
171                         }
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)
176                         }
177
178                 }
179         }
180 }