From b9d77fd4024278d885d5c7ff578dc028cd19de62 Mon Sep 17 00:00:00 2001 From: meloalright Date: Tue, 12 Nov 2024 22:32:45 +0800 Subject: [PATCH] feat(flex): complete test cases form taitank_flex_test.cc --- src/lib.rs | 349 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 349 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 8d0b181..94ec394 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -188,4 +188,353 @@ mod tests { assert_eq!(100.0, get_height(&root_child1)); } + + #[test] + fn flex_basis_flex_shrink_column() { + let root = node_create(); + set_width(&root, 100.0); + set_height(&root, 100.0); + + let root_child0 = node_create(); + set_flex_shrink(&root_child0, 1.0); + set_flex_basis(&root_child0, 100.0); + insert_child(&root, &root_child0, 0); + + let root_child1 = node_create(); + set_flex_basis(&root_child1, 50.0); + insert_child(&root, &root_child1, 1); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::LTR); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(100.0, get_width(&root)); + assert_eq!(100.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(100.0, get_width(&root_child0)); + assert_eq!(50.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child1)); + assert_eq!(50.0, get_top(&root_child1)); + assert_eq!(100.0, get_width(&root_child1)); + assert_eq!(50.0, get_height(&root_child1)); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::RTL); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(100.0, get_width(&root)); + assert_eq!(100.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(100.0, get_width(&root_child0)); + assert_eq!(50.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child1)); + assert_eq!(50.0, get_top(&root_child1)); + assert_eq!(100.0, get_width(&root_child1)); + assert_eq!(50.0, get_height(&root_child1)); + } + + #[test] + fn flex_basis_flex_shrink_row() { + let root = node_create(); + set_flex_direction(&root, FlexDirection::FlexDirectionRow); + set_width(&root, 100.0); + set_height(&root, 100.0); + + let root_child0 = node_create(); + set_flex_shrink(&root_child0, 1.0); + set_flex_basis(&root_child0, 100.0); + insert_child(&root, &root_child0, 0); + + let root_child1 = node_create(); + set_flex_basis(&root_child1, 50.0); + insert_child(&root, &root_child1, 1); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::LTR); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(100.0, get_width(&root)); + assert_eq!(100.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(50.0, get_width(&root_child0)); + assert_eq!(100.0, get_height(&root_child0)); + + assert_eq!(50.0, get_left(&root_child1)); + assert_eq!(0.0, get_top(&root_child1)); + assert_eq!(50.0, get_width(&root_child1)); + assert_eq!(100.0, get_height(&root_child1)); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::RTL); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(100.0, get_width(&root)); + assert_eq!(100.0, get_height(&root)); + + assert_eq!(50.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(50.0, get_width(&root_child0)); + assert_eq!(100.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child1)); + assert_eq!(0.0, get_top(&root_child1)); + assert_eq!(50.0, get_width(&root_child1)); + assert_eq!(100.0, get_height(&root_child1)); + } + + + #[test] + fn flex_shrink_to_zero() { + let root = node_create(); + set_height(&root, 75.0); + + let root_child0 = node_create(); + set_width(&root_child0, 50.0); + set_height(&root_child0, 50.0); + insert_child(&root, &root_child0, 0); + + let root_child1 = node_create(); + set_flex_shrink(&root_child1, 1.0); + set_width(&root_child1, 50.0); + set_height(&root_child1, 50.0); + insert_child(&root, &root_child1, 1); + + let root_child2 = node_create(); + set_width(&root_child2, 50.0); + set_height(&root_child2, 50.0); + insert_child(&root, &root_child2, 2); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::LTR); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(50.0, get_width(&root)); + assert_eq!(75.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(50.0, get_width(&root_child0)); + assert_eq!(50.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child1)); + assert_eq!(50.0, get_top(&root_child1)); + assert_eq!(50.0, get_width(&root_child1)); + assert_eq!(0.0, get_height(&root_child1)); + + assert_eq!(0.0, get_left(&root_child2)); + assert_eq!(50.0, get_top(&root_child2)); + assert_eq!(50.0, get_width(&root_child2)); + assert_eq!(50.0, get_height(&root_child2)); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::RTL); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(50.0, get_width(&root)); + assert_eq!(75.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(50.0, get_width(&root_child0)); + assert_eq!(50.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child1)); + assert_eq!(50.0, get_top(&root_child1)); + assert_eq!(50.0, get_width(&root_child1)); + assert_eq!(0.0, get_height(&root_child1)); + + assert_eq!(0.0, get_left(&root_child2)); + assert_eq!(50.0, get_top(&root_child2)); + assert_eq!(50.0, get_width(&root_child2)); + assert_eq!(50.0, get_height(&root_child2)); + } + + + #[test] + fn flex_basis_overrides_main_size() { + let root = node_create(); + set_width(&root, 100.0); + set_height(&root, 100.0); + + let root_child0 = node_create(); + set_flex_grow(&root_child0, 1.0); + set_flex_basis(&root_child0, 50.0); + set_height(&root_child0, 20.0); + insert_child(&root, &root_child0, 0); + + let root_child1 = node_create(); + set_flex_grow(&root_child1, 1.0); + set_height(&root_child1, 10.0); + insert_child(&root, &root_child1, 1); + + let root_child2 = node_create(); + set_flex_grow(&root_child2, 1.0); + set_height(&root_child2, 10.0); + insert_child(&root, &root_child2, 2); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::LTR); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(100.0, get_width(&root)); + assert_eq!(100.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(100.0, get_width(&root_child0)); + assert_eq!(60.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child1)); + assert_eq!(60.0, get_top(&root_child1)); + assert_eq!(100.0, get_width(&root_child1)); + assert_eq!(20.0, get_height(&root_child1)); + + assert_eq!(0.0, get_left(&root_child2)); + assert_eq!(80.0, get_top(&root_child2)); + assert_eq!(100.0, get_width(&root_child2)); + assert_eq!(20.0, get_height(&root_child2)); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::RTL); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(100.0, get_width(&root)); + assert_eq!(100.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(100.0, get_width(&root_child0)); + assert_eq!(60.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child1)); + assert_eq!(60.0, get_top(&root_child1)); + assert_eq!(100.0, get_width(&root_child1)); + assert_eq!(20.0, get_height(&root_child1)); + + assert_eq!(0.0, get_left(&root_child2)); + assert_eq!(80.0, get_top(&root_child2)); + assert_eq!(100.0, get_width(&root_child2)); + assert_eq!(20.0, get_height(&root_child2)); + } + + #[test] + fn flex_grow_shrink_at_most() { + let root = node_create(); + set_width(&root, 100.0); + set_height(&root, 100.0); + + let root_child0 = node_create(); + insert_child(&root, &root_child0, 0); + + let root_child0_child0 = node_create(); + set_flex_grow(&root_child0_child0, 1.0); + set_flex_shrink(&root_child0_child0, 1.0); + insert_child(&root_child0, &root_child0_child0, 0); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::LTR); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(100.0, get_width(&root)); + assert_eq!(100.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(100.0, get_width(&root_child0)); + assert_eq!(0.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child0_child0)); + assert_eq!(0.0, get_top(&root_child0_child0)); + assert_eq!(100.0, get_width(&root_child0_child0)); + assert_eq!(0.0, get_height(&root_child0_child0)); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::RTL); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(100.0, get_width(&root)); + assert_eq!(100.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(100.0, get_width(&root_child0)); + assert_eq!(0.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child0_child0)); + assert_eq!(0.0, get_top(&root_child0_child0)); + assert_eq!(100.0, get_width(&root_child0_child0)); + assert_eq!(0.0, get_height(&root_child0_child0)); + } + + #[test] + fn flex_grow_less_than_factor_one() { + let root = node_create(); + set_width(&root, 200.0); + set_height(&root, 500.0); + + let root_child0 = node_create(); + set_flex_grow(&root_child0, 0.2); + set_flex_basis(&root_child0, 40.0); + insert_child(&root, &root_child0, 0); + + let root_child1 = node_create(); + set_flex_grow(&root_child1, 0.2); + insert_child(&root, &root_child1, 1); + + let root_child2 = node_create(); + set_flex_grow(&root_child2, 0.4); + insert_child(&root, &root_child2, 2); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::LTR); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(200.0, get_width(&root)); + assert_eq!(500.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(200.0, get_width(&root_child0)); + assert_eq!(132.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child1)); + assert_eq!(132.0, get_top(&root_child1)); + assert_eq!(200.0, get_width(&root_child1)); + assert_eq!(92.0, get_height(&root_child1)); + + assert_eq!(0.0, get_left(&root_child2)); + assert_eq!(224.0, get_top(&root_child2)); + assert_eq!(200.0, get_width(&root_child2)); + assert_eq!(184.0, get_height(&root_child2)); + + do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::RTL); + + assert_eq!(0.0, get_left(&root)); + assert_eq!(0.0, get_top(&root)); + assert_eq!(200.0, get_width(&root)); + assert_eq!(500.0, get_height(&root)); + + assert_eq!(0.0, get_left(&root_child0)); + assert_eq!(0.0, get_top(&root_child0)); + assert_eq!(200.0, get_width(&root_child0)); + assert_eq!(132.0, get_height(&root_child0)); + + assert_eq!(0.0, get_left(&root_child1)); + assert_eq!(132.0, get_top(&root_child1)); + assert_eq!(200.0, get_width(&root_child1)); + assert_eq!(92.0, get_height(&root_child1)); + + assert_eq!(0.0, get_left(&root_child2)); + assert_eq!(224.0, get_top(&root_child2)); + assert_eq!(200.0, get_width(&root_child2)); + assert_eq!(184.0, get_height(&root_child2)); + } } \ No newline at end of file