Updated Tests

- changed auction
- modified tests for auction
- fixed some dead code in lists
This commit is contained in:
2024-06-05 20:57:12 +02:00
parent 63bd32bfec
commit f966a8047c
5 changed files with 160 additions and 60 deletions

View File

@@ -158,9 +158,9 @@ fn test_get() {
assert_eq!(Some(20), list.get(1));
// [50, 30, 40]
assert_eq!(Some(30), list.get(1));
// [30, 40]
// [50, 40]
assert_eq!(Some(40), list.get(1));
// [30]
// [50]
assert_eq!(Some(50), list.get(0));
// []
assert_eq!(None, list.get(0));

View File

@@ -1,6 +1,18 @@
use esercizi::es09_auction::{Auction, Product};
use esercizi::es09_auction::{Auction, Product, Strategy};
use std::collections::VecDeque;
#[derive(Debug)]
pub struct UpMax;
impl Strategy for UpMax {
fn updated_price(&mut self, total_money: f32, price: f32) -> Option<f32> {
if price <= total_money {
Some(total_money)
} else {
None
}
}
}
#[test]
fn test_auction() {
let products = VecDeque::from([
@@ -8,12 +20,45 @@ fn test_auction() {
Product::new("woof", 321.0, 18554.0),
Product::new("woof2", 31.0, 1854.0),
]);
let mut auction = Auction::new(products, 0);
let mut auction = Auction::new(products);
auction.add_participant("th1".to_string(), 4520.0);
auction.add_participant("th2".to_string(), 6500.0);
auction.add_participant("th3".to_string(), 15020.0);
auction.add_participant("th4".to_string(), 8520.0);
let name1 = "th1".to_string();
let name2 = "th2".to_string();
let name3 = "th3".to_string();
let name4 = "th4".to_string();
auction.start();
auction.add_participant(name1.clone(), 4520.0, Box::new(UpMax));
auction.add_participant(name2.clone(), 6500.0, Box::new(UpMax));
auction.add_participant(name3.clone(), 15020.0, Box::new(UpMax));
auction.add_participant(name4.clone(), 8520.0, Box::new(UpMax));
let mut results = auction.start();
assert_eq!(results.len(), 3);
let auction = results.pop_front();
assert!(matches!(auction, Some(_)));
let (product, winner) = auction.unwrap();
assert_eq!(product.name, "bau".to_string());
assert_eq!(product.price, 15020.0);
assert_eq!(product.reserve, 1000.0);
assert!(matches!(winner, Some(name) if name == name3));
let auction = results.pop_front();
assert!(matches!(auction, Some(_)));
let (product, winner) = auction.unwrap();
assert_eq!(product.name, "woof".to_string());
assert_eq!(product.price, 8520.0);
assert_eq!(product.reserve, 18554.0);
assert_eq!(winner, None);
let auction = results.pop_front();
assert!(matches!(auction, Some(_)));
let (product, winner) = auction.unwrap();
assert_eq!(product.name, "woof2".to_string());
assert_eq!(product.price, 8520.0);
assert_eq!(product.reserve, 1854.0);
assert!(matches!(winner, Some(name) if name == name4));
let auction = results.pop_front();
assert!(matches!(auction, None));
}