Macro parameters can be converted to strings inside the macro, a process called stringizing. Use that to modify the ASSERT macro so that it prints the assertion which failed.
#ifndef MYASSERT_H#define MYASSERT_H
#include #define ASSERT(CONDITION, MSG) \if (!(CONDITION)) std::cerr < "assert="" failed:="">< msg="">< std::endl;="">else std::cerr < "assert="" passed"=""><>#endifTester.cpp#include #include #include #include using namespace std; #include "myassert.h" int main() { double n = 1 / 4; ASSERT(n == .25, "Invalid division"); cout < "expected:="" assert="" failed:="" n="=" .25:="" invalid="" division"="">< endl;="" double="" x="12" %="" 5;="" assert(x="=" 2.0,="" "invalid="" remainder");="" cout="">< "expected:="" assert="" passed:="" x="=" 2.0"="">< endl;="">
#endif
Tester.cpp
#include #include #include #include using namespace std; #include "myassert.h" int main() { double n = 1 / 4; ASSERT(n == .25, "Invalid division"); cout < "expected:="" assert="" failed:="" n="=" .25:="" invalid="" division"="">< endl;="" double="" x="12" %="" 5;="" assert(x="=" 2.0,="" "invalid="" remainder");="" cout="">< "expected:="" assert="" passed:="" x="=" 2.0"="">< endl;="">
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here