Advertisement
chevengur

СПРИНТ № 3 | Фреймворк для юнит-тестов | Урок 4: Учим фреймворк проверять элементы контейнеров

Nov 14th, 2023 (edited)
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. https://t.me/hitmyfeet канал с музыкой от начинающего программиста.
  2.  
  3. #include <cstdlib>
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. template <typename First, typename Second>
  14. ostream& operator<<(ostream& out, const pair<First, Second>& p) {
  15.     return out << p.first << ": "s << p.second;
  16. }
  17.  
  18. template <typename Container>
  19. void Print(ostream& out, const Container& container) {
  20.     bool is_first = true;
  21.     for (const auto& element : container) {
  22.         if (!is_first) {
  23.             out << ", "s;
  24.         }
  25.         is_first = false;
  26.         out << element;
  27.     }
  28. }
  29.  
  30. template <typename Element>
  31. ostream& operator<<(ostream& out, const vector<Element>& container) {
  32.     out << '[';
  33.     Print(out, container);
  34.     out << ']';
  35.     return out;
  36. }
  37.  
  38. template <typename Element>
  39. ostream& operator<<(ostream& out, const set<Element>& container) {
  40.     out << '{';
  41.     Print(out, container);
  42.     out << '}';
  43.     return out;
  44. }
  45.  
  46. template <typename Key, typename Value>
  47. ostream& operator<<(ostream& out, const map<Key, Value>& container) {
  48.     out << '{';
  49.     Print(out, container);
  50.     out << '}';
  51.     return out;
  52. }
  53.  
  54.  
  55. template <typename T, typename U>
  56. void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
  57.                      const string& func, unsigned line, const string& hint) {
  58.     if (t != u) {
  59.         cout << boolalpha;
  60.         cout << file << "("s << line << "): "s << func << ": "s;
  61.         cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
  62.         cout << t << " != "s << u << "."s;
  63.         if (!hint.empty()) {
  64.             cout << " Hint: "s << hint;
  65.         }
  66.         cout << endl;
  67.         abort();
  68.     }
  69. }
  70.  
  71. #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
  72.  
  73. #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
  74.  
  75. void AssertImpl(bool value, const string& expr_str, const string& file, const string& func, unsigned line,
  76.                 const string& hint) {
  77.     if (!value) {
  78.         cout << file << "("s << line << "): "s << func << ": "s;
  79.         cout << "ASSERT("s << expr_str << ") failed."s;
  80.         if (!hint.empty()) {
  81.             cout << " Hint: "s << hint;
  82.         }
  83.         cout << endl;
  84.         abort();
  85.     }
  86. }
  87.  
  88. #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  89.  
  90. #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  91.  
  92. vector<int> TakeEvens(const vector<int>& numbers) {
  93.     vector<int> evens;
  94.     for (int x : numbers) {
  95.         if (x % 2 == 0) {
  96.             evens.push_back(x);
  97.         }
  98.     }
  99.     return evens;
  100. }
  101.  
  102. map<string, int> TakeAdults(const map<string, int>& people) {
  103.     map<string, int> adults;
  104.     for (const auto& [name, age] : people) {
  105.         if (age >= 18) {
  106.             adults[name] = age;
  107.         }
  108.     }
  109.     return adults;
  110. }
  111.  
  112. bool IsPrime(int n) {
  113.     if (n < 2) {
  114.         return false;
  115.     }
  116.     int i = 2;
  117.     while (i * i <= n) {
  118.         if (n % i == 0) {
  119.             return false;
  120.         }
  121.         ++i;
  122.     }
  123.     return true;
  124. }
  125.  
  126. set<int> TakePrimes(const set<int>& numbers) {
  127.     set<int> primes;
  128.     for (int number : numbers) {
  129.         if (IsPrime(number)) {
  130.             primes.insert(number);
  131.         }
  132.     }
  133.     return primes;
  134. }
  135.  
  136. int main() {
  137.     {
  138.         const set<int> numbers = {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  139.         const set<int> expected_primes = {2, 3, 5, 7, 11, 13};
  140.         ASSERT_EQUAL(TakePrimes(numbers), expected_primes);
  141.     }
  142.  
  143.     {
  144.         const map<string, int> people = {{"Ivan"s, 19}, {"Sergey"s, 16}, {"Alexey"s, 18}};
  145.         const map<string, int> expected_adults = {{"Alexey"s, 18}, {"Ivan"s, 19}};
  146.         ASSERT_EQUAL(TakeAdults(people), expected_adults);
  147.     }
  148.  
  149.     {
  150.         const vector<int> numbers = {3, 2, 1, 0, 3, 6};
  151.         const vector<int> expected_evens = {2, 0, 6};
  152.         ASSERT_EQUAL(TakeEvens(numbers), expected_evens);
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement