github.com/gopherd/gonum@v0.0.4/blas/testblas/zswap.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  	"fmt"
     9  	"testing"
    10  
    11  	"math/rand"
    12  )
    13  
    14  type Zswaper interface {
    15  	Zswap(n int, x []complex128, incX int, y []complex128, incY int)
    16  }
    17  
    18  func ZswapTest(t *testing.T, impl Zswaper) {
    19  	rnd := rand.New(rand.NewSource(1))
    20  	for n := 0; n < 20; n++ {
    21  		for _, inc := range allPairs([]int{-5, -1, 1, 2, 5, 10}, []int{-3, -1, 1, 3, 7, 12}) {
    22  			incX := inc[0]
    23  			incY := inc[1]
    24  			aincX := abs(incX)
    25  			aincY := abs(incY)
    26  
    27  			var x, y []complex128
    28  			if n > 0 {
    29  				x = make([]complex128, (n-1)*aincX+1)
    30  				y = make([]complex128, (n-1)*aincY+1)
    31  			}
    32  			for i := range x {
    33  				x[i] = znan
    34  			}
    35  			for i := range y {
    36  				y[i] = znan
    37  			}
    38  			for i := 0; i < n; i++ {
    39  				x[i*aincX] = complex(rnd.NormFloat64(), rnd.NormFloat64())
    40  				y[i*aincY] = complex(rnd.NormFloat64(), rnd.NormFloat64())
    41  			}
    42  
    43  			xWant := make([]complex128, len(x))
    44  			for i := range xWant {
    45  				xWant[i] = znan
    46  			}
    47  			yWant := make([]complex128, len(y))
    48  			for i := range yWant {
    49  				yWant[i] = znan
    50  			}
    51  			if incX*incY > 0 {
    52  				for i := 0; i < n; i++ {
    53  					xWant[i*aincX] = y[i*aincY]
    54  					yWant[i*aincY] = x[i*aincX]
    55  				}
    56  			} else {
    57  				for i := 0; i < n; i++ {
    58  					xWant[(n-i-1)*aincX] = y[i*aincY]
    59  					yWant[(n-i-1)*aincY] = x[i*aincX]
    60  				}
    61  			}
    62  
    63  			impl.Zswap(n, x, incX, y, incY)
    64  
    65  			prefix := fmt.Sprintf("Case n=%v,incX=%v,incY=%v:", n, incX, incY)
    66  			if !zsame(x, xWant) {
    67  				t.Errorf("%v: unexpected x:\nwant %v\ngot %v", prefix, xWant, x)
    68  			}
    69  			if !zsame(y, yWant) {
    70  				t.Errorf("%v: unexpected y:\nwant %v\ngot %v", prefix, yWant, y)
    71  			}
    72  		}
    73  	}
    74  }