-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
runtests.jl
212 lines (178 loc) · 6.71 KB
/
runtests.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using BGZFStreams
using Test
@info "Environment" Threads.nthreads()
@testset "VirtualOffset" begin
voff = VirtualOffset(0, 0)
@test voff == VirtualOffset(0, 0)
@test voff != VirtualOffset(0, 1)
@test voff <= VirtualOffset(0, 1)
@test voff != VirtualOffset(1, 0)
@test voff <= VirtualOffset(1, 0)
@test voff[1] == 0
@test voff[2] == 0
@test string(voff) == "VirtualOffset(0, 0)"
voff += 1
@test voff[1] == 0
@test voff[2] == 1
voff = VirtualOffset(1234, 555)
@test voff[1] == 1234
@test voff[2] == 555
@test string(voff) == "VirtualOffset(1234, 555)"
voff = VirtualOffset(1234, 555)
buf = IOBuffer()
@test write(buf, voff) == sizeof(UInt64)
seekstart(buf)
@test read(buf, VirtualOffset) === voff
@test eof(buf)
@test_throws ArgumentError VirtualOffset(1 << 48, 0)
@test_throws ArgumentError VirtualOffset(0, 1 << 16)
end
@testset "BGZFStream" begin
filename = joinpath(dirname(@__FILE__), "bar.bgz")
stream = BGZFStream(filename, "r")
@test read(stream, UInt8) === UInt8('b')
@test read(stream, UInt8) === UInt8('a')
@test read(stream, UInt8) === UInt8('r')
@test eof(stream)
@test flush(stream) === nothing
@test close(stream) === nothing
@test string(stream) == "BGZFStream{IOStream}(<mode=read>)"
@test_throws ArgumentError read(stream, UInt8)
stream = BGZFStream(filename, "r")
data = zeros(Int8, 3)
unsafe_read(stream, pointer(data), 3)
@test data == UInt8['b', 'a', 'r']
close(stream)
@test_throws ArgumentError unsafe_read(stream, pointer(data), 3)
open(BGZFStream, filename, "r") do stream
@test virtualoffset(stream) === VirtualOffset(0, 0)
read(stream, UInt8)
read(stream, UInt8)
@test virtualoffset(stream) === VirtualOffset(0, 2)
seek(stream, VirtualOffset(0, 1))
@test read(stream, UInt8) === UInt8('a')
@test read(stream, UInt8) === UInt8('r')
seekstart(stream)
@test read(stream, UInt8) === UInt8('b')
@test read(stream, UInt8) === UInt8('a')
@test read(stream, UInt8) === UInt8('r')
end
# Empty data.
empty_block = copy(BGZFStreams.EOF_BLOCK)
stream = BGZFStream(IOBuffer(empty_block))
@test_throws EOFError read(stream, UInt8)
@test_throws EOFError read(stream, UInt8)
stream = BGZFStream(IOBuffer(vcat(empty_block, empty_block)))
@test_throws EOFError read(stream, UInt8)
@test_throws EOFError read(stream, UInt8)
stream = BGZFStream(IOBuffer(empty_block))
@test isempty(read(stream))
@test isempty(read(stream))
stream = BGZFStream(IOBuffer(vcat(empty_block, empty_block)))
@test isempty(read(stream))
@test isempty(read(stream))
# invalid data
stream = BGZFStream(IOBuffer(vcat([0x1f, 0x00, 0x08, 0x04], rand(UInt8, 6))))
@test_throws BGZFDataError read(stream) # invalid gzip flag
stream = BGZFStream(IOBuffer(vcat([0x1f, 0x8b, 0x00, 0x04], rand(UInt8, 6))))
@test_throws BGZFDataError read(stream) # invalid compression method
stream = BGZFStream(IOBuffer(vcat([0x1f, 0x8b, 0x08, 0x01], rand(UInt8, 6))))
@test_throws BGZFDataError read(stream) # invalid flag
filename = tempname()
try
stream = BGZFStream(filename, "w")
@test virtualoffset(stream) == VirtualOffset(0, 0)
@test write(stream, 'b') === 1
@test write(stream, 'a') === 1
@test write(stream, 'r') === 1
@test virtualoffset(stream) == VirtualOffset(0, 3)
@test write(stream, "baz") === 3
@test eof(stream)
@test flush(stream) === nothing
@test close(stream) === nothing
@test string(stream) == "BGZFStream{IOStream}(<mode=write>)"
catch
rethrow()
finally
if isfile(filename)
rm(filename)
end
end
# Pipe I/O
filename = joinpath(dirname(@__FILE__), "bar.bgz")
@test open(p -> read(BGZFStream(p)), `cat $(filename)`) == UInt8['b', 'a', 'r']
@test_throws ArgumentError open(p -> virtualoffset(BGZFStream(p)), `cat $(filename)`)
@test_throws ArgumentError open(p -> seek(BGZFStream(p), VirtualOffset(0, 0)), `cat $(filename)`)
# TODO: test writing to a pipe?
buffer = IOBuffer()
stream = BGZFStream(buffer, "w")
write(stream, "foo")
close(stream)
@test !isopen(buffer)
@test_throws ArgumentError write(stream, 0x01)
# Round trip
for n in [0, 1, 2, 5, 10, 50, 100, 10_000, 100_000, 1_000_000]
data = rand(0x00:0xf0, n)
# bulk read/write
buffer = IOBuffer()
stream = BGZFStream(buffer, "w")
# HACK: do not close the buffer after the stream is closed
stream.onclose = io -> nothing
write(stream, data)
close(stream)
seekstart(buffer)
stream = BGZFStream(buffer)
@test data == read(stream)
close(stream)
# read/write byte by byte
buffer = IOBuffer()
stream = BGZFStream(buffer, "w")
# HACK: do not close the buffer after the stream is closed
stream.onclose = io -> nothing
for x in data
write(stream, x)
end
close(stream)
seekstart(buffer)
stream = BGZFStream(buffer)
data′ = UInt8[]
while !eof(stream)
push!(data′, read(stream, UInt8))
end
@test data == data′
close(stream)
end
n = Threads.nthreads()
# Setup buffer for writing.
buffer = IOBuffer()
stream = BGZFStream(buffer, "w")
stream.onclose = io -> nothing # HACK: do not close the buffer after the stream is closed
@test stream.blocks |> length == 1 # Note: only one block when writing,
@test stream.blocks[1].size == BGZFStreams.BGZF_SAFE_BLOCK_SIZE
# Generate n+1 blocks of data.
data = rand(0x00:0xf0, ((n+1)*BGZFStreams.BGZF_SAFE_BLOCK_SIZE) )
write_offsets = BGZFStreams.VirtualOffset[]
# Write and record virtual offset.
for d in data
write(stream, d)
push!(write_offsets, virtualoffset(stream))
end
close(stream) # Note: closing the stream will write the EOF_BLOCK.
last_write_offset = virtualoffset(stream)
# Setup buffer for reading.
seekstart(buffer)
stream = BGZFStream(buffer)
@test stream.blocks |> length == n # Note: same number of blocks as threads when reading,
read_offsets = BGZFStreams.VirtualOffset[]
data_read = typeof(data)()
# Read and record virtual offset.
while !eof(stream)
push!(data_read, read(stream, UInt8))
push!(read_offsets, virtualoffset(stream))
end
close(stream)
last_read_offset = virtualoffset(stream)
@test data == data_read
@test write_offsets == read_offsets
@test last_write_offset == last_read_offset == write_offsets[end]
end