Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when indexing StepRange with begin and end #83

Merged
merged 2 commits into from
Dec 9, 2024
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: 2 additions & 0 deletions src/momentintime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,8 @@ Base.:(:)(start::MIT{F}, step::Duration{F}, stop::MIT{F}) where {F<:Frequency} =

Base.length(rng::UnitRange{<:MIT}) = convert(Int, last(rng) - first(rng) + 1)
Base.step(rng::UnitRange{<:MIT}) = convert(Int, 1)
Base.length(rng::StepRange{<:MIT}) = length(StepRange{Int,Int}(rng.start,rng.step,rng.stop))
Base.step(rng::StepRange{<:MIT}) = step(StepRange{Int,Int}(rng.start,rng.step,rng.stop))

"""
rangeof_span(args...)
Expand Down
8 changes: 7 additions & 1 deletion test/test_mit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ end
@test first(rng) <= m <= last(rng)
@test rng[i] == m
end
@test rng[begin] == first(rng)
@test rng[end] == last(rng)
@test_throws ArgumentError 2020Q1:2020M12

@test rangeof_span(3U:5U, 4U:6U) === 3U:6U
Expand All @@ -138,16 +140,20 @@ end
# step ranges
sr1 = 1Q1:1Q3-1Q1:4Q4
@test length(sr1) == 8
# @test length(sr1) isa Int
@test step(sr1) == 2
# @test step(sr1) isa Int
@test first(sr1) == 1Q1
@test last(sr1) == 4Q3
@test sr1[begin] == first(sr1) && sr1[end] == last(sr1)
@test collect(sr1) == [1Q1, 1Q3, 2Q1, 2Q3, 3Q1, 3Q3, 4Q1, 4Q3]

sr2 = 1Q1:2:4Q4
@test length(sr2) == 8
@test step(sr2) == 2
@test first(sr2) == 1Q1
@test last(sr2) == 4Q3
@test sr2[begin] == first(sr1) && sr1[end] == last(sr1)
@test collect(sr2) == [1Q1, 1Q3, 2Q1, 2Q3, 3Q1, 3Q3, 4Q1, 4Q3]

@test_throws ArgumentError 1Q2:2:5U
Expand Down
Loading