code wiki / _hdl_build / nx_html_extract_data_test.nx

nx_html_extract_data_test.nx source

↩ module page · 65 lines · 3251 B

1// nx_html_extract_data_test.nx -- KAT: pull JSON-LD prose from HTML; then a REAL 2// run on /tmp/cadhub_raw.html to show what rung-1 recovers (honest). expect_exit: 0. 3 4import "nx_syscalls.nx" 5import "nx_research_extract.nx" 6import "nx_html_extract_data.nx" 7 8func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 9func wn(v: i64) -> i64 { 10 let b: *u8 = sys_mmap(28); var m: i64 = v 11 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } 12 let t: *u8 = sys_mmap(28); var k: i64 = 0 13 if m == 0 { t[0] = 48 as u8; k = 1 } 14 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 15 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 16 sys_write(1, b, k); return 0 17} 18func contains(hay: *u8, hn: i64, ndl: *u8) -> i64 { 19 var m: i64 = 0; while ndl[m] != (0 as u8) { m = m + 1 } 20 var i: i64 = 0 21 while i + m <= hn { var j: i64 = 0; var ok: i64 = 1; while j < m { if hay[i + j] != ndl[j] { ok = 0; j = m } else { j = j + 1 } } if ok == 1 { return 1 } i = i + 1 } 22 return 0 23} 24func chk(cond: i64, pass: *i64, fail: *i64, label: *u8) -> i64 { 25 if cond == 1 { pass[0] = pass[0] + 1; w(" ok " as *u8); w(label); w("\n" as *u8) } 26 else { fail[0] = fail[0] + 1; w(" XX " as *u8); w(label); w("\n" as *u8) } 27 return 0 28} 29 30func main() -> i64 { 31 let pass: *i64 = (sys_mmap(8)) as *i64 32 let fail: *i64 = (sys_mmap(8)) as *i64 33 pass[0] = 0 34 fail[0] = 0 35 w("=== nx_html_extract_data KAT (JSON-LD prose from HTML) ===\n" as *u8) 36 37 let html: *u8 = "<html><head><script type=\"application/ld+json\">{\"headline\":\"Best 3D Printing Software\",\"description\":\"covers Cura and PrusaSlicer\",\"articleBody\":\"Blender is great for modeling\"}</script></head></html>" as *u8 38 let hn: i64 = re_strlen(html) 39 let out: *u8 = sys_mmap(16384) 40 let len: i64 = nx_html_extract_data(html, hn, out, 16384) 41 w("--- recovered ---\n" as *u8); w(out); w("--- end ---\n" as *u8) 42 let on: i64 = re_strlen(out) 43 chk(contains(out, on, "Best 3D Printing Software" as *u8) == 1, pass, fail, "headline recovered" as *u8) 44 chk(contains(out, on, "Cura and PrusaSlicer" as *u8) == 1, pass, fail, "description recovered" as *u8) 45 chk(contains(out, on, "Blender is great" as *u8) == 1, pass, fail, "articleBody recovered (JS-only content)" as *u8) 46 47 // real run on the CADHub page we fetched raw 48 w("=== REAL: nx_html_extract_data on /tmp/cadhub_raw.html ===\n" as *u8) 49 let rl: *i64 = (sys_mmap(8)) as *i64 50 let raw: *u8 = sys_read_file("/tmp/cadhub_raw.html" as *u8, rl) 51 if rl[0] > 0 { 52 let rout: *u8 = sys_mmap(131072) 53 let rn: i64 = nx_html_extract_data(raw, rl[0], rout, 131072) 54 w("recovered bytes=" as *u8); wn(rn); w(" (vs 71 from static strip)\n" as *u8) 55 var show: i64 = rn; if show > 700 { show = 700 } 56 sys_write(1, rout, show); w("\n" as *u8) 57 chk(rn > 71, pass, fail, "recovered MORE than the 71-byte static strip" as *u8) 58 } else { 59 w("(no /tmp/cadhub_raw.html -- run nx_page_fetch_raw first)\n" as *u8) 60 } 61 62 w("=== VERDICT pass=" as *u8); wn(pass[0]); w(" fail=" as *u8); wn(fail[0]); w(" ===\n" as *u8) 63 if fail[0] == 0 { return 0 } 64 return 1 65}