From 68b3b864d6144f374d3245e060495d78a42e6da3 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 30 Apr 2023 21:28:23 +0200 Subject: [PATCH] Assert that parameter ranges are valid --- CHANGELOG.md | 7 +++++++ src/params/float.rs | 2 ++ src/params/integer.rs | 2 ++ src/params/range.rs | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 230ad41a..6cb8df7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,13 @@ Since there is no stable release yet, the changes are organized per day in reverse chronological order. The main purpose of this document in its current state is to list breaking changes. +## [2023-04-30] + +### Changes + +- Added debug assertions to make sure parameter ranges are valid. The minimum + value must always be lower than the maximum value and they cannot be equal. + ## [2023-04-27] ### Changed diff --git a/src/params/float.rs b/src/params/float.rs index 803ad9e6..9d94dea5 100644 --- a/src/params/float.rs +++ b/src/params/float.rs @@ -269,6 +269,8 @@ impl FloatParam { /// Build a new [`FloatParam`]. Use the other associated functions to modify the behavior of the /// parameter. pub fn new(name: impl Into, default: f32, range: FloatRange) -> Self { + range.assert_validity(); + Self { value: AtomicF32::new(default), normalized_value: AtomicF32::new(range.normalize(default)), diff --git a/src/params/integer.rs b/src/params/integer.rs index 13a2575f..7dd63596 100644 --- a/src/params/integer.rs +++ b/src/params/integer.rs @@ -249,6 +249,8 @@ impl IntParam { /// Build a new [`IntParam`]. Use the other associated functions to modify the behavior of the /// parameter. pub fn new(name: impl Into, default: i32, range: IntRange) -> Self { + range.assert_validity(); + Self { value: AtomicI32::new(default), normalized_value: AtomicF32::new(range.normalize(default)), diff --git a/src/params/range.rs b/src/params/range.rs index 5992f079..414b8b80 100644 --- a/src/params/range.rs +++ b/src/params/range.rs @@ -195,6 +195,25 @@ impl FloatRange { FloatRange::Reversed(range) => range.snap_to_step(value, step_size), } } + + /// Emits debug assertions to make sure that range minima are always less than the maxima and + /// that they are not equal. + pub(super) fn assert_validity(&self) { + match self { + FloatRange::Linear { min, max } + | FloatRange::Skewed { min, max, .. } + | FloatRange::SymmetricalSkewed { min, max, .. } => { + nih_debug_assert!( + min < max, + "The range minimum ({}) needs to be less than the range maximum ({}) and they \ + cannot be equal", + min, + max + ); + } + FloatRange::Reversed(range) => range.assert_validity(), + } + } } impl IntRange { @@ -249,6 +268,23 @@ impl IntRange { IntRange::Reversed(range) => range.inner_range(), } } + + /// Emits debug assertions to make sure that range minima are always less than the maxima and + /// that they are not equal. + pub(super) fn assert_validity(&self) { + match self { + IntRange::Linear { min, max } => { + nih_debug_assert!( + min < max, + "The range minimum ({}) needs to be less than the range maximum ({}) and they \ + cannot be equal", + min, + max + ); + } + IntRange::Reversed(range) => range.assert_validity(), + } + } } #[cfg(test)]