code wiki / (root) / nx_layout_box_test.nx

nx_layout_box_test.nx source

↩ module page · 123 lines · 5566 B

1// nx_layout_box_test.nx -- smoke for the layout-box tree primitive. 2// Builds a small box tree: 3// root (BLOCK) 4// ├── header (BLOCK) 5// │ ├── h1 (BLOCK) 6// │ └── nav (INLINE) 7// ├── main (BLOCK) 8// │ └── p (BLOCK) 9// │ └── text (TEXT) 10// └── footer (BLOCK) 11// and asserts tree structure: 7 boxes, parent/child/sibling links 12// correct, child-counts match. 13 14import "nx_syscalls.nx" 15import "nx_layout_box.nx" 16 17func main() -> i64 { 18 let max_boxes: i64 = 16 19 let boxes: *LayoutBox = (sys_mmap((max_boxes * NX_LAYOUT_BOX_BYTES) as nx_size)) as *LayoutBox 20 let tree: *LayoutTree = (sys_mmap(NX_LAYOUT_TREE_BYTES as nx_size)) as *LayoutTree 21 nx_layout_tree_init(tree, boxes, max_boxes) 22 23 // Append 7 boxes; source_node_idx = the corresponding DOM node 24 // index (synthetic for this test: 0..6). 25 let root_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, 0) 26 if root_idx != 0 { return 1 } 27 28 let header_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, 1) 29 if header_idx != 1 { return 2 } 30 31 let h1_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, 2) 32 if h1_idx != 2 { return 3 } 33 34 let nav_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_INLINE, 3) 35 if nav_idx != 3 { return 4 } 36 37 let main_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, 4) 38 if main_idx != 4 { return 5 } 39 40 let p_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, 5) 41 if p_idx != 5 { return 6 } 42 43 let text_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_TEXT, 6) 44 if text_idx != 6 { return 7 } 45 46 let footer_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, 7) 47 if footer_idx != 7 { return 8 } 48 49 if tree.count != 8 { return 9 } 50 51 // Wire tree. 52 if nx_layout_box_attach_child(tree, root_idx, header_idx) != 0 { return 10 } 53 if nx_layout_box_attach_child(tree, root_idx, main_idx) != 0 { return 11 } 54 if nx_layout_box_attach_child(tree, root_idx, footer_idx) != 0 { return 12 } 55 if nx_layout_box_attach_child(tree, header_idx, h1_idx) != 0 { return 13 } 56 if nx_layout_box_attach_child(tree, header_idx, nav_idx) != 0 { return 14 } 57 if nx_layout_box_attach_child(tree, main_idx, p_idx) != 0 { return 15 } 58 if nx_layout_box_attach_child(tree, p_idx, text_idx) != 0 { return 16 } 59 60 // Child-counts. 61 if nx_layout_box_child_count(tree, root_idx) != 3 { return 20 } 62 if nx_layout_box_child_count(tree, header_idx) != 2 { return 21 } 63 if nx_layout_box_child_count(tree, h1_idx) != 0 { return 22 } 64 if nx_layout_box_child_count(tree, nav_idx) != 0 { return 23 } 65 if nx_layout_box_child_count(tree, main_idx) != 1 { return 24 } 66 if nx_layout_box_child_count(tree, p_idx) != 1 { return 25 } 67 if nx_layout_box_child_count(tree, text_idx) != 0 { return 26 } 68 if nx_layout_box_child_count(tree, footer_idx) != 0 { return 27 } 69 70 // Parent links. 71 let header: *LayoutBox = (boxes as *u8 + (1 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox 72 if header.parent_idx != 0 { return 30 } 73 let h1: *LayoutBox = (boxes as *u8 + (2 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox 74 if h1.parent_idx != 1 { return 31 } 75 let nav: *LayoutBox = (boxes as *u8 + (3 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox 76 if nav.parent_idx != 1 { return 32 } 77 let text: *LayoutBox = (boxes as *u8 + (6 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox 78 if text.parent_idx != 5 { return 33 } 79 let footer: *LayoutBox = (boxes as *u8 + (7 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox 80 if footer.parent_idx != 0 { return 34 } 81 82 // Sibling chain: root's children are header -> main -> footer. 83 let root: *LayoutBox = boxes 84 if root.first_child_idx != 1 { return 40 } 85 let root_first: *LayoutBox = (boxes as *u8 + (1 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox 86 if root_first.next_sibling_idx != 4 { return 41 } 87 let root_second: *LayoutBox = (boxes as *u8 + (4 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox 88 if root_second.next_sibling_idx != 7 { return 42 } 89 let root_third: *LayoutBox = (boxes as *u8 + (7 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox 90 if root_third.next_sibling_idx != -1 { return 43 } 91 92 // Header's children: h1 -> nav. 93 if header.first_child_idx != 2 { return 50 } 94 if h1.next_sibling_idx != 3 { return 51 } 95 if nav.next_sibling_idx != -1 { return 52 } 96 97 // Set + check computed_decl slice. 98 if nx_layout_box_set_style(tree, h1_idx, 42, 3) != 0 { return 60 } 99 if h1.computed_decl_first != 42 { return 61 } 100 if h1.computed_decl_count != 3 { return 62 } 101 102 // Set + check text body. 103 if nx_layout_box_set_text(tree, text_idx, 100, 17) != 0 { return 70 } 104 if text.text_off != 100 { return 71 } 105 if text.text_len != 17 { return 72 } 106 107 // Kind preservation. 108 if root.kind != NX_LAYOUT_BOX_BLOCK { return 80 } 109 if nav.kind != NX_LAYOUT_BOX_INLINE { return 81 } 110 if text.kind != NX_LAYOUT_BOX_TEXT { return 82 } 111 112 // Arena overflow: try to append one box past capacity. We've used 113 // 8; arena holds 16; fill 8 more then verify -1 on the 17th. 114 var fill: i64 = 0 115 while fill < 8 { 116 if nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, -1) < 0 { return 90 } 117 fill = fill + 1 118 } 119 if tree.count != 16 { return 91 } 120 if nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, -1) != -1 { return 92 } 121 122 return 0 123}