Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fkastner committed Feb 23, 2020
1 parent 8d288f6 commit f917762
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ version = "0.1.0"

[deps]
Primes = "27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
92 changes: 92 additions & 0 deletions docs/Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# This file is machine-generated - editing it directly is not advised

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[DocStringExtensions]]
deps = ["LibGit2", "Markdown", "Pkg", "Test"]
git-tree-sha1 = "88bb0edb352b16608036faadcc071adda068582a"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.8.1"

[[Documenter]]
deps = ["Base64", "Dates", "DocStringExtensions", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"]
git-tree-sha1 = "d497bcc45bb98a1fbe19445a774cfafeabc6c6df"
uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
version = "0.24.5"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[JSON]]
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
git-tree-sha1 = "b34d7cef7b337321e97d22242c3c2b91f476748e"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.0"

[[LibGit2]]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[Parsers]]
deps = ["Dates", "Test"]
git-tree-sha1 = "d112c19ccca00924d5d3a38b11ae2b4b268dda39"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "0.3.11"

[[Pkg]]
deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Test", "UUIDs"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
7 changes: 5 additions & 2 deletions src/NiceNumbers.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module NiceNumbers

using Primes
import Base: +, -, *, /, inv
import Primes: factor, prodfactors
import Base: +, -, *, /, inv, one, zero, isinteger
import Base.promote_rule

export NiceNumber
export isrational

struct NiceNumber <: Real
a::Rational{Int}
Expand Down
43 changes: 43 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using NiceNumbers
using Test


@testset "NiceNumbers" begin
@testset "Constructors" begin
@test NiceNumber(8) == NiceNumber(8, 0, 0)
@test NiceNumber(2, 3, 4) == NiceNumber(8)
@test NiceNumber(2, 3, 0) == NiceNumber(2)
@test NiceNumber(2, 0, 5) == NiceNumber(2)
end

n = NiceNumber(2, 3, 5)
m = NiceNumber(5, -3, 5)
k = NiceNumber(8, 4, 3)

@testset "Basic Arithmetic" begin
@test n + m == NiceNumber(7)
@test_broken n + k
@test n - m == NiceNumber(-3, 6, 5)
@test_broken n - k
@test n * m == NiceNumber(-35, 9, 5)
@test_broken n * k
@test n / m == NiceNumber(-11 // 4, -21 // 20, 5)
@test_broken n / k
end

@testset "Scalar Arithmetic" begin
@test 4 + n == n + 4 == NiceNumber(6, 3, 5)
@test 4 - n == -(n - 4) == NiceNumber(2, -3, 5)
@test 3 * n == n * 3 == NiceNumber(6, 9, 5)
@test n / 3 == inv(3 / n) == NiceNumber(2 // 3, 1, 5)
end

@testset "Conversion" begin
@test float(NiceNumber(13, 3, 7)) == 13 + 3 * 7
end

@testset "Misc" begin
@test isrational(NiceNumber(139 // 7, 0, 17))
@test isinteger(NiceNumber(12))
end
end

0 comments on commit f917762

Please sign in to comment.