Skip to content

Commit 30d4d05

Browse files
authored
update tests for complex-numbers (#962)
[no important files changed]
1 parent b558c8d commit 30d4d05

File tree

5 files changed

+114
-47
lines changed

5 files changed

+114
-47
lines changed

exercises/practice/complex-numbers/.docs/instructions.append.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ The Julia Base implementation of complex numbers can be found here: https://gith
44

55
---
66

7-
You can work on the bonus exercises by changing `@test_skip` to `@test`.
8-
97
## Bonus A
10-
Implement the exponential function on complex numbers `exp(::ComplexNumber)`.
8+
You can work on the bonus exercises involving arithmetic between real numbers and complex numbers by uncommenting:
9+
`# enable_realcomplex_tests = true` in the stub `complex-numbers.jl` or in `runtests.jl`
1110

1211
## Bonus B
13-
Implement `jm` analogous to `im` so that `1 + 1jm == ComplexNumber(1, 1)`.
12+
You can work on the bonus exercises between real numbers and complex numbers by uncommenting:
13+
`# enable_syntaxsugar_tests = true` in the stub `complex-numbers.jl` or in `runtests.jl`
14+
Then, implement `jm` analogous to `im` so that `1 + 1jm == ComplexNumber(1, 1)`.

exercises/practice/complex-numbers/.meta/example.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ function exp(z::ComplexNumber)
3939
e_re = exp(z_re)
4040
ComplexNumber(e_re * cos(z_im), e_re * sin(z_im))
4141
end
42+
43+
enable_syntaxsugar_tests = true
44+
enable_realcomplex_tests = true
Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,130 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[9f98e133-eb7f-45b0-9676-cce001cd6f7a]
6-
description = "Real part of a purely real number"
13+
description = "Real part -> Real part of a purely real number"
714

815
[07988e20-f287-4bb7-90cf-b32c4bffe0f3]
9-
description = "Real part of a purely imaginary number"
16+
description = "Real part -> Real part of a purely imaginary number"
1017

1118
[4a370e86-939e-43de-a895-a00ca32da60a]
12-
description = "Real part of a number with real and imaginary part"
19+
description = "Real part -> Real part of a number with real and imaginary part"
1320

1421
[9b3fddef-4c12-4a99-b8f8-e3a42c7ccef6]
15-
description = "Imaginary part of a purely real number"
22+
description = "Imaginary part -> Imaginary part of a purely real number"
1623

1724
[a8dafedd-535a-4ed3-8a39-fda103a2b01e]
18-
description = "Imaginary part of a purely imaginary number"
25+
description = "Imaginary part -> Imaginary part of a purely imaginary number"
1926

2027
[0f998f19-69ee-4c64-80ef-01b086feab80]
21-
description = "Imaginary part of a number with real and imaginary part"
28+
description = "Imaginary part -> Imaginary part of a number with real and imaginary part"
2229

2330
[a39b7fd6-6527-492f-8c34-609d2c913879]
2431
description = "Imaginary unit"
2532

2633
[9a2c8de9-f068-4f6f-b41c-82232cc6c33e]
27-
description = "Add purely real numbers"
34+
description = "Arithmetic -> Addition -> Add purely real numbers"
2835

2936
[657c55e1-b14b-4ba7-bd5c-19db22b7d659]
30-
description = "Add purely imaginary numbers"
37+
description = "Arithmetic -> Addition -> Add purely imaginary numbers"
3138

3239
[4e1395f5-572b-4ce8-bfa9-9a63056888da]
33-
description = "Add numbers with real and imaginary part"
40+
description = "Arithmetic -> Addition -> Add numbers with real and imaginary part"
3441

3542
[1155dc45-e4f7-44b8-af34-a91aa431475d]
36-
description = "Subtract purely real numbers"
43+
description = "Arithmetic -> Subtraction -> Subtract purely real numbers"
3744

3845
[f95e9da8-acd5-4da4-ac7c-c861b02f774b]
39-
description = "Subtract purely imaginary numbers"
46+
description = "Arithmetic -> Subtraction -> Subtract purely imaginary numbers"
4047

4148
[f876feb1-f9d1-4d34-b067-b599a8746400]
42-
description = "Subtract numbers with real and imaginary part"
49+
description = "Arithmetic -> Subtraction -> Subtract numbers with real and imaginary part"
4350

4451
[8a0366c0-9e16-431f-9fd7-40ac46ff4ec4]
45-
description = "Multiply purely real numbers"
52+
description = "Arithmetic -> Multiplication -> Multiply purely real numbers"
4653

4754
[e560ed2b-0b80-4b4f-90f2-63cefc911aaf]
48-
description = "Multiply purely imaginary numbers"
55+
description = "Arithmetic -> Multiplication -> Multiply purely imaginary numbers"
4956

5057
[4d1d10f0-f8d4-48a0-b1d0-f284ada567e6]
51-
description = "Multiply numbers with real and imaginary part"
58+
description = "Arithmetic -> Multiplication -> Multiply numbers with real and imaginary part"
5259

5360
[b0571ddb-9045-412b-9c15-cd1d816d36c1]
54-
description = "Divide purely real numbers"
61+
description = "Arithmetic -> Division -> Divide purely real numbers"
5562

5663
[5bb4c7e4-9934-4237-93cc-5780764fdbdd]
57-
description = "Divide purely imaginary numbers"
64+
description = "Arithmetic -> Division -> Divide purely imaginary numbers"
5865

5966
[c4e7fef5-64ac-4537-91c2-c6529707701f]
60-
description = "Divide numbers with real and imaginary part"
67+
description = "Arithmetic -> Division -> Divide numbers with real and imaginary part"
6168

6269
[c56a7332-aad2-4437-83a0-b3580ecee843]
63-
description = "Absolute value of a positive purely real number"
70+
description = "Absolute value -> Absolute value of a positive purely real number"
6471

6572
[cf88d7d3-ee74-4f4e-8a88-a1b0090ecb0c]
66-
description = "Absolute value of a negative purely real number"
73+
description = "Absolute value -> Absolute value of a negative purely real number"
6774

6875
[bbe26568-86c1-4bb4-ba7a-da5697e2b994]
69-
description = "Absolute value of a purely imaginary number with positive imaginary part"
76+
description = "Absolute value -> Absolute value of a purely imaginary number with positive imaginary part"
7077

7178
[3b48233d-468e-4276-9f59-70f4ca1f26f3]
72-
description = "Absolute value of a purely imaginary number with negative imaginary part"
79+
description = "Absolute value -> Absolute value of a purely imaginary number with negative imaginary part"
7380

7481
[fe400a9f-aa22-4b49-af92-51e0f5a2a6d3]
75-
description = "Absolute value of a number with real and imaginary part"
82+
description = "Absolute value -> Absolute value of a number with real and imaginary part"
7683

7784
[fb2d0792-e55a-4484-9443-df1eddfc84a2]
78-
description = "Conjugate a purely real number"
85+
description = "Complex conjugate -> Conjugate a purely real number"
7986

8087
[e37fe7ac-a968-4694-a460-66cb605f8691]
81-
description = "Conjugate a purely imaginary number"
88+
description = "Complex conjugate -> Conjugate a purely imaginary number"
8289

8390
[f7704498-d0be-4192-aaf5-a1f3a7f43e68]
84-
description = "Conjugate a number with real and imaginary part"
91+
description = "Complex conjugate -> Conjugate a number with real and imaginary part"
8592

8693
[6d96d4c6-2edb-445b-94a2-7de6d4caaf60]
87-
description = "Euler's identity/formula"
94+
description = "Complex exponential function -> Euler's identity/formula"
8895

8996
[2d2c05a0-4038-4427-a24d-72f6624aa45f]
90-
description = "Exponential of 0"
97+
description = "Complex exponential function -> Exponential of 0"
9198

9299
[ed87f1bd-b187-45d6-8ece-7e331232c809]
93-
description = "Exponential of a purely real number"
100+
description = "Complex exponential function -> Exponential of a purely real number"
94101

95102
[08eedacc-5a95-44fc-8789-1547b27a8702]
96-
description = "Exponential of a number with real and imaginary part"
103+
description = "Complex exponential function -> Exponential of a number with real and imaginary part"
104+
105+
[d2de4375-7537-479a-aa0e-d474f4f09859]
106+
description = "Complex exponential function -> Exponential resulting in a number with real and imaginary part"
107+
108+
[06d793bf-73bd-4b02-b015-3030b2c952ec]
109+
description = "Operations between real numbers and complex numbers -> Add real number to complex number"
110+
111+
[d77dbbdf-b8df-43f6-a58d-3acb96765328]
112+
description = "Operations between real numbers and complex numbers -> Add complex number to real number"
113+
114+
[20432c8e-8960-4c40-ba83-c9d910ff0a0f]
115+
description = "Operations between real numbers and complex numbers -> Subtract real number from complex number"
116+
117+
[b4b38c85-e1bf-437d-b04d-49bba6e55000]
118+
description = "Operations between real numbers and complex numbers -> Subtract complex number from real number"
119+
120+
[dabe1c8c-b8f4-44dd-879d-37d77c4d06bd]
121+
description = "Operations between real numbers and complex numbers -> Multiply complex number by real number"
122+
123+
[6c81b8c8-9851-46f0-9de5-d96d314c3a28]
124+
description = "Operations between real numbers and complex numbers -> Multiply real number by complex number"
125+
126+
[8a400f75-710e-4d0c-bcb4-5e5a00c78aa0]
127+
description = "Operations between real numbers and complex numbers -> Divide complex number by real number"
128+
129+
[9a867d1b-d736-4c41-a41e-90bd148e9d5e]
130+
description = "Operations between real numbers and complex numbers -> Divide real number by complex number"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Uncomment the following line to enable bonus tests involving arithmetic between real numbers and complex numbers.
2+
# enable_realcomplex_tests = true
3+
4+
# Uncomment the following line to enable bonus tests for syntax sugar.
5+
# enable_syntaxsugar_tests = true

exercises/practice/complex-numbers/runtests.jl

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,43 @@ include("complex-numbers.jl")
5959
@test imag(ComplexNumber(1, 2)) == 2
6060
end
6161

62-
# Bonus A
6362
@testset "Complex exponential" begin
64-
@test_skip exp(ComplexNumber(0, π)) ComplexNumber(-1.0, 0.0) atol = 1e-15
65-
@test_skip exp(ComplexNumber(0, 0)) == ComplexNumber(1.0, 0.0)
66-
@test_skip exp(ComplexNumber(1, 0)) ComplexNumber(ℯ, 0.0) atol = 1e-15
67-
@test_skip exp(ComplexNumber(log(2), π)) ComplexNumber(-2.0, 0.0) atol = 1e-15
63+
@test exp(ComplexNumber(0, π)) ComplexNumber(-1.0, 0.0) atol = 1e-15
64+
@test exp(ComplexNumber(0, 0)) == ComplexNumber(1.0, 0.0)
65+
@test exp(ComplexNumber(1, 0)) ComplexNumber(ℯ, 0.0) atol = 1e-15
66+
@test exp(ComplexNumber(log(2), π)) ComplexNumber(-2.0, 0.0) atol = 1e-15
67+
@test exp(ComplexNumber(log(2)/2, π/4)) ComplexNumber(1.0, 1.0) atol = 1e-15
68+
end
69+
70+
# Bonus A
71+
# Uncomment the following line to enable bonus tests involving arithmetic between real numbers and complex numbers.
72+
# enable_realcomplex_tests = true
73+
if @isdefined(enable_realcomplex_tests) && enable_realcomplex_tests
74+
println("\nBonus real/complex tests enabled.\n")
75+
76+
@testset "Operations between real numbers and complex numbers" begin
77+
@test ComplexNumber(1, 2) + 5 == ComplexNumber(6, 2)
78+
@test 5 + ComplexNumber(1, 2) == ComplexNumber(6, 2)
79+
@test ComplexNumber(5, 7) - 4 == ComplexNumber(1, 7)
80+
@test 4 - ComplexNumber(5, 7) == ComplexNumber(-1, -7)
81+
@test ComplexNumber(2, 5) * 5 == ComplexNumber(10, 25)
82+
@test 5 * ComplexNumber(2, 5) == ComplexNumber(10, 25)
83+
@test ComplexNumber(10, 100) / 10 == ComplexNumber(1, 10)
84+
@test 5 / ComplexNumber(1, 1) == ComplexNumber(2.5, -2.5)
85+
end
6886
end
6987

7088
# Bonus B
71-
@testset "Syntax sugar jm" begin
72-
@test_skip ComplexNumber(0, 1) == jm
73-
@test_skip ComplexNumber(1, 0) == 1 + 0jm
74-
@test_skip ComplexNumber(1, 1) == 1 + 1jm
75-
@test_skip ComplexNumber(-1, 0) == jm^2
89+
# Uncomment the following line to enable bonus tests for syntax sugar.
90+
# enable_syntaxsugar_tests = true
91+
if @isdefined(enable_syntaxsugar_tests) && enable_syntaxsugar_tests
92+
println("\nBonus syntax sugar tests enabled.\n")
93+
94+
@testset "Syntax sugar jm" begin
95+
@test ComplexNumber(0, 1) == jm
96+
@test ComplexNumber(1, 0) == 1 + 0jm
97+
@test ComplexNumber(1, 1) == 1 + 1jm
98+
@test ComplexNumber(-1, 0) == jm^2
99+
end
76100
end
77101
end

0 commit comments

Comments
 (0)