diff --git a/exercises/concept/calculator-conundrum/.meta/exemplar.cairo b/exercises/concept/calculator-conundrum/.meta/exemplar.cairo index 020dd2c9..19f3cdff 100644 --- a/exercises/concept/calculator-conundrum/.meta/exemplar.cairo +++ b/exercises/concept/calculator-conundrum/.meta/exemplar.cairo @@ -1,16 +1,15 @@ #[generate_trait] pub impl SimpleCalculatorImpl of SimpleCalculatorTrait { - fn calculate(a: i32, b: i32, operation: Option) -> Result { - let operation = operation.expect('Operation cannot be null'); + fn calculate(a: i32, b: i32, operation: ByteArray) -> Result { + assert!(operation != "", "Operation cannot be an empty string"); + if operation == "+" { Result::Ok(format!("{} + {} = {}", a, b, a + b)) } else if operation == "*" { Result::Ok(format!("{} * {} = {}", a, b, a * b)) } else if operation == "/" { - assert!(b != 0, "Division by zero is not allowed"); + assert(b != 0, 'Division by zero is not allowed'); Result::Ok(format!("{} / {} = {}", a, b, a / b)) - } else if operation == "" { - Result::Err("Operation cannot be an empty string") } else { Result::Err("Operation is out of range") } diff --git a/exercises/concept/calculator-conundrum/src/lib.cairo b/exercises/concept/calculator-conundrum/src/lib.cairo index 7f4ea3d1..d87f391a 100644 --- a/exercises/concept/calculator-conundrum/src/lib.cairo +++ b/exercises/concept/calculator-conundrum/src/lib.cairo @@ -1,6 +1,6 @@ #[generate_trait] pub impl SimpleCalculatorImpl of SimpleCalculatorTrait { - fn calculate(a: i32, b: i32, operation: Option) -> Result { + fn calculate(a: i32, b: i32, operation: ByteArray) -> Result { panic!("implement `calculate`") } } diff --git a/exercises/concept/calculator-conundrum/tests/calculator_conundrum.cairo b/exercises/concept/calculator-conundrum/tests/calculator_conundrum.cairo index 53b7a08f..6c0cb948 100644 --- a/exercises/concept/calculator-conundrum/tests/calculator_conundrum.cairo +++ b/exercises/concept/calculator-conundrum/tests/calculator_conundrum.cairo @@ -2,76 +2,61 @@ use calculator_conundrum::SimpleCalculatorTrait as SimpleCalculator; #[test] fn addition_with_small_operands() { - assert_eq!(SimpleCalculator::calculate(22, 25, Option::Some("+")).unwrap(), "22 + 25 = 47"); + assert_eq!(SimpleCalculator::calculate(22, 25, "+").unwrap(), "22 + 25 = 47"); } #[test] #[ignore] fn addition_with_large_operands() { assert_eq!( - SimpleCalculator::calculate(378_961, 399_635, Option::Some("+")).unwrap(), - "378961 + 399635 = 778596" + SimpleCalculator::calculate(378_961, 399_635, "+").unwrap(), "378961 + 399635 = 778596" ); } #[test] #[ignore] fn multiplication_with_small_operands() { - assert_eq!(SimpleCalculator::calculate(3, 21, Option::Some("*")).unwrap(), "3 * 21 = 63"); + assert_eq!(SimpleCalculator::calculate(3, 21, "*").unwrap(), "3 * 21 = 63"); } #[test] #[ignore] fn multiplication_with_large_operands() { assert_eq!( - SimpleCalculator::calculate(72_441, 2_048, Option::Some("*")).unwrap(), - "72441 * 2048 = 148359168" + SimpleCalculator::calculate(72_441, 2_048, "*").unwrap(), "72441 * 2048 = 148359168" ); } #[test] #[ignore] fn division_with_small_operands() { - assert_eq!(SimpleCalculator::calculate(72, 9, Option::Some("/")).unwrap(), "72 / 9 = 8"); + assert_eq!(SimpleCalculator::calculate(72, 9, "/").unwrap(), "72 / 9 = 8"); } #[test] #[ignore] fn division_with_large_operands() { assert_eq!( - SimpleCalculator::calculate(1_338_800, 83_675, Option::Some("/")).unwrap(), - "1338800 / 83675 = 16" + SimpleCalculator::calculate(1_338_800, 83_675, "/").unwrap(), "1338800 / 83675 = 16" ); } #[test] #[ignore] fn calculate_returns_result_err_for_non_valid_operations() { - assert_eq!( - SimpleCalculator::calculate(1, 2, Option::Some("**")).unwrap_err(), - "Operation is out of range" - ); -} - -#[test] -#[ignore] -#[should_panic(expected: ('Operation cannot be null',))] -fn calculate_panics_for_null_as_operation() { - let _ = SimpleCalculator::calculate(1, 2, Option::None); + assert_eq!(SimpleCalculator::calculate(1, 2, "**").unwrap_err(), "Operation is out of range"); } #[test] #[ignore] +#[should_panic(expected: ("Operation cannot be an empty string",))] fn calculate_returns_result_err_for_empty_string_as_operation() { - assert_eq!( - SimpleCalculator::calculate(1, 2, Option::Some("")).unwrap_err(), - "Operation cannot be an empty string" - ); + let _ = SimpleCalculator::calculate(1, 2, ""); } #[test] #[ignore] -#[should_panic(expected: ("Division by zero is not allowed",))] +#[should_panic(expected: ('Division by zero is not allowed',))] fn calculate_panics_for_division_with_0() { - let _ = SimpleCalculator::calculate(33, 0, Option::Some("/")); + let _ = SimpleCalculator::calculate(33, 0, "/"); }