From 5123c55627b5a9c4f3dbe5d8da2eec250d4592f4 Mon Sep 17 00:00:00 2001 From: Tomjerri1 <151880307+Tomjerri1@users.noreply.github.com> Date: Fri, 13 Nov 2026 01:05:02 +0200 Subject: [PATCH 1/3] Refactor config for testing --- src/config.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index df39abd..b42b838 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,12 +27,8 @@ pub fn snapshot(state: &AppState) -> [usize; 9] { ] } -/// Loads saved settings into `state`. Missing file or bad values are ignored. -pub fn load(state: &AppState) { - let Some(path) = config_path() else { return }; - let Ok(text) = std::fs::read_to_string(&path) else { - return; - }; +/// Parses and applies settings from text to `state`. +pub fn apply_settings(state: &AppState, text: &str) { for line in text.lines() { let Some((key, value)) = line.split_once('=') else { continue; @@ -61,17 +57,31 @@ pub fn load(state: &AppState) { } } +/// Loads saved settings into `state`. Missing file or bad values are ignored. +pub fn load(state: &AppState) { + let Some(path) = config_path() else { return }; + let Ok(text) = std::fs::read_to_string(&path) else { + return; + }; + apply_settings(state, &text); +} + +/// Formats current settings into string representation. +pub fn format_settings(state: &AppState) -> String { + let s = snapshot(state); + format!( + "flakes={}\nfps={}\nopacity={}\nheight_limit={}\nacc_windows={}\nacc_dock={}\nacc_ground={}\nmouse_flakes={}\nstrength={}\n", + s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8] + ) +} + /// Saves current settings. Errors are ignored (settings are best-effort). pub fn save(state: &AppState) { let Some(path) = config_path() else { return }; if let Some(dir) = path.parent() { let _ = std::fs::create_dir_all(dir); } - let s = snapshot(state); - let text = format!( - "flakes={}\nfps={}\nopacity={}\nheight_limit={}\nacc_windows={}\nacc_dock={}\nacc_ground={}\nmouse_flakes={}\nstrength={}\n", - s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8] - ); + let text = format_settings(state); let tmp = path.with_extension("tmp"); if std::fs::write(&tmp, text).is_ok() { let _ = std::fs::rename(&tmp, &path); From 75c2beac530af8b9ff263f49b04691de2751a867 Mon Sep 17 00:00:00 2001 From: Tomjerri1 <151880307+Tomjerri1@users.noreply.github.com> Date: Fri, 13 Nov 2026 01:10:03 +0200 Subject: [PATCH 2/3] Refactor config for testing and add config tests --- src/config.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/src/config.rs b/src/config.rs index b42b838..4e895fd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -87,3 +87,80 @@ pub fn save(state: &AppState) { let _ = std::fs::rename(&tmp, &path); } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn defaults() { + let state = AppState::new(); + let s = format_settings(&state); + assert!(s.contains("flakes=400")); + assert!(s.contains("fps=30")); + assert!(s.contains("opacity=100")); + assert!(s.contains("strength=100")); + } + + #[test] + fn clamps() { + let state = AppState::new(); + apply_settings(&state, "flakes=1\nopacity=0\nstrength=999\n"); + assert_eq!(state.target_flakes.load(Ordering::Relaxed), 50); + assert_eq!(state.opacity.load(Ordering::Relaxed), 10); + assert_eq!(state.strength.load(Ordering::Relaxed), 200); + + apply_settings(&state, "flakes=99999\n"); + assert_eq!(state.target_flakes.load(Ordering::Relaxed), 3000); + } + + #[test] + fn fps() { + let state = AppState::new(); + apply_settings(&state, "fps=60\n"); + assert_eq!(state.target_fps.load(Ordering::Relaxed), 60); + + let state2 = AppState::new(); + apply_settings(&state2, "fps=45\n"); + assert_eq!(state2.target_fps.load(Ordering::Relaxed), 30); + } + + #[test] + fn bools() { + let state = AppState::new(); + apply_settings(&state, "height_limit=0\nacc_ground=1\n"); + assert!(!state.height_limit.load(Ordering::Relaxed)); + assert!(state.acc_ground.load(Ordering::Relaxed)); + + apply_settings(&state, "acc_ground=7\n"); + assert!(state.acc_ground.load(Ordering::Relaxed)); + } + + #[test] + fn garbage() { + let state = AppState::new(); + apply_settings( + &state, + "abc\n=\nflakes=notanumber\nunknown=5\n\n opacity = 50 \n", + ); + assert_eq!(state.opacity.load(Ordering::Relaxed), 50); + assert_eq!(state.target_flakes.load(Ordering::Relaxed), 400); + } + + #[test] + fn roundtrip() { + let a = AppState::new(); + a.target_flakes.store(500, Ordering::Relaxed); + a.target_fps.store(60, Ordering::Relaxed); + a.opacity.store(80, Ordering::Relaxed); + a.height_limit.store(false, Ordering::Relaxed); + a.acc_ground.store(true, Ordering::Relaxed); + a.strength.store(150, Ordering::Relaxed); + + let text = format_settings(&a); + let b = AppState::new(); + apply_settings(&b, &text); + + assert_eq!(snapshot(&a), snapshot(&b)); + } +} From 0f3cd15e66a9f4ef294a0e2417e59fd24e9077bf Mon Sep 17 00:00:00 2001 From: Tomjerri1 <151880307+Tomjerri1@users.noreply.github.com> Date: Fri, 13 Nov 2026 01:12:27 +0200 Subject: [PATCH 3/3] Add basic snow unit tests --- src/snow.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/snow.rs b/src/snow.rs index de83bcc..a4dd201 100644 --- a/src/snow.rs +++ b/src/snow.rs @@ -2019,3 +2019,70 @@ impl Snow { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_bucket_idx() { + assert_eq!(Snow::get_bucket_idx(0.8), 0); + assert_eq!(Snow::get_bucket_idx(4.0), 7); + assert_eq!(Snow::get_bucket_idx(100.0), 7); + assert_eq!(Snow::get_bucket_idx(-5.0), 0); + for i in 0..=32 { + let v = 0.8 + i as f32 * 0.1; + assert!(Snow::get_bucket_idx(v) <= 7); + } + } + + #[test] + fn test_floor_like() { + assert!(is_floor_like(None)); + assert!(is_floor_like(Some(GROUND_ID))); + assert!(!is_floor_like(Some(42))); + } + + #[test] + fn test_new() { + let snow_100 = Snow::new(800.0, 600.0, 100); + assert_eq!(snow_100.flakes.len(), 100); + for flake in &snow_100.flakes { + assert!(flake.x >= 0.0 && flake.x <= 800.0); + assert!(flake.y >= 0.0 && flake.y <= 600.0); + } + + let snow_0 = Snow::new(800.0, 600.0, 0); + assert!(snow_0.flakes.is_empty()); + } + + #[test] + fn test_relax() { + let mut heights = vec![0.0, 10.0, 0.0]; + let mut changed = false; + for _ in 0..20 { + if relax_range(&mut heights, 0, 2, 2.2) { + changed = true; + } + } + assert!(changed); + for i in 0..heights.len() - 1 { + let diff = (heights[i] - heights[i + 1]).abs(); + assert!(diff <= 2.2 + 0.001); + } + + let mut smooth = vec![1.0, 1.5, 2.0]; + let orig = smooth.clone(); + let res = relax_range(&mut smooth, 0, 2, 2.2); + assert!(!res); + assert_eq!(smooth, orig); + } + + #[test] + fn test_smoothstep() { + assert_eq!(smoothstep(0.0, 1.0, -1.0), 0.0); + assert_eq!(smoothstep(0.0, 1.0, 2.0), 1.0); + let val = smoothstep(0.0, 1.0, 0.5); + assert!((val - 0.5).abs() <= 0.001); + } +}