gonum.org/v1/gonum@v0.14.0/blas/testblas/dzasum.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 testblas
     6  
     7  import (
     8  	"testing"
     9  
    10  	"golang.org/x/exp/rand"
    11  
    12  	"gonum.org/v1/gonum/floats/scalar"
    13  )
    14  
    15  type Dzasumer interface {
    16  	Dzasum(n int, x []complex128, incX int) float64
    17  }
    18  
    19  func DzasumTest(t *testing.T, impl Dzasumer) {
    20  	const tol = 1e-14
    21  	rnd := rand.New(rand.NewSource(1))
    22  	for _, n := range []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 50, 100} {
    23  		for _, incX := range []int{-5, 1, 2, 10} {
    24  			aincX := abs(incX)
    25  			var x []complex128
    26  			if n > 0 {
    27  				x = make([]complex128, (n-1)*aincX+1)
    28  			}
    29  			for i := range x {
    30  				x[i] = znan
    31  			}
    32  			for i := 0; i < n; i++ {
    33  				re := float64(2*i + 1)
    34  				if rnd.Intn(2) == 0 {
    35  					re *= -1
    36  				}
    37  				im := float64(2 * (i + 1))
    38  				if rnd.Intn(2) == 0 {
    39  					im *= -1
    40  				}
    41  				x[i*aincX] = complex(re, im)
    42  			}
    43  
    44  			want := float64(n * (2*n + 1))
    45  			got := impl.Dzasum(n, x, incX)
    46  
    47  			if incX < 0 {
    48  				if got != 0 {
    49  					t.Errorf("Case n=%v,incX=%v: non-zero result when incX < 0. got %v", n, incX, got)
    50  				}
    51  				continue
    52  			}
    53  			if !scalar.EqualWithinAbsOrRel(got, want, tol, tol) {
    54  				t.Errorf("Case n=%v,incX=%v: unexpected result. want %v, got %v", n, incX, want, got)
    55  			}
    56  		}
    57  	}
    58  }