Skip to content

Commit

Permalink
Simplify cache and add more iteration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dalum committed Jul 29, 2020
1 parent 9559db5 commit be56964
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/purse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ julia> Purses.value(Purse(2.0))
"""
cache(x[, idx])
If `x` is a purse, return its stored cache as a tuple, otherwise return an empty tuple. If
the optional argument `idx` is supplied, return the cache item stored at index `idx`.
Return the cache associated with the purse `x`. If the optional argument `idx` is supplied,
return the cache item stored at index `idx`.
# Examples
```jldoctest
Expand All @@ -101,6 +101,5 @@ julia> Purses.cache(purse, 1)
-1.0
```
"""
@inline cache(x) = ()
@inline cache(x::Purse) = x.cache
@inline cache(x, idx) = cache(x)[idx]
3 changes: 1 addition & 2 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ using Purses
x = 1.5
purse = Purse(x)
@test Purses.value(purse) === Purses.value(x) === x
@test Purses.cache(purse) === Purses.cache(x) === ()
@test Purses.cache(purse) === ()
@test_throws BoundsError Purses.cache(purse, 1)
@test_throws BoundsError Purses.cache(x, 1)
@test Purses.cache_signature(typeof(purse)) === Tuple{}

x = 1.5
Expand Down
18 changes: 13 additions & 5 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,21 @@ end
end

@testset "Iteration protocol" begin
purse = Purse(1:10, sum)
purse = Purse(1:5, sum)
@test map(x -> x^2, purse) == map(x -> x^2, value(purse))
begin
for idx in eachindex(purse)
@test purse[idx] == value(purse)[idx]
end

@test iterate(purse) === (1, 1)
@test iterate(purse, 1) === (2, 2)
@test iterate(purse, 5) === nothing

for idx in eachindex(purse)
@test purse[idx] == value(purse)[idx]
end

for (x1, x2) in zip(purse, value(purse))
@test x1 == x2
end

collected_purse = Purse(collect(purse), sum)
@test_throws ErrorException purse[1] = 2
end
Expand Down

0 comments on commit be56964

Please sign in to comment.