gonum.org/v1/gonum@v0.14.0/internal/asm/c128/util_test.go (about)

     1  // Copyright ©2016 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
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  )
    11  
    12  const (
    13  	msgVal   = "%v: unexpected value at %v Got: %v Expected: %v"
    14  	msgGuard = "%v: Guard violated in %s vector %v %v"
    15  )
    16  
    17  var (
    18  	inf       = math.Inf(1)
    19  	benchSink complex128
    20  )
    21  
    22  func guardVector(vec []complex128, guard_val complex128, guard_len int) (guarded []complex128) {
    23  	guarded = make([]complex128, len(vec)+guard_len*2)
    24  	copy(guarded[guard_len:], vec)
    25  	for i := 0; i < guard_len; i++ {
    26  		guarded[i] = guard_val
    27  		guarded[len(guarded)-1-i] = guard_val
    28  	}
    29  	return guarded
    30  }
    31  
    32  func isValidGuard(vec []complex128, guard_val complex128, guard_len int) bool {
    33  	for i := 0; i < guard_len; i++ {
    34  		if vec[i] != guard_val || vec[len(vec)-1-i] != guard_val {
    35  			return false
    36  		}
    37  	}
    38  	return true
    39  }
    40  
    41  func guardIncVector(vec []complex128, guard_val complex128, inc, guard_len int) (guarded []complex128) {
    42  	s_ln := len(vec) * inc
    43  	if inc < 0 {
    44  		s_ln = len(vec) * -inc
    45  	}
    46  	guarded = make([]complex128, s_ln+guard_len*2)
    47  	for i, cas := 0, 0; i < len(guarded); i++ {
    48  		switch {
    49  		case i < guard_len, i > guard_len+s_ln:
    50  			guarded[i] = guard_val
    51  		case (i-guard_len)%(inc) == 0 && cas < len(vec):
    52  			guarded[i] = vec[cas]
    53  			cas++
    54  		default:
    55  			guarded[i] = guard_val
    56  		}
    57  	}
    58  	return guarded
    59  }
    60  
    61  func checkValidIncGuard(t *testing.T, vec []complex128, guard_val complex128, inc, guard_len int) {
    62  	s_ln := len(vec) - 2*guard_len
    63  	if inc < 0 {
    64  		s_ln = len(vec) * -inc
    65  	}
    66  
    67  	for i := range vec {
    68  		switch {
    69  		case vec[i] == guard_val:
    70  			// Correct value
    71  		case i < guard_len:
    72  			t.Errorf("Front guard violated at %d %v", i, vec[:guard_len])
    73  		case i > guard_len+s_ln:
    74  			t.Errorf("Back guard violated at %d %v", i-guard_len-s_ln, vec[guard_len+s_ln:])
    75  		case (i-guard_len)%inc == 0 && (i-guard_len)/inc < len(vec):
    76  			// Ignore input values
    77  		default:
    78  			t.Errorf("Internal guard violated at %d %v", i-guard_len, vec[guard_len:guard_len+s_ln])
    79  		}
    80  	}
    81  }
    82  
    83  var ( // Offset sets for testing alignment handling in Unitary assembly functions.
    84  	align1 = []int{0, 1}
    85  	align2 = newIncSet(0, 1)
    86  	align3 = newIncToSet(0, 1)
    87  )
    88  
    89  type incSet struct {
    90  	x, y int
    91  }
    92  
    93  // genInc will generate all (x,y) combinations of the input increment set.
    94  func newIncSet(inc ...int) []incSet {
    95  	n := len(inc)
    96  	is := make([]incSet, n*n)
    97  	for x := range inc {
    98  		for y := range inc {
    99  			is[x*n+y] = incSet{inc[x], inc[y]}
   100  		}
   101  	}
   102  	return is
   103  }
   104  
   105  type incToSet struct {
   106  	dst, x, y int
   107  }
   108  
   109  // genIncTo will generate all (dst,x,y) combinations of the input increment set.
   110  func newIncToSet(inc ...int) []incToSet {
   111  	n := len(inc)
   112  	is := make([]incToSet, n*n*n)
   113  	for i, dst := range inc {
   114  		for x := range inc {
   115  			for y := range inc {
   116  				is[i*n*n+x*n+y] = incToSet{dst, inc[x], inc[y]}
   117  			}
   118  		}
   119  	}
   120  	return is
   121  }