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" << std::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 ==
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 <iostream>
#define ASSERT(CONDITION, MSG) \
if (!(CONDITION)) std::cerr << "ASSERT failed: "<< MSG << std::endl; \
else std::cerr << "ASSERT passed" << std::endl;
#endif
Tester.cpp
#include <iostream> #include <iomanip> #include <string> #include <cmath> 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; }
Trending now
This is a popular solution!
Step by step
Solved in 2 steps