1 // Copyright ©2017 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.
14 msgRes = "%v: unexpected result Got: %v Expected: %v"
15 msgGuard = "%v: Guard violated in %s vector %v %v"
18 var dotTests = []struct {
20 sWant float32 // single-precision
21 dWant float64 // double-precision
22 sWantRev float32 // single-precision increment
23 dWantRev float64 // double-precision increment
32 sWantRev: 0, dWantRev: 0,
40 sWantRev: 0, dWantRev: 0,
48 sWantRev: 1, dWantRev: 1,
52 x: []float32{1, 2, 3, 4, 5, 6, 7, 8},
53 y: []float32{2, 2, 2, 2, 2, 2, 2, 2},
56 sWantRev: 72, dWantRev: 72,
60 x: []float32{math.MaxFloat32},
63 sWant: inf, dWant: 2 * float64(math.MaxFloat32),
64 sWantRev: inf, dWantRev: 2 * float64(math.MaxFloat32),
68 x: []float32{1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2},
69 y: []float32{3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2},
72 sWantRev: 80, dWantRev: 80,
77 func TestDotUnitary(t *testing.T) {
78 const xGdVal, yGdVal = 0.5, 0.25
79 for i, test := range dotTests {
80 for _, align := range align2 {
81 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y)
82 xgLn, ygLn := 8+align.x, 8+align.y
83 xg, yg := guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
84 x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
85 res := DotUnitary(x, y)
86 if !same(res, test.sWant) {
87 t.Errorf(msgRes, prefix, res, test.sWant)
89 if !isValidGuard(xg, xGdVal, xgLn) {
90 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
92 if !isValidGuard(yg, yGdVal, ygLn) {
93 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
99 func TestDotInc(t *testing.T) {
100 const xGdVal, yGdVal, gdLn = 0.5, 0.25, 8
101 for i, test := range dotTests {
102 for _, inc := range newIncSet(1, 2, 3, 4, 7, 10, -1, -2, -5, -10) {
103 xg, yg := guardIncVector(test.x, xGdVal, inc.x, gdLn), guardIncVector(test.y, yGdVal, inc.y, gdLn)
104 x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
108 ix = -inc.x * (test.n - 1)
111 iy = -inc.y * (test.n - 1)
116 prefix := fmt.Sprintf("Test %v (x:%v y:%v) (ix:%v iy:%v)", i, inc.x, inc.y, ix, iy)
117 res := DotInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
118 if !same(res, want) {
119 t.Errorf(msgRes, prefix, res, want)
121 checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
122 checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn)
127 func TestDdotUnitary(t *testing.T) {
128 const xGdVal, yGdVal = 0.5, 0.25
129 for i, test := range dotTests {
130 for _, align := range align2 {
131 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y)
132 xgLn, ygLn := 8+align.x, 8+align.y
133 xg, yg := guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
134 x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
135 res := DdotUnitary(x, y)
136 if !same64(res, test.dWant) {
137 t.Errorf(msgRes, prefix, res, test.dWant)
139 if !isValidGuard(xg, xGdVal, xgLn) {
140 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
142 if !isValidGuard(yg, yGdVal, ygLn) {
143 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
149 func TestDdotInc(t *testing.T) {
150 const xGdVal, yGdVal, gdLn = 0.5, 0.25, 8
151 for i, test := range dotTests {
152 for _, inc := range newIncSet(1, 2, 3, 4, 7, 10, -1, -2, -5, -10) {
153 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, inc.x, inc.y)
154 xg, yg := guardIncVector(test.x, xGdVal, inc.x, gdLn), guardIncVector(test.y, yGdVal, inc.y, gdLn)
155 x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
159 ix = -inc.x * (test.n - 1)
162 iy = -inc.y * (test.n - 1)
167 res := DdotInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
168 if !same64(res, want) {
169 t.Errorf(msgRes, prefix, res, want)
171 checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
172 checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn)