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 msgVal = "%v: unexpected value at %v Got: %v Expected: %v"
15 msgGuard = "%v: Guard violated in %s vector %v %v"
19 inf = float32(math.Inf(1))
22 var dotTests = []struct {
24 wantu, wantc complex64
25 wantuRev, wantcRev complex64
33 wantuRev: 0, wantcRev: 0,
36 x: []complex64{1 + 1i},
37 y: []complex64{1 + 1i},
39 wantu: 0 + 2i, wantc: 2,
40 wantuRev: 0 + 2i, wantcRev: 2,
43 x: []complex64{1 + 2i},
44 y: []complex64{1 + 1i},
46 wantu: -1 + 3i, wantc: 3 - 1i,
47 wantuRev: -1 + 3i, wantcRev: 3 - 1i,
50 x: []complex64{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i},
51 y: []complex64{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i},
53 wantu: -210 + 2860i, wantc: 2870 + 0i,
54 wantuRev: -210 + 1540i, wantcRev: 1550 + 0i,
57 x: []complex64{1 + 1i, 1 + 1i, 1 + 2i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 3i, 1 + 1i, 1 + 1i, 1 + 4i},
58 y: []complex64{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i},
60 wantu: -22 + 36i, wantc: 42 + 4i,
61 wantuRev: -22 + 36i, wantcRev: 42 + 4i,
64 x: []complex64{1 + 1i, 1 + 1i, 2 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 2 + 1i},
65 y: []complex64{1 + 2i, 1 + 2i, 1 + 3i, 1 + 2i, 1 + 3i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i},
67 wantu: -10 + 37i, wantc: 34 + 17i,
68 wantuRev: -10 + 36i, wantcRev: 34 + 16i,
71 x: []complex64{1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, complex(inf, 1), 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i},
72 y: []complex64{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i},
74 wantu: complex(inf, inf), wantc: complex(inf, inf),
75 wantuRev: complex(inf, inf), wantcRev: complex(inf, inf),
79 func TestDotcUnitary(t *testing.T) {
81 for i, test := range dotTests {
82 for _, align := range align2 {
83 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y)
84 xgLn, ygLn := 4+align.x, 4+align.y
85 xg, yg := guardVector(test.x, gd, xgLn), guardVector(test.y, gd, ygLn)
86 x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
87 res := DotcUnitary(x, y)
88 if !same(res, test.wantc) {
89 t.Errorf(msgVal, prefix, i, res, test.wantc)
91 if !isValidGuard(xg, gd, xgLn) {
92 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
94 if !isValidGuard(yg, gd, ygLn) {
95 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
101 func TestDotcInc(t *testing.T) {
102 const gd, gdLn = 2 + 5i, 4
103 for i, test := range dotTests {
104 for _, inc := range newIncSet(1, 2, 5, 10, -1, -2, -5, -10) {
105 xg, yg := guardIncVector(test.x, gd, inc.x, gdLn), guardIncVector(test.y, gd, inc.y, gdLn)
106 x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
110 ix, want = -inc.x*(test.n-1), test.wantcRev
113 iy, want = -inc.y*(test.n-1), test.wantcRev
115 prefix := fmt.Sprintf("Test %v (x:%v y:%v) (ix:%v iy:%v)", i, inc.x, inc.y, ix, iy)
116 res := DotcInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
120 if !same(res, want) {
121 t.Errorf(msgVal, prefix, i, res, want)
124 checkValidIncGuard(t, xg, gd, inc.x, gdLn)
125 checkValidIncGuard(t, yg, gd, inc.y, gdLn)
130 func TestDotuUnitary(t *testing.T) {
132 for i, test := range dotTests {
133 for _, align := range align2 {
134 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y)
135 xgLn, ygLn := 4+align.x, 4+align.y
136 xg, yg := guardVector(test.x, gd, xgLn), guardVector(test.y, gd, ygLn)
137 x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
138 res := DotuUnitary(x, y)
139 if !same(res, test.wantu) {
140 t.Errorf(msgVal, prefix, i, res, test.wantu)
142 if !isValidGuard(xg, gd, xgLn) {
143 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
145 if !isValidGuard(yg, gd, ygLn) {
146 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
152 func TestDotuInc(t *testing.T) {
153 const gd, gdLn = 1 + 5i, 4
154 for i, test := range dotTests {
155 for _, inc := range newIncSet(1, 2, 5, 10, -1, -2, -5, -10) {
156 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, inc.x, inc.y)
157 xg, yg := guardIncVector(test.x, gd, inc.x, gdLn), guardIncVector(test.y, gd, inc.y, gdLn)
158 x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
162 ix, want = -inc.x*(test.n-1), test.wantuRev
165 iy, want = -inc.y*(test.n-1), test.wantuRev
167 res := DotuInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
171 if !same(res, want) {
172 t.Errorf(msgVal, prefix, i, res, want)
174 checkValidIncGuard(t, xg, gd, inc.x, gdLn)
175 checkValidIncGuard(t, yg, gd, inc.y, gdLn)