#![ cfg( feature = "test-support" ) ] use ltk::test_support::{ value_from_x_in_rect, thumb_design_px }; use ltk::Rect; fn rect( x: f32, y: f32, w: f32, h: f32 ) -> Rect { Rect { x, y, width: w, height: h } } /// The thumb size the pure math is exercised with — the design constant, so /// the pad matches what the renderer draws with an unscaled thumb. fn thumb() -> f32 { thumb_design_px() } #[ test ] fn left_edge_clamps_to_zero() { let r = rect( 0.0, 0.0, 200.0, 36.0 ); assert_eq!( value_from_x_in_rect( r, 0.0, thumb() ), 0.0 ); assert_eq!( value_from_x_in_rect( r, -50.0, thumb() ), 0.0 ); } #[ test ] fn right_edge_clamps_to_one() { let r = rect( 0.0, 0.0, 200.0, 36.0 ); assert_eq!( value_from_x_in_rect( r, 200.0, thumb() ), 1.0 ); assert_eq!( value_from_x_in_rect( r, 999.0, thumb() ), 1.0 ); } #[ test ] fn center_returns_half() { // The thumb adds padding on both sides, so the geometric center of the // rect produces ~0.5 (within the THUMB_SIZE/2 tolerance). let r = rect( 0.0, 0.0, 200.0, 36.0 ); let v = value_from_x_in_rect( r, 100.0, thumb() ); assert!( ( v - 0.5 ).abs() < 0.1, "expected ~0.5 got {}", v ); } #[ test ] fn rect_offset_translates_correctly() { let r = rect( 500.0, 0.0, 200.0, 36.0 ); assert_eq!( value_from_x_in_rect( r, 500.0, thumb() ), 0.0 ); assert_eq!( value_from_x_in_rect( r, 700.0, thumb() ), 1.0 ); let v = value_from_x_in_rect( r, 600.0, thumb() ); assert!( ( v - 0.5 ).abs() < 0.1, "expected ~0.5 got {}", v ); } #[ test ] fn output_is_always_in_unit_range() { let r = rect( 10.0, 10.0, 300.0, 36.0 ); for x in -100i32 ..= 500 { let v = value_from_x_in_rect( r, x as f32, thumb() ); assert!( ( 0.0..= 1.0 ).contains( &v ), "v={} out of range at x={}", v, x ); } } #[ test ] fn very_narrow_rect_does_not_divide_by_zero() { // Width < THUMB_SIZE — track_w is clamped to 1.0 internally so the // formula stays defined. let r = rect( 0.0, 0.0, 4.0, 36.0 ); let v = value_from_x_in_rect( r, 2.0, thumb() ); assert!( v.is_finite() ); assert!( ( 0.0..= 1.0 ).contains( &v ) ); }