From 1cd5cbb0e4ac70ac773675c6dce6353679eaf6cb Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Mon, 26 Aug 2024 00:36:00 -0400 Subject: [PATCH] Test `io` unexpected pointer movement --- tests/core/io/test_core_io.odin | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/core/io/test_core_io.odin b/tests/core/io/test_core_io.odin index e40c79375..3f1ecdeda 100644 --- a/tests/core/io/test_core_io.odin +++ b/tests/core/io/test_core_io.odin @@ -85,6 +85,15 @@ _test_stream :: proc( testing.expectf(t, size_err == nil, "Size expected no error: %v", size_err, loc = loc) or_return + // Ensure Size does not move the underlying pointer from the start. + // + // Some implementations may use seeking to determine file sizes. + if .Seek in mode_set { + pos, seek_err := io.seek(stream, 0, .Current) + testing.expectf(t, pos == 0 && seek_err == nil, + "Size+Seek Current isn't 0 after getting size: %v, %v", pos, seek_err, loc = loc) or_return + } + passed += { .Size } } @@ -120,6 +129,13 @@ _test_stream :: proc( "Read_At+Size expected no error: %v", size_err, loc = loc) or_return } + // Ensure Read_At does not move the underlying pointer from the start. + if .Seek in mode_set { + pos, seek_err := io.seek(stream, 0, .Current) + testing.expectf(t, pos == 0 && seek_err == nil, + "Read_At+Seek Current isn't 0 after reading: %v, %v", pos, seek_err, loc = loc) or_return + } + passed += { .Read_At } } @@ -190,6 +206,15 @@ _test_stream :: proc( // Test Write_At. if .Write_At in mode_set { + // Ensure Write_At does not move the underlying pointer from the start. + starting_offset : i64 = -1 + if .Seek in mode_set { + pos, seek_err := io.seek(stream, 0, .Current) + testing.expectf(t, pos >= 0 && seek_err == nil, + "Write_At+Seek Current failed: %v, %v", pos, seek_err, loc = loc) or_return + starting_offset = pos + } + if size > 0 { write_buf, write_buf_alloc_err := make([]u8, size) testing.expectf(t, write_buf_alloc_err == nil, "allocation failed", loc = loc) or_return @@ -231,6 +256,13 @@ _test_stream :: proc( } } + // Ensure Write_At does not move the underlying pointer from the start. + if starting_offset != -1 && .Seek in mode_set { + pos, seek_err := io.seek(stream, 0, .Current) + testing.expectf(t, pos == starting_offset && seek_err == nil, + "Write_At+Seek Current isn't %v after writing: %v, %v", starting_offset, pos, seek_err, loc = loc) or_return + } + passed += { .Write_At } }