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
5package c64
6
7import "testing"
8
9var tests = []struct {
10	incX, incY, incDst int
11	ix, iy, idst       uintptr
12	a                  complex64
13	dst, x, y          []complex64
14	ex                 []complex64
15}{
16	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
17		a:   1 + 1i,
18		dst: []complex64{5},
19		x:   []complex64{1},
20		y:   []complex64{1i},
21		ex:  []complex64{1 + 2i}},
22	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
23		a:   1 + 2i,
24		dst: []complex64{0, 0, 0},
25		x:   []complex64{0, 0, 0},
26		y:   []complex64{1, 1, 1},
27		ex:  []complex64{1, 1, 1}},
28	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
29		a:   1 + 2i,
30		dst: []complex64{0, 0, 0},
31		x:   []complex64{0, 0},
32		y:   []complex64{1, 1, 1},
33		ex:  []complex64{1, 1}},
34	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
35		a:   1 + 2i,
36		dst: []complex64{1i, 1i, 1i},
37		x:   []complex64{1i, 1i, 1i},
38		y:   []complex64{1, 2, 1},
39		ex:  []complex64{-1 + 1i, 1i, -1 + 1i}},
40	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
41		a:   -1i,
42		dst: []complex64{1i, 1i, 1i},
43		x:   []complex64{1i, 1i, 1i},
44		y:   []complex64{1, 2, 1},
45		ex:  []complex64{2, 3, 2}},
46	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
47		a:   -1i,
48		dst: []complex64{1i, 1i, 1i},
49		x:   []complex64{1i, 1i, 1i, 1i, 1i}[1:4],
50		y:   []complex64{1, 1, 2, 1, 1}[1:4],
51		ex:  []complex64{2, 3, 2}},
52	{incX: 2, incY: 4, incDst: 3, ix: 0, iy: 0, idst: 0,
53		a:   -2,
54		dst: []complex64{1i, 1i, 1i, 1i, 1i},
55		x:   []complex64{2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i},
56		y:   []complex64{1, 1, 2, 1, 1},
57		ex:  []complex64{-3 - 2i, -3 - 2i, -2 - 2i, -3 - 2i, -3 - 2i}},
58	// Run big test twice, once aligned once unaligned.
59	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
60		a:   1 - 1i,
61		dst: make([]complex64, 10),
62		x:   []complex64{1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i},
63		y:   []complex64{1, 1, 2, 1, 1, 1, 1, 2, 1, 1},
64		ex:  []complex64{2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i}},
65	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
66		a:   1 - 1i,
67		dst: make([]complex64, 10),
68		x:   []complex64{1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i},
69		y:   []complex64{1, 1, 2, 1, 1, 1, 1, 2, 1, 1},
70		ex:  []complex64{2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i}},
71	{incX: -2, incY: -2, incDst: -3, ix: 18, iy: 18, idst: 27,
72		a:   1 - 1i,
73		dst: make([]complex64, 10),
74		x:   []complex64{1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i},
75		y:   []complex64{1, 1, 2, 1, 1, 1, 1, 2, 1, 1},
76		ex:  []complex64{2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i}},
77	{incX: -2, incY: 2, incDst: -3, ix: 18, iy: 0, idst: 27,
78		a:   1 - 1i,
79		dst: make([]complex64, 10),
80		x:   []complex64{1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i},
81		y:   []complex64{1, 1, 2, 1, 1, 1, 1, 2, 1, 1},
82		ex:  []complex64{2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i}},
83}
84
85func TestAxpyUnitary(t *testing.T) {
86	var x_gd, y_gd complex64 = 1, 1
87	for cas, test := range tests {
88		xg_ln, yg_ln := 4+cas%2, 4+cas%3
89		test.x, test.y = guardVector(test.x, x_gd, xg_ln), guardVector(test.y, y_gd, yg_ln)
90		x, y := test.x[xg_ln:len(test.x)-xg_ln], test.y[yg_ln:len(test.y)-yg_ln]
91		AxpyUnitary(test.a, x, y)
92		for i := range test.ex {
93			if y[i] != test.ex[i] {
94				t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, y[i], test.ex[i])
95			}
96		}
97		if !isValidGuard(test.x, x_gd, xg_ln) {
98			t.Errorf("Test %d Guard violated in x vector %v %v", cas, test.x[:xg_ln], test.x[len(test.x)-xg_ln:])
99		}
100		if !isValidGuard(test.y, y_gd, yg_ln) {
101			t.Errorf("Test %d Guard violated in y vector %v %v", cas, test.y[:yg_ln], test.y[len(test.y)-yg_ln:])
102		}
103	}
104}
105
106func TestAxpyUnitaryTo(t *testing.T) {
107	var x_gd, y_gd, dst_gd complex64 = 1, 1, 0
108	for cas, test := range tests {
109		xg_ln, yg_ln := 4+cas%2, 4+cas%3
110		test.x, test.y = guardVector(test.x, x_gd, xg_ln), guardVector(test.y, y_gd, yg_ln)
111		test.dst = guardVector(test.dst, dst_gd, xg_ln)
112		x, y := test.x[xg_ln:len(test.x)-xg_ln], test.y[yg_ln:len(test.y)-yg_ln]
113		dst := test.dst[xg_ln : len(test.dst)-xg_ln]
114		AxpyUnitaryTo(dst, test.a, x, y)
115		for i := range test.ex {
116			if dst[i] != test.ex[i] {
117				t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, dst[i], test.ex[i])
118			}
119		}
120		if !isValidGuard(test.x, x_gd, xg_ln) {
121			t.Errorf("Test %d Guard violated in x vector %v %v", cas, test.x[:xg_ln], test.x[len(test.x)-xg_ln:])
122		}
123		if !isValidGuard(test.y, y_gd, yg_ln) {
124			t.Errorf("Test %d Guard violated in y vector %v %v", cas, test.y[:yg_ln], test.y[len(test.y)-yg_ln:])
125		}
126		if !isValidGuard(test.dst, dst_gd, xg_ln) {
127			t.Errorf("Test %d Guard violated in dst vector %v %v", cas, test.dst[:xg_ln], test.dst[len(test.dst)-xg_ln:])
128		}
129	}
130}
131
132func TestAxpyInc(t *testing.T) {
133	var x_gd, y_gd complex64 = 1, 1
134	for cas, test := range tests {
135		xg_ln, yg_ln := 4+cas%2, 4+cas%3
136		test.x, test.y = guardIncVector(test.x, x_gd, test.incX, xg_ln), guardIncVector(test.y, y_gd, test.incY, yg_ln)
137		x, y := test.x[xg_ln:len(test.x)-xg_ln], test.y[yg_ln:len(test.y)-yg_ln]
138		AxpyInc(test.a, x, y, uintptr(len(test.ex)), uintptr(test.incX), uintptr(test.incY), test.ix, test.iy)
139		for i := range test.ex {
140			if y[int(test.iy)+i*int(test.incY)] != test.ex[i] {
141				t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, y[i*int(test.incY)], test.ex[i])
142			}
143		}
144		checkValidIncGuard(t, test.x, x_gd, test.incX, xg_ln)
145		checkValidIncGuard(t, test.y, y_gd, test.incY, yg_ln)
146	}
147}
148
149func TestAxpyIncTo(t *testing.T) {
150	var x_gd, y_gd, dst_gd complex64 = 1, 1, 0
151	for cas, test := range tests {
152		xg_ln, yg_ln := 4+cas%2, 4+cas%3
153		test.x, test.y = guardIncVector(test.x, x_gd, test.incX, xg_ln), guardIncVector(test.y, y_gd, test.incY, yg_ln)
154		test.dst = guardIncVector(test.dst, dst_gd, test.incDst, xg_ln)
155		x, y := test.x[xg_ln:len(test.x)-xg_ln], test.y[yg_ln:len(test.y)-yg_ln]
156		dst := test.dst[xg_ln : len(test.dst)-xg_ln]
157		AxpyIncTo(dst, uintptr(test.incDst), test.idst, test.a, x, y, uintptr(len(test.ex)), uintptr(test.incX), uintptr(test.incY), test.ix, test.iy)
158		for i := range test.ex {
159			if dst[int(test.idst)+i*int(test.incDst)] != test.ex[i] {
160				t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, dst[i*int(test.incDst)], test.ex[i])
161			}
162		}
163		checkValidIncGuard(t, test.x, x_gd, test.incX, xg_ln)
164		checkValidIncGuard(t, test.y, y_gd, test.incY, yg_ln)
165		checkValidIncGuard(t, test.dst, dst_gd, test.incDst, xg_ln)
166	}
167}
168