-
Notifications
You must be signed in to change notification settings - Fork 1
/
complex_test.go
89 lines (79 loc) · 1.84 KB
/
complex_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gp
import (
"testing"
)
func TestComplex(t *testing.T) {
setupTest(t)
tests := []struct {
name string
input complex128
wantReal float64
wantImag float64
}{
{
name: "zero complex",
input: complex(0, 0),
wantReal: 0,
wantImag: 0,
},
{
name: "positive real and imaginary",
input: complex(3.14, 2.718),
wantReal: 3.14,
wantImag: 2.718,
},
{
name: "negative real and imaginary",
input: complex(-1.5, -2.5),
wantReal: -1.5,
wantImag: -2.5,
},
{
name: "mixed signs",
input: complex(-1.23, 4.56),
wantReal: -1.23,
wantImag: 4.56,
},
}
for _, tt := range tests {
c := MakeComplex(tt.input)
// Test Real() method
if got := c.Real(); got != tt.wantReal {
t.Errorf("Complex.Real() = %v, want %v", got, tt.wantReal)
}
// Test Imag() method
if got := c.Imag(); got != tt.wantImag {
t.Errorf("Complex.Imag() = %v, want %v", got, tt.wantImag)
}
// Test Complex128() method
if got := c.Complex128(); got != tt.input {
t.Errorf("Complex.Complex128() = %v, want %v", got, tt.input)
}
}
}
func TestComplexZeroValue(t *testing.T) {
setupTest(t)
// Create a proper zero complex number instead of using zero-value struct
c := MakeComplex(complex(0, 0))
// Test that zero complex behaves correctly
if got := c.Real(); got != 0 {
t.Errorf("Zero Complex.Real() = %v, want 0", got)
}
if got := c.Imag(); got != 0 {
t.Errorf("Zero Complex.Imag() = %v, want 0", got)
}
if got := c.Complex128(); got != 0 {
t.Errorf("Zero Complex.Complex128() = %v, want 0", got)
}
}
func TestComplexNilHandling(t *testing.T) {
setupTest(t)
var c Complex // zero-value struct with nil pointer
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for nil pointer access, but got none")
}
}()
// This should panic
_ = c.Real()
}