code wiki / (root) / nx_tissue_test.nx

nx_tissue_test.nx source

↩ module page · 113 lines · 4145 B

1// nx_tissue_test.nx -- smoke for nx_tissue. 2 3import "nx_syscalls.nx" 4import "nx_palette.nx" 5import "nx_tissue.nx" 6 7func main() -> i64 { 8 // 1: construction with cubic 4x4x4 at 3bpp 9 let t: *NxTissue = nx_tissue_new(4, 4, 4, NX_BPP_3) 10 if (t as i64) == 0 { return 1 } 11 if t.dim_x != 4 { return 2 } 12 if t.dim_y != 4 { return 3 } 13 if t.dim_z != 4 { return 4 } 14 if t.bpp != NX_BPP_3 { return 5 } 15 if t.n_voxels != 64 { return 6 } 16 17 // 2: storage = 64 voxels * 3 bits = 192 bits = 24 bytes 18 if nx_tissue_storage_bytes(t) != 24 { return 7 } 19 20 // 3: bounds predicate 21 if nx_tissue_in_bounds(t, 0, 0, 0) != 1 { return 8 } 22 if nx_tissue_in_bounds(t, 3, 3, 3) != 1 { return 9 } 23 if nx_tissue_in_bounds(t, 4, 0, 0) != 0 { return 10 } 24 if nx_tissue_in_bounds(t, 0, 4, 0) != 0 { return 11 } 25 if nx_tissue_in_bounds(t, 0, 0, 4) != 0 { return 12 } 26 27 // 4: initial state all zero 28 if nx_tissue_get(t, 0, 0, 0) != 0 { return 13 } 29 if nx_tissue_get(t, 3, 3, 3) != 0 { return 14 } 30 31 // 5: set + get roundtrip at corner 32 if nx_tissue_set(t, 0, 0, 0, 5) != NX_TIS_OK { return 15 } 33 if nx_tissue_get(t, 0, 0, 0) != 5 { return 16 } 34 35 // 6: set + get roundtrip at opposite corner (max index for 3bpp) 36 if nx_tissue_set(t, 3, 3, 3, 7) != NX_TIS_OK { return 17 } 37 if nx_tissue_get(t, 3, 3, 3) != 7 { return 18 } 38 39 // 7: out-of-range index rejected 40 if nx_tissue_set(t, 0, 0, 0, 8) != NX_TIS_ERR_BAD_INDEX { return 19 } 41 if nx_tissue_set(t, 0, 0, 0, -1) != NX_TIS_ERR_BAD_INDEX { return 20 } 42 // first voxel still equals 5 (unchanged by failed set) 43 if nx_tissue_get(t, 0, 0, 0) != 5 { return 21 } 44 45 // 8: out-of-bounds coord rejected 46 if nx_tissue_set(t, 99, 0, 0, 1) != NX_TIS_ERR_BAD_COORD { return 22 } 47 if nx_tissue_get(t, 99, 0, 0) != -1 { return 23 } 48 49 // 9: overwrite -- voxel 0,0,0 was 5, now write 1, then read 50 // (THIS catches bit-field OR-leak bugs in pack) 51 if nx_tissue_set(t, 0, 0, 0, 1) != NX_TIS_OK { return 24 } 52 if nx_tissue_get(t, 0, 0, 0) != 1 { return 25 } 53 54 // 10: every voxel set+get roundtrip across the full lattice 55 // (exercises every byte boundary for 3bpp) 56 var z: nx_size = 0 57 while z < 4 { 58 var y: nx_size = 0 59 while y < 4 { 60 var x: nx_size = 0 61 while x < 4 { 62 let val: nx_int = ((x + y * 4 + z * 16) as i64) & 7 63 if nx_tissue_set(t, x, y, z, val) != NX_TIS_OK { return 26 } 64 x = x + 1 65 } 66 y = y + 1 67 } 68 z = z + 1 69 } 70 // Read back 71 var z2: nx_size = 0 72 while z2 < 4 { 73 var y2: nx_size = 0 74 while y2 < 4 { 75 var x2: nx_size = 0 76 while x2 < 4 { 77 let expected: nx_int = ((x2 + y2 * 4 + z2 * 16) as i64) & 7 78 if nx_tissue_get(t, x2, y2, z2) != expected { return 27 } 79 x2 = x2 + 1 80 } 81 y2 = y2 + 1 82 } 83 z2 = z2 + 1 84 } 85 86 // 11: count_nonzero -- voxels where (x+4y+16z) & 7 == 0 are zero. 87 // 16z mod 8 = 0 always, so condition reduces to (x+4y) mod 8 = 0. 88 // In range 0..3 x 0..3 that's (0,0) and (0,2): 2 combos x 4 z = 8 89 // zero voxels. So 64 - 8 = 56 nonzero. 90 if nx_tissue_count_nonzero(t) != 56 { return 28 } 91 92 // 12: fill bulk operation 93 let t2: *NxTissue = nx_tissue_new(2, 2, 2, NX_BPP_4) 94 if nx_tissue_fill(t2, 9) != NX_TIS_OK { return 29 } 95 if nx_tissue_get(t2, 0, 0, 0) != 9 { return 30 } 96 if nx_tissue_get(t2, 1, 1, 1) != 9 { return 31 } 97 if nx_tissue_count_nonzero(t2) != 8 { return 32 } 98 99 // 13: T1 demo size check: 32x32x32 @ 3bpp = 12288 bytes (12 KiB) 100 let mc: *NxTissue = nx_tissue_new(32, 32, 32, NX_BPP_3) 101 if mc.n_voxels != 32768 { return 33 } 102 if nx_tissue_storage_bytes(mc) != 12288 { return 34 } 103 104 // 14: bad bpp rejected by constructor 105 let bad: *NxTissue = nx_tissue_new(2, 2, 2, 5) 106 if (bad as i64) != 0 { return 35 } 107 108 // 15: bad dim rejected by constructor 109 let bad2: *NxTissue = nx_tissue_new(0, 2, 2, NX_BPP_4) 110 if (bad2 as i64) != 0 { return 36 } 111 112 return 0 113}