Skip to content
Snippets Groups Projects
Commit 3299b740 authored by Christian Wunder's avatar Christian Wunder
Browse files

Fix Modulo zero.

parent 2cf4381a
No related branches found
No related tags found
1 merge request!6Fix Modulo zero.
......@@ -8,7 +8,12 @@ int plus( int a, int b) { return a + b; }
int minus( int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int power( int a, int b) { return std::pow(a, b); }
int modulo( int a, int b) { return a % b; }
int modulo( int a, int b) {
if(b == 0) {
throw std::domain_error("Modulo by 0 not defined.");
}
return a % b;
}
int divide( int a, int b) {
if(b == 0) {
throw std::domain_error("Division by 0.");
......
......@@ -67,3 +67,9 @@ TEST_CASE( "Unrecognized characters are detected and exception is thrown", "[cal
REQUIRE_THROWS( evaluate("11+3?4") );
REQUIRE_THROWS( evaluate("1F+") );
}
TEST_CASE( "modulo by zero is detected and exception is thrown", "[calc]") {
REQUIRE_THROWS( evaluate("40%") );
REQUIRE_THROWS( evaluate("00%") );
REQUIRE_THROWS( evaluate("90%") );
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment