gonum.org/v1/gonum@v0.14.0/internal/asm/c64/dot_test.go (about) 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. 4 5 package c64 6 7 import ( 8 "fmt" 9 "math" 10 "testing" 11 ) 12 13 const ( 14 msgVal = "%v: unexpected value at %v Got: %v Expected: %v" 15 msgGuard = "%v: Guard violated in %s vector %v %v" 16 ) 17 18 var ( 19 inf = float32(math.Inf(1)) 20 ) 21 22 var dotTests = []struct { 23 x, y []complex64 24 wantu, wantc complex64 25 wantuRev, wantcRev complex64 26 n int 27 }{ 28 { 29 x: []complex64{}, 30 y: []complex64{}, 31 n: 0, 32 wantu: 0, wantc: 0, 33 wantuRev: 0, wantcRev: 0, 34 }, 35 { 36 x: []complex64{1 + 1i}, 37 y: []complex64{1 + 1i}, 38 n: 1, 39 wantu: 0 + 2i, wantc: 2, 40 wantuRev: 0 + 2i, wantcRev: 2, 41 }, 42 { 43 x: []complex64{1 + 2i}, 44 y: []complex64{1 + 1i}, 45 n: 1, 46 wantu: -1 + 3i, wantc: 3 - 1i, 47 wantuRev: -1 + 3i, wantcRev: 3 - 1i, 48 }, 49 { 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}, 52 n: 10, 53 wantu: -210 + 2860i, wantc: 2870 + 0i, 54 wantuRev: -210 + 1540i, wantcRev: 1550 + 0i, 55 }, 56 { 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}, 59 n: 10, 60 wantu: -22 + 36i, wantc: 42 + 4i, 61 wantuRev: -22 + 36i, wantcRev: 42 + 4i, 62 }, 63 { 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}, 66 n: 10, 67 wantu: -10 + 37i, wantc: 34 + 17i, 68 wantuRev: -10 + 36i, wantcRev: 34 + 16i, 69 }, 70 { 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}, 73 n: 10, 74 wantu: complex(inf, inf), wantc: complex(inf, inf), 75 wantuRev: complex(inf, inf), wantcRev: complex(inf, inf), 76 }, 77 } 78 79 func TestDotcUnitary(t *testing.T) { 80 const gd = 1 + 5i 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) 90 } 91 if !isValidGuard(xg, gd, xgLn) { 92 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:]) 93 } 94 if !isValidGuard(yg, gd, ygLn) { 95 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:]) 96 } 97 } 98 } 99 } 100 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] 107 want := test.wantc 108 var ix, iy int 109 if inc.x < 0 { 110 ix, want = -inc.x*(test.n-1), test.wantcRev 111 } 112 if inc.y < 0 { 113 iy, want = -inc.y*(test.n-1), test.wantcRev 114 } 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)) 117 if inc.x*inc.y > 0 { 118 want = test.wantc 119 } 120 if !same(res, want) { 121 t.Errorf(msgVal, prefix, i, res, want) 122 t.Error(x, y) 123 } 124 checkValidIncGuard(t, xg, gd, inc.x, gdLn) 125 checkValidIncGuard(t, yg, gd, inc.y, gdLn) 126 } 127 } 128 } 129 130 func TestDotuUnitary(t *testing.T) { 131 const gd = 1 + 5i 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) 141 } 142 if !isValidGuard(xg, gd, xgLn) { 143 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:]) 144 } 145 if !isValidGuard(yg, gd, ygLn) { 146 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:]) 147 } 148 } 149 } 150 } 151 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] 159 want := test.wantc 160 var ix, iy int 161 if inc.x < 0 { 162 ix, want = -inc.x*(test.n-1), test.wantuRev 163 } 164 if inc.y < 0 { 165 iy, want = -inc.y*(test.n-1), test.wantuRev 166 } 167 res := DotuInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy)) 168 if inc.x*inc.y > 0 { 169 want = test.wantu 170 } 171 if !same(res, want) { 172 t.Errorf(msgVal, prefix, i, res, want) 173 } 174 checkValidIncGuard(t, xg, gd, inc.x, gdLn) 175 checkValidIncGuard(t, yg, gd, inc.y, gdLn) 176 } 177 } 178 }