Skip to content

Commit 9ef2949

Browse files
committed
Cross-platform test coverage for n-byte reads and throwing EOF on insufficient bytes read.
1 parent 0a79189 commit 9ef2949

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

test/file.jl

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,5 +942,51 @@ function test_13559()
942942
close(r)
943943
rm(fn)
944944
end
945-
946945
@unix_only test_13559()
946+
947+
function test_read_nbyte()
948+
fn = tempname()
949+
# Write one byte. One byte read should work once
950+
# but 2-byte read should throw EOFError.
951+
f = open(fn, "w+") do f
952+
write(f, 0x55)
953+
flush(f)
954+
seek(f, 0)
955+
@test read(f, UInt8) == 0x55
956+
@test_throws EOFError read(f, UInt8)
957+
seek(f, 0)
958+
@test_throws EOFError read(f, UInt16)
959+
end
960+
# Write 2 more bytes. Now 2-byte read should work once
961+
# but 4-byte read should fail with EOFError.
962+
open(fn, "a+") do f
963+
write(f, 0x4444)
964+
flush(f)
965+
seek(f, 0)
966+
@test read(f, UInt16) == 0x4455
967+
@test_throws EOFError read(f, UInt16)
968+
seek(f,0)
969+
@test_throws EOFError read(f, UInt32)
970+
end
971+
# Write 4 more bytes. Now 4-byte read should work once
972+
# but 8-byte read should fail with EOFError.
973+
open(fn, "a+") do f
974+
write(f, 0x33333333)
975+
flush(f)
976+
seek(f, 0)
977+
@test read(f, UInt32) == 0x33444455
978+
@test_throws EOFError read(f, UInt32)
979+
seek(f,0)
980+
@test_throws EOFError read(f, UInt64)
981+
end
982+
# Writing one more byte should allow an 8-byte
983+
# read to proceed.
984+
open(fn, "a+") do f
985+
write(f, 0x22)
986+
flush(f)
987+
seek(f, 0)
988+
@test read(f, UInt64) == 0x2233333333444455
989+
end
990+
rm(fn)
991+
end
992+
test_read_nbyte()

0 commit comments

Comments
 (0)