1
0
Fork 0

Assert that parameter ranges are valid

This commit is contained in:
Robbert van der Helm 2023-04-30 21:28:23 +02:00
parent 0afe6852b3
commit 68b3b864d6
4 changed files with 47 additions and 0 deletions

View file

@ -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 reverse chronological order. The main purpose of this document in its current
state is to list breaking changes. 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] ## [2023-04-27]
### Changed ### Changed

View file

@ -269,6 +269,8 @@ impl FloatParam {
/// Build a new [`FloatParam`]. Use the other associated functions to modify the behavior of the /// Build a new [`FloatParam`]. Use the other associated functions to modify the behavior of the
/// parameter. /// parameter.
pub fn new(name: impl Into<String>, default: f32, range: FloatRange) -> Self { pub fn new(name: impl Into<String>, default: f32, range: FloatRange) -> Self {
range.assert_validity();
Self { Self {
value: AtomicF32::new(default), value: AtomicF32::new(default),
normalized_value: AtomicF32::new(range.normalize(default)), normalized_value: AtomicF32::new(range.normalize(default)),

View file

@ -249,6 +249,8 @@ impl IntParam {
/// Build a new [`IntParam`]. Use the other associated functions to modify the behavior of the /// Build a new [`IntParam`]. Use the other associated functions to modify the behavior of the
/// parameter. /// parameter.
pub fn new(name: impl Into<String>, default: i32, range: IntRange) -> Self { pub fn new(name: impl Into<String>, default: i32, range: IntRange) -> Self {
range.assert_validity();
Self { Self {
value: AtomicI32::new(default), value: AtomicI32::new(default),
normalized_value: AtomicF32::new(range.normalize(default)), normalized_value: AtomicF32::new(range.normalize(default)),

View file

@ -195,6 +195,25 @@ impl FloatRange {
FloatRange::Reversed(range) => range.snap_to_step(value, step_size), 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 { impl IntRange {
@ -249,6 +268,23 @@ impl IntRange {
IntRange::Reversed(range) => range.inner_range(), 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)] #[cfg(test)]