summaryrefslogtreecommitdiff
path: root/go/complex-numbers/complex_numbers_test.go
blob: 097f75ad09db9eab9fb067467e2efa79793d56a6 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package complexnumbers

import (
	"math"
	"testing"
)

const floatEqualityThreshold = 1e-5

func floatingPointEquals(got, want float64) bool {
	absoluteDifferenceBelowThreshold := math.Abs(got-want) <= floatEqualityThreshold
	relativeDifferenceBelowThreshold := math.Abs(got-want)/(math.Abs(got)+math.Abs(want)) <= floatEqualityThreshold
	return absoluteDifferenceBelowThreshold || relativeDifferenceBelowThreshold
}

func TestNumber_Real(t *testing.T) {
	for _, tt := range realTestCases {
		t.Run(tt.description, func(t *testing.T) {
			n := Number{tt.in.a, tt.in.b}
			if got := n.Real(); !floatingPointEquals(got, tt.want) {
				t.Errorf("Number%+v.Real() = %v, want %v", tt.in, got, tt.want)
			}
		})
	}
}

func TestNumber_Imaginary(t *testing.T) {
	for _, tt := range imaginaryTestCases {
		t.Run(tt.description, func(t *testing.T) {
			n := Number{tt.in.a, tt.in.b}
			if got := n.Imaginary(); !floatingPointEquals(got, tt.want) {
				t.Errorf("Number%+v.Imaginary() = %v, want %v", tt.in, got, tt.want)
			}
		})
	}
}

func TestNumber_Add(t *testing.T) {
	for _, tt := range addTestCases {
		t.Run(tt.description, func(t *testing.T) {
			n1 := Number{tt.n1.a, tt.n1.b}
			n2 := Number{tt.n2.a, tt.n2.b}
			if got := n1.Add(n2); !floatingPointEquals(got.Real(), tt.want.a) || !floatingPointEquals(got.Imaginary(), tt.want.b) {
				t.Errorf("Number%+v.Add%+v\n got: %+v\nwant: %+v", tt.n1, tt.n2, got, tt.want)
			}
		})
	}
}

func TestNumber_Subtract(t *testing.T) {
	for _, tt := range subtractTestCases {
		t.Run(tt.description, func(t *testing.T) {
			n1 := Number{tt.n1.a, tt.n1.b}
			n2 := Number{tt.n2.a, tt.n2.b}
			if got := n1.Subtract(n2); !floatingPointEquals(got.Real(), tt.want.a) || !floatingPointEquals(got.Imaginary(), tt.want.b) {
				t.Errorf("Number%+v.Subtract%+v\n got: %+v\nwant: %+v", tt.n1, tt.n2, got, tt.want)
			}
		})
	}
}

func TestNumber_Multiply(t *testing.T) {
	for _, tt := range multiplyTestCases {
		t.Run(tt.description, func(t *testing.T) {
			if tt.n2 == nil {
				t.Skip("skipping tests with factor used withNumber.Times()")
			}
			n1 := Number{tt.n1.a, tt.n1.b}
			n2 := Number{tt.n2.a, tt.n2.b}
			if got := n1.Multiply(n2); !floatingPointEquals(got.Real(), tt.want.a) || !floatingPointEquals(got.Imaginary(), tt.want.b) {
				t.Errorf("Number%+v.Multiply%+v\n got: %+v\nwant: %+v", tt.n1, tt.n2, got, tt.want)
			}
		})
	}
}

func TestNumber_Times(t *testing.T) {
	for _, tt := range multiplyTestCases {
		t.Run(tt.description, func(t *testing.T) {
			if tt.n2 != nil {
				t.Skip("skipping tests with complex multiplier used withNumber.Multiply()")
			}
			n := Number{tt.n1.a, tt.n1.b}
			if got := n.Times(tt.factor); !floatingPointEquals(got.Real(), tt.want.a) || !floatingPointEquals(got.Imaginary(), tt.want.b) {
				t.Errorf("Number%+v.Times(%v)\n got: %+v\nwant: %+v", tt.n1, tt.factor, got, tt.want)
			}
		})
	}
}

func TestNumber_Divide(t *testing.T) {
	for _, tt := range divideTestCases {
		t.Run(tt.description, func(t *testing.T) {
			n1 := Number{tt.n1.a, tt.n1.b}
			n2 := Number{tt.n2.a, tt.n2.b}
			if got := n1.Divide(n2); !floatingPointEquals(got.Real(), tt.want.a) || !floatingPointEquals(got.Imaginary(), tt.want.b) {
				t.Errorf("Number%+v.Divide%+v\n got: %+v\nwant: %+v", tt.n1, tt.n2, got, tt.want)
			}
		})
	}
}

func TestNumber_Abs(t *testing.T) {
	for _, tt := range absTestCases {
		t.Run(tt.description, func(t *testing.T) {
			n := Number{tt.in.a, tt.in.b}
			if got := n.Abs(); !floatingPointEquals(got, tt.want) {
				t.Errorf("Number.Abs%+v = %v, want %v", tt.in, got, tt.want)
			}
		})
	}
}

func TestNumber_Conjugate(t *testing.T) {
	for _, tt := range conjugateTestCases {
		t.Run(tt.description, func(t *testing.T) {
			n := Number{tt.in.a, tt.in.b}
			if got := n.Conjugate(); !floatingPointEquals(got.Real(), tt.want.a) || !floatingPointEquals(got.Imaginary(), tt.want.b) {
				t.Errorf("Number%+v.Conjugate()\n got: %+v\nwant: %+v", tt.in, got, tt.want)
			}
		})
	}
}

func TestNumber_Exp(t *testing.T) {
	for _, tt := range expTestCases {
		t.Run(tt.description, func(t *testing.T) {
			n := Number{tt.in.a, tt.in.b}
			if got := n.Exp(); !floatingPointEquals(got.Real(), tt.want.a) || !floatingPointEquals(got.Imaginary(), tt.want.b) {
				t.Errorf("Number%+v.Exp()\n got: %+v\nwant: %+v", tt.in, got, tt.want)
			}
		})
	}
}