code wiki / (root) / nx_urf_test.nx

nx_urf_test.nx source

↩ module page · 129 lines · 6789 B

1// nx_urf_test.nx -- gate for the sovereign URF/PWG raster encoder (R-RASTER-0). 2// Unique exit codes per invariant: 3// 1x decode the EXACT CUPS-spec worked example line -> documented pixel sequence (decoder == spec) 4// 2x encode a known 6x3 gray bitmap -> EXACT expected RLE bytes (byte-level KAT), then decode -> bit-exact 5// 3x URF file header ("UNIRAST\0" + page count BE) byte-exact 6// 4x URF 32-byte page header byte-exact (bpp/cs/duplex/quality + width/height/dpi BE) 7// 5x full nx_urf_encode_gray_page: size = 12+32+rle, magic 'U', embedded RLE round-trips 8// 6x liar-kill: truncated RLE stream -> decode returns -1 (no_silent_failure) 9// expect_exit: 0 license_tier: ORIGINAL 10 11import "nx_syscalls.nx" 12import "nx_pwg_rle.nx" 13import "nx_urf.nx" 14 15func t_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 16 17func main() -> i64 { 18 // ===== KAT 1: decode the spec worked example (RGB, width 8, height 1) ===== 19 // bytes: 00 FE FFFF00 0000FF FFFF00 02 FFFFFF 00 00FF00 00 FFFFFF 20 let s1: *u8 = sys_mmap(64) 21 var q: i64 = 0 22 s1[q]=0x00 as u8; q=q+1 // line-repeat = 1 line 23 s1[q]=0xFE as u8; q=q+1 // literal 257-254 = 3 pixels 24 s1[q]=0xFF as u8;q=q+1; s1[q]=0xFF as u8;q=q+1; s1[q]=0x00 as u8;q=q+1 // yellow 25 s1[q]=0x00 as u8;q=q+1; s1[q]=0x00 as u8;q=q+1; s1[q]=0xFF as u8;q=q+1 // blue 26 s1[q]=0xFF as u8;q=q+1; s1[q]=0xFF as u8;q=q+1; s1[q]=0x00 as u8;q=q+1 // yellow 27 s1[q]=0x02 as u8; q=q+1 // repeat 3x 28 s1[q]=0xFF as u8;q=q+1; s1[q]=0xFF as u8;q=q+1; s1[q]=0xFF as u8;q=q+1 // white 29 s1[q]=0x00 as u8; q=q+1 // repeat 1x 30 s1[q]=0x00 as u8;q=q+1; s1[q]=0xFF as u8;q=q+1; s1[q]=0x00 as u8;q=q+1 // green 31 s1[q]=0x00 as u8; q=q+1 // repeat 1x 32 s1[q]=0xFF as u8;q=q+1; s1[q]=0xFF as u8;q=q+1; s1[q]=0xFF as u8;q=q+1 // white 33 34 let pix: *u8 = sys_mmap(64) 35 let dn: i64 = nx_rle_decode(s1, q, 8, 1, 3, pix, 8) 36 if dn != 8 { return 1 } 37 // expected 8 RGB pixels 38 let exp: *u8 = sys_mmap(64) 39 exp[0]=0xFF as u8;exp[1]=0xFF as u8;exp[2]=0x00 as u8 // yellow 40 exp[3]=0x00 as u8;exp[4]=0x00 as u8;exp[5]=0xFF as u8 // blue 41 exp[6]=0xFF as u8;exp[7]=0xFF as u8;exp[8]=0x00 as u8 // yellow 42 exp[9]=0xFF as u8;exp[10]=0xFF as u8;exp[11]=0xFF as u8 // white 43 exp[12]=0xFF as u8;exp[13]=0xFF as u8;exp[14]=0xFF as u8 // white 44 exp[15]=0xFF as u8;exp[16]=0xFF as u8;exp[17]=0xFF as u8 // white 45 exp[18]=0x00 as u8;exp[19]=0xFF as u8;exp[20]=0x00 as u8 // green 46 exp[21]=0xFF as u8;exp[22]=0xFF as u8;exp[23]=0xFF as u8 // white 47 var i: i64 = 0 48 while i < 24 { if (pix[i] as i64) != (exp[i] as i64) { return 2 } i = i + 1 } 49 50 // ===== KAT 2: encode a 6x3 gray bitmap -> exact RLE bytes, then decode round-trip ===== 51 // line0=line1 = [10 10 10 20 30 30], line2 = [40 40 40 40 40 40] 52 let bm: *u8 = sys_mmap(64) 53 let row: *u8 = sys_mmap(8) 54 row[0]=10 as u8;row[1]=10 as u8;row[2]=10 as u8;row[3]=20 as u8;row[4]=30 as u8;row[5]=30 as u8 55 var b: i64 = 0 56 while b < 6 { bm[b] = row[b]; b = b + 1 } // line0 57 while b < 12 { bm[b] = row[b-6]; b = b + 1 } // line1 (identical) 58 b = 12 59 while b < 18 { bm[b] = 40 as u8; b = b + 1 } // line2 60 61 let enc: *u8 = sys_mmap(64) 62 let elen: i64 = nx_rle_encode_page(bm, 6, 3, 1, enc) 63 // expected: 01 02 10 00 20 01 30 00 05 40 64 let eexp: *u8 = sys_mmap(16) 65 eexp[0]=0x01 as u8;eexp[1]=0x02 as u8;eexp[2]=10 as u8;eexp[3]=0x00 as u8;eexp[4]=20 as u8 66 eexp[5]=0x01 as u8;eexp[6]=30 as u8;eexp[7]=0x00 as u8;eexp[8]=0x05 as u8;eexp[9]=40 as u8 67 if elen != 10 { return 20 } 68 var j: i64 = 0 69 while j < 10 { if (enc[j] as i64) != (eexp[j] as i64) { return 21 } j = j + 1 } 70 // decode back -> 18 px bit-exact 71 let dec: *u8 = sys_mmap(64) 72 let dl: i64 = nx_rle_decode(enc, elen, 6, 3, 1, dec, 18) 73 if dl != 18 { return 22 } 74 var k: i64 = 0 75 while k < 18 { if (dec[k] as i64) != (bm[k] as i64) { return 23 } k = k + 1 } 76 77 // ===== KAT 3: URF file header ===== 78 let fh: *u8 = sys_mmap(32) 79 if nx_urf_file_header(fh, 3) != 12 { return 30 } 80 if (fh[0] as i64)!=0x55 { return 31 } 81 if (fh[1] as i64)!=0x4E { return 32 } 82 if (fh[2] as i64)!=0x49 { return 33 } 83 if (fh[3] as i64)!=0x52 { return 34 } 84 if (fh[4] as i64)!=0x41 { return 35 } 85 if (fh[5] as i64)!=0x53 { return 36 } 86 if (fh[6] as i64)!=0x54 { return 37 } 87 if (fh[7] as i64)!=0x00 { return 38 } 88 if (fh[8] as i64)!=0 { return 39 } 89 if (fh[11] as i64)!=3 { return 40 } 90 91 // ===== KAT 4: URF page header (8bpp gray, long-edge duplex, normal, 2550x3300, 300dpi) ===== 92 let ph: *u8 = sys_mmap(64) 93 if nx_urf_page_header(ph, 0, 8, NX_URF_CS_SGRAY, NX_URF_DUP_LONG, NX_URF_Q_NORMAL, 2550, 3300, 300) != 32 { return 41 } 94 if (ph[0] as i64)!=8 { return 42 } // bpp 95 if (ph[1] as i64)!=0 { return 43 } // sGray 96 if (ph[2] as i64)!=2 { return 44 } // duplex long-edge 97 if (ph[3] as i64)!=4 { return 45 } // quality normal 98 // width 2550 = 0x000009F6 99 if (ph[12] as i64)!=0x00 { return 46 } 100 if (ph[14] as i64)!=0x09 { return 47 } 101 if (ph[15] as i64)!=0xF6 { return 48 } 102 // height 3300 = 0x00000CE4 103 if (ph[18] as i64)!=0x0C { return 49 } 104 if (ph[19] as i64)!=0xE4 { return 50 } 105 // dpi 300 = 0x0000012C 106 if (ph[22] as i64)!=0x01 { return 51 } 107 if (ph[23] as i64)!=0x2C { return 52 } 108 if (ph[31] as i64)!=0 { return 53 } // trailing reserved 109 110 // ===== KAT 5: full single-page URF doc ===== 111 let doc: *u8 = sys_mmap(256) 112 let dlen: i64 = nx_urf_encode_gray_page(doc, bm, 6, 3, 300, NX_URF_DUP_LONG, NX_URF_Q_NORMAL) 113 if dlen != (12 + 32 + 10) { return 60 } 114 if (doc[0] as i64)!=0x55 { return 61 } // 'U' 115 // embedded RLE starts at offset 44 -> round-trips to the bitmap 116 let dec2: *u8 = sys_mmap(64) 117 let dl2: i64 = nx_rle_decode(((doc as i64)+44) as *u8, dlen-44, 6, 3, 1, dec2, 18) 118 if dl2 != 18 { return 62 } 119 var m: i64 = 0 120 while m < 18 { if (dec2[m] as i64) != (bm[m] as i64) { return 63 } m = m + 1 } 121 122 // ===== KAT 6: liar-kill -- truncated RLE ===== 123 let bad: *u8 = sys_mmap(8) 124 bad[0]=0x00 as u8; bad[1]=0x02 as u8; bad[2]=10 as u8 // claims width 6 but stream ends after 3 px 125 if nx_rle_decode(bad, 3, 6, 1, 1, pix, 8) != (0 - 1) { return 70 } 126 127 t_puts("nx_urf: 6/6 KAT groups PASS (spec-example decode + encode byte-exact + round-trip + URF headers + full-doc + liar-kill)\n") 128 return 0 129}