code wiki / (root) / fs_unlink_tail_test.nx

fs_unlink_tail_test.nx source

↩ module page · 87 lines · 2726 B

1// fs_unlink_tail_test.nx -- end-to-end smoke for fs_unlink + fs_tail. 2// 3// Builds a 5-line file, asks fs_tail for the last 3 lines, verifies 4// the byte count and the newline count, then unlinks and verifies 5// the file is gone. Re-unlinks to confirm the NotFound errno path. 6 7import "nx_syscalls.nx" 8import "nx_stdlib.nx" 9import "nx_fs.nx" 10 11func count_newlines(buf: *u8, n: i64) -> i64 { 12 var c: i64 = 0 13 var i: i64 = 0 14 while i < n { 15 if buf[i] == 10 { c = c + 1 } 16 i = i + 1 17 } 18 return c 19} 20 21func main() -> i64 { 22 let path: *u8 = ".tmp-fs-tail-smoke.txt" as *u8 23 24 // Build a 5-line payload: "L1\nL2\nL3\nL4\nL5\n" = 15 bytes. 25 let src: *u8 = sys_mmap(32) 26 src[0]=76; src[1]=49; src[2]=10 27 src[3]=76; src[4]=50; src[5]=10 28 src[6]=76; src[7]=51; src[8]=10 29 src[9]=76; src[10]=52; src[11]=10 30 src[12]=76; src[13]=53; src[14]=10 31 32 let wr: *Result<i64, FsError> = fs_write_all(path, src, 15, 420) 33 match wr { 34 Result::Ok(n) => { if n != 15 { return 10 } }, 35 Result::Err(e) => { return 11 }, 36 } 37 38 // fs_tail with n_lines=3 should return the last 3 lines = "L3\nL4\nL5\n" = 9 bytes. 39 let out: *u8 = sys_mmap(64) 40 let tail3: *Result<i64, FsError> = fs_tail(path, 3, out, 64) 41 var got: i64 = 0 42 match tail3 { 43 Result::Ok(b) => { got = b }, 44 Result::Err(e) => { return 20 }, 45 } 46 if got != 9 { return 21 } 47 if count_newlines(out, got) != 3 { return 22 } 48 if out[0] != 76 { return 23 } // 'L' 49 if out[1] != 51 { return 24 } // '3' 50 51 // fs_tail with n_lines=10 returns all 15 bytes (file has only 5). 52 let tail_all: *Result<i64, FsError> = fs_tail(path, 10, out, 64) 53 match tail_all { 54 Result::Ok(b) => { got = b }, 55 Result::Err(e) => { return 30 }, 56 } 57 if got != 15 { return 31 } 58 if count_newlines(out, got) != 5 { return 32 } 59 60 // fs_tail with out_cap smaller than the suffix truncates safely. 61 let small_out: *u8 = sys_mmap(8) 62 let tail_capped: *Result<i64, FsError> = fs_tail(path, 3, small_out, 8) 63 match tail_capped { 64 Result::Ok(b) => { got = b }, 65 Result::Err(e) => { return 40 }, 66 } 67 if got != 8 { return 41 } 68 69 // Unlink the file. Ok(0) expected. 70 let u1: *Result<i64, FsError> = fs_unlink(path) 71 match u1 { 72 Result::Ok(_) => { }, 73 Result::Err(e) => { return 50 }, 74 } 75 if fs_exists(path) != 0 { return 51 } 76 77 // Re-unlink should surface FsError::NotFound (errno -2). 78 let u2: *Result<i64, FsError> = fs_unlink(path) 79 match u2 { 80 Result::Ok(_) => { return 60 }, 81 Result::Err(e) => { 82 if e != FsError::NotFound { return 61 } 83 }, 84 } 85 86 return 0 87}