tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Definiciones

⎇ cf90bc5b79228137deafa45cbe75c4b523c24846 tests/caching.rs Rust · 67 líneas · 2.2 KB Historia Quién tocó qué crudo

Minor: In a few tests simplified default Style construction (#495) cf90bc5b7 · Yurii Nakonechnyi ·

1#[cfg(test)]
2mod caching {
3    use taffy::prelude::*;
4    use taffy::tree::MeasureFunc;
5
6    #[test]
7    fn measure_count_flexbox() {
8        use std::sync::atomic::{AtomicU32, Ordering};
9
10        let mut taffy = Taffy::new();
11        static NUM_MEASURES: AtomicU32 = AtomicU32::new(0);
12
13        let leaf = taffy
14            .new_leaf_with_measure(
15                Style::default(),
16                MeasureFunc::Raw(|known_dimensions, _available_space| {
17                    NUM_MEASURES.fetch_add(1, Ordering::SeqCst);
18                    Size {
19                        width: known_dimensions.width.unwrap_or(50.0),
20                        height: known_dimensions.height.unwrap_or(50.0),
21                    }
22                }),
23            )
24            .unwrap();
25
26        let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
27        for _ in 0..100 {
28            node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
29        }
30
31        taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
32
33        assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 3);
34    }
35
36    #[test]
37    #[cfg(feature = "grid")]
38    fn measure_count_grid() {
39        use std::sync::atomic::{AtomicU32, Ordering};
40
41        let style = || Style { display: Display::Grid, ..Default::default() };
42
43        let mut taffy = Taffy::new();
44        static NUM_MEASURES: AtomicU32 = AtomicU32::new(0);
45
46        let leaf = taffy
47            .new_leaf_with_measure(
48                style(),
49                MeasureFunc::Raw(|known_dimensions, _available_space| {
50                    NUM_MEASURES.fetch_add(1, Ordering::SeqCst);
51                    Size {
52                        width: known_dimensions.width.unwrap_or(50.0),
53                        height: known_dimensions.height.unwrap_or(50.0),
54                    }
55                }),
56            )
57            .unwrap();
58
59        let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
60        for _ in 0..100 {
61            node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
62        }
63
64        taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
65        assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 3);
66    }
67}