Skip to content

Commit

Permalink
Merge f242345 into 39dc22d
Browse files Browse the repository at this point in the history
  • Loading branch information
kmsquire committed Feb 19, 2020
2 parents 39dc22d + f242345 commit 1ff8074
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 38 deletions.
7 changes: 4 additions & 3 deletions src/Memoize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ macro memoize(args...)
end

fcachename = Symbol("##", f, "_memoized_cache")
fcache = isdefined(Main, fcachename) ?
getfield(Main, fcachename) :
Core.eval(Main, :(const $fcachename = ($dicttype)()))
mod = __module__
fcache = isdefined(mod, fcachename) ?
getfield(mod, fcachename) :
Core.eval(mod, :(const $fcachename = ($dicttype)()))

if length(kws) == 0
lookup = :($fcache[($(tup...),)]::Core.Compiler.return_type($u, typeof(($(identargs...),))))
Expand Down
98 changes: 63 additions & 35 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ arun = 0
print("evaluating memadd $x $y\n")
return x + y
end
@test memadd(1,2) == 3
@test memadd(1,4) == 5
@test memadd(1,2) == 3
@test memadd(1, 2) == 3
@test memadd(1, 4) == 5
@test memadd(1, 2) == 3
@test arun == 2

run = 0
Expand Down Expand Up @@ -44,7 +44,7 @@ end
@test run == 2

run = 0
@memoize function default(a=2)
@memoize function default(a = 2)
global run += 1
a
end
Expand All @@ -60,7 +60,7 @@ end
@test run == 2

run = 0
@memoize function default_typed(a::Int=2)
@memoize function default_typed(a::Int = 2)
global run += 1
a
end
Expand All @@ -76,89 +76,89 @@ end
@test run == 2

run = 0
@memoize function kw(; a=2)
@memoize function kw(; a = 2)
global run += 1
a
end
@test kw() == 2
@test run == 1
@test kw() == 2
@test run == 1
@test kw(a=2) == 2
@test kw(a = 2) == 2
@test run == 1
@test kw(a=6) == 6
@test kw(a = 6) == 6
@test run == 2
@test kw(a=6) == 6
@test kw(a = 6) == 6
@test run == 2

run = 0
@memoize function kw_typed(; a::Int=2)
@memoize function kw_typed(; a::Int = 2)
global run += 1
a
end
@test kw_typed() == 2
@test run == 1
@test kw_typed() == 2
@test run == 1
@test kw_typed(a=2) == 2
@test kw_typed(a = 2) == 2
@test run == 1
@test kw_typed(a=6) == 6
@test kw_typed(a = 6) == 6
@test run == 2
@test kw_typed(a=6) == 6
@test kw_typed(a = 6) == 6
@test run == 2

run = 0
@memoize function default_kw(a=1; b=2)
@memoize function default_kw(a = 1; b = 2)
global run += 1
(a, b)
end
@test default_kw() == (1, 2)
@test run == 1
@test default_kw() == (1, 2)
@test run == 1
@test default_kw(1, b=2) == (1, 2)
@test default_kw(1, b = 2) == (1, 2)
@test run == 1
@test default_kw(1, b=3) == (1, 3)
@test default_kw(1, b = 3) == (1, 3)
@test run == 2
@test default_kw(1, b=3) == (1, 3)
@test default_kw(1, b = 3) == (1, 3)
@test run == 2
@test default_kw(2, b=3) == (2, 3)
@test default_kw(2, b = 3) == (2, 3)
@test run == 3
@test default_kw(2, b=3) == (2, 3)
@test default_kw(2, b = 3) == (2, 3)
@test run == 3

run = 0
@memoize function default_kw_typed(a::Int=1; b::Int=2)
@memoize function default_kw_typed(a::Int = 1; b::Int = 2)
global run += 1
(a, b)
end
@test default_kw_typed() == (1, 2)
@test run == 1
@test default_kw_typed() == (1, 2)
@test run == 1
@test default_kw_typed(1, b=2) == (1, 2)
@test default_kw_typed(1, b = 2) == (1, 2)
@test run == 1
@test default_kw_typed(1, b=3) == (1, 3)
@test default_kw_typed(1, b = 3) == (1, 3)
@test run == 2
@test default_kw_typed(1, b=3) == (1, 3)
@test default_kw_typed(1, b = 3) == (1, 3)
@test run == 2
@test default_kw_typed(2, b=3) == (2, 3)
@test default_kw_typed(2, b = 3) == (2, 3)
@test run == 3
@test default_kw_typed(2, b=3) == (2, 3)
@test default_kw_typed(2, b = 3) == (2, 3)
@test run == 3

run = 0
@memoize function required_kw(; a)
global run += 1
a
end
@test required_kw(a=1) == 1
@test required_kw(a = 1) == 1
@test run == 1
@test required_kw(a=1) == 1
@test required_kw(a = 1) == 1
@test run == 1
@test required_kw(a=2) == 2
@test required_kw(a = 2) == 2
@test run == 2
@test required_kw(a=2) == 2
@test required_kw(a = 2) == 2
@test run == 2
@test_throws UndefKeywordError required_kw()

Expand All @@ -185,13 +185,13 @@ end
@test run == 1
@test isempty(kw_ellipsis())
@test run == 1
@test kw_ellipsis(a=1) == pairs((a=1,))
@test kw_ellipsis(a = 1) == pairs((a = 1,))
@test run == 2
@test kw_ellipsis(a=1) == pairs((a=1,))
@test kw_ellipsis(a = 1) == pairs((a = 1,))
@test run == 2
@test kw_ellipsis(a=1, b=2) == pairs((a=1,b=2))
@test kw_ellipsis(a = 1, b = 2) == pairs((a = 1, b = 2))
@test run == 3
@test kw_ellipsis(a=1, b=2) == pairs((a=1,b=2))
@test kw_ellipsis(a = 1, b = 2) == pairs((a = 1, b = 2))
@test run == 3

run = 0
Expand Down Expand Up @@ -230,7 +230,7 @@ function outer()
end
@memoize function inner(x, y)
run += 1
x+y
x + y
end
@test inner(5) == 5
@test run == 1
Expand All @@ -257,7 +257,7 @@ end
finalized = false
@memoize function method_rewrite()
x = []
finalizer(x->(global finalized; finalized = true),x)
finalizer(x->(global finalized; finalized = true), x)
x
end
method_rewrite()
Expand All @@ -280,3 +280,31 @@ end
@test run == 2
@test documented_function(2) == 2
@test run == 2


module MemoizeTest
using Test
using Memoize

const MyDict = Dict

println(MemoizeTest)

run = 0
@memoize MyDict function custom_dict(a)
global run += 1
a
end
@test custom_dict(1) == 1
@test run == 1
@test custom_dict(1) == 1
@test run == 1
@test custom_dict(2) == 2
@test run == 2
@test custom_dict(2) == 2
@test run == 2
@test custom_dict(1) == 1
@test run == 2

end

0 comments on commit 1ff8074

Please sign in to comment.