Skip to content

Commit 0e84f14

Browse files
authored
Test and capture edge cases for empty views into diskarrays (#232)
* Test and capture edge cases for empty views into diskarrays * bug in test
1 parent 235825f commit 0e84f14

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/chunks.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function subsetchunks_fallback(chunks::ChunkVector, subsets)
2020
# This is a fallback method that should work for Regular and Irregular chunks.
2121
# Assuming the desired subset is sorted, we simply compute the chunk for every element
2222
# in subs and collect everything together again in either a Regular or IrregularChunk
23+
if isempty(subsets)
24+
return RegularChunks(1, 0, 0)
25+
end
2326
rev = if issorted(subsets)
2427
false
2528
elseif issorted(subsets; rev=true)
@@ -108,7 +111,7 @@ function subsetchunks(chunks::RegularChunks, subsets::AbstractUnitRange)
108111
chunks = RegularChunks(chunks.chunksize, newoffset, newsize)
109112
# In case the new chunk is trivial and has length 1, we shorten the chunk size
110113
if length(chunks) == 1
111-
chunks = RegularChunks(newsize, 0, newsize)
114+
chunks = RegularChunks(max(newsize, 1), 0, newsize)
112115
end
113116
return chunks
114117
end
@@ -176,6 +179,9 @@ Base.show(io::IO, chunks::IrregularChunks) =
176179
Base.print(io, "IrregularChunks($(chunks.offsets))")
177180

178181
function subsetchunks(chunks::IrregularChunks, subsets::UnitRange)
182+
if isempty(subsets)
183+
return IrregularChunks([0])
184+
end
179185
c1 = findchunk(chunks, first(subsets))
180186
c2 = findchunk(chunks, last(subsets))
181187
newoffsets = chunks.offsets[c1:(c2+1)]
@@ -253,7 +259,7 @@ end
253259
function chunktype_from_chunksizes(chunksizes::AbstractVector)
254260
if length(chunksizes) == 1
255261
# only a single chunk is affected
256-
return RegularChunks(chunksizes[1], 0, chunksizes[1])
262+
return RegularChunks(max(chunksizes[1], 1), 0, chunksizes[1])
257263
elseif length(chunksizes) == 2
258264
# Two affected chunks
259265
chunksize = max(chunksizes[1], chunksizes[2])

test/runtests.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ function test_view(a)
119119
@test trueparent(a)[2:3, 3:4] == [4 4; 4 4]
120120
@test getindex_count(a) == 2
121121
@test setindex_count(a) == 2
122+
123+
v2 = view(a, 2:3, 2:4, Int[])
124+
@test size(eachchunk(v2)) == (1, 1, 0)
122125
end
123126

124127
function test_reductions(af)
@@ -204,10 +207,12 @@ end
204207
@test a1[3] == 9:10
205208
@test length(a1) == 3
206209
@test size(a1) == (3,)
207-
v1 = subsetchunks(a1, 1:10)
208-
v2 = subsetchunks(a1, 4:9)
209-
@test v1 === a1
210-
@test v2 === RegularChunks(5, 0, 6)
210+
@test subsetchunks(a1, 1:10) === a1
211+
@test subsetchunks(a1, 4:9) === RegularChunks(5, 0, 6)
212+
@test subsetchunks(a1, Int[]) === RegularChunks(1, 0, 0)
213+
@test subsetchunks(a1, 5:4) === RegularChunks(1, 0, 0)
214+
@test subsetchunks(a1, 2:2:8) === RegularChunks(3, 2, 4)
215+
@test subsetchunks(a1, 10:-2:1) === RegularChunks(3, 2, 5)
211216
a2 = RegularChunks(2, 0, 20)
212217
@test a2[1] == 1:2
213218
@test a2[2] == 3:4
@@ -232,6 +237,10 @@ end
232237
@test_throws BoundsError b1[6]
233238
@test subsetchunks(b1, 1:15) == IrregularChunks(; chunksizes=[3, 3, 4, 3, 2])
234239
@test subsetchunks(b1, 3:10) == IrregularChunks(; chunksizes=[1, 3, 4])
240+
@test subsetchunks(b1, Int[]) == IrregularChunks(; chunksizes=Int[])
241+
@test subsetchunks(b1, 5:4) == IrregularChunks(; chunksizes=Int[])
242+
@test subsetchunks(b1, 2:2:15) == IrregularChunks(; chunksizes=[1, 2, 2, 1, 1])
243+
@test subsetchunks(b1, 14:-2:1) == IrregularChunks(; chunksizes=[1, 1, 2, 2, 1])
235244
gridc = GridChunks(a1, a2, b1)
236245
@test eltype(gridc) <: Tuple{UnitRange,UnitRange,UnitRange}
237246
@test gridc[1, 1, 1] == (1:3, 1:2, 1:3)

0 commit comments

Comments
 (0)