1
exercism fetch ecmascript complex-numbers

complex-numbers.spec.js

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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import ComplexNumber from './complex-numbers.js';

describe('Complex numbers', () => {
  test('Real part of a purely real number', () => {
    const expected = 1;
    const actual = new ComplexNumber(1, 0).real;

    expect(actual).toEqual(expected);
  });

  xtest('Real part of a purely imaginary number', () => {
    const expected = 0;
    const actual = new ComplexNumber(0, 1).real;

    expect(actual).toEqual(expected);
  });

  xtest('Real part of a number with real and imaginary part', () => {
    const expected = 1;
    const actual = new ComplexNumber(1, 2).real;

    expect(actual).toEqual(expected);
  });

  xtest('Imaginary part of a purely real number', () => {
    const expected = 0;
    const actual = new ComplexNumber(1, 0).imag;

    expect(actual).toEqual(expected);
  });

  xtest('Imaginary part of a purely imaginary number', () => {
    const expected = 1;
    const actual = new ComplexNumber(0, 1).imag;

    expect(actual).toEqual(expected);
  });

  xtest('Imaginary part of a number with real and imaginary part', () => {
    const expected = 2;
    const actual = new ComplexNumber(1, 2).imag;

    expect(actual).toEqual(expected);
  });
  
  xtest('Add purely real numbers', () => {
    const expected = new ComplexNumber(3, 0);
    const actual = new ComplexNumber(1, 0).add(new ComplexNumber(2, 0));

    expect(actual).toEqual(expected);
  });

  xtest('Add purely imaginary numbers', () => {
    const expected = new ComplexNumber(0, 3);
    const actual = new ComplexNumber(0, 1).add(new ComplexNumber(0, 2));

    expect(actual).toEqual(expected);
  });

  xtest('Add numbers with real and imaginary part', () => {
    const expected = new ComplexNumber(4, 6);
    const actual = new ComplexNumber(1, 2).add(new ComplexNumber(3, 4));

    expect(actual).toEqual(expected);
  });

  xtest('Subtract purely real numbers', () => {
    const expected = new ComplexNumber(-1, 0);
    const actual = new ComplexNumber(1, 0).sub(new ComplexNumber(2, 0));

    expect(actual).toEqual(expected);
  });

  xtest('Subtract purely imaginary numbers', () => {
    const expected = new ComplexNumber(0, -1);
    const actual = new ComplexNumber(0, 1).sub(new ComplexNumber(0, 2));

    expect(actual).toEqual(expected);
  });

  xtest('Subtract numbers with real and imaginary part', () => {
    const expected = new ComplexNumber(-2, -2);
    const actual = new ComplexNumber(1, 2).sub(new ComplexNumber(3, 4));

    expect(actual).toEqual(expected);
  });

  xtest('Multiply purely real numbers', () => {
    const expected = new ComplexNumber(2, 0);
    const actual = new ComplexNumber(1, 0).mul(new ComplexNumber(2, 0));

    expect(actual).toEqual(expected);
  });

  xtest('Multiply imaginary unit', () => {
    const expected = new ComplexNumber(-1, 0);
    const actual = new ComplexNumber(0, 1).mul(new ComplexNumber(0, 1));

    expect(actual).toEqual(expected);
  });

  xtest('Multiply purely imaginary numbers', () => {
    const expected = new ComplexNumber(-2, 0);
    const actual = new ComplexNumber(0, 1).mul(new ComplexNumber(0, 2));

    expect(actual).toEqual(expected);
  });

  xtest('Multiply numbers with real and imaginary part', () => {
    const expected = new ComplexNumber(-5, 10);
    const actual = new ComplexNumber(1, 2).mul(new ComplexNumber(3, 4));

    expect(actual).toEqual(expected);
  });

  xtest('Divide purely real numbers', () => {
    const expected = new ComplexNumber(0.5, 0);
    const actual = new ComplexNumber(1, 0).div(new ComplexNumber(2, 0));

    expect(actual).toEqual(expected);
  });

  xtest('Divide purely imaginary numbers', () => {
    const expected = new ComplexNumber(0.5, 0);
    const actual = new ComplexNumber(0, 1).div(new ComplexNumber(0, 2));

    expect(actual).toEqual(expected);
  });

  xtest('Divide numbers with real and imaginary part', () => {
    const expected = new ComplexNumber(0.44, 0.08);
    const actual = new ComplexNumber(1, 2).div(new ComplexNumber(3, 4));

    expect(actual).toEqual(expected);
  });

  xtest('Absolute value of a positive purely real number', () => {
    const expected = 5;
    const actual = new ComplexNumber(5, 0).abs;

    expect(actual).toEqual(expected);
  });

  xtest('Absolute value of a negative purely real number', () => {
    const expected = 5;
    const actual = new ComplexNumber(-5, 0).abs;

    expect(actual).toEqual(expected);
  });

  xtest('Absolute value of a purely imaginary number with positive imaginary part', () => {
    const expected = 5;
    const actual = new ComplexNumber(0, 5).abs;

    expect(actual).toEqual(expected);
  });

  xtest('Absolute value of a purely imaginary number with negative imaginary part', () => {
    const expected = 5;
    const actual = new ComplexNumber(0, -5).abs;

    expect(actual).toEqual(expected);
  });

  xtest('Absolute value of a number with real and imaginary part', () => {
    const expected = 5;
    const actual = new ComplexNumber(3, 4).abs;

    expect(actual).toEqual(expected);
  });

  xtest('Conjugate a purely real number', () => {
    const expected = new ComplexNumber(5, 0);
    const actual = new ComplexNumber(5, 0).conj;

    expect(actual).toEqual(expected);
  });

  xtest('Conjugate a purely imaginary number', () => {
    const expected = new ComplexNumber(0, -5);
    const actual = new ComplexNumber(0, 5).conj;

    expect(actual).toEqual(expected);
  });

  xtest('Conjugate a number with real and imaginary part', () => {
    const expected = new ComplexNumber(1, -1);
    const actual = new ComplexNumber(1, 1).conj;

    expect(actual).toEqual(expected);
  });

  xtest('Euler\'s identity/formula', () => {
    const expected = new ComplexNumber(-1, 0);
    const actual = new ComplexNumber(0, Math.PI).exp;

    expect(actual.real).toBeCloseTo(expected.real);
    expect(actual.imag).toBeCloseTo(expected.imag);
  });

  xtest('Exponential of 0', () => {
    const expected = new ComplexNumber(1, 0);
    const actual = new ComplexNumber(0, 0).exp;

    expect(actual.real).toBeCloseTo(expected.real);
    expect(actual.imag).toBeCloseTo(expected.imag);
  });

  xtest('Exponential of a purely real number', () => {
    const expected = new ComplexNumber(Math.E, 0);
    const actual = new ComplexNumber(1, 0).exp;

    expect(actual.real).toBeCloseTo(expected.real);
    expect(actual.imag).toBeCloseTo(expected.imag);
  });
});