shushus

Спринт 3. Используем макросы и улучшаем фреймворк.

Dec 9th, 2022
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void Assert(bool value, const string& string_value, const string& file_name, const string& func_name, unsigned line_number, const string& hint) {
  9.     if (!value) {
  10.         cout << file_name << "("s << line_number << "): "s << func_name << ": "s;
  11.         cout << "ASSERT("s << string_value << ") failed."s;
  12.         if (!hint.empty()) {
  13.             cout << " Hint: "s << hint;
  14.         }
  15.         cout << endl;
  16.         abort();
  17.     }
  18. }
  19.  
  20. #define ASSERT(expr) Assert(expr, #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  21.  
  22. #define ASSERT_HINT(expr, hint) Assert(expr, #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  23.  
  24. int main() {
  25.     string hello = "hello"s;
  26.     ASSERT(!hello.empty());
  27.     ASSERT_HINT(2 + 2 == 5, "This will fail"s);
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment