From be569648d8f1054329504c8f3c47089630fb35e9 Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Thu, 30 Jul 2020 00:37:09 +0200 Subject: [PATCH] Simplify cache and add more iteration tests --- src/purse.jl | 5 ++--- test/basic.jl | 3 +-- test/operators.jl | 18 +++++++++++++----- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/purse.jl b/src/purse.jl index 5515ef1..899d4dd 100644 --- a/src/purse.jl +++ b/src/purse.jl @@ -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 @@ -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] diff --git a/test/basic.jl b/test/basic.jl index 3f6ca4a..e6527b7 100644 --- a/test/basic.jl +++ b/test/basic.jl @@ -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 diff --git a/test/operators.jl b/test/operators.jl index e161d31..01f3410 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -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