/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ // @generated by gentest/gentest.rb from gentest/fixtures/YGFlexTest.html extern crate ordered_float; extern crate yoga; use yoga::*; #[test] fn test_flex_basis_flex_grow_column() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_width(StyleUnit::Point(100_f32.into())); root.set_height(StyleUnit::Point(100_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root_child0.set_flex_grow(1_f32); root_child0.set_flex_basis(StyleUnit::Point(50_f32.into())); root.insert_child(&mut root_child0, 0); let mut root_child1 = Node::new_with_config(&mut config); root_child1.set_flex_grow(1_f32); root.insert_child(&mut root_child1, 1); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(100_f32, root_child0.get_layout_width()); assert_eq!(75_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(75_f32, root_child1.get_layout_top()); assert_eq!(100_f32, root_child1.get_layout_width()); assert_eq!(25_f32, root_child1.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(100_f32, root_child0.get_layout_width()); assert_eq!(75_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(75_f32, root_child1.get_layout_top()); assert_eq!(100_f32, root_child1.get_layout_width()); assert_eq!(25_f32, root_child1.get_layout_height()); } #[test] fn test_flex_shrink_flex_grow_row() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_flex_direction(FlexDirection::Row); root.set_width(StyleUnit::Point(500_f32.into())); root.set_height(StyleUnit::Point(500_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root_child0.set_flex_shrink(1_f32); root_child0.set_width(StyleUnit::Point(500_f32.into())); root_child0.set_height(StyleUnit::Point(100_f32.into())); root.insert_child(&mut root_child0, 0); let mut root_child1 = Node::new_with_config(&mut config); root_child1.set_flex_shrink(1_f32); root_child1.set_width(StyleUnit::Point(500_f32.into())); root_child1.set_height(StyleUnit::Point(100_f32.into())); root.insert_child(&mut root_child1, 1); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(500_f32, root.get_layout_width()); assert_eq!(500_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(250_f32, root_child0.get_layout_width()); assert_eq!(100_f32, root_child0.get_layout_height()); assert_eq!(250_f32, root_child1.get_layout_left()); assert_eq!(0_f32, root_child1.get_layout_top()); assert_eq!(250_f32, root_child1.get_layout_width()); assert_eq!(100_f32, root_child1.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(500_f32, root.get_layout_width()); assert_eq!(500_f32, root.get_layout_height()); assert_eq!(250_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(250_f32, root_child0.get_layout_width()); assert_eq!(100_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(0_f32, root_child1.get_layout_top()); assert_eq!(250_f32, root_child1.get_layout_width()); assert_eq!(100_f32, root_child1.get_layout_height()); } #[test] fn test_flex_shrink_flex_grow_child_flex_shrink_other_child() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_flex_direction(FlexDirection::Row); root.set_width(StyleUnit::Point(500_f32.into())); root.set_height(StyleUnit::Point(500_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root_child0.set_flex_shrink(1_f32); root_child0.set_width(StyleUnit::Point(500_f32.into())); root_child0.set_height(StyleUnit::Point(100_f32.into())); root.insert_child(&mut root_child0, 0); let mut root_child1 = Node::new_with_config(&mut config); root_child1.set_flex_grow(1_f32); root_child1.set_flex_shrink(1_f32); root_child1.set_width(StyleUnit::Point(500_f32.into())); root_child1.set_height(StyleUnit::Point(100_f32.into())); root.insert_child(&mut root_child1, 1); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(500_f32, root.get_layout_width()); assert_eq!(500_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(250_f32, root_child0.get_layout_width()); assert_eq!(100_f32, root_child0.get_layout_height()); assert_eq!(250_f32, root_child1.get_layout_left()); assert_eq!(0_f32, root_child1.get_layout_top()); assert_eq!(250_f32, root_child1.get_layout_width()); assert_eq!(100_f32, root_child1.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(500_f32, root.get_layout_width()); assert_eq!(500_f32, root.get_layout_height()); assert_eq!(250_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(250_f32, root_child0.get_layout_width()); assert_eq!(100_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(0_f32, root_child1.get_layout_top()); assert_eq!(250_f32, root_child1.get_layout_width()); assert_eq!(100_f32, root_child1.get_layout_height()); } #[test] fn test_flex_basis_flex_grow_row() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_flex_direction(FlexDirection::Row); root.set_width(StyleUnit::Point(100_f32.into())); root.set_height(StyleUnit::Point(100_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root_child0.set_flex_grow(1_f32); root_child0.set_flex_basis(StyleUnit::Point(50_f32.into())); root.insert_child(&mut root_child0, 0); let mut root_child1 = Node::new_with_config(&mut config); root_child1.set_flex_grow(1_f32); root.insert_child(&mut root_child1, 1); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(75_f32, root_child0.get_layout_width()); assert_eq!(100_f32, root_child0.get_layout_height()); assert_eq!(75_f32, root_child1.get_layout_left()); assert_eq!(0_f32, root_child1.get_layout_top()); assert_eq!(25_f32, root_child1.get_layout_width()); assert_eq!(100_f32, root_child1.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(25_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(75_f32, root_child0.get_layout_width()); assert_eq!(100_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(0_f32, root_child1.get_layout_top()); assert_eq!(25_f32, root_child1.get_layout_width()); assert_eq!(100_f32, root_child1.get_layout_height()); } #[test] fn test_flex_basis_flex_shrink_column() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_width(StyleUnit::Point(100_f32.into())); root.set_height(StyleUnit::Point(100_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root_child0.set_flex_shrink(1_f32); root_child0.set_flex_basis(StyleUnit::Point(100_f32.into())); root.insert_child(&mut root_child0, 0); let mut root_child1 = Node::new_with_config(&mut config); root_child1.set_flex_basis(StyleUnit::Point(50_f32.into())); root.insert_child(&mut root_child1, 1); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(100_f32, root_child0.get_layout_width()); assert_eq!(50_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(50_f32, root_child1.get_layout_top()); assert_eq!(100_f32, root_child1.get_layout_width()); assert_eq!(50_f32, root_child1.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(100_f32, root_child0.get_layout_width()); assert_eq!(50_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(50_f32, root_child1.get_layout_top()); assert_eq!(100_f32, root_child1.get_layout_width()); assert_eq!(50_f32, root_child1.get_layout_height()); } #[test] fn test_flex_basis_flex_shrink_row() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_flex_direction(FlexDirection::Row); root.set_width(StyleUnit::Point(100_f32.into())); root.set_height(StyleUnit::Point(100_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root_child0.set_flex_shrink(1_f32); root_child0.set_flex_basis(StyleUnit::Point(100_f32.into())); root.insert_child(&mut root_child0, 0); let mut root_child1 = Node::new_with_config(&mut config); root_child1.set_flex_basis(StyleUnit::Point(50_f32.into())); root.insert_child(&mut root_child1, 1); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(50_f32, root_child0.get_layout_width()); assert_eq!(100_f32, root_child0.get_layout_height()); assert_eq!(50_f32, root_child1.get_layout_left()); assert_eq!(0_f32, root_child1.get_layout_top()); assert_eq!(50_f32, root_child1.get_layout_width()); assert_eq!(100_f32, root_child1.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(50_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(50_f32, root_child0.get_layout_width()); assert_eq!(100_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(0_f32, root_child1.get_layout_top()); assert_eq!(50_f32, root_child1.get_layout_width()); assert_eq!(100_f32, root_child1.get_layout_height()); } #[test] fn test_flex_shrink_to_zero() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_height(StyleUnit::Point(75_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root_child0.set_width(StyleUnit::Point(50_f32.into())); root_child0.set_height(StyleUnit::Point(50_f32.into())); root.insert_child(&mut root_child0, 0); let mut root_child1 = Node::new_with_config(&mut config); root_child1.set_flex_shrink(1_f32); root_child1.set_width(StyleUnit::Point(50_f32.into())); root_child1.set_height(StyleUnit::Point(50_f32.into())); root.insert_child(&mut root_child1, 1); let mut root_child2 = Node::new_with_config(&mut config); root_child2.set_width(StyleUnit::Point(50_f32.into())); root_child2.set_height(StyleUnit::Point(50_f32.into())); root.insert_child(&mut root_child2, 2); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(50_f32, root.get_layout_width()); assert_eq!(75_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(50_f32, root_child0.get_layout_width()); assert_eq!(50_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(50_f32, root_child1.get_layout_top()); assert_eq!(50_f32, root_child1.get_layout_width()); assert_eq!(0_f32, root_child1.get_layout_height()); assert_eq!(0_f32, root_child2.get_layout_left()); assert_eq!(50_f32, root_child2.get_layout_top()); assert_eq!(50_f32, root_child2.get_layout_width()); assert_eq!(50_f32, root_child2.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(50_f32, root.get_layout_width()); assert_eq!(75_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(50_f32, root_child0.get_layout_width()); assert_eq!(50_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(50_f32, root_child1.get_layout_top()); assert_eq!(50_f32, root_child1.get_layout_width()); assert_eq!(0_f32, root_child1.get_layout_height()); assert_eq!(0_f32, root_child2.get_layout_left()); assert_eq!(50_f32, root_child2.get_layout_top()); assert_eq!(50_f32, root_child2.get_layout_width()); assert_eq!(50_f32, root_child2.get_layout_height()); } #[test] fn test_flex_basis_overrides_main_size() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_width(StyleUnit::Point(100_f32.into())); root.set_height(StyleUnit::Point(100_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root_child0.set_flex_grow(1_f32); root_child0.set_flex_basis(StyleUnit::Point(50_f32.into())); root_child0.set_height(StyleUnit::Point(20_f32.into())); root.insert_child(&mut root_child0, 0); let mut root_child1 = Node::new_with_config(&mut config); root_child1.set_flex_grow(1_f32); root_child1.set_height(StyleUnit::Point(10_f32.into())); root.insert_child(&mut root_child1, 1); let mut root_child2 = Node::new_with_config(&mut config); root_child2.set_flex_grow(1_f32); root_child2.set_height(StyleUnit::Point(10_f32.into())); root.insert_child(&mut root_child2, 2); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(100_f32, root_child0.get_layout_width()); assert_eq!(60_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(60_f32, root_child1.get_layout_top()); assert_eq!(100_f32, root_child1.get_layout_width()); assert_eq!(20_f32, root_child1.get_layout_height()); assert_eq!(0_f32, root_child2.get_layout_left()); assert_eq!(80_f32, root_child2.get_layout_top()); assert_eq!(100_f32, root_child2.get_layout_width()); assert_eq!(20_f32, root_child2.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(100_f32, root_child0.get_layout_width()); assert_eq!(60_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(60_f32, root_child1.get_layout_top()); assert_eq!(100_f32, root_child1.get_layout_width()); assert_eq!(20_f32, root_child1.get_layout_height()); assert_eq!(0_f32, root_child2.get_layout_left()); assert_eq!(80_f32, root_child2.get_layout_top()); assert_eq!(100_f32, root_child2.get_layout_width()); assert_eq!(20_f32, root_child2.get_layout_height()); } #[test] fn test_flex_grow_shrink_at_most() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_width(StyleUnit::Point(100_f32.into())); root.set_height(StyleUnit::Point(100_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root.insert_child(&mut root_child0, 0); let mut root_child0_child0 = Node::new_with_config(&mut config); root_child0_child0.set_flex_grow(1_f32); root_child0_child0.set_flex_shrink(1_f32); root_child0.insert_child(&mut root_child0_child0, 0); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(100_f32, root_child0.get_layout_width()); assert_eq!(0_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child0_child0.get_layout_left()); assert_eq!(0_f32, root_child0_child0.get_layout_top()); assert_eq!(100_f32, root_child0_child0.get_layout_width()); assert_eq!(0_f32, root_child0_child0.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(100_f32, root.get_layout_width()); assert_eq!(100_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(100_f32, root_child0.get_layout_width()); assert_eq!(0_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child0_child0.get_layout_left()); assert_eq!(0_f32, root_child0_child0.get_layout_top()); assert_eq!(100_f32, root_child0_child0.get_layout_width()); assert_eq!(0_f32, root_child0_child0.get_layout_height()); } #[test] fn test_flex_grow_less_than_factor_one() { let mut config = Config::new(); let mut root = Node::new_with_config(&mut config); root.set_width(StyleUnit::Point(200_f32.into())); root.set_height(StyleUnit::Point(500_f32.into())); let mut root_child0 = Node::new_with_config(&mut config); root_child0.set_flex_grow(0.2_f32); root_child0.set_flex_basis(StyleUnit::Point(40_f32.into())); root.insert_child(&mut root_child0, 0); let mut root_child1 = Node::new_with_config(&mut config); root_child1.set_flex_grow(0.2_f32); root.insert_child(&mut root_child1, 1); let mut root_child2 = Node::new_with_config(&mut config); root_child2.set_flex_grow(0.4_f32); root.insert_child(&mut root_child2, 2); root.calculate_layout(Undefined, Undefined, Direction::LTR); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(200_f32, root.get_layout_width()); assert_eq!(500_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(200_f32, root_child0.get_layout_width()); assert_eq!(132_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(132_f32, root_child1.get_layout_top()); assert_eq!(200_f32, root_child1.get_layout_width()); assert_eq!(92_f32, root_child1.get_layout_height()); assert_eq!(0_f32, root_child2.get_layout_left()); assert_eq!(224_f32, root_child2.get_layout_top()); assert_eq!(200_f32, root_child2.get_layout_width()); assert_eq!(184_f32, root_child2.get_layout_height()); root.calculate_layout(Undefined, Undefined, Direction::RTL); assert_eq!(0_f32, root.get_layout_left()); assert_eq!(0_f32, root.get_layout_top()); assert_eq!(200_f32, root.get_layout_width()); assert_eq!(500_f32, root.get_layout_height()); assert_eq!(0_f32, root_child0.get_layout_left()); assert_eq!(0_f32, root_child0.get_layout_top()); assert_eq!(200_f32, root_child0.get_layout_width()); assert_eq!(132_f32, root_child0.get_layout_height()); assert_eq!(0_f32, root_child1.get_layout_left()); assert_eq!(132_f32, root_child1.get_layout_top()); assert_eq!(200_f32, root_child1.get_layout_width()); assert_eq!(92_f32, root_child1.get_layout_height()); assert_eq!(0_f32, root_child2.get_layout_left()); assert_eq!(224_f32, root_child2.get_layout_top()); assert_eq!(200_f32, root_child2.get_layout_width()); assert_eq!(184_f32, root_child2.get_layout_height()); }