Advertisement
Alaricy

ассерт с контейнерами если зайду в тупик

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