github.com/gopherd/gonum@v0.0.4/blas/testblas/zdotu.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  
    12  type Zdotuer interface {
    13  	Zdotu(n int, x []complex128, incX int, y []complex128, incY int) complex128
    14  }
    15  
    16  func ZdotuTest(t *testing.T, impl Zdotuer) {
    17  	for tc, test := range []struct {
    18  		x, y []complex128
    19  
    20  		want    complex128 // Result when both increments have the same sign.
    21  		wantRev complex128 // Result when the increments have opposite sign.
    22  	}{
    23  		{
    24  			x:       nil,
    25  			y:       nil,
    26  			want:    0,
    27  			wantRev: 0,
    28  		},
    29  		{
    30  			x:       []complex128{1 + 2i},
    31  			y:       []complex128{30 + 31i},
    32  			want:    -32 + 91i,
    33  			wantRev: -32 + 91i,
    34  		},
    35  		{
    36  			x:       []complex128{1 + 2i, 3 + 4i},
    37  			y:       []complex128{30 + 31i, 33 + 34i},
    38  			want:    -69 + 325i,
    39  			wantRev: -69 + 313i,
    40  		},
    41  		{
    42  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i},
    43  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i},
    44  			want:    -111 + 726i,
    45  			wantRev: -111 + 678i,
    46  		},
    47  		{
    48  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i},
    49  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i, 39 + 40i},
    50  			want:    -158 + 1318i,
    51  			wantRev: -158 + 1198i,
    52  		},
    53  		{
    54  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i},
    55  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i, 39 + 40i, 42 + 43i},
    56  			want:    -210 + 2125i,
    57  			wantRev: -210 + 1885i,
    58  		},
    59  		{
    60  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i},
    61  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i, 39 + 40i, 42 + 43i, 45 + 46i},
    62  			want:    -267 + 3171i,
    63  			wantRev: -267 + 2751i,
    64  		},
    65  		{
    66  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i},
    67  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i, 39 + 40i, 42 + 43i, 45 + 46i, 48 + 49i},
    68  			want:    -329 + 4480i,
    69  			wantRev: -329 + 3808i,
    70  		},
    71  		{
    72  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i},
    73  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i, 39 + 40i, 42 + 43i, 45 + 46i, 48 + 49i, 51 + 52i},
    74  			want:    -396 + 6076i,
    75  			wantRev: -396 + 5068i,
    76  		},
    77  		{
    78  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i},
    79  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i, 39 + 40i, 42 + 43i, 45 + 46i, 48 + 49i, 51 + 52i, 54 + 55i},
    80  			want:    -468 + 7983i,
    81  			wantRev: -468 + 6543i,
    82  		},
    83  		{
    84  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i},
    85  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i, 39 + 40i, 42 + 43i, 45 + 46i, 48 + 49i, 51 + 52i, 54 + 55i, 57 + 58i},
    86  			want:    -545 + 10225i,
    87  			wantRev: -545 + 8245i,
    88  		},
    89  		{
    90  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i, 21 + 22i},
    91  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i, 39 + 40i, 42 + 43i, 45 + 46i, 48 + 49i, 51 + 52i, 54 + 55i, 57 + 58i, 60 + 61i},
    92  			want:    -627 + 12826i,
    93  			wantRev: -627 + 10186i,
    94  		},
    95  		{
    96  			x:       []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i, 21 + 22i, 23 + 24i},
    97  			y:       []complex128{30 + 31i, 33 + 34i, 36 + 37i, 39 + 40i, 42 + 43i, 45 + 46i, 48 + 49i, 51 + 52i, 54 + 55i, 57 + 58i, 60 + 61i, 63 + 64i},
    98  			want:    -714 + 15810i,
    99  			wantRev: -714 + 12378i,
   100  		},
   101  	} {
   102  		n := len(test.x)
   103  		if len(test.y) != n {
   104  			panic("bad test")
   105  		}
   106  		for _, inc := range allPairs([]int{-7, -3, 1, 13}, []int{-11, -5, 1, 17}) {
   107  			incX := inc[0]
   108  			incY := inc[1]
   109  
   110  			x := makeZVector(test.x, incX)
   111  			xCopy := make([]complex128, len(x))
   112  			copy(xCopy, x)
   113  
   114  			y := makeZVector(test.y, incY)
   115  			yCopy := make([]complex128, len(y))
   116  			copy(yCopy, y)
   117  
   118  			want := test.want
   119  			if incX*incY < 0 {
   120  				want = test.wantRev
   121  			}
   122  
   123  			got := impl.Zdotu(n, x, incX, y, incY)
   124  
   125  			prefix := fmt.Sprintf("Case %v (incX=%v,incY=%v):", tc, incX, incY)
   126  
   127  			if !zsame(x, xCopy) {
   128  				t.Errorf("%v: unexpected modification of x", prefix)
   129  			}
   130  			if !zsame(y, yCopy) {
   131  				t.Errorf("%v: unexpected modification of y", prefix)
   132  			}
   133  
   134  			if got != want {
   135  				t.Errorf("%v: unexpected result. want %v, got %v", prefix, want, got)
   136  			}
   137  		}
   138  	}
   139  }