Advertisement
RobertDeMilo

Теория. Учим фреймворк проверять элементы контейнеров

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