nx_budget_test.nx source
↩ module page · 65 lines · 2441 B
1// nx_budget_test.nx -- smoke for nx_budget.
2//
3// Exercises:
4// - construction with five resource ceilings
5// - request within ceiling grants and decrements headroom
6// - request beyond ceiling denies without state mutation
7// - release returns headroom; underflow clamps to 0
8// - pressure_q10 reports correct ratio
9// - bad kind returns BAD_KIND verdict
10
11import "nx_syscalls.nx"
12import "nx_budget.nx"
13
14func main() -> i64 {
15 let b: *NxBudget = nx_budget_new(42, 1000, 2000, 3000, 4000, 5000)
16
17 // 1: initial state is zero used
18 if b.ram_used != 0 { return 1 }
19 if b.vram_used != 0 { return 2 }
20
21 // 2: in-bound request grants
22 if nx_budget_request(b, NX_RES_RAM, 600) != NX_BUDGET_OK { return 3 }
23 if b.ram_used != 600 { return 4 }
24
25 // 3: second request that fits
26 if nx_budget_request(b, NX_RES_RAM, 400) != NX_BUDGET_OK { return 5 }
27 if b.ram_used != 1000 { return 6 }
28
29 // 4: over-budget request denies WITHOUT mutation
30 if nx_budget_request(b, NX_RES_RAM, 1) != NX_BUDGET_ERR_OVER { return 7 }
31 if b.ram_used != 1000 { return 8 }
32
33 // 5: release returns headroom
34 if nx_budget_release(b, NX_RES_RAM, 500) != NX_BUDGET_OK { return 9 }
35 if b.ram_used != 500 { return 10 }
36
37 // 6: release more than used underflows, clamps to 0
38 if nx_budget_release(b, NX_RES_RAM, 9999) != NX_BUDGET_ERR_UNDERFLOW { return 11 }
39 if b.ram_used != 0 { return 12 }
40
41 // 7: pressure_q10 at half capacity
42 if nx_budget_request(b, NX_RES_VRAM, 1000) != NX_BUDGET_OK { return 13 }
43 let q: nx_int = nx_budget_pressure_q10(b, NX_RES_VRAM)
44 if q < 510 { return 14 } // 50% allow +/- 1
45 if q > 514 { return 15 }
46
47 // 8: remaining headroom
48 let rem: nx_size = nx_budget_remaining(b, NX_RES_VRAM)
49 if rem != 1000 { return 16 }
50
51 // 9: bad kind rejected
52 if nx_budget_request(b, 99, 1) != NX_BUDGET_ERR_BAD_KIND { return 17 }
53 if nx_budget_release(b, -1, 1) != NX_BUDGET_ERR_BAD_KIND { return 18 }
54
55 // 10: net resource exercised
56 if nx_budget_request(b, NX_RES_NET, 5000) != NX_BUDGET_OK { return 19 }
57 if nx_budget_request(b, NX_RES_NET, 1) != NX_BUDGET_ERR_OVER { return 20 }
58
59 // 11: disk + cpu independent
60 if nx_budget_request(b, NX_RES_DISK, 4000) != NX_BUDGET_OK { return 21 }
61 if nx_budget_request(b, NX_RES_CPU, 3000) != NX_BUDGET_OK { return 22 }
62 if nx_budget_pressure_q10(b, NX_RES_CPU) != 1024 { return 23 }
63
64 return 0
65}