code wiki / (root) / nx_chromatin_test.nx

nx_chromatin_test.nx source

↩ module page · 85 lines · 3009 B

1// nx_chromatin_test.nx -- smoke for nx_chromatin. 2 3import "nx_syscalls.nx" 4import "nx_methyl.nx" 5import "nx_chromatin.nx" 6 7func main() -> i64 { 8 let sig: *u8 = (sys_mmap(96)) as *u8 9 sig[0] = 1 as u8 10 let snap: *u8 = (sys_mmap(64)) as *u8 11 var i: nx_size = 0 12 while i < 32 { 13 snap[i] = (65 + i) as u8 14 i = i + 1 15 } 16 let mark: *NxMethylMark = nx_methyl_new(7, 0xcafe, 1000, sig, 96) 17 18 // 1: capture 19 let c: *NxChromatin = nx_chromatin_capture(7, 0xfeed, 1000, 20 snap, 32, mark, 1) 21 if (c as i64) == 0 { return 1 } 22 if c.originator_id != 7 { return 2 } 23 if c.snapshot_len != 32 { return 3 } 24 if c.capture_seq != 1 { return 4 } 25 26 // 2: size + originator getters 27 if nx_chromatin_size(c) != 32 { return 5 } 28 if nx_chromatin_originator(c) != 7 { return 6 } 29 30 // 3: NULL inputs rejected 31 let null_snap: *u8 = (0 as i64) as *u8 32 if (nx_chromatin_capture(7, 0, 0, null_snap, 32, mark, 1) as i64) != 0 { return 7 } 33 if (nx_chromatin_capture(7, 0, 0, snap, 0, mark, 1) as i64) != 0 { return 8 } 34 let null_mark: *NxMethylMark = (0 as i64) as *NxMethylMark 35 if (nx_chromatin_capture(7, 0, 0, snap, 32, null_mark, 1) as i64) != 0 { return 9 } 36 37 // 4: restore -- byte-equal recovery 38 let dst: *u8 = (sys_mmap(64)) as *u8 39 let rc: nx_int = nx_chromatin_restore(c, dst, 64, 2000, 5000, 7) 40 if rc != NX_CH_OK { return 10 } 41 var j: nx_size = 0 42 while j < 32 { 43 if dst[j] != snap[j] { return 11 } 44 j = j + 1 45 } 46 47 // 5: restore refused when dst too small 48 let small_dst: *u8 = (sys_mmap(8)) as *u8 49 if nx_chromatin_restore(c, small_dst, 8, 2000, 5000, 7) != NX_CH_ERR_DST_TOO_SMALL { return 12 } 50 51 // 6: restore refused on bad originator (mark check) 52 if nx_chromatin_restore(c, dst, 64, 2000, 5000, 999) != NX_CH_ERR_BAD_MARK { return 13 } 53 54 // 7: restore refused when mark is stale 55 if nx_chromatin_restore(c, dst, 64, 99999, 5000, 7) != NX_CH_ERR_BAD_MARK { return 14 } 56 57 // 8: verify_against_buf -- positive (same bytes as captured) 58 let same_buf: *u8 = (sys_mmap(64)) as *u8 59 var p: nx_size = 0 60 while p < 32 { 61 same_buf[p] = (65 + p) as u8 62 p = p + 1 63 } 64 if nx_chromatin_verify_against_buf(c, same_buf, 32) != 1 { return 15 } 65 66 // 9: verify_against_buf -- negative on different buffer 67 let diff_buf: *u8 = (sys_mmap(64)) as *u8 68 var q: nx_size = 0 69 while q < 32 { 70 diff_buf[q] = (90 + q) as u8 71 q = q + 1 72 } 73 if nx_chromatin_verify_against_buf(c, diff_buf, 32) != 0 { return 16 } 74 75 // 10: is_fresher comparator 76 let c2: *NxChromatin = nx_chromatin_capture(7, 0xbeef, 3000, 77 snap, 32, mark, 5) 78 if nx_chromatin_is_fresher(c2, c) != 1 { return 17 } 79 if nx_chromatin_is_fresher(c, c2) != 0 { return 18 } 80 let null_c: *NxChromatin = (0 as i64) as *NxChromatin 81 if nx_chromatin_is_fresher(c, null_c) != 1 { return 19 } 82 if nx_chromatin_is_fresher(null_c, c) != 0 { return 20 } 83 84 return 0 85}