Skip to content

Commit

Permalink
Reduce exports (#14)
Browse files Browse the repository at this point in the history
And a few other fixups
  • Loading branch information
BioTurboNick committed May 28, 2024
1 parent 28b3b6a commit e4c80fb
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OverflowContexts"
uuid = "649716ba-0eb1-4560-ace2-251185f55281"
authors = ["Nicholas Bauer <nicholasbauer@outlook.com>"]
version = "0.3"
version = "0.3.0"

[compat]
julia = "1"
Expand Down
5 changes: 1 addition & 4 deletions src/OverflowContexts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ include("base_ext.jl")
include("base_ext_sat.jl")
include("abstractarraymath_ext.jl")

export @default_checked, @default_unchecked, @default_saturating, @checked, @unchecked, @saturating,
checked_neg, checked_add, checked_sub, checked_mul, checked_pow, checked_negsub, checked_abs, checked_div, checked_fld, checked_cld, checked_rem, checked_mod,
unchecked_neg, unchecked_add, unchecked_sub, unchecked_mul, unchecked_negsub, unchecked_pow, unchecked_abs, unchecked_div, unchecked_fld, unchecked_cld, unchecked_rem, unchecked_mod,
saturating_neg, saturating_add, saturating_sub, saturating_mul, saturating_pow, saturating_negsub, saturating_abs, saturating_div, saturating_fld, saturating_cld, saturating_rem, saturating_mod
export @default_checked, @default_unchecked, @default_saturating, @checked, @unchecked, @saturating

end # module
136 changes: 68 additions & 68 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ macro default_checked()
else
@warn "A previous default was set for this module. Previously defined methods in this module will be recompiled with this new default."
end
(@__MODULE__).eval(:(-(x) = checked_neg(x)))
(@__MODULE__).eval(:(+(x...) = checked_add(x...)))
(@__MODULE__).eval(:(-(x...) = checked_sub(x...)))
(@__MODULE__).eval(:(*(x...) = checked_mul(x...)))
(@__MODULE__).eval(:(^(x...) = checked_pow(x...)))
(@__MODULE__).eval(:(abs(x) = checked_abs(x)))
(@__MODULE__).eval(:(÷(x...) = checked_div(x...)))
(@__MODULE__).eval(:(div(x) = checked_div(x)))
(@__MODULE__).eval(:(fld(x) = checked_fld(x)))
(@__MODULE__).eval(:(cld(x) = checked_cld(x)))
(@__MODULE__).eval(:(%(x...) = checked_mod(x...)))
(@__MODULE__).eval(:(rem(x) = checked_rem(x)))
(@__MODULE__).eval(:(mod(x) = checked_mod(x)))
(@__MODULE__).eval(:(-(x) = OverflowContexts.checked_neg(x)))
(@__MODULE__).eval(:(+(x...) = OverflowContexts.checked_add(x...)))
(@__MODULE__).eval(:(-(x...) = OverflowContexts.checked_sub(x...)))
(@__MODULE__).eval(:(*(x...) = OverflowContexts.checked_mul(x...)))
(@__MODULE__).eval(:(^(x...) = OverflowContexts.checked_pow(x...)))
(@__MODULE__).eval(:(abs(x) = OverflowContexts.checked_abs(x)))
(@__MODULE__).eval(:(÷(x...) = OverflowContexts.checked_div(x...)))
(@__MODULE__).eval(:(div(x) = OverflowContexts.checked_div(x)))
(@__MODULE__).eval(:(fld(x) = OverflowContexts.checked_fld(x)))
(@__MODULE__).eval(:(cld(x) = OverflowContexts.checked_cld(x)))
(@__MODULE__).eval(:(%(x...) = OverflowContexts.checked_mod(x...)))
(@__MODULE__).eval(:(rem(x) = OverflowContexts.checked_rem(x)))
(@__MODULE__).eval(:(mod(x) = OverflowContexts.checked_mod(x)))
(@__MODULE__).eval(:(__OverflowContextDefaultSet = true))
nothing
end
Expand All @@ -46,19 +46,19 @@ macro default_unchecked()
else
@warn "A previous default was set for this module. Previously defined methods in this module will be recompiled with this new default."
end
(@__MODULE__).eval(:(-(x) = unchecked_neg(x)))
(@__MODULE__).eval(:(+(x...) = unchecked_add(x...)))
(@__MODULE__).eval(:(-(x...) = unchecked_sub(x...)))
(@__MODULE__).eval(:(*(x...) = unchecked_mul(x...)))
(@__MODULE__).eval(:(^(x...) = unchecked_pow(x...)))
(@__MODULE__).eval(:(abs(x) = unchecked_abs(x)))
(@__MODULE__).eval(:(÷(x...) = unchecked_div(x...)))
(@__MODULE__).eval(:(div(x) = unchecked_div(x)))
(@__MODULE__).eval(:(fld(x) = unchecked_fld(x)))
(@__MODULE__).eval(:(cld(x) = unchecked_cld(x)))
(@__MODULE__).eval(:(%(x...) = unchecked_mod(x...)))
(@__MODULE__).eval(:(rem(x) = unchecked_rem(x)))
(@__MODULE__).eval(:(mod(x) = unchecked_mod(x)))
(@__MODULE__).eval(:(-(x) = OverflowContexts.unchecked_neg(x)))
(@__MODULE__).eval(:(+(x...) = OverflowContexts.unchecked_add(x...)))
(@__MODULE__).eval(:(-(x...) = OverflowContexts.unchecked_sub(x...)))
(@__MODULE__).eval(:(*(x...) = OverflowContexts.unchecked_mul(x...)))
(@__MODULE__).eval(:(^(x...) = OverflowContexts.unchecked_pow(x...)))
(@__MODULE__).eval(:(abs(x) = OverflowContexts.unchecked_abs(x)))
(@__MODULE__).eval(:(÷(x...) = OverflowContexts.unchecked_div(x...)))
(@__MODULE__).eval(:(div(x) = OverflowContexts.unchecked_div(x)))
(@__MODULE__).eval(:(fld(x) = OverflowContexts.unchecked_fld(x)))
(@__MODULE__).eval(:(cld(x) = OverflowContexts.unchecked_cld(x)))
(@__MODULE__).eval(:(%(x...) = OverflowContexts.unchecked_mod(x...)))
(@__MODULE__).eval(:(rem(x) = OverflowContexts.unchecked_rem(x)))
(@__MODULE__).eval(:(mod(x) = OverflowContexts.unchecked_mod(x)))
(@__MODULE__).eval(:(__OverflowContextDefaultSet = true))
nothing
end
Expand Down Expand Up @@ -129,54 +129,54 @@ macro saturating(expr)
end

const op_checked = Dict(
Symbol("unary-") => :(checked_neg),
Symbol("ambig-") => :(checked_negsub),
:+ => :(checked_add),
:- => :(checked_sub),
:* => :(checked_mul),
:^ => :(checked_pow),
:abs => :(checked_abs),
:÷ => :(checked_div),
:div => :(checked_div),
:fld => :(checked_fld),
:cld => :(checked_cld),
:% => :(checked_rem),
:rem => :(checked_rem),
:mod => :(checked_mod)
Symbol("unary-") => :(OverflowContexts.checked_neg),
Symbol("ambig-") => :(OverflowContexts.checked_negsub),
:+ => :(OverflowContexts.checked_add),
:- => :(OverflowContexts.checked_sub),
:* => :(OverflowContexts.checked_mul),
:^ => :(OverflowContexts.checked_pow),
:abs => :(OverflowContexts.checked_abs),
:÷ => :(OverflowContexts.checked_div),
:div => :(OverflowContexts.checked_div),
:fld => :(OverflowContexts.checked_fld),
:cld => :(OverflowContexts.checked_cld),
:% => :(OverflowContexts.checked_rem),
:rem => :(OverflowContexts.checked_rem),
:mod => :(OverflowContexts.checked_mod)
)

const op_unchecked = Dict(
Symbol("unary-") => :(unchecked_neg),
Symbol("ambig-") => :(unchecked_negsub),
:+ => :(unchecked_add),
:- => :(unchecked_sub),
:* => :(unchecked_mul),
:^ => :(unchecked_pow),
:abs => :(unchecked_abs),
:÷ => :(unchecked_div),
:div => :(unchecked_div),
:fld => :(unchecked_fld),
:cld => :(unchecked_cld),
:% => :(unchecked_rem),
:rem => :(unchecked_rem),
:mod => :(unchecked_mod)
Symbol("unary-") => :(OverflowContexts.unchecked_neg),
Symbol("ambig-") => :(OverflowContexts.unchecked_negsub),
:+ => :(OverflowContexts.unchecked_add),
:- => :(OverflowContexts.unchecked_sub),
:* => :(OverflowContexts.unchecked_mul),
:^ => :(OverflowContexts.unchecked_pow),
:abs => :(OverflowContexts.unchecked_abs),
:÷ => :(OverflowContexts.unchecked_div),
:div => :(OverflowContexts.unchecked_div),
:fld => :(OverflowContexts.unchecked_fld),
:cld => :(OverflowContexts.unchecked_cld),
:% => :(OverflowContexts.unchecked_rem),
:rem => :(OverflowContexts.unchecked_rem),
:mod => :(OverflowContexts.unchecked_mod)
)

const op_saturating = Dict(
Symbol("unary-") => :(saturating_neg),
Symbol("ambig-") => :(saturating_negsub),
:+ => :(saturating_add),
:- => :(saturating_sub),
:* => :(saturating_mul),
:^ => :(saturating_pow),
:abs => :(saturating_abs),
:÷ => :(saturating_div),
:div => :(saturating_div),
:fld => :(saturating_fld),
:cld => :(saturating_cld),
:% => :(saturating_rem),
:rem => :(saturating_rem),
:mod => :(saturating_mod)
Symbol("unary-") => :(OverflowContexts.saturating_neg),
Symbol("ambig-") => :(OverflowContexts.saturating_negsub),
:+ => :(OverflowContexts.saturating_add),
:- => :(OverflowContexts.saturating_sub),
:* => :(OverflowContexts.saturating_mul),
:^ => :(OverflowContexts.saturating_pow),
:abs => :(OverflowContexts.saturating_abs),
:÷ => :(OverflowContexts.saturating_div),
:div => :(OverflowContexts.saturating_div),
:fld => :(OverflowContexts.saturating_fld),
:cld => :(OverflowContexts.saturating_cld),
:% => :(OverflowContexts.saturating_rem),
:rem => :(OverflowContexts.saturating_rem),
:mod => :(OverflowContexts.saturating_mod)
)

const broadcast_op_map = Dict(
Expand Down
103 changes: 65 additions & 38 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using OverflowContexts
using OverflowContexts: checked_neg, checked_add, checked_sub, checked_mul, checked_pow, checked_negsub, checked_abs,
unchecked_neg, unchecked_add, unchecked_sub, unchecked_mul, unchecked_negsub, unchecked_pow, unchecked_abs,
saturating_neg, saturating_add, saturating_sub, saturating_mul, saturating_pow, saturating_negsub, saturating_abs
using Test

@test isempty(detect_ambiguities(OverflowContexts, Base, Core))
Expand Down Expand Up @@ -377,50 +380,74 @@ end
end

@testset "symbol replacement" begin
expr = @macroexpand @checked foldl(+, [])
@test expr.args[2] == :checked_add

expr = @macroexpand @unchecked foldl(+, [])
@test expr.args[2] == :unchecked_add

expr = @macroexpand @saturating foldl(+, [])
@test expr.args[2] == :saturating_add

expr = @macroexpand @checked foldl(-, [])
@test expr.args[2] == :checked_negsub

expr = @macroexpand @unchecked foldl(-, [])
@test expr.args[2] == :unchecked_negsub

expr = @macroexpand @saturating foldl(-, [])
@test expr.args[2] == :saturating_negsub

@test expr.args[2] == :(OverflowContexts.checked_negsub)
expr = @macroexpand @checked foldl(+, [])
@test expr.args[2] == :(OverflowContexts.checked_add)
expr = @macroexpand @checked foldl(*, [])
@test expr.args[2] == :checked_mul

expr = @macroexpand @unchecked foldl(*, [])
@test expr.args[2] == :unchecked_mul

expr = @macroexpand @saturating foldl(*, [])
@test expr.args[2] == :saturating_mul

@test expr.args[2] == :(OverflowContexts.checked_mul)
expr = @macroexpand @checked foldl(^, [])
@test expr.args[2] == :checked_pow

@test expr.args[2] == :(OverflowContexts.checked_pow)
expr = @macroexpand @checked foldl(÷, [])
@test expr.args[2] == :(OverflowContexts.checked_div)
expr = @macroexpand @checked foldl(div, [])
@test expr.args[2] == :(OverflowContexts.checked_div)
expr = @macroexpand @checked foldl(fld, [])
@test expr.args[2] == :(OverflowContexts.checked_fld)
expr = @macroexpand @checked foldl(cld, [])
@test expr.args[2] == :(OverflowContexts.checked_cld)
expr = @macroexpand @checked foldl(%, [])
@test expr.args[2] == :(OverflowContexts.checked_rem)
expr = @macroexpand @checked foldl(rem, [])
@test expr.args[2] == :(OverflowContexts.checked_rem)
expr = @macroexpand @checked foldl(mod, [])
@test expr.args[2] == :(OverflowContexts.checked_mod)

expr = @macroexpand @unchecked foldl(-, [])
@test expr.args[2] == :(OverflowContexts.unchecked_negsub)
expr = @macroexpand @unchecked foldl(+, [])
@test expr.args[2] == :(OverflowContexts.unchecked_add)
expr = @macroexpand @unchecked foldl(*, [])
@test expr.args[2] == :(OverflowContexts.unchecked_mul)
expr = @macroexpand @unchecked foldl(^, [])
@test expr.args[2] == :unchecked_pow
@test expr.args[2] == :(OverflowContexts.unchecked_pow)
expr = @macroexpand @unchecked foldl(÷, [])
@test expr.args[2] == :(OverflowContexts.unchecked_div)
expr = @macroexpand @unchecked foldl(div, [])
@test expr.args[2] == :(OverflowContexts.unchecked_div)
expr = @macroexpand @unchecked foldl(fld, [])
@test expr.args[2] == :(OverflowContexts.unchecked_fld)
expr = @macroexpand @unchecked foldl(cld, [])
@test expr.args[2] == :(OverflowContexts.unchecked_cld)
expr = @macroexpand @unchecked foldl(%, [])
@test expr.args[2] == :(OverflowContexts.unchecked_rem)
expr = @macroexpand @unchecked foldl(rem, [])
@test expr.args[2] == :(OverflowContexts.unchecked_rem)
expr = @macroexpand @unchecked foldl(mod, [])
@test expr.args[2] == :(OverflowContexts.unchecked_mod)

expr = @macroexpand @saturating foldl(-, [])
@test expr.args[2] == :(OverflowContexts.saturating_negsub)
expr = @macroexpand @saturating foldl(+, [])
@test expr.args[2] == :(OverflowContexts.saturating_add)
expr = @macroexpand @saturating foldl(*, [])
@test expr.args[2] == :(OverflowContexts.saturating_mul)
expr = @macroexpand @saturating foldl(^, [])
@test expr.args[2] == :saturating_pow

expr = @macroexpand @checked foldl(:abs, [])
@test expr.args[2] == :checked_abs

expr = @macroexpand @unchecked foldl(:abs, [])
@test expr.args[2] == :unchecked_abs

expr = @macroexpand @saturating foldl(:abs, [])
@test expr.args[2] == :saturating_abs
@test expr.args[2] == :(OverflowContexts.saturating_pow)
expr = @macroexpand @saturating foldl(÷, [])
@test expr.args[2] == :(OverflowContexts.saturating_div)
expr = @macroexpand @saturating foldl(div, [])
@test expr.args[2] == :(OverflowContexts.saturating_div)
expr = @macroexpand @saturating foldl(fld, [])
@test expr.args[2] == :(OverflowContexts.saturating_fld)
expr = @macroexpand @saturating foldl(cld, [])
@test expr.args[2] == :(OverflowContexts.saturating_cld)
expr = @macroexpand @saturating foldl(%, [])
@test expr.args[2] == :(OverflowContexts.saturating_rem)
expr = @macroexpand @saturating foldl(rem, [])
@test expr.args[2] == :(OverflowContexts.saturating_rem)
expr = @macroexpand @saturating foldl(mod, [])
@test expr.args[2] == :(OverflowContexts.saturating_mod)
end

@testset "negsub helper methods dispatch correctly" begin
Expand Down

0 comments on commit e4c80fb

Please sign in to comment.