nx_css_dimension_to_px_test.nx source
↩ module page · 97 lines · 3404 B
1// nx_css_dimension_to_px_test.nx -- smoke for dimension -> px.
2// Verifies all four supported units + unknown unit error +
3// percent with unknown parent dimension.
4
5import "nx_syscalls.nx"
6import "nx_css_number_parse.nx"
7import "nx_css_dimension_to_px.nx"
8
9func _put_byte(buf: *u8, i: i64, v: i64) -> i64 {
10 buf[i] = v as u8
11 return 0
12}
13
14func main() -> i64 {
15 let src: *u8 = (sys_mmap(64)) as *u8
16
17 // "16" at offset 0 len 2
18 _put_byte(src, 0, 49)
19 _put_byte(src, 1, 54)
20
21 // "1.5" at offset 3 len 3
22 _put_byte(src, 3, 49)
23 _put_byte(src, 4, 46)
24 _put_byte(src, 5, 53)
25
26 // "100" at offset 7 len 3
27 _put_byte(src, 7, 49)
28 _put_byte(src, 8, 48)
29 _put_byte(src, 9, 48)
30
31 // "px" at offset 11 len 2
32 _put_byte(src, 11, 112)
33 _put_byte(src, 12, 120)
34
35 // "PX" at offset 14 len 2 (case-insensitive)
36 _put_byte(src, 14, 80)
37 _put_byte(src, 15, 88)
38
39 // "em" at offset 17 len 2
40 _put_byte(src, 17, 101)
41 _put_byte(src, 18, 109)
42
43 // "rem" at offset 20 len 3
44 _put_byte(src, 20, 114)
45 _put_byte(src, 21, 101)
46 _put_byte(src, 22, 109)
47
48 // "%" at offset 24 len 1
49 _put_byte(src, 24, 37)
50
51 // "vh" at offset 26 len 2 (unsupported)
52 _put_byte(src, 26, 118)
53 _put_byte(src, 27, 104)
54
55 // Resolution context: root font-size 16px, parent font-size 20px,
56 // parent dimension 800px.
57 let ctx: *CssResolveCtx = (sys_mmap(NX_CSS_RESOLVE_CTX_BYTES as nx_size)) as *CssResolveCtx
58 ctx.root_font_size_px = 16
59 ctx.parent_font_size_px = 20
60 ctx.parent_dimension_px = 800
61
62 // 16 px = 16 px.
63 if nx_css_dimension_to_px(src, 0, 2, 11, 2, ctx) != 16 { return 1 }
64 // 16 PX = 16 px (case-insensitive).
65 if nx_css_dimension_to_px(src, 0, 2, 14, 2, ctx) != 16 { return 2 }
66 // 1.5 em = 1.5 * 20 = 30 px.
67 if nx_css_dimension_to_px(src, 3, 3, 17, 2, ctx) != 30 { return 3 }
68 // 1.5 rem = 1.5 * 16 = 24 px.
69 if nx_css_dimension_to_px(src, 3, 3, 20, 3, ctx) != 24 { return 4 }
70 // 100 % of parent 800 = 800 px.
71 if nx_css_dimension_to_px(src, 7, 3, 24, 1, ctx) != 800 { return 5 }
72 // 16 em = 16 * 20 = 320 px.
73 if nx_css_dimension_to_px(src, 0, 2, 17, 2, ctx) != 320 { return 6 }
74
75 // ---- unknown unit "vh" -> error ----
76 if nx_css_dimension_to_px(src, 0, 2, 26, 2, ctx) != NX_CSS_DIMENSION_PARSE_ERROR { return 10 }
77
78 // ---- percent with parent_dimension_px = -1 (unknown) -> 0 ----
79 ctx.parent_dimension_px = -1
80 if nx_css_dimension_to_px(src, 7, 3, 24, 1, ctx) != 0 { return 11 }
81
82 // ---- bad number "1.5.x" via len trick -> error ----
83 // Reusing the "1.5" body but lengthening to include the next byte
84 // "1.5p" doesn't error on number parse since "p" is non-digit;
85 // simulate by pointing at "px" body which starts with 'p'.
86 if nx_css_dimension_to_px(src, 11, 2, 11, 2, ctx) != NX_CSS_DIMENSION_PARSE_ERROR { return 12 }
87
88 // ---- unit classifier coverage ----
89 if nx_css_unit_classify(src, 11, 2) != NX_CSS_UNIT_PX { return 20 }
90 if nx_css_unit_classify(src, 14, 2) != NX_CSS_UNIT_PX { return 21 }
91 if nx_css_unit_classify(src, 17, 2) != NX_CSS_UNIT_EM { return 22 }
92 if nx_css_unit_classify(src, 20, 3) != NX_CSS_UNIT_REM { return 23 }
93 if nx_css_unit_classify(src, 24, 1) != NX_CSS_UNIT_PERCENT { return 24 }
94 if nx_css_unit_classify(src, 26, 2) != NX_CSS_UNIT_UNKNOWN { return 25 }
95
96 return 0
97}