Advertisement
chevengur

СПРИНТ № 3 | Фреймворк для юнит-тестов | Урок 2: Создаём альтернативу assert

Nov 13th, 2023 (edited)
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void Assert(bool value, const string& hint) {
  8.     if(value == false){
  9.         if(hint.empty()){
  10.             cout << "Assertion failed." << endl;
  11.             abort();
  12.         }
  13.         else{
  14.             cout << "Assertion failed. Hint: " << hint << endl;
  15.             abort();
  16.         }
  17.     }
  18. }
  19.  
  20. int main() {
  21.     const string greeting = "Hello"s;
  22.     // Намеренная ошибка в условии, чтобы показать работу Assert
  23.     Assert(greeting.empty(), "Greeting must be non-empty"s);
  24.     // Следующая строка не должна выполниться, так как Assert аварийно завершит работу программы
  25.     cout << "This line will not be printed"s << endl;
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement