nx_bam_file_compose_test.nx source
↩ module page · 101 lines · 3938 B
1// nx_bam_file_compose_test.nx -- end-to-end .bam stream composition.
2//
3// Validates the full BAM-on-disk loop:
4// nx_bam_write_record -> binary record bytes
5// nx_bgzf_write_stored_block -> wrapped as a BGZF block
6// nx_bgzf_eof_marker -> appended as the spec-required terminator
7// nx_gzip_inflate -> inflate the BGZF block back
8// inflated bytes equal the original binary record byte-for-byte
9//
10// This is the substrate-honesty audit smoke for G6.0d + G6.0e:
11// individual KATs already verify each primitive in isolation; this
12// one verifies the composition contract that real BAM files require.
13//
14// Composes against the canonical "r1" / 3M / ACG / qual=[37,37,37]
15// record from nx_bam_test.nx Section D so the byte-exact reference
16// payload is known (48 bytes).
17//
18// expect_exit: 0
19//
20// license_tier: ORIGINAL
21
22import "nx_syscalls.nx"
23import "nx_const.nx"
24import "nx_bam.nx"
25import "nx_bgzf.nx"
26import "nx_gzip_wrap.nx"
27
28func main() -> i64 {
29 // ---- build the canonical binary record (same as nx_bam_test §D) ----
30 let rname: *u8 = sys_mmap(8)
31 rname[0]=0x72; rname[1]=0x31 // "r1"
32 let ops: *i64 = sys_mmap(8) as *i64
33 let lens: *i64 = sys_mmap(8) as *i64
34 ops[0] = NX_CIGAR_M
35 lens[0] = 3
36 let seq: *u8 = sys_mmap(8)
37 seq[0]=0x41; seq[1]=0x43; seq[2]=0x47 // ACG
38 let qual: *u8 = sys_mmap(8)
39 qual[0]=37; qual[1]=37; qual[2]=37
40
41 let fields: *i64 = sys_mmap(8 * 12) as *i64
42 fields[NX_BAM_REC_REF_ID] = 0
43 fields[NX_BAM_REC_POS] = 99
44 fields[NX_BAM_REC_MAPQ] = 60
45 fields[NX_BAM_REC_BIN] = 4681
46 fields[NX_BAM_REC_FLAG] = 0
47 fields[NX_BAM_REC_NEXT_REF] = -1
48 fields[NX_BAM_REC_NEXT_POS] = -1
49 fields[NX_BAM_REC_TLEN] = 0
50 fields[NX_BAM_REC_NO_QUAL] = 0
51 fields[NX_BAM_REC_READ_NAME_LEN] = 2
52 fields[NX_BAM_REC_N_CIGAR_OP] = 1
53 fields[NX_BAM_REC_L_SEQ] = 3
54
55 let rec_buf: *u8 = sys_mmap(128)
56 let rec_len: i64 = nx_bam_write_record(rname, ops, lens, seq, qual,
57 fields, rec_buf, 128)
58 if rec_len != 48 { return 1 }
59
60 // ---- wrap record in a BGZF stored block ----
61 let file_buf: *u8 = sys_mmap(256)
62 let blk_len: i64 = nx_bgzf_write_stored_block(rec_buf, rec_len,
63 file_buf, 256)
64 // total = 31 + 48 = 79
65 if blk_len != 79 { return blk_len + 100 }
66
67 // ---- append EOF marker ----
68 let eof_dst: *u8 = ((file_buf as i64) + blk_len) as *u8
69 let eof_len: i64 = nx_bgzf_eof_marker(eof_dst, 256 - blk_len)
70 if eof_len != 28 { return eof_len + 200 }
71 let total_file: i64 = blk_len + eof_len // 79 + 28 = 107
72
73 // ---- validate: the assembled bytes are a real gzip stream ----
74 // Inflate the first BGZF block and confirm we recover the 48-byte
75 // binary record byte-for-byte.
76 let gr: *NxGzipResult = nx_gzip_inflate(file_buf, blk_len, 128)
77 if gr.error_code != NX_GZ_OK { return 300 + gr.error_code }
78 if gr.output_size != 48 { return gr.output_size + 400 }
79 if gr.crc_expected != gr.crc_computed { return 500 }
80
81 // Byte-exact equivalence to the source record.
82 var i: i64 = 0
83 while i < 48 {
84 let got: i64 = (gr.output_data[i] as i64) & 0xff
85 let exp: i64 = (rec_buf[i] as i64) & 0xff
86 if got != exp { return 1000 + i }
87 i = i + 1
88 }
89
90 // ---- validate: EOF marker survives unchanged at end of file ----
91 let eof_check: *u8 = ((file_buf as i64) + blk_len) as *u8
92 if (eof_check[0] & 0xff) != 0x1F { return 600 }
93 if (eof_check[1] & 0xff) != 0x8B { return 601 }
94 if (eof_check[16] & 0xff) != 0x1B { return 602 } // BSIZE = 27
95 if (eof_check[27] & 0xff) != 0x00 { return 603 } // last ISIZE byte
96
97 // ---- validate: total file size matches expectation (107 bytes) ----
98 if total_file != 107 { return 700 }
99
100 return 0
101}