nx_decl_reflection_test.nx source
↩ module page · 73 lines · 3064 B
1// nx_decl_reflection_test.nx -- verify Elm-style reflection helpers
2// across the decl family. Each enum has a name() that returns a
3// readable string for debug / error output.
4//
5// expect_exit: 0
6//
7// license_tier: ORIGINAL
8
9import "nx_syscalls.nx"
10import "nx_types.nx"
11import "nx_tier.nx"
12import "nx_decl_random_environment.nx"
13import "nx_decl_simple_scene.nx"
14import "nx_decl_combat_arena.nx"
15
16// Byte-compare two strings up to `n` bytes. Returns 1 on equal.
17func _eq_n(a: *u8, b: *u8, n: i64) -> i64 {
18 var i: i64 = 0
19 while i < n {
20 if (a[i] as i64 & 255) != (b[i] as i64 & 255) { return 0 }
21 i = i + 1
22 }
23 return 1
24}
25
26func main() -> nx_int {
27 // Modifier names.
28 if _eq_n(nx_re_modifier_name(NX_RE_MODIFIER_REAL), "REAL" as *u8, 4) != 1 { return 1 }
29 if _eq_n(nx_re_modifier_name(NX_RE_MODIFIER_FANTASY), "FANTASY" as *u8, 7) != 1 { return 2 }
30 if _eq_n(nx_re_modifier_name(NX_RE_MODIFIER_SCI_FI), "SCI_FI" as *u8, 6) != 1 { return 3 }
31
32 // Invalid -> diagnostic placeholder.
33 if _eq_n(nx_re_modifier_name(999), "<invalid modifier>" as *u8, 18) != 1 { return 4 }
34
35 // Light names.
36 if _eq_n(nx_ss_light_name(NX_SS_LIGHT_DAWN), "DAWN" as *u8, 4) != 1 { return 10 }
37 if _eq_n(nx_ss_light_name(NX_SS_LIGHT_NOON), "NOON" as *u8, 4) != 1 { return 11 }
38 if _eq_n(nx_ss_light_name(NX_SS_LIGHT_DUSK), "DUSK" as *u8, 4) != 1 { return 12 }
39 if _eq_n(nx_ss_light_name(NX_SS_LIGHT_NIGHT), "NIGHT" as *u8, 5) != 1 { return 13 }
40 if _eq_n(nx_ss_light_name(NX_SS_LIGHT_OVERCAST), "OVERCAST" as *u8, 8) != 1 { return 14 }
41 if _eq_n(nx_ss_light_name(NX_SS_LIGHT_ALIEN), "ALIEN" as *u8, 5) != 1 { return 15 }
42 if _eq_n(nx_ss_light_name(NX_SS_LIGHT_FANTASTICAL), "FANTASTICAL" as *u8, 11) != 1 { return 16 }
43
44 // Arena shape names.
45 if _eq_n(nx_arena_shape_name(NX_ARENA_SHAPE_SQUARE), "SQUARE" as *u8, 6) != 1 { return 20 }
46 if _eq_n(nx_arena_shape_name(NX_ARENA_SHAPE_CIRCLE), "CIRCLE" as *u8, 6) != 1 { return 21 }
47 if _eq_n(nx_arena_shape_name(NX_ARENA_SHAPE_OCTAGON), "OCTAGON" as *u8, 7) != 1 { return 22 }
48 if _eq_n(nx_arena_shape_name(NX_ARENA_SHAPE_HEXAGON), "HEXAGON" as *u8, 7) != 1 { return 23 }
49
50 // Print a debug line via the reflectors -- this is the actual
51 // ergonomic win: user can drop a single sys_write line to see
52 // the scene/arena state in symbolic form.
53 let a: *NxCombatArena = nx_arena_alloc()
54 nx_decl_combat_arena(a, NX_RE_MODIFIER_FANTASY, 0xCAFE)
55 sys_write(1, "modifier=" as *u8, 9)
56 let mn: *u8 = nx_re_modifier_name(a.modifier)
57 var i: i64 = 0
58 while mn[i] != 0 { i = i + 1 }
59 sys_write(1, mn, i)
60 sys_write(1, " shape=" as *u8, 7)
61 let sn: *u8 = nx_arena_shape_name(a.shape)
62 var j: i64 = 0
63 while sn[j] != 0 { j = j + 1 }
64 sys_write(1, sn, j)
65 sys_write(1, " light=" as *u8, 7)
66 let ln: *u8 = nx_ss_light_name(a.scene.light_kind)
67 var k: i64 = 0
68 while ln[k] != 0 { k = k + 1 }
69 sys_write(1, ln, k)
70 sys_write(1, "\n" as *u8, 1)
71
72 return 0
73}