nx_lib_search_test.nx source
↩ module page · 74 lines · 3052 B
1// nx_lib_search_test.nx -- gate for the sovereign library search.
2// Ingests REAL works, then proves case-insensitive multi-term AND search over
3// nx_lib_store returns the right work_hks (retires FTS5's function). No sqlite.
4import "nx_syscalls.nx"
5import "nx_lib_ingest.nx"
6import "nx_lib_ingest_tsv.nx" // nx_lib_ingest_buf
7import "nx_lib_search.nx"
8
9func lst_has(hay: *u8, hayn: i64, needle: *u8) -> i64 {
10 let nn: i64 = ls_strlen(needle)
11 if nn == 0 { return 1 }
12 if hayn < nn { return 0 }
13 let last: i64 = hayn - nn
14 var i: i64 = 0
15 while i <= last {
16 var j: i64 = 0
17 var st: i64 = 0
18 while st == 0 {
19 if j >= nn { st = 2 }
20 if st == 0 { if hay[i+j] != needle[j] { st = 1 } if st == 0 { j = j + 1 } }
21 }
22 if st == 2 { return 1 }
23 i = i + 1
24 }
25 return 0
26}
27
28func main() -> i64 {
29 let tsv: *u8 = "W_ATTN\tAttention Is All You Need\t10.48550/arXiv.1706.03762\t2017\tarxiv-perpetual\nW_ALPHAFOLD\tHighly accurate protein structure prediction with AlphaFold\t10.1038/s41586-021-03819-2\t2021\tcc-by\nW_CRISPR\tA Programmable Dual-RNA-Guided DNA Endonuclease\t10.1126/science.1225829\t2012\tcc-by\n" as *u8
30 nx_lib_ingest_buf(tsv, ls_strlen(tsv))
31
32 let out: *u8 = sys_mmap(262144)
33
34 // single term hits the right work only
35 let q1: *u8 = "attention" as *u8
36 let n1: i64 = nx_lib_search_query(q1, ls_strlen(q1), out)
37 if lst_has(out, n1, "\"W_ATTN\"" as *u8) != 1 { return 1 }
38 if lst_has(out, n1, "W_ALPHAFOLD" as *u8) == 1 { return 2 }
39
40 // a different term hits a different work
41 let q2: *u8 = "protein" as *u8
42 let n2: i64 = nx_lib_search_query(q2, ls_strlen(q2), out)
43 if lst_has(out, n2, "\"W_ALPHAFOLD\"" as *u8) != 1 { return 3 }
44
45 // case-insensitive
46 let q3: *u8 = "ATTENTION" as *u8
47 let n3: i64 = nx_lib_search_query(q3, ls_strlen(q3), out)
48 if lst_has(out, n3, "\"W_ATTN\"" as *u8) != 1 { return 4 }
49
50 // multi-term AND, both in the same title
51 let q4: *u8 = "protein structure" as *u8
52 let n4: i64 = nx_lib_search_query(q4, ls_strlen(q4), out)
53 if lst_has(out, n4, "\"W_ALPHAFOLD\"" as *u8) != 1 { return 5 }
54
55 // multi-term AND across different titles -> no work has both -> no hit
56 let q5: *u8 = "attention protein" as *u8
57 let n5: i64 = nx_lib_search_query(q5, ls_strlen(q5), out)
58 if lst_has(out, n5, "W_ATTN" as *u8) == 1 { return 6 }
59 if lst_has(out, n5, "W_ALPHAFOLD" as *u8) == 1 { return 7 }
60
61 // term unique to the CRISPR title
62 let q6: *u8 = "endonuclease" as *u8
63 let n6: i64 = nx_lib_search_query(q6, ls_strlen(q6), out)
64 if lst_has(out, n6, "\"W_CRISPR\"" as *u8) != 1 { return 8 }
65
66 // no-match query -> empty array "[]"
67 let q7: *u8 = "zzznomatchzzz" as *u8
68 let n7: i64 = nx_lib_search_query(q7, ls_strlen(q7), out)
69 if n7 != 2 { return 9 }
70
71 let msg: *u8 = "nx_lib_search: 9/9 sovereign search assertions PASS (ci, multi-term AND, over nx_lib_store; no sqlite/fts5)\n" as *u8
72 sys_write(1, msg, ls_strlen(msg))
73 return 0
74}