#include"calculator.hpp"#include<stdexcept>intadd(inta,intb){returna+b;}intdivide(inta,intb){if(b==0)throwstd::invalid_argument("division by zero");returna/b;}
#include<catch2/catch_test_macros.hpp>#include"calculator.hpp"#include<stdexcept>TEST_CASE("add two numbers"){REQUIRE(add(2,3)==5);REQUIRE(add(-1,1)==0);}TEST_CASE("divide two numbers"){REQUIRE(divide(10,2)==5);}TEST_CASE("division by zero throws"){REQUIRE_THROWS_AS(divide(10,0),std::invalid_argument);}
TEST_CASE defines an independently runnable test. Give it a description that
states the behavior being verified. An optional tag, such as [calculator],
can be used to select related tests from the command line.
TEST_CASE("division by zero throws"){REQUIRE_THROWS_AS(divide(10,0),std::invalid_argument);}
This assertion fails if divide(10, 0) does not throw or if the thrown
exception does not match std::invalid_argument. The same passing test is
included in code/tests/test_calculator.cpp.