shushus

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

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