github.com/gopherd/gonum@v0.0.4/internal/asm/c128/asm_test.go (about)

     1  // Copyright ©2015 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 c128_test
     6  
     7  import (
     8  	"math"
     9  	"math/cmplx"
    10  	"testing"
    11  
    12  	"github.com/gopherd/gonum/cmplxs/cscalar"
    13  	"github.com/gopherd/gonum/floats/scalar"
    14  )
    15  
    16  const (
    17  	msgVal   = "%v: unexpected value at %v Got: %v Expected: %v"
    18  	msgGuard = "%v: Guard violated in %s vector %v %v"
    19  )
    20  
    21  var (
    22  	nan = math.NaN()
    23  
    24  	cnan = cmplx.NaN()
    25  	cinf = cmplx.Inf()
    26  )
    27  
    28  // TODO(kortschak): Harmonise the situation in asm/{f32,f64} and their sinks.
    29  const testLen = 1e5
    30  
    31  var x = make([]complex128, testLen)
    32  
    33  // guardVector copies the source vector (vec) into a new slice with guards.
    34  // Guards guarded[:gdLn] and guarded[len-gdLn:] will be filled with sigil value gdVal.
    35  func guardVector(vec []complex128, gdVal complex128, gdLn int) (guarded []complex128) {
    36  	guarded = make([]complex128, len(vec)+gdLn*2)
    37  	copy(guarded[gdLn:], vec)
    38  	for i := 0; i < gdLn; i++ {
    39  		guarded[i] = gdVal
    40  		guarded[len(guarded)-1-i] = gdVal
    41  	}
    42  	return guarded
    43  }
    44  
    45  // isValidGuard will test for violated guards, generated by guardVector.
    46  func isValidGuard(vec []complex128, gdVal complex128, gdLn int) bool {
    47  	for i := 0; i < gdLn; i++ {
    48  		if !cscalar.Same(vec[i], gdVal) || !cscalar.Same(vec[len(vec)-1-i], gdVal) {
    49  			return false
    50  		}
    51  	}
    52  	return true
    53  }
    54  
    55  // guardIncVector copies the source vector (vec) into a new incremented slice with guards.
    56  // End guards will be length gdLen.
    57  // Internal and end guards will be filled with sigil value gdVal.
    58  func guardIncVector(vec []complex128, gdVal complex128, inc, gdLen int) (guarded []complex128) {
    59  	if inc < 0 {
    60  		inc = -inc
    61  	}
    62  	inrLen := len(vec) * inc
    63  	guarded = make([]complex128, inrLen+gdLen*2)
    64  	for i := range guarded {
    65  		guarded[i] = gdVal
    66  	}
    67  	for i, v := range vec {
    68  		guarded[gdLen+i*inc] = v
    69  	}
    70  	return guarded
    71  }
    72  
    73  // checkValidIncGuard will test for violated guards, generated by guardIncVector
    74  func checkValidIncGuard(t *testing.T, vec []complex128, gdVal complex128, inc, gdLen int) {
    75  	srcLn := len(vec) - 2*gdLen
    76  	for i := range vec {
    77  		switch {
    78  		case cscalar.Same(vec[i], gdVal):
    79  			// Correct value
    80  		case (i-gdLen)%inc == 0 && (i-gdLen)/inc < len(vec):
    81  			// Ignore input values
    82  		case i < gdLen:
    83  			t.Errorf("Front guard violated at %d %v", i, vec[:gdLen])
    84  		case i > gdLen+srcLn:
    85  			t.Errorf("Back guard violated at %d %v", i-gdLen-srcLn, vec[gdLen+srcLn:])
    86  		default:
    87  			t.Errorf("Internal guard violated at %d %v", i-gdLen, vec[gdLen:gdLen+srcLn])
    88  		}
    89  	}
    90  }
    91  
    92  // sameApprox tests for nan-aware equality within tolerance.
    93  func sameApprox(a, b, tol float64) bool {
    94  	return scalar.Same(a, b) || scalar.EqualWithinAbsOrRel(a, b, tol, tol)
    95  }
    96  
    97  // sameCmplxApprox tests for nan-aware equality within tolerance.
    98  func sameCmplxApprox(a, b complex128, tol float64) bool {
    99  	return cscalar.Same(a, b) || cscalar.EqualWithinAbsOrRel(a, b, tol, tol)
   100  }
   101  
   102  var ( // Offset sets for testing alignment handling in Unitary assembly functions.
   103  	align1 = []int{0, 1}
   104  )