Skip to content

Commit

Permalink
Verbose Unit Testing for Comprehensive Testing Part 1/3 (#686)
Browse files Browse the repository at this point in the history
* add: verbose testing

- killer-sudoku-helper
- hamming
- saddle-points
- pangram (fix)
- leap

* add: verbose testing

- pythagorean-triplet
- collatz-conjecture
- hello-world
- complex-numbers
- raindrops

* saddle-points: Type Specification

Specify the input type to align the output type for easier unit testing.

* add: verbose testing

- nucleotide-count
- rotational-cipher
- difference-of-squares
- secret-handshake
- darts

* add: verbose testing

- rna-transcription
- scrabble-score
- reverse-string
- isogram
- anagram

* fix: coding style (rotational-cipher)
  • Loading branch information
yctai1994 authored Jan 15, 2024
1 parent 20ba426 commit 0f57523
Show file tree
Hide file tree
Showing 20 changed files with 726 additions and 689 deletions.
158 changes: 80 additions & 78 deletions exercises/practice/anagram/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,84 @@ using Test

include("anagram.jl")

@testset "no matches" begin
@test detect_anagrams("diaper", ["hello", "world", "zombies", "pants"]) == []
end

@testset "detects simple anagram" begin
@test detect_anagrams("ant", ["tan", "stand", "at"]) == ["tan"]
end

@testset "does not detect false positives" begin
@test detect_anagrams("galea", ["eagle"]) == []
end

@testset "detects two anagrams" begin
@test detect_anagrams("master", ["stream", "pigeon", "maters"]) == ["stream", "maters"]
end

@testset "does not detect anagram subsets" begin
@test detect_anagrams("good", ["dog", "goody"]) == []
end

@testset "detects anagram" begin
@test detect_anagrams("listen", ["enlists", "google", "inlets", "banana"]) == ["inlets"]
end

@testset "detects three anagrams" begin
@test detect_anagrams("allergy", ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]) == ["gallery", "regally", "largely"]
end

@testset "detects multiple anagrams with different case" begin
@test detect_anagrams("nose", ["Eons", "ONES"]) == ["Eons", "ONES"]
end

