Skip to content

Commit

Permalink
make operation simple ByteArray (no Option)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi committed Nov 12, 2024
1 parent ec01cb5 commit 14697eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
9 changes: 4 additions & 5 deletions exercises/concept/calculator-conundrum/.meta/exemplar.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#[generate_trait]
pub impl SimpleCalculatorImpl of SimpleCalculatorTrait {
fn calculate(a: i32, b: i32, operation: Option<ByteArray>) -> Result<ByteArray, ByteArray> {
let operation = operation.expect('Operation cannot be null');
fn calculate(a: i32, b: i32, operation: ByteArray) -> Result<ByteArray, ByteArray> {
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")
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/concept/calculator-conundrum/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[generate_trait]
pub impl SimpleCalculatorImpl of SimpleCalculatorTrait {
fn calculate(a: i32, b: i32, operation: Option<ByteArray>) -> Result<ByteArray, ByteArray> {
fn calculate(a: i32, b: i32, operation: ByteArray) -> Result<ByteArray, ByteArray> {
panic!("implement `calculate`")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/");
}

0 comments on commit 14697eb

Please sign in to comment.