nx_disk_budget_test.nx source
↩ module page · 50 lines · 1892 B
1// nx_disk_budget_test.nx -- smoke for the hard-ceiling disk budget.
2
3import "nx_syscalls.nx"
4import "nx_tier.nx"
5import "nx_disk_budget.nx"
6
7func main() -> nx_exit {
8 let path: *nx_byte = "/dev/null" as *nx_byte
9 let fd: nx_fd = sys_openat_wr(path, 0x1A4)
10 if fd < 0 { return 90 }
11
12 // ----- T1: fresh budget -----
13 let b: *NxDiskBudget = nx_disk_budget_new(100 as nx_size)
14 if nx_disk_budget_used(b) != 0 { return 11 }
15 if nx_disk_budget_remaining(b) != 100 { return 12 }
16 if nx_disk_budget_n_writes_ok(b) != 0 { return 13 }
17 if nx_disk_budget_n_refused(b) != 0 { return 14 }
18 if nx_disk_budget_pct_used(b) != 0 { return 15 }
19
20 // ----- T2: within-budget write -----
21 let line: *nx_byte = "hello world!" as *nx_byte
22 let len: nx_size = 12
23 let r1: nx_int = nx_disk_budget_write(b, fd, line, len)
24 if r1 != 12 { return 21 }
25 if nx_disk_budget_used(b) != 12 { return 22 }
26 if nx_disk_budget_n_writes_ok(b) != 1 { return 23 }
27 if nx_disk_budget_remaining(b) != 88 { return 24 }
28
29 // ----- T3: over-budget write refused -----
30 let big: *nx_byte = sys_mmap(128)
31 var i: nx_idx = 0
32 while i < 89 { big[i] = 65 as nx_byte; i = i + 1 }
33 let r2: nx_int = nx_disk_budget_write(b, fd, big, 89 as nx_size)
34 if r2 != NX_DISK_BUDGET_EXCEEDED { return 31 }
35 if nx_disk_budget_used(b) != 12 { return 32 }
36 if nx_disk_budget_n_refused(b) != 1 { return 33 }
37 if nx_disk_budget_n_writes_ok(b) != 1 { return 34 }
38 if nx_disk_budget_remaining(b) != 88 { return 35 }
39
40 // ----- T4: pct_used -----
41 if nx_disk_budget_pct_used(b) != 12 { return 41 }
42
43 // ----- T5: predicate -----
44 if nx_disk_budget_can_write(b, 88 as nx_size) != 1 { return 51 }
45 if nx_disk_budget_can_write(b, 89 as nx_size) != 0 { return 52 }
46 if nx_disk_budget_can_write(b, 0 as nx_size) != 1 { return 53 }
47
48 sys_close(fd)
49 return 0
50}