mirror of
https://github.com/gonum/gonum.git
synced 2025-10-06 07:37:03 +08:00
106 lines
3.0 KiB
Go
106 lines
3.0 KiB
Go
// Copyright ©2019 The Gonum Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package mat
|
|
|
|
import "testing"
|
|
|
|
func TestCDenseNewAtSet(t *testing.T) {
|
|
t.Parallel()
|
|
for cas, test := range []struct {
|
|
a []complex128
|
|
rows, cols int
|
|
}{
|
|
{
|
|
a: []complex128{0, 0, 0,
|
|
0, 0, 0,
|
|
0, 0, 0},
|
|
rows: 3,
|
|
cols: 3,
|
|
},
|
|
} {
|
|
aCopy := make([]complex128, len(test.a))
|
|
copy(aCopy, test.a)
|
|
mZero := NewCDense(test.rows, test.cols, nil)
|
|
rows, cols := mZero.Dims()
|
|
if rows != test.rows {
|
|
t.Errorf("unexpected number of rows for test %d: got: %d want: %d", cas, rows, test.rows)
|
|
}
|
|
if cols != test.cols {
|
|
t.Errorf("unexpected number of cols for test %d: got: %d want: %d", cas, cols, test.cols)
|
|
}
|
|
m := NewCDense(test.rows, test.cols, aCopy)
|
|
for i := 0; i < test.rows; i++ {
|
|
for j := 0; j < test.cols; j++ {
|
|
v := m.At(i, j)
|
|
idx := i*test.rows + j
|
|
if v != test.a[idx] {
|
|
t.Errorf("unexpected get value for test %d at i=%d, j=%d: got: %v, want: %v", cas, i, j, v, test.a[idx])
|
|
}
|
|
add := complex(float64(i+1), float64(j+1))
|
|
m.Set(i, j, v+add)
|
|
if m.At(i, j) != test.a[idx]+add {
|
|
t.Errorf("unexpected set value for test %d at i=%d, j=%d: got: %v, want: %v", cas, i, j, v, test.a[idx]+add)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCDenseGrow(t *testing.T) {
|
|
t.Parallel()
|
|
m := &CDense{}
|
|
m = m.Grow(10, 10).(*CDense)
|
|
rows, cols := m.Dims()
|
|
capRows, capCols := m.Caps()
|
|
if rows != 10 {
|
|
t.Errorf("unexpected value for rows: got: %d want: 10", rows)
|
|
}
|
|
if cols != 10 {
|
|
t.Errorf("unexpected value for cols: got: %d want: 10", cols)
|
|
}
|
|
if capRows != 10 {
|
|
t.Errorf("unexpected value for capRows: got: %d want: 10", capRows)
|
|
}
|
|
if capCols != 10 {
|
|
t.Errorf("unexpected value for capCols: got: %d want: 10", capCols)
|
|
}
|
|
|
|
// Test grow within caps is in-place.
|
|
m.Set(1, 1, 1)
|
|
v := m.Slice(1, 5, 1, 5).(*CDense)
|
|
if v.At(0, 0) != m.At(1, 1) {
|
|
t.Errorf("unexpected viewed element value: got: %v want: %v", v.At(0, 0), m.At(1, 1))
|
|
}
|
|
v = v.Grow(5, 5).(*CDense)
|
|
if !CEqual(v, m.Slice(1, 10, 1, 10)) {
|
|
t.Error("unexpected view value after grow")
|
|
}
|
|
|
|
// Test grow bigger than caps copies.
|
|
v = v.Grow(5, 5).(*CDense)
|
|
if !CEqual(v.Slice(0, 9, 0, 9), m.Slice(1, 10, 1, 10)) {
|
|
t.Error("unexpected mismatched common view value after grow")
|
|
}
|
|
v.Set(0, 0, 0)
|
|
if CEqual(v.Slice(0, 9, 0, 9), m.Slice(1, 10, 1, 10)) {
|
|
t.Error("unexpected matching view value after grow past capacity")
|
|
}
|
|
|
|
// Test grow uses existing data slice when matrix is zero size.
|
|
v.Reset()
|
|
p, l := &v.mat.Data[:1][0], cap(v.mat.Data)
|
|
*p = 1 // This element is at position (-1, -1) relative to v and so should not be visible.
|
|
v = v.Grow(5, 5).(*CDense)
|
|
if &v.mat.Data[:1][0] != p {
|
|
t.Error("grow unexpectedly copied slice within cap limit")
|
|
}
|
|
if cap(v.mat.Data) != l {
|
|
t.Errorf("unexpected change in data slice capacity: got: %d want: %d", cap(v.mat.Data), l)
|
|
}
|
|
if v.At(0, 0) != 0 {
|
|
t.Errorf("unexpected value for At(0, 0): got: %v want: 0", v.At(0, 0))
|
|
}
|
|
}
|