| 1 | import os |
| 2 | import compress.zstd |
| 3 | |
| 4 | const samples_folder = os.join_path(os.dir(@FILE), 'samples') |
| 5 | |
| 6 | fn s(fname string) string { |
| 7 | return os.join_path(samples_folder, fname) |
| 8 | } |
| 9 | |
| 10 | fn read_and_decode_file(fpath string) !([]u8, string) { |
| 11 | compressed := os.read_bytes(fpath)! |
| 12 | decoded := zstd.decompress(compressed)! |
| 13 | content := decoded.bytestr() |
| 14 | return compressed, content |
| 15 | } |
| 16 | |
| 17 | fn test_reading_and_decoding_a_known_zstded_file() { |
| 18 | compressed, content := read_and_decode_file(s('known.zst'))! |
| 19 | assert compressed#[0..3] == [u8(40), 181, 47] |
| 20 | assert compressed#[-5..] == [u8(10), 78, 32, 170, 44] |
| 21 | assert content.contains('## Description') |
| 22 | assert content.contains('## Examples:') |
| 23 | assert content.ends_with('```') |
| 24 | } |
| 25 | |
| 26 | fn test_decoding_all_samples_files() { |
| 27 | for zstd_file in os.walk_ext(samples_folder, '.zst') { |
| 28 | _, content := read_and_decode_file(zstd_file)! |
| 29 | assert content.len > 0, 'decoded content should not be empty: `${content}`' |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn test_reading_zstd_files_compressed_with_different_compress_level() { |
| 34 | _, content1 := read_and_decode_file(s('readme_level_1.zst'))! |
| 35 | _, content5 := read_and_decode_file(s('readme_level_5.zst'))! |
| 36 | _, content9 := read_and_decode_file(s('readme_level_9.zst'))! |
| 37 | _, content19 := read_and_decode_file(s('readme_level_19.zst'))! |
| 38 | assert content19 == content9 |
| 39 | assert content9 == content5 |
| 40 | assert content5 == content1 |
| 41 | } |
| 42 | |