From ec7cce80b4234306e97864a6069a379b3be24301 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 28 Mar 2025 14:27:44 -0400 Subject: [PATCH] util: test improvements --- src/util.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 5 deletions(-) diff --git a/src/util.rs b/src/util.rs index 1bd3359..6743df9 100644 --- a/src/util.rs +++ b/src/util.rs @@ -17,10 +17,95 @@ pub const fn index(width: usize, height: usize, x: f32, y: f32) -> usize { mod test { use super::*; - #[test] - fn wrap_test() { - assert_eq!(wrap(1.1, 1.0), 0.100000024); // floating point weirdness - assert_eq!(wrap(0.5, 1.0), 0.5); - assert_eq!(wrap(-1.0, 2.0), 1.0); + mod wrap { + use super::*; + #[test] + fn over() { + assert_eq!(wrap(1.1, 1.0), 0.100000024); // floating point weirdness + } + + #[test] + fn middle() { + assert_eq!(wrap(0.5, 1.0), 0.5); + } + + #[test] + fn under() { + assert_eq!(wrap(-1.0, 2.0), 1.0); + } + } + + mod index { + use super::*; + + #[test] + fn basic_positive_coordinates() { + let width = 4; + let height = 4; + assert_eq!(index(width, height, 1.5, 2.5), 9); + } + + #[test] + fn negative_x_coordinate() { + let width = 8; + let height = 8; + assert_eq!(index(width, height, -3.2, 5.6), 44); + } + + #[test] + fn exact_boundary_values() { + let width = 16; + let height = 16; + assert_eq!(index(width, height, 16.0, 0.0), 0); + } + + #[test] + fn large_coordinates() { + let width = 2; + let height = 2; + assert_eq!(index(width, height, 1000.0, 2000.0), 0); + } + + #[test] + fn negative_x_and_y() { + let width = 4; + let height = 4; + assert_eq!(index(width, height, -1.5, -0.5), 14); + } + + #[test] + fn fractional_truncation() { + let width = 4; + let height = 4; + assert_eq!(index(width, height, 3.9, 3.999), 15); + } + + #[test] + fn zero_coordinates() { + let width = 4; + let height = 4; + assert_eq!(index(width, height, 0.0, 0.0), 0); + } + + #[test] + fn x_equals_width() { + let width = 8; + let height = 8; + assert_eq!(index(width, height, 8.0, 0.0), 0); + } + + #[test] + fn y_negative_beyond_height() { + let width = 4; + let height = 4; + assert_eq!(index(width, height, 0.0, -4.5), 0); + } + + #[test] + fn width_and_height_one() { + let width = 1; + let height = 1; + assert_eq!(index(width, height, 123.4, -56.7), 0); + } } }