Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/release_notes/upcoming.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ ready to be released, carry out the following steps:
- Users can now set demand to zero in `demand.csv` ([#871])
- Fix: sign for levies of type `net` was wrong for inputs ([#969])
- Fix `--overwrite` option for `save-graphs` command ([#1001])
- Fix broken `Eq`/`Ord` consistency for `AssetCapacity`: equality is now aligned with
`total_cmp` ordering ([#1158])

[#767]: https://github.com/EnergySystemsModellingLab/MUSE2/pull/767
[#868]: https://github.com/EnergySystemsModellingLab/MUSE2/pull/868
Expand All @@ -72,3 +74,4 @@ ready to be released, carry out the following steps:
[#1021]: https://github.com/EnergySystemsModellingLab/MUSE2/pull/1021
[#1022]: https://github.com/EnergySystemsModellingLab/MUSE2/pull/1022
[#1030]: https://github.com/EnergySystemsModellingLab/MUSE2/pull/1030
[#1158]: https://github.com/EnergySystemsModellingLab/MUSE2/pull/1158
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the correct PR number. This PR is #1206. Fix the link and the text.

45 changes: 44 additions & 1 deletion src/asset/capacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::cmp::Ordering;
use std::ops::{Add, Sub};

/// Capacity of an asset, which may be continuous or a discrete number of indivisible units
#[derive(Clone, PartialEq, Copy, Debug)]
#[derive(Clone, Copy, Debug)]
pub enum AssetCapacity {
/// Continuous capacity
Continuous(Capacity),
Expand All @@ -13,6 +13,12 @@ pub enum AssetCapacity {
Discrete(u32, Capacity),
}

impl PartialEq for AssetCapacity {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}

impl Add for AssetCapacity {
type Output = Self;

Expand Down Expand Up @@ -213,4 +219,41 @@ mod tests {
let got = orig.apply_limit_factor(factor);
assert_eq!(got, AssetCapacity::Discrete(expected_units, unit_size));
}

/// `PartialEq` must be consistent with `Ord`: two values are equal iff `cmp` returns
/// `Ordering::Equal`.
#[rstest]
#[case::continuous_equal(
AssetCapacity::Continuous(Capacity(1.0)),
AssetCapacity::Continuous(Capacity(1.0)),
true
)]
#[case::continuous_positive_zero_vs_negative_zero(
AssetCapacity::Continuous(Capacity(0.0)),
AssetCapacity::Continuous(Capacity(-0.0)),
false
)]
#[case::continuous_unequal(
AssetCapacity::Continuous(Capacity(1.0)),
AssetCapacity::Continuous(Capacity(2.0)),
false
)]
#[case::discrete_equal(
AssetCapacity::Discrete(3, Capacity(4.0)),
AssetCapacity::Discrete(3, Capacity(4.0)),
true
)]
#[case::discrete_unequal(
AssetCapacity::Discrete(2, Capacity(4.0)),
AssetCapacity::Discrete(3, Capacity(4.0)),
false
)]
fn eq_consistent_with_ord(
#[case] a: AssetCapacity,
#[case] b: AssetCapacity,
#[case] expected_eq: bool,
) {
assert_eq!(a == b, expected_eq);
assert_eq!(a.cmp(&b) == Ordering::Equal, expected_eq);
}
}
Loading