internal/asm: clean up tolerant testing
MODIFY
internal/asm/f32/dot_test.go
internal/asm/f32/dot_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f32
+package f32_test
import (
"fmt"
"math"
"testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f32"
)
var dotTests = []struct {

MODIFY
internal/asm/f32/ge_test.go
internal/asm/f32/ge_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f32
+package f32_test
import (
"fmt"
"testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f32"
)
var gerTests = []struct {
@@ -143,6 +145,8 @@ var gerTests = []struct {
func TestGer(t *testing.T) {
const (
+ tol = 1e-6
+
xGdVal, yGdVal, aGdVal = -0.5, 1.5, 10
gdLn = 4
)
@@ -160,7 +164,7 @@ func TestGer(t *testing.T) {
var alpha float32 = 1.0
Ger(uintptr(m), uintptr(n), alpha, x, 1, y, 1, a, uintptr(n))
for i := range test.want {
- if !within(a[i], test.want[i]) {
+ if !sameApprox(a[i], test.want[i], tol) {
t.Errorf(msgVal, prefix, i, a[i], test.want[i])
return
}
@@ -195,7 +199,7 @@ func TestGer(t *testing.T) {
a, uintptr(n))
for i := range test.want {
want := alpha*test.x[i/n]*test.y[i%n] + test.a[i]
- if !within(a[i], want) {
+ if !sameApprox(a[i], want, tol) {
t.Errorf(msgVal, prefix, i, a[i], want)
}
}

MODIFY
internal/asm/f32/l2norm_test.go
internal/asm/f32/l2norm_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f32
+package f32_test
-import "testing"
+import (
+ "testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f32"
+)
func TestL2NormUnitary(t *testing.T) {
+ const tol = 1e-7
+
var src_gd float32 = 1
for j, v := range []struct {
want float32
@@ -24,7 +30,7 @@ func TestL2NormUnitary(t *testing.T) {
v.x = guardVector(v.x, src_gd, g_ln)
src := v.x[g_ln : len(v.x)-g_ln]
ret := L2NormUnitary(src)
- if !within(ret, v.want) {
+ if !sameApprox(ret, v.want, tol) {
t.Errorf("Test %d L2Norm error Got: %f Expected: %f", j, ret, v.want)
}
if !isValidGuard(v.x, src_gd, g_ln) {
@@ -34,6 +40,8 @@ func TestL2NormUnitary(t *testing.T) {
}
func TestL2NormInc(t *testing.T) {
+ const tol = 1e-7
+
var src_gd float32 = 1
for j, v := range []struct {
inc int
@@ -52,7 +60,7 @@ func TestL2NormInc(t *testing.T) {
v.x = guardIncVector(v.x, src_gd, v.inc, g_ln)
src := v.x[g_ln : len(v.x)-g_ln]
ret := L2NormInc(src, uintptr(ln), uintptr(v.inc))
- if !within(ret, v.want) {
+ if !sameApprox(ret, v.want, tol) {
t.Errorf("Test %d L2NormInc error Got: %f Expected: %f", j, ret, v.want)
}
checkValidIncGuard(t, v.x, src_gd, v.inc, g_ln)
@@ -60,6 +68,8 @@ func TestL2NormInc(t *testing.T) {
}
func TestL2DistanceUnitary(t *testing.T) {
+ const tol = 1e-7
+
var src_gd float32 = 1
for j, v := range []struct {
want float32
@@ -79,7 +89,7 @@ func TestL2DistanceUnitary(t *testing.T) {
srcX := v.x[g_ln : len(v.x)-g_ln]
srcY := v.y[g_ln : len(v.y)-g_ln]
ret := L2DistanceUnitary(srcX, srcY)
- if !within(ret, v.want) {
+ if !sameApprox(ret, v.want, tol) {
t.Errorf("Test %d L2Distance error Got: %f Expected: %f", j, ret, v.want)
}
if !isValidGuard(v.x, src_gd, g_ln) {

MODIFY
internal/asm/f32/stubs_test.go
internal/asm/f32/stubs_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f32
+package f32_test
-import "testing"
+import (
+ "testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f32"
+)
var tests = []struct {
incX, incY, incDst uintptr

MODIFY
internal/asm/f32/util_test.go
internal/asm/f32/util_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f32
+package f32_test
import (
"math"
"testing"
+
+ "gonum.org/v1/gonum/floats"
)
const (
@@ -14,7 +16,6 @@ const (
msgVal = "%v: unexpected value at %v Got: %v Expected: %v"
msgGuard = "%v: guard violated in %s vector %v %v"
msgReadOnly = "%v: modified read-only %v argument"
- epsilon = 5e-3
)
var (
@@ -22,10 +23,10 @@ var (
inf = float32(math.Inf(1))
)
-// within tests for nan-aware equality within epsilon.
-func within(x, y float32) bool {
+// sameApprox tests for nan-aware equality within tolerance.
+func sameApprox(x, y, tol float32) bool {
a, b := float64(x), float64(y)
- return same(x, y) || math.Abs(a-b) <= epsilon
+ return same(x, y) || floats.EqualWithinAbsOrRel(a, b, float64(tol), float64(tol))
}
func same(x, y float32) bool {

MODIFY
internal/asm/f64/asm_test.go
internal/asm/f64/asm_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"math"
"testing"
"golang.org/x/exp/rand"
+
+ "gonum.org/v1/gonum/floats"
)
const (
msgVal = "%v: unexpected value at %v Got: %v Expected: %v"
msgGuard = "%v: Guard violated in %s vector %v %v"
msgReadOnly = "%v: modified read-only %v argument"
- epsilon = 1e-12
)
var (
@@ -147,9 +148,9 @@ func same(a, b float64) bool {
return a == b || (math.IsNaN(a) && math.IsNaN(b))
}
-// within tests for nan-aware equality within epsilon.
-func within(a, b float64) bool {
- return same(a, b) || math.Abs(a-b) <= epsilon
+// sameApprox tests for nan-aware equality within tolerance.
+func sameApprox(a, b, tol float64) bool {
+ return same(a, b) || floats.EqualWithinAbsOrRel(a, b, tol, tol)
}
var ( // Offset sets for testing alignment handling in Unitary assembly functions.

MODIFY
internal/asm/f64/axpy_test.go
internal/asm/f64/axpy_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"fmt"
"testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f64"
)
var axpyTests = []struct {

MODIFY
internal/asm/f64/benchAxpy_test.go
internal/asm/f64/benchAxpy_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"fmt"
"testing"
-)
-const (
- testLen = 1e5
+ . "gonum.org/v1/gonum/internal/asm/f64"
)
+const testLen = 1e5
+
var (
a = 2.0
x = make([]float64, testLen)

MODIFY
internal/asm/f64/benchScal_test.go
internal/asm/f64/benchScal_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"fmt"
"testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f64"
)
var uniScal = []int64{1, 3, 10, 30, 1e2, 3e2, 1e3, 3e3, 1e4, 3e4}

MODIFY
internal/asm/f64/bench_other_test.go
internal/asm/f64/bench_other_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"math"
"testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f64"
)
func benchL1Norm(f func(x []float64) float64, sz int, t *testing.B) {

MODIFY
internal/asm/f64/dot_test.go
internal/asm/f64/dot_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"fmt"
@@ -10,6 +10,8 @@ import (
"testing"
"golang.org/x/exp/rand"
+
+ . "gonum.org/v1/gonum/internal/asm/f64"
)
func TestDotUnitary(t *testing.T) {

MODIFY
internal/asm/f64/ge_test.go
internal/asm/f64/ge_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"fmt"
"testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f64"
)
var gerTests = []struct {
@@ -105,6 +107,8 @@ var gerTests = []struct {
func TestGer(t *testing.T) {
const (
+ tol = 1e-15
+
xGdVal, yGdVal, aGdVal = -0.5, 1.5, 10
gdLn = 4
)
@@ -122,7 +126,7 @@ func TestGer(t *testing.T) {
alpha := 1.0
Ger(uintptr(m), uintptr(n), alpha, x, 1, y, 1, a, uintptr(n))
for i := range test.want {
- if !within(a[i], test.want[i]) {
+ if !sameApprox(a[i], test.want[i], tol) {
t.Errorf(msgVal, prefix, i, a[i], test.want[i])
t.Error(a)
return
@@ -157,7 +161,7 @@ func TestGer(t *testing.T) {
Ger(uintptr(m), uintptr(n), alpha, x, uintptr(inc.x), y, uintptr(inc.y), a, uintptr(n))
for i := range test.want {
want := alpha*(test.want[i]-test.a[i]) + test.a[i]
- if !within(a[i], want) {
+ if !sameApprox(a[i], want, tol) {
t.Errorf(msgVal, prefix, i, a[i], want)
}
}

MODIFY
internal/asm/f64/gemv_test.go
internal/asm/f64/gemv_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"fmt"
"testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f64"
)
type DgemvCase struct {
@@ -521,6 +523,8 @@ func TestGemv(t *testing.T) {
func dgemvcomp(t *testing.T, test DgemvCase, trans bool, cas DgemvSubcase, i int) {
const (
+ tol = 1e-15
+
xGdVal, yGdVal, aGdVal = 0.5, 1.5, 10
gdLn = 4
)
@@ -540,7 +544,7 @@ func dgemvcomp(t *testing.T, test DgemvCase, trans bool, cas DgemvSubcase, i int
GemvN(uintptr(test.m), uintptr(test.n), cas.alpha, a, lda, x, 1, cas.beta, y, 1)
}
for i := range cas.want {
- if !within(y[i], cas.want[i]) {
+ if !sameApprox(y[i], cas.want[i], tol) {
t.Errorf(msgVal, prefix, i, y[i], cas.want[i])
}
}
@@ -589,7 +593,7 @@ func dgemvcomp(t *testing.T, test DgemvCase, trans bool, cas DgemvSubcase, i int
cas.beta, y, uintptr(inc.y))
}
for i := range want {
- if !within(y[i*incY], want[i]) {
+ if !sameApprox(y[i*incY], want[i], tol) {
t.Errorf(msgVal, incPrefix, i, y[i*incY], want[i])
t.Error(y[i*incY] - want[i])
}

MODIFY
internal/asm/f64/l2norm_test.go
internal/asm/f64/l2norm_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"fmt"
"math"
"testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f64"
)
// nanwith copied from floats package
@@ -20,6 +22,8 @@ func nanwith(payload uint64) float64 {
}
func TestL2NormUnitary(t *testing.T) {
+ const tol = 1e-15
+
var src_gd float64 = 1
for j, v := range []struct {
want float64
@@ -38,7 +42,7 @@ func TestL2NormUnitary(t *testing.T) {
v.x = guardVector(v.x, src_gd, g_ln)
src := v.x[g_ln : len(v.x)-g_ln]
ret := L2NormUnitary(src)
- if !within(ret, v.want) {
+ if !sameApprox(ret, v.want, tol) {
t.Errorf("Test %d L2Norm error Got: %f Expected: %f", j, ret, v.want)
}
if !isValidGuard(v.x, src_gd, g_ln) {
@@ -48,6 +52,8 @@ func TestL2NormUnitary(t *testing.T) {
}
func TestL2NormInc(t *testing.T) {
+ const tol = 1e-15
+
var src_gd float64 = 1
for j, v := range []struct {
inc int
@@ -66,7 +72,7 @@ func TestL2NormInc(t *testing.T) {
v.x = guardIncVector(v.x, src_gd, v.inc, g_ln)
src := v.x[g_ln : len(v.x)-g_ln]
ret := L2NormInc(src, uintptr(ln), uintptr(v.inc))
- if !within(ret, v.want) {
+ if !sameApprox(ret, v.want, tol) {
t.Errorf("Test %d L2NormInc error Got: %f Expected: %f", j, ret, v.want)
}
checkValidIncGuard(t, v.x, src_gd, v.inc, g_ln)
@@ -74,6 +80,8 @@ func TestL2NormInc(t *testing.T) {
}
func TestL2DistanceUnitary(t *testing.T) {
+ const tol = 1e-15
+
var src_gd float64 = 1
for j, v := range []struct {
want float64
@@ -93,7 +101,7 @@ func TestL2DistanceUnitary(t *testing.T) {
srcX := v.x[g_ln : len(v.x)-g_ln]
srcY := v.y[g_ln : len(v.y)-g_ln]
ret := L2DistanceUnitary(srcX, srcY)
- if !within(ret, v.want) {
+ if !sameApprox(ret, v.want, tol) {
t.Errorf("Test %d L2Distance error Got: %f Expected: %f", j, ret, v.want)
}
if !isValidGuard(v.x, src_gd, g_ln) {

MODIFY
internal/asm/f64/scal_test.go
internal/asm/f64/scal_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
import (
"fmt"
"testing"
"golang.org/x/exp/rand"
+
+ . "gonum.org/v1/gonum/internal/asm/f64"
)
var scalTests = []struct {

MODIFY
internal/asm/f64/stubs_test.go
internal/asm/f64/stubs_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package f64
+package f64_test
-import "testing"
+import (
+ "testing"
+
+ . "gonum.org/v1/gonum/internal/asm/f64"
+)
func TestL1Norm(t *testing.T) {
var src_gd float64 = 1

internal/asm: clean up tolerant testing