1
exercism fetch erlang complex-numbers

test/complex_numbers_tests.erl

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
-module(complex_numbers_tests).

% This file is automatically generated from the exercises canonical data.

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").


imaginary_unit_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(-1,
						      0),
				  complex_numbers:mul(complex_numbers:new(0, 1),
						      complex_numbers:new(0,
									  1)))).

add_purely_real_numbers_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(3, 0),
				  complex_numbers:add(complex_numbers:new(1, 0),
						      complex_numbers:new(2,
									  0)))).

add_purely_imaginary_numbers_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(0, 3),
				  complex_numbers:add(complex_numbers:new(0, 1),
						      complex_numbers:new(0,
									  2)))).

add_numbers_with_real_and_imaginary_part_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(4, 6),
				  complex_numbers:add(complex_numbers:new(1, 2),
						      complex_numbers:new(3,
									  4)))).

subtract_purely_real_numbers_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(-1,
						      0),
				  complex_numbers:sub(complex_numbers:new(1, 0),
						      complex_numbers:new(2,
									  0)))).

subtract_purely_imaginary_numbers_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(0,
						      -1),
				  complex_numbers:sub(complex_numbers:new(0, 1),
						      complex_numbers:new(0,
									  2)))).

subtract_numbers_with_real_and_imaginary_part_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(-2,
						      -2),
				  complex_numbers:sub(complex_numbers:new(1, 2),
						      complex_numbers:new(3,
									  4)))).

multiply_purely_real_numbers_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(2, 0),
				  complex_numbers:mul(complex_numbers:new(1, 0),
						      complex_numbers:new(2,
									  0)))).

multiply_purely_imaginary_numbers_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(-2,
						      0),
				  complex_numbers:mul(complex_numbers:new(0, 1),
						      complex_numbers:new(0,
									  2)))).

multiply_numbers_with_real_and_imaginary_part_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(-5,
						      10),
				  complex_numbers:mul(complex_numbers:new(1, 2),
						      complex_numbers:new(3,
									  4)))).

divide_purely_real_numbers_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(5.0e-1,
						      0),
				  complex_numbers:divide(complex_numbers:new(1,
									     0),
							 complex_numbers:new(2,
									     0)))).

divide_purely_imaginary_numbers_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(5.0e-1,
						      0),
				  complex_numbers:divide(complex_numbers:new(0,
									     1),
							 complex_numbers:new(0,
									     2)))).

divide_numbers_with_real_and_imaginary_part_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(4.4e-1,
						      8.0e-2),
				  complex_numbers:divide(complex_numbers:new(1,
									     2),
							 complex_numbers:new(3,
									     4)))).

absolute_value_of_a_positive_purely_real_number_test() ->
    ?assert(5 ==
	      complex_numbers:abs(complex_numbers:new(5, 0))).

absolute_value_of_a_negative_purely_real_number_test() ->
    ?assert(5 ==
	      complex_numbers:abs(complex_numbers:new(-5, 0))).

absolute_value_of_a_purely_imaginary_number_with_positive_imaginary_part_test() ->
    ?assert(5 ==
	      complex_numbers:abs(complex_numbers:new(0, 5))).

absolute_value_of_a_purely_imaginary_number_with_negative_imaginary_part_test() ->
    ?assert(5 ==
	      complex_numbers:abs(complex_numbers:new(0, -5))).

absolute_value_of_a_number_with_real_and_imaginary_part_test() ->
    ?assert(5 ==
	      complex_numbers:abs(complex_numbers:new(3, 4))).

conjugate_a_purely_real_number_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(5, 0),
				  complex_numbers:conjugate(complex_numbers:new(5,
										0)))).

conjugate_a_purely_imaginary_number_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(0,
						      -5),
				  complex_numbers:conjugate(complex_numbers:new(0,
										5)))).

conjugate_a_number_with_real_and_imaginary_part_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(1,
						      -1),
				  complex_numbers:conjugate(complex_numbers:new(1,
										1)))).

real_part_of_a_purely_real_number_test() ->
    ?assert(1 ==
	      complex_numbers:real(complex_numbers:new(1, 0))).

real_part_of_a_purely_imaginary_number_test() ->
    ?assert(0 ==
	      complex_numbers:real(complex_numbers:new(0, 1))).

real_part_of_a_number_with_real_and_imaginary_part_test() ->
    ?assert(1 ==
	      complex_numbers:real(complex_numbers:new(1, 2))).

imaginary_part_of_a_purely_real_number_test() ->
    ?assert(0 ==
	      complex_numbers:imaginary(complex_numbers:new(1, 0))).

imaginary_part_of_a_purely_imaginary_number_test() ->
    ?assert(1 ==
	      complex_numbers:imaginary(complex_numbers:new(0, 1))).

imaginary_part_of_a_number_with_real_and_imaginary_part_test() ->
    ?assert(2 ==
	      complex_numbers:imaginary(complex_numbers:new(1, 2))).

eulers_identityformula_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(-1,
						      0),
				  complex_numbers:exp(complex_numbers:new(0,
									  3.14159265358979311600)))).

exponential_of_0_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(1, 0),
				  complex_numbers:exp(complex_numbers:new(0,
									  0)))).

exponential_of_a_purely_real_number_test() ->
    ?assert(complex_numbers:equal(complex_numbers:new(2.71828182845904509080,
						      0),
				  complex_numbers:exp(complex_numbers:new(1,
									  0)))).

version_test() ->
    ?assertMatch(1, complex_numbers:test_version()).