|
| 1 | +package sk.ainet.io |
| 2 | + |
| 3 | +import kotlinx.io.buffered |
| 4 | +import kotlinx.io.files.Path |
| 5 | +import kotlinx.io.files.SystemFileSystem |
| 6 | +import kotlinx.io.files.SystemTemporaryDirectory |
| 7 | +import kotlinx.io.write |
| 8 | +import kotlin.test.AfterTest |
| 9 | +import kotlin.test.BeforeTest |
| 10 | +import kotlin.test.Test |
| 11 | +import kotlin.test.assertContentEquals |
| 12 | +import kotlin.test.assertEquals |
| 13 | +import kotlin.test.assertFailsWith |
| 14 | +import kotlin.test.assertNull |
| 15 | +import kotlin.test.assertTrue |
| 16 | + |
| 17 | +class PosixPreadRandomAccessSourceTest { |
| 18 | + |
| 19 | + private val expected = ByteArray(8192) { (it and 0xFF).toByte() } // 0..255 repeating |
| 20 | + private lateinit var path: Path |
| 21 | + |
| 22 | + @BeforeTest |
| 23 | + fun setUp() { |
| 24 | + path = Path(SystemTemporaryDirectory, "pread-test-${kotlin.random.Random.nextLong()}.bin") |
| 25 | + SystemFileSystem.sink(path).buffered().use { it.write(expected) } |
| 26 | + } |
| 27 | + |
| 28 | + @AfterTest |
| 29 | + fun tearDown() { |
| 30 | + if (SystemFileSystem.exists(path)) SystemFileSystem.delete(path) |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun open_reports_correct_size() { |
| 35 | + val src = PosixPreadRandomAccessSource.open(path.toString())!! |
| 36 | + try { |
| 37 | + assertEquals(expected.size.toLong(), src.size) |
| 38 | + } finally { |
| 39 | + src.close() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun read_at_zero_returns_prefix() { |
| 45 | + PosixPreadRandomAccessSource.open(path.toString())!!.use { src -> |
| 46 | + val got = src.readAt(0, 16) |
| 47 | + assertContentEquals(expected.copyOfRange(0, 16), got) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun read_at_arbitrary_offset_returns_slice() { |
| 53 | + PosixPreadRandomAccessSource.open(path.toString())!!.use { src -> |
| 54 | + val got = src.readAt(1234, 256) |
| 55 | + assertContentEquals(expected.copyOfRange(1234, 1234 + 256), got) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun read_at_end_returns_suffix() { |
| 61 | + PosixPreadRandomAccessSource.open(path.toString())!!.use { src -> |
| 62 | + val got = src.readAt(expected.size - 32L, 32) |
| 63 | + assertContentEquals(expected.copyOfRange(expected.size - 32, expected.size), got) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun read_into_buffer_reports_bytes_read() { |
| 69 | + PosixPreadRandomAccessSource.open(path.toString())!!.use { src -> |
| 70 | + val buf = ByteArray(64) |
| 71 | + val n = src.readAt(100L, buf, 0, 64) |
| 72 | + assertEquals(64, n) |
| 73 | + assertContentEquals(expected.copyOfRange(100, 164), buf) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun read_into_buffer_with_offset() { |
| 79 | + PosixPreadRandomAccessSource.open(path.toString())!!.use { src -> |
| 80 | + val buf = ByteArray(128) |
| 81 | + val n = src.readAt(50L, buf, offset = 32, length = 64) |
| 82 | + assertEquals(64, n) |
| 83 | + assertContentEquals(expected.copyOfRange(50, 114), buf.copyOfRange(32, 96)) |
| 84 | + // Bytes outside the requested window must remain zero. |
| 85 | + for (i in 0 until 32) assertEquals(0, buf[i]) |
| 86 | + for (i in 96 until 128) assertEquals(0, buf[i]) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun read_past_end_throws() { |
| 92 | + PosixPreadRandomAccessSource.open(path.toString())!!.use { src -> |
| 93 | + assertFailsWith<IllegalArgumentException> { src.readAt(expected.size - 1L, 16) } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun negative_position_throws() { |
| 99 | + PosixPreadRandomAccessSource.open(path.toString())!!.use { src -> |
| 100 | + assertFailsWith<IllegalArgumentException> { src.readAt(-1L, 4) } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun read_after_close_throws() { |
| 106 | + val src = PosixPreadRandomAccessSource.open(path.toString())!! |
| 107 | + src.close() |
| 108 | + assertFailsWith<IllegalStateException> { |
| 109 | + src.readAt(0L, ByteArray(4), 0, 4) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun close_is_idempotent() { |
| 115 | + val src = PosixPreadRandomAccessSource.open(path.toString())!! |
| 116 | + src.close() |
| 117 | + src.close() // must not throw |
| 118 | + assertTrue(true) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun open_missing_file_returns_null() { |
| 123 | + val missing = Path(SystemTemporaryDirectory, "definitely-does-not-exist-${kotlin.random.Random.nextLong()}.bin") |
| 124 | + assertNull(PosixPreadRandomAccessSource.open(missing.toString())) |
| 125 | + } |
| 126 | +} |
0 commit comments