gonum.org/v1/gonum@v0.14.0/internal/asm/c64/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 c64_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"gonum.org/v1/gonum/cmplxs/cscalar"
    11  	"gonum.org/v1/gonum/floats/scalar"
    12  	"gonum.org/v1/gonum/internal/cmplx64"
    13  	"gonum.org/v1/gonum/internal/math32"
    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 = math32.NaN()
    23  
    24  	cnan = cmplx64.NaN()
    25  	cinf = cmplx64.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([]complex64, 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 []complex64, gdVal complex64, gdLn int) (guarded []complex64) {
    36  	guarded = make([]complex64, 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 []complex64, gdVal complex64, gdLn int) bool {
    47  	for i := 0; i < gdLn; i++ {
    48  		if !sameCmplx(vec[i], gdVal) || !sameCmplx(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 []complex64, gdVal complex64, inc, gdLen int) (guarded []complex64) {
    59  	if inc < 0 {
    60  		inc = -inc
    61  	}
    62  	inrLen := len(vec) * inc
    63  	guarded = make([]complex64, 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 []complex64, gdVal complex64, inc, gdLen int) {
    75  	srcLn := len(vec) - 2*gdLen
    76  	for i := range vec {
    77  		switch {
    78  		case sameCmplx(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 float32) bool {
    94  	return scalar.Same(float64(a), float64(b)) || scalar.EqualWithinAbsOrRel(float64(a), float64(b), float64(tol), float64(tol))
    95  }
    96  
    97  // sameCmplx tests for nan-aware equality.
    98  func sameCmplx(a, b complex64) bool {
    99  	return cscalar.Same(complex128(a), complex128(b))
   100  }
   101  
   102  // sameCmplxApprox tests for nan-aware equality within tolerance.
   103  func sameCmplxApprox(a, b complex64, tol float32) bool {
   104  	return sameCmplx(a, b) || cscalar.EqualWithinAbsOrRel(complex128(a), complex128(b), float64(tol), float64(tol))
   105  }
   106  
   107  var ( // Offset sets for testing alignment handling in Unitary assembly functions.
   108  	align1 = []int{0, 1}
   109  )