Use tagged pointer to represent size/length values (#769)
1#[test]
2#[allow(non_snake_case)]
3fn grid_margins_fixed_center__border_box() {
4 #[allow(unused_imports)]
5 use taffy::{prelude::*, Layout};
6 let mut taffy = crate::new_test_tree();
7 let node0 = taffy
8 .new_leaf(taffy::style::Style {
9 align_self: Some(taffy::style::AlignSelf::Center),
10 justify_self: Some(taffy::style::JustifySelf::Center),
11 size: taffy::geometry::Size {
12 width: taffy::style::Dimension::from_length(20f32),
13 height: taffy::style::Dimension::from_length(20f32),
14 },
15 margin: taffy::geometry::Rect {
16 left: length(8f32),
17 right: length(4f32),
18 top: length(2f32),
19 bottom: length(6f32),
20 },
21 ..Default::default()
22 })
23 .unwrap();
24 let node1 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
25 let node2 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
26 let node3 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
27 let node4 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
28 let node5 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
29 let node = taffy
30 .new_with_children(
31 taffy::style::Style {
32 display: taffy::style::Display::Grid,
33 grid_template_rows: vec![length(40f32), length(40f32), length(40f32)],
34 grid_template_columns: vec![length(40f32), length(40f32), length(40f32)],
35 padding: taffy::geometry::Rect {
36 left: length(40f32),
37 right: length(20f32),
38 top: length(10f32),
39 bottom: length(30f32),
40 },
41 ..Default::default()
42 },
43 &[node0, node1, node2, node3, node4, node5],
44 )
45 .unwrap();
46 taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
47 println!("\nComputed tree:");
48 taffy.print_tree(node);
49 println!();
50 let layout = taffy.layout(node).unwrap();
51 let Layout { size, location, .. } = layout;
52 assert_eq!(size.width, 180f32, "width of node {:?}. Expected {}. Actual {}", node, 180f32, size.width);
53 assert_eq!(size.height, 160f32, "height of node {:?}. Expected {}. Actual {}", node, 160f32, size.height);
54 assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
55 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
56 let layout = taffy.layout(node0).unwrap();
57 let Layout { size, location, .. } = layout;
58 assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node0, 20f32, size.width);
59 assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node0, 20f32, size.height);
60 assert_eq!(location.x, 52f32, "x of node {:?}. Expected {}. Actual {}", node0, 52f32, location.x);
61 assert_eq!(location.y, 18f32, "y of node {:?}. Expected {}. Actual {}", node0, 18f32, location.y);
62 let layout = taffy.layout(node1).unwrap();
63 let Layout { size, location, .. } = layout;
64 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node1, 40f32, size.width);
65 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node1, 40f32, size.height);
66 assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node1, 80f32, location.x);
67 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node1, 10f32, location.y);
68 let layout = taffy.layout(node2).unwrap();
69 let Layout { size, location, .. } = layout;
70 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node2, 40f32, size.width);
71 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node2, 40f32, size.height);
72 assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node2, 120f32, location.x);
73 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node2, 10f32, location.y);
74 let layout = taffy.layout(node3).unwrap();
75 let Layout { size, location, .. } = layout;
76 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node3, 40f32, size.width);
77 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node3, 40f32, size.height);
78 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node3, 40f32, location.x);
79 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node3, 50f32, location.y);
80 let layout = taffy.layout(node4).unwrap();
81 let Layout { size, location, .. } = layout;
82 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node4, 40f32, size.width);
83 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node4, 40f32, size.height);
84 assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node4, 80f32, location.x);
85 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node4, 50f32, location.y);
86 let layout = taffy.layout(node5).unwrap();
87 let Layout { size, location, .. } = layout;
88 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node5, 40f32, size.width);
89 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node5, 40f32, size.height);
90 assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node5, 120f32, location.x);
91 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node5, 50f32, location.y);
92}
93
94#[test]
95#[allow(non_snake_case)]
96fn grid_margins_fixed_center__content_box() {
97 #[allow(unused_imports)]
98 use taffy::{prelude::*, Layout};
99 let mut taffy = crate::new_test_tree();
100 let node0 = taffy
101 .new_leaf(taffy::style::Style {
102 box_sizing: taffy::style::BoxSizing::ContentBox,
103 align_self: Some(taffy::style::AlignSelf::Center),
104 justify_self: Some(taffy::style::JustifySelf::Center),
105 size: taffy::geometry::Size {
106 width: taffy::style::Dimension::from_length(20f32),
107 height: taffy::style::Dimension::from_length(20f32),
108 },
109 margin: taffy::geometry::Rect {
110 left: length(8f32),
111 right: length(4f32),
112 top: length(2f32),
113 bottom: length(6f32),
114 },
115 ..Default::default()
116 })
117 .unwrap();
118 let node1 = taffy
119 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
120 .unwrap();
121 let node2 = taffy
122 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
123 .unwrap();
124 let node3 = taffy
125 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
126 .unwrap();
127 let node4 = taffy
128 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
129 .unwrap();
130 let node5 = taffy
131 .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() })
132 .unwrap();
133 let node = taffy
134 .new_with_children(
135 taffy::style::Style {
136 display: taffy::style::Display::Grid,
137 box_sizing: taffy::style::BoxSizing::ContentBox,
138 grid_template_rows: vec![length(40f32), length(40f32), length(40f32)],
139 grid_template_columns: vec![length(40f32), length(40f32), length(40f32)],
140 padding: taffy::geometry::Rect {
141 left: length(40f32),
142 right: length(20f32),
143 top: length(10f32),
144 bottom: length(30f32),
145 },
146 ..Default::default()
147 },
148 &[node0, node1, node2, node3, node4, node5],
149 )
150 .unwrap();
151 taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap();
152 println!("\nComputed tree:");
153 taffy.print_tree(node);
154 println!();
155 let layout = taffy.layout(node).unwrap();
156 let Layout { size, location, .. } = layout;
157 assert_eq!(size.width, 180f32, "width of node {:?}. Expected {}. Actual {}", node, 180f32, size.width);
158 assert_eq!(size.height, 160f32, "height of node {:?}. Expected {}. Actual {}", node, 160f32, size.height);
159 assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
160 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
161 let layout = taffy.layout(node0).unwrap();
162 let Layout { size, location, .. } = layout;
163 assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node0, 20f32, size.width);
164 assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node0, 20f32, size.height);
165 assert_eq!(location.x, 52f32, "x of node {:?}. Expected {}. Actual {}", node0, 52f32, location.x);
166 assert_eq!(location.y, 18f32, "y of node {:?}. Expected {}. Actual {}", node0, 18f32, location.y);
167 let layout = taffy.layout(node1).unwrap();
168 let Layout { size, location, .. } = layout;
169 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node1, 40f32, size.width);
170 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node1, 40f32, size.height);
171 assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node1, 80f32, location.x);
172 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node1, 10f32, location.y);
173 let layout = taffy.layout(node2).unwrap();
174 let Layout { size, location, .. } = layout;
175 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node2, 40f32, size.width);
176 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node2, 40f32, size.height);
177 assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node2, 120f32, location.x);
178 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node2, 10f32, location.y);
179 let layout = taffy.layout(node3).unwrap();
180 let Layout { size, location, .. } = layout;
181 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node3, 40f32, size.width);
182 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node3, 40f32, size.height);
183 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node3, 40f32, location.x);
184 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node3, 50f32, location.y);
185 let layout = taffy.layout(node4).unwrap();
186 let Layout { size, location, .. } = layout;
187 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node4, 40f32, size.width);
188 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node4, 40f32, size.height);
189 assert_eq!(location.x, 80f32, "x of node {:?}. Expected {}. Actual {}", node4, 80f32, location.x);
190 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node4, 50f32, location.y);
191 let layout = taffy.layout(node5).unwrap();
192 let Layout { size, location, .. } = layout;
193 assert_eq!(size.width, 40f32, "width of node {:?}. Expected {}. Actual {}", node5, 40f32, size.width);
194 assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node5, 40f32, size.height);
195 assert_eq!(location.x, 120f32, "x of node {:?}. Expected {}. Actual {}", node5, 120f32, location.x);
196 assert_eq!(location.y, 50f32, "y of node {:?}. Expected {}. Actual {}", node5, 50f32, location.y);
197}