| 1 | import os |
| 2 | |
| 3 | const tfolder = os.join_path(os.vtmp_dir(), 'os_file_buffering_tests') |
| 4 | |
| 5 | fn testsuite_begin() { |
| 6 | os.rmdir_all(tfolder) or {} |
| 7 | assert !os.is_dir(tfolder) |
| 8 | os.mkdir_all(tfolder)! |
| 9 | os.chdir(tfolder)! |
| 10 | assert os.is_dir(tfolder) |
| 11 | } |
| 12 | |
| 13 | fn testsuite_end() { |
| 14 | os.rmdir_all(tfolder) or {} |
| 15 | } |
| 16 | |
| 17 | fn test_set_buffer_line_buffered() { |
| 18 | dump(@LOCATION) |
| 19 | mut buf := []u8{len: 25} |
| 20 | dump(buf) |
| 21 | mut wfile := os.open_file('text.txt', 'wb', 0o666)! |
| 22 | wfile.set_buffer(mut buf, .line_buffered) |
| 23 | wfile.write_string('----------------------------------\n')! |
| 24 | for line in ['hello\n', 'world\n', 'hi\n'] { |
| 25 | wfile.write_string(line)! |
| 26 | wfile.flush() |
| 27 | dump(buf) |
| 28 | print(buf.bytestr()) |
| 29 | // assert buf.bytestr().contains(line) // this works on GLIBC, but fails on MUSL. |
| 30 | unsafe { buf.reset() } |
| 31 | } |
| 32 | wfile.close() |
| 33 | |
| 34 | content := os.read_lines('text.txt')! |
| 35 | dump(content) |
| 36 | assert content == ['----------------------------------', 'hello', 'world', 'hi'] |
| 37 | } |
| 38 | |
| 39 | fn test_set_buffer_fully_buffered() { |
| 40 | dump(@LOCATION) |
| 41 | mut buf := []u8{len: 30} |
| 42 | dump(buf) |
| 43 | mut wfile := os.open_file('text.txt', 'wb', 0o666)! |
| 44 | wfile.set_buffer(mut buf, .fully_buffered) |
| 45 | // Ubuntu GLIBC 2.31 seems to not use the buffer for the first write call, but it does write to the buffer first for the subsequent ones. |
| 46 | // MUSL (detecting the MUSL version is deliberately made hard by its authors, because of course it is :-( ...), will skip the first 8 bytes |
| 47 | // of the buffer, and write everything after those. |
| 48 | wfile.write_string('S')! |
| 49 | wfile.write_string('---\n')! |
| 50 | dump(buf) |
| 51 | for line in ['hello\n', 'world\n', 'hi\n'] { |
| 52 | wfile.write_string(line)! |
| 53 | dump(buf) |
| 54 | // print(buf.bytestr()) |
| 55 | } |
| 56 | wfile.close() |
| 57 | dump(buf) |
| 58 | // assert buf.bytestr().starts_with('---\nhello\nworld\nhi\n') // works on GLIBC, fails on MUSL |
| 59 | assert buf.bytestr().contains('---\nhello\nworld\n') |
| 60 | |
| 61 | content := os.read_lines('text.txt')! |
| 62 | dump(content) |
| 63 | assert content == ['S---', 'hello', 'world', 'hi'] |
| 64 | } |
| 65 | |
| 66 | fn test_set_unbuffered() { |
| 67 | dump(@LOCATION) |
| 68 | mut buf := []u8{len: 30} |
| 69 | dump(buf) |
| 70 | mut wfile := os.open_file('text.txt', 'wb', 0o666)! |
| 71 | wfile.set_buffer(mut buf, .not_buffered) |
| 72 | wfile.write_string('S')! |
| 73 | wfile.write_string('---\n')! |
| 74 | dump(buf) |
| 75 | for line in ['hello\n', 'world\n', 'hi\n'] { |
| 76 | wfile.write_string(line)! |
| 77 | dump(buf) |
| 78 | // print(buf.bytestr()) |
| 79 | } |
| 80 | wfile.close() |
| 81 | // dump(buf.bytestr()) |
| 82 | assert buf.all(it == 0) |
| 83 | |
| 84 | content := os.read_lines('text.txt')! |
| 85 | dump(content) |
| 86 | assert content == ['S---', 'hello', 'world', 'hi'] |
| 87 | } |
| 88 | |