@testset "does not detect identical words" begin
@test detect_anagrams("corn", ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"]) == ["cron"]
end

@testset "does not detect non-anagrams with identical checksum" begin
@test detect_anagrams("mass", ["last"]) == []
end

@testset "detects anagrams case-insensitively" begin
@test detect_anagrams("Orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
end

@testset "detects anagrams using case-insensitive subject" begin
@test detect_anagrams("Orchestra", ["cashregister", "carthorse", "radishes"]) == ["carthorse"]
end

@testset "detects anagrams using case-insensitive possible matches" begin
@test detect_anagrams("orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
end

@testset "does not detect an anagram if the original word is repeated" begin
@test detect_anagrams("go", ["go Go GO"]) == []
end

@testset "does not detect a word as its own anagram" begin
@test detect_anagrams("banana", ["Banana"]) == []
end

@testset "does not detect a anagram if the original word is repeated" begin
@test detect_anagrams("go", ["go Go GO"]) == []
end

@testset "anagrams must use all letters exactly once" begin
@test detect_anagrams("tapper", ["patter"]) == []
end

@testset "words are not anagrams of themselves (case-insensitive)" begin
@test detect_anagrams("BANANA", ["BANANA", "Banana", "banana"]) == []
end

@testset "words other than themselves can be anagrams" begin
@test detect_anagrams("LISTEN", ["Listen", "Silent", "LISTEN"]) == ["Silent"]
end

@testset "capital word is not own anagram" begin
@test detect_anagrams("BANANA", ["Banana"]) == []
@testset verbose = true "Anagram" begin
@testset "no matches" begin
@test detect_anagrams("diaper", ["hello", "world", "zombies", "pants"]) == String[]
end

@testset "detects simple anagram" begin
@test detect_anagrams("ant", ["tan", "stand", "at"]) == ["tan"]
end

@testset "does not detect false positives" begin
@test detect_anagrams("galea", ["eagle"]) == String[]
end

@testset "detects two anagrams" begin
@test detect_anagrams("master", ["stream", "pigeon", "maters"]) == ["stream", "maters"]
end

@testset "does not detect anagram subsets" begin
@test detect_anagrams("good", ["dog", "goody"]) == String[]
end

@testset "detects anagram" begin
@test detect_anagrams("listen", ["enlists", "google", "inlets", "banana"]) == ["inlets"]
end

@testset "detects three anagrams" begin
@test detect_anagrams("allergy", ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]) == ["gallery", "regally", "largely"]
end

@testset "detects multiple anagrams with different case" begin
@test detect_anagrams("nose", ["Eons", "ONES"]) == ["Eons", "ONES"]
end

@testset "does not detect identical words" begin
@test detect_anagrams("corn", ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"]) == ["cron"]
end

@testset "does not detect non-anagrams with identical checksum" begin
@test detect_anagrams("mass", ["last"]) == String[]
end

@testset "detects anagrams case-insensitively" begin
@test detect_anagrams("Orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
end

@testset "detects anagrams using case-insensitive subject" begin
@test detect_anagrams("Orchestra", ["cashregister", "carthorse", "radishes"]) == ["carthorse"]
end

@testset "detects anagrams using case-insensitive possible matches" begin
@test detect_anagrams("orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
end

@testset "does not detect an anagram if the original word is repeated" begin
@test detect_anagrams("go", ["go Go GO"]) == String[]
end

@testset "does not detect a word as its own anagram" begin
@test detect_anagrams("banana", ["Banana"]) == String[]
end

@testset "does not detect a anagram if the original word is repeated" begin
@test detect_anagrams("go", ["go Go GO"]) == String[]
end

@testset "anagrams must use all letters exactly once" begin
@test detect_anagrams("tapper", ["patter"]) == String[]
end

@testset "words are not anagrams of themselves (case-insensitive)" begin
@test detect_anagrams("BANANA", ["BANANA", "Banana", "banana"]) == String[]
end

@testset "words other than themselves can be anagrams" begin
@test detect_anagrams("LISTEN", ["Listen", "Silent", "LISTEN"]) == ["Silent"]
end

@testset "capital word is not own anagram" begin
@test detect_anagrams("BANANA", ["Banana"]) == String[]
end
end
18 changes: 10 additions & 8 deletions exercises/practice/collatz-conjecture/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ using Test

include("collatz-conjecture.jl")

# canonical data
@testset "Canonical data" begin
@test collatz_steps(1) == 0
@test collatz_steps(16) == 4
@test collatz_steps(12) == 9
@test collatz_steps(1000000) == 152
@test_throws DomainError collatz_steps(0)
@test_throws DomainError collatz_steps(-15)
@testset verbose = true "Collatz Conjecture" begin
# canonical data
@testset "Canonical data" begin
@test collatz_steps(1) == 0
@test collatz_steps(16) == 4
@test collatz_steps(12) == 9
@test collatz_steps(1000000) == 152
@test_throws DomainError collatz_steps(0)
@test_throws DomainError collatz_steps(-15)
end
end
118 changes: 60 additions & 58 deletions exercises/practice/complex-numbers/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,76 @@ using Test

include("complex-numbers.jl")

@test ComplexNumber <: Number
@testset verbose = true "Complex Numbers" begin
@test ComplexNumber <: Number

@test ComplexNumber(0, 1)^2 == ComplexNumber(-1, 0)
@test ComplexNumber(0, 1)^2 == ComplexNumber(-1, 0)

@testset "Arithmetic" begin
@testset "Addition" begin
@test ComplexNumber(1, 0) + ComplexNumber(2, 0) == ComplexNumber(3, 0)
@test ComplexNumber(0, 1) + ComplexNumber(0, 2) == ComplexNumber(0, 3)
@test ComplexNumber(1, 2) + ComplexNumber(3, 4) == ComplexNumber(4, 6)
end
@testset "Arithmetic" begin
@testset "Addition" begin
@test ComplexNumber(1, 0) + ComplexNumber(2, 0) == ComplexNumber(3, 0)
@test ComplexNumber(0, 1) + ComplexNumber(0, 2) == ComplexNumber(0, 3)
@test ComplexNumber(1, 2) + ComplexNumber(3, 4) == ComplexNumber(4, 6)
end

@testset "Subtraction" begin
@test ComplexNumber(1, 0) - ComplexNumber(2, 0) == ComplexNumber(-1, 0)
@test ComplexNumber(0, 1) - ComplexNumber(0, 2) == ComplexNumber(0, -1)
@test ComplexNumber(1, 2) - ComplexNumber(3, 4) == ComplexNumber(-2, -2)
end
@testset "Subtraction" begin
@test ComplexNumber(1, 0) - ComplexNumber(2, 0) == ComplexNumber(-1, 0)
@test ComplexNumber(0, 1) - ComplexNumber(0, 2) == ComplexNumber(0, -1)
@test ComplexNumber(1, 2) - ComplexNumber(3, 4) == ComplexNumber(-2, -2)
end

@testset "Multiplication" begin
@test ComplexNumber(1, 0) * ComplexNumber(2, 0) == ComplexNumber(2, 0)
@test ComplexNumber(0, 1) * ComplexNumber(0, 2) == ComplexNumber(-2, 0)
@test ComplexNumber(1, 2) * ComplexNumber(3, 4) == ComplexNumber(-5, 10)
end
@testset "Multiplication" begin
@test ComplexNumber(1, 0) * ComplexNumber(2, 0) == ComplexNumber(2, 0)
@test ComplexNumber(0, 1) * ComplexNumber(0, 2) == ComplexNumber(-2, 0)
@test ComplexNumber(1, 2) * ComplexNumber(3, 4) == ComplexNumber(-5, 10)
end

@testset "Division" begin
@test ComplexNumber(1, 0) / ComplexNumber(2, 0) == ComplexNumber(0.5, 0)
@test ComplexNumber(0, 1) / ComplexNumber(0, 2) == ComplexNumber(0.5, 0)
@test ComplexNumber(1, 2) / ComplexNumber(3, 4) ComplexNumber(0.44, 0.08)
@testset "Division" begin
@test ComplexNumber(1, 0) / ComplexNumber(2, 0) == ComplexNumber(0.5, 0)
@test ComplexNumber(0, 1) / ComplexNumber(0, 2) == ComplexNumber(0.5, 0)
@test ComplexNumber(1, 2) / ComplexNumber(3, 4) ComplexNumber(0.44, 0.08)
end
end
end

@testset "Absolute value" begin
@test abs(ComplexNumber(5, 0)) == 5
@test abs(ComplexNumber(-5, 0)) == 5
@test abs(ComplexNumber(0, 5)) == 5
@test abs(ComplexNumber(0, -5)) == 5
@test abs(ComplexNumber(3, 4)) == 5
end
@testset "Absolute value" begin
@test abs(ComplexNumber(5, 0)) == 5
@test abs(ComplexNumber(-5, 0)) == 5
@test abs(ComplexNumber(0, 5)) == 5
@test abs(ComplexNumber(0, -5)) == 5
@test abs(ComplexNumber(3, 4)) == 5
end

@testset "Complex conjugate" begin
@test conj(ComplexNumber(5, 0)) == ComplexNumber(5, 0)
@test conj(ComplexNumber(0, 5)) == ComplexNumber(0, -5)
@test conj(ComplexNumber(1, 1)) == ComplexNumber(1, -1)
end
@testset "Complex conjugate" begin
@test conj(ComplexNumber(5, 0)) == ComplexNumber(5, 0)
@test conj(ComplexNumber(0, 5)) == ComplexNumber(0, -5)
@test conj(ComplexNumber(1, 1)) == ComplexNumber(1, -1)
end

@testset "Real part" begin
@test real(ComplexNumber(1, 0)) == 1
@test real(ComplexNumber(0, 1)) == 0
@test real(ComplexNumber(1, 2)) == 1
end
@testset "Real part" begin
@test real(ComplexNumber(1, 0)) == 1
@test real(ComplexNumber(0, 1)) == 0
@test real(ComplexNumber(1, 2)) == 1
end

@testset "Imaginary part" begin
@test imag(ComplexNumber(1, 0)) == 0
@test imag(ComplexNumber(0, 1)) == 1
@test imag(ComplexNumber(1, 2)) == 2
end
@testset "Imaginary part" begin
@test imag(ComplexNumber(1, 0)) == 0
@test imag(ComplexNumber(0, 1)) == 1
@test imag(ComplexNumber(1, 2)) == 2
end

# Bonus A
@testset "Complex exponential" begin
@test_skip exp(ComplexNumber(0, π)) ComplexNumber(-1, 0)
@test_skip exp(ComplexNumber(0, 0)) == ComplexNumber(1, 0)
@test_skip exp(ComplexNumber(1, 0)) ComplexNumber(ℯ, 0)
@test_skip exp(ComplexNumber(log(2), π)) ComplexNumber(-2, 0)
end
# Bonus A
@testset "Complex exponential" begin
@test_skip exp(ComplexNumber(0, π)) ComplexNumber(-1.0, 0.0) atol = 1e-15
@test_skip exp(ComplexNumber(0, 0)) == ComplexNumber(1.0, 0.0)
@test_skip exp(ComplexNumber(1, 0)) ComplexNumber(ℯ, 0.0) atol = 1e-15
@test_skip exp(ComplexNumber(log(2), π)) ComplexNumber(-2.0, 0.0) atol = 1e-15
end

# Bonus B
@testset "Syntax sugar jm" begin
@test_skip ComplexNumber(0, 1) == jm
@test_skip ComplexNumber(1, 0) == 1 + 0jm
@test_skip ComplexNumber(1, 1) == 1 + 1jm
@test_skip ComplexNumber(-1, 0) == jm^2
# Bonus B
@testset "Syntax sugar jm" begin
@test_skip ComplexNumber(0, 1) == jm
@test_skip ComplexNumber(1, 0) == 1 + 0jm
@test_skip ComplexNumber(1, 1) == 1 + 1jm
@test_skip ComplexNumber(-1, 0) == jm^2
end
end
Loading

0 comments on commit 0f57523

Please sign in to comment.