Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ makedocs(;
)

deploydocs(;
repo="github.com/cthonios/TabularFunctions.jl",
repo="github.com/Cthonios/TabularFunctions.jl",
devbranch="main"
)
18 changes: 16 additions & 2 deletions src/TabularFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ function _is_number_expr(ex)
return ex isa Number
end

struct BadMacroInput <: Exception
msg::String
end

function _bad_macro_input(msg::String)
throw(BadMacroInput(msg))
end

"""
$(TYPEDSIGNATURES)
Example:
Expand Down Expand Up @@ -233,7 +241,10 @@ macro piecewise_analytic(expr)
elseif line isa LineNumberNode
continue
else
error("@piecewise_analytic: each line must be 'x, func'")
_bad_macro_input(
"@piecewise_analytic: each line must be 'x, func'" *
"\nbad line provided: $line"
)
end

x_expr = esc(args[1])
Expand Down Expand Up @@ -279,7 +290,10 @@ macro piecewise_linear(expr)
push!(xs, esc(p.args[1]))
push!(ys, esc(p.args[2]))
else
error("@piecewise_linear: each line must be 'x, y'")
_bad_macro_input(
"@piecewise_linear: each line must be 'x, y'" *
"\nbad line provided: $line"
)
end
end

Expand Down
77 changes: 47 additions & 30 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import TabularFunctions: BadMacroInput
import TabularFunctions: XsNotMonotonicallyIncreasing
using Adapt
using Aqua
Expand All @@ -24,6 +25,52 @@ end
@testset "Error checking" begin
@test_throws AssertionError PiecewiseLinearFunction([0., 1.], [0., 1., 2.])
@test_throws XsNotMonotonicallyIncreasing PiecewiseLinearFunction([0., 0.1, -0.1, 1.], [0., 0., 0., 0.])

code = """
@piecewise_analytic begin
0.0 # malformed
1.0, cos
end
"""
@test_throws LoadError eval(Meta.parse(code))

# code = """
# @piecewise_linear begin
# 0.0
# 1.0, 1.0
# end
# """
# @test_throws LoadError eval(Meta.parse(code))
end

@testset "Macros" begin
func = @piecewise_analytic begin
0.0, sin
1.0, cos
end
@test func(0.5) == sin(0.5)
@test func(1.5) == cos(1.5)

func = @piecewise_analytic begin
0.0, x -> sin(x)
1.0, x -> cos(x)
end
@test func(0.5) == sin(0.5)
@test func(1.5) == cos(1.5)

func = @piecewise_analytic begin
0.0, 0.0
1.0, sin
end
@test func(0.5) == 0.0
@test func(1.5) == sin(1.5)

func = @piecewise_linear begin
0.0, 0.0
0.25, 0.25
1.0, 1.0
end
@test func(0.5) == 0.5
end

@testset "PiecewiseAnalyticFunction - Identity function" begin
Expand Down Expand Up @@ -78,36 +125,6 @@ end
@test f(1.05) ≈ 1.
end

@testset "Macros" begin
func = @piecewise_analytic begin
0.0, sin
1.0, cos
end
@test func(0.5) == sin(0.5)
@test func(1.5) == cos(1.5)

func = @piecewise_analytic begin
0.0, x -> sin(x)
1.0, x -> cos(x)
end
@test func(0.5) == sin(0.5)
@test func(1.5) == cos(1.5)

func = @piecewise_analytic begin
0.0, 0.0
1.0, sin
end
@test func(0.5) == 0.0
@test func(1.5) == sin(1.5)

func = @piecewise_linear begin
0.0, 0.0
0.25, 0.25
1.0, 1.0
end
@test func(0.5) == 0.5
end

# Aqua testing
@testset "Aqua.jl" begin
Aqua.test_all(TabularFunctions)
Expand Down