diff --git a/exercises/03_ticket_v1/08_stack/src/lib.rs b/exercises/03_ticket_v1/08_stack/src/lib.rs index e97518c..ad7d2a4 100644 --- a/exercises/03_ticket_v1/08_stack/src/lib.rs +++ b/exercises/03_ticket_v1/08_stack/src/lib.rs @@ -6,16 +6,16 @@ mod tests { #[test] fn u16_size() { - assert_eq!(size_of::(), todo!()); + assert_eq!(size_of::(), 2); } #[test] fn i32_size() { - assert_eq!(size_of::(), todo!()); + assert_eq!(size_of::(), 4); } #[test] fn bool_size() { - assert_eq!(size_of::(), todo!()); + assert_eq!(size_of::(), 1); } } diff --git a/exercises/03_ticket_v1/09_heap/src/lib.rs b/exercises/03_ticket_v1/09_heap/src/lib.rs index 7273c20..89da015 100644 --- a/exercises/03_ticket_v1/09_heap/src/lib.rs +++ b/exercises/03_ticket_v1/09_heap/src/lib.rs @@ -13,7 +13,7 @@ mod tests { #[test] fn string_size() { - assert_eq!(size_of::(), todo!()); + assert_eq!(size_of::(), 24); } #[test] @@ -23,6 +23,6 @@ mod tests { // but, in general, the memory layout of structs is a more complex topic. // If you're curious, check out the "Type layout" section of The Rust Reference // https://doc.rust-lang.org/reference/type-layout.html for more information. - assert_eq!(size_of::(), todo!()); + assert_eq!(size_of::(), 72); } } diff --git a/exercises/03_ticket_v1/10_references_in_memory/src/lib.rs b/exercises/03_ticket_v1/10_references_in_memory/src/lib.rs index d580758..d184a19 100644 --- a/exercises/03_ticket_v1/10_references_in_memory/src/lib.rs +++ b/exercises/03_ticket_v1/10_references_in_memory/src/lib.rs @@ -13,16 +13,16 @@ mod tests { #[test] fn u16_ref_size() { - assert_eq!(size_of::<&u16>(), todo!()); + assert_eq!(size_of::<&u16>(), 8); } #[test] fn u64_mut_ref_size() { - assert_eq!(size_of::<&mut u64>(), todo!()); + assert_eq!(size_of::<&mut u64>(), 8); } #[test] fn ticket_ref_size() { - assert_eq!(size_of::<&Ticket>(), todo!()); + assert_eq!(size_of::<&Ticket>(), 8); } } diff --git a/exercises/03_ticket_v1/11_destructor/src/lib.rs b/exercises/03_ticket_v1/11_destructor/src/lib.rs index 45bc89c..6555fbb 100644 --- a/exercises/03_ticket_v1/11_destructor/src/lib.rs +++ b/exercises/03_ticket_v1/11_destructor/src/lib.rs @@ -2,7 +2,7 @@ // We'll pick the concept up again in a later chapter after covering traits and // interior mutability. fn outro() -> &'static str { - "I have a basic understanding of __!" + "I have a basic understanding of destructors!" } #[cfg(test)] diff --git a/exercises/03_ticket_v1/12_outro/src/lib.rs b/exercises/03_ticket_v1/12_outro/src/lib.rs index 15c99b8..7f59a29 100644 --- a/exercises/03_ticket_v1/12_outro/src/lib.rs +++ b/exercises/03_ticket_v1/12_outro/src/lib.rs @@ -11,3 +11,74 @@ // Integration here has a very specific meaning: they test **the public API** of your project. // You'll need to pay attention to the visibility of your types and methods; integration // tests can't access private or `pub(crate)` items. + +pub struct Order { + product_name: String, + quantity: u32, + unit_price: u32, +} + +impl Order { + fn cName(name: &String) { + if name.is_empty() { + panic!("Name must be valid"); + } + if name.len() > 300 { + panic!("Name too long"); + } + } + + fn cQuantity(quantity: &u32) { + if quantity.eq(&0) { + panic!("not enough quantity"); + } + } + + fn cPrice(price: &u32) { + if price.eq(&0) { + panic!("invalid price"); + } + } + + pub fn total(&self) -> u32 { + self.unit_price * self.quantity + } + + pub fn new(name: String, quantity: u32, price: u32) -> Order { + Order::cName(&name); + Order::cPrice(&price); + Order::cQuantity(&quantity); + Order { + product_name: name, + quantity, + unit_price: price, + } + } + + pub fn product_name(&self) -> String { + self.product_name.clone() + } + + pub fn quantity(&self) -> &u32 { + &self.quantity + } + + pub fn unit_price(&self) -> &u32 { + &self.unit_price + } + + pub fn set_product_name(&mut self, name: String) { + Order::cName(&name); + self.product_name = name; + } + + pub fn set_quantity(&mut self, quantity: u32) { + Order::cQuantity(&quantity); + self.quantity = quantity; + } + + pub fn set_unit_price(&mut self, price: u32) { + Order::cPrice(&price); + self.unit_price = price; + } +}