Skip to content

Commit 6d0592c

Browse files
authored
Merge pull request #621 from EnergySystemsModellingLab/fix-replaced-assets-ordering
Fix: Sort assets by ID after replacing pool
2 parents ef7d5f2 + 7297920 commit 6d0592c

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/asset.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ impl Hash for AssetRef {
187187
}
188188
}
189189

190+
impl PartialOrd for AssetRef {
191+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
192+
Some(self.cmp(other))
193+
}
194+
}
195+
196+
impl Ord for AssetRef {
197+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
198+
self.id.unwrap().cmp(&other.id.unwrap())
199+
}
200+
}
201+
190202
/// A pool of [`Asset`]s
191203
pub struct AssetPool {
192204
/// The pool of active assets
@@ -295,6 +307,9 @@ impl AssetPool {
295307

296308
self.active.clear();
297309
self.active.extend(new_pool);
310+
311+
// New pool may not have been sorted, but active needs to be sorted by ID
312+
self.active.sort();
298313
}
299314
}
300315

@@ -526,4 +541,33 @@ mod tests {
526541
assert_eq!(asset_pool.active[0].id, Some(AssetID(2)));
527542
assert_eq!(asset_pool.active[0].agent_id, "some_other_agent".into());
528543
}
544+
545+
#[rstest]
546+
fn test_asset_pool_replace_active_pool_out_of_order(
547+
mut asset_pool: AssetPool,
548+
process: Process,
549+
) {
550+
let new_asset = Asset::new(
551+
"some_other_agent".into(),
552+
process.into(),
553+
"GBR".into(),
554+
2.0,
555+
2010,
556+
)
557+
.unwrap();
558+
559+
asset_pool.commission_new(2020);
560+
assert_eq!(asset_pool.active.len(), 2);
561+
let mut new_pool: Vec<Rc<Asset>> = asset_pool
562+
.iter()
563+
.map(|asset| asset.clone().into())
564+
.collect();
565+
new_pool.push(new_asset.into());
566+
new_pool.reverse();
567+
568+
asset_pool.replace_active_pool(new_pool);
569+
assert_equal(asset_pool.iter().map(|asset| asset.id.unwrap().0), 0..3);
570+
assert_eq!(asset_pool.active[2].id, Some(AssetID(2)));
571+
assert_eq!(asset_pool.active[2].agent_id, "some_other_agent".into());
572+
}
529573
}

0 commit comments

Comments
 (0)