nx_compost_test.nx source
↩ module page · 118 lines · 5257 B
1// nx_compost_test.nx -- smoke for nx_compost.
2
3import "nx_syscalls.nx"
4import "nx_compost.nx"
5
6func main() -> i64 {
7 let now: nx_size = 1000000
8
9 // 1: enum validity
10 if nx_cp_nut_is_valid(NX_CP_NUT_CONST_DEFINITION) != 1 { return 1 }
11 if nx_cp_nut_is_valid(NX_CP_NUT_BENCHMARK) != 1 { return 2 }
12 if nx_cp_nut_is_valid(-1) != 0 { return 3 }
13 if nx_cp_nut_is_valid(7) != 0 { return 4 }
14 if NX_CP_NUT_N != 7 { return 5 }
15
16 if nx_cp_stage_is_valid(NX_CP_STAGE_FRESH) != 1 { return 6 }
17 if nx_cp_stage_is_valid(NX_CP_STAGE_REFUSED) != 1 { return 7 }
18 if nx_cp_stage_is_valid(-1) != 0 { return 8 }
19 if nx_cp_stage_is_valid(4) != 0 { return 9 }
20
21 if nx_cp_v_is_valid(NX_CP_V_OK) != 1 { return 10 }
22 if nx_cp_v_is_valid(NX_CP_V_REFUSED_CLIMAX) != 1 { return 11 }
23
24 // 2: fresh soma pile
25 let p: *NxCompostPile = nx_cp_new(42, 16, 0, 0, now)
26 if p.primitive_id != 42 { return 12 }
27 if p.stage != NX_CP_STAGE_FRESH { return 13 }
28 if p.capacity != 16 { return 14 }
29 if p.n_nutrients != 0 { return 15 }
30 if p.is_germline != 0 { return 16 }
31 if p.is_climax != 0 { return 17 }
32
33 // 3: germline pile refused
34 let p_g: *NxCompostPile = nx_cp_new(99, 16, 1, 0, now)
35 if p_g.stage != NX_CP_STAGE_REFUSED { return 18 }
36 if nx_cp_begin_decompose(p_g) != NX_CP_V_REFUSED_GERMLINE { return 19 }
37 if nx_cp_extract_nutrient(p_g, NX_CP_NUT_CONST_DEFINITION, 0xCAFE, now) != NX_CP_V_REFUSED_GERMLINE { return 20 }
38
39 // 4: climax pile refused
40 let p_c: *NxCompostPile = nx_cp_new(77, 16, 0, 1, now)
41 if p_c.stage != NX_CP_STAGE_REFUSED { return 21 }
42 if nx_cp_begin_decompose(p_c) != NX_CP_V_REFUSED_CLIMAX { return 22 }
43 if nx_cp_extract_nutrient(p_c, NX_CP_NUT_CONST_DEFINITION, 0xCAFE, now) != NX_CP_V_REFUSED_CLIMAX { return 23 }
44
45 // 5: invalid construction
46 if nx_cp_new(0, 16, 0, 0, now) != (0 as *NxCompostPile) { return 24 }
47 if nx_cp_new(1, 0, 0, 0, now) != (0 as *NxCompostPile) { return 25 }
48
49 // 6: begin decompose
50 if nx_cp_begin_decompose(p) != NX_CP_V_OK { return 26 }
51 if p.stage != NX_CP_STAGE_DECOMPOSING { return 27 }
52
53 // 7: double-decompose refused
54 if nx_cp_begin_decompose(p) != NX_CP_V_INVALID { return 28 }
55
56 // 8: extract nutrients only from DECOMPOSING
57 if nx_cp_extract_nutrient(p, NX_CP_NUT_CONST_DEFINITION, 0xAAAA, now) != NX_CP_V_OK { return 29 }
58 if nx_cp_extract_nutrient(p, NX_CP_NUT_TYPE_DEFINITION, 0xBBBB, now) != NX_CP_V_OK { return 30 }
59 if nx_cp_extract_nutrient(p, NX_CP_NUT_HELPER_FUNCTION, 0xCCCC, now) != NX_CP_V_OK { return 31 }
60 if nx_cp_extract_nutrient(p, NX_CP_NUT_CONST_DEFINITION, 0xDDDD, now) != NX_CP_V_OK { return 32 }
61 if p.n_nutrients != 4 { return 33 }
62
63 // 9: invalid kind refused
64 if nx_cp_extract_nutrient(p, 99, 0xFADE, now) != NX_CP_V_INVALID { return 34 }
65
66 // 10: count by kind
67 if nx_cp_count_by_kind(p, NX_CP_NUT_CONST_DEFINITION) != 2 { return 35 }
68 if nx_cp_count_by_kind(p, NX_CP_NUT_TYPE_DEFINITION) != 1 { return 36 }
69 if nx_cp_count_by_kind(p, NX_CP_NUT_HELPER_FUNCTION) != 1 { return 37 }
70 if nx_cp_count_by_kind(p, NX_CP_NUT_SCHEMA) != 0 { return 38 }
71
72 // 11: unclaimed = 4, reused = 0
73 if nx_cp_unclaimed_count(p) != 4 { return 39 }
74 if nx_cp_reused_count(p) != 0 { return 40 }
75
76 // 12: reabsorb 2 nutrients
77 if nx_cp_reabsorb(p, 0xAAAA, 100) != NX_CP_V_OK { return 41 }
78 if nx_cp_reabsorb(p, 0xBBBB, 101) != NX_CP_V_OK { return 42 }
79 if nx_cp_reused_count(p) != 2 { return 43 }
80 if nx_cp_unclaimed_count(p) != 2 { return 44 }
81
82 // 13: reabsorb unknown hash refused
83 if nx_cp_reabsorb(p, 0xDEAD, 102) != NX_CP_V_INVALID { return 45 }
84
85 // 14: reabsorb invalid reusing_id
86 if nx_cp_reabsorb(p, 0xCCCC, 0) != NX_CP_V_INVALID { return 46 }
87
88 // 15: mark harvested
89 if nx_cp_stage(p) != NX_CP_STAGE_DECOMPOSING { return 47 }
90 if nx_cp_mark_harvested(p) != NX_CP_V_OK { return 48 }
91 if nx_cp_stage(p) != NX_CP_STAGE_HARVESTED { return 49 }
92
93 // 16: double-harvest refused
94 if nx_cp_mark_harvested(p) != NX_CP_V_INVALID { return 50 }
95
96 // 17: extract after harvested refused
97 if nx_cp_extract_nutrient(p, NX_CP_NUT_SCHEMA, 0xBEEF, now) != NX_CP_V_INVALID { return 51 }
98
99 // 18: capacity exceeded
100 let p_full: *NxCompostPile = nx_cp_new(11, 2, 0, 0, now)
101 nx_cp_begin_decompose(p_full)
102 nx_cp_extract_nutrient(p_full, NX_CP_NUT_CONST_DEFINITION, 0x1, now)
103 nx_cp_extract_nutrient(p_full, NX_CP_NUT_TYPE_DEFINITION, 0x2, now)
104 if nx_cp_extract_nutrient(p_full, NX_CP_NUT_SCHEMA, 0x3, now) != NX_CP_V_INVALID { return 52 }
105
106 // 19: null handling
107 let null_p: *NxCompostPile = (0 as i64) as *NxCompostPile
108 if nx_cp_begin_decompose(null_p) != NX_CP_V_NULL { return 53 }
109 if nx_cp_extract_nutrient(null_p, NX_CP_NUT_CONST_DEFINITION, 0, now) != NX_CP_V_NULL { return 54 }
110 if nx_cp_reabsorb(null_p, 0, 1) != NX_CP_V_NULL { return 55 }
111 if nx_cp_mark_harvested(null_p) != NX_CP_V_NULL { return 56 }
112 if nx_cp_count_by_kind(null_p, NX_CP_NUT_CONST_DEFINITION) != 0 { return 57 }
113 if nx_cp_reused_count(null_p) != 0 { return 58 }
114 if nx_cp_unclaimed_count(null_p) != 0 { return 59 }
115 if nx_cp_stage(null_p) != NX_CP_V_NULL { return 60 }
116
117 return 0
118}