Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include "entry.h"
  2. #include <vector>
  3. #include <iostream>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int error_counter = 0;
  11.     //konstruktor, operator* i operator(int)
  12.     vector<string> vals_1 { "Yossarian", "Mitsubishi", "Major" };
  13.     cout << "Testing constructor:" << endl;
  14.  
  15.     for (int i = 0; i < (int)vals_1.size(); i++)
  16.     {
  17.         entry test(vals_1[i]);
  18.         string test_1_1 = *test;
  19.         int test_1_2 = int(test);
  20.  
  21.         if (test_1_1 == vals_1[i] && test_1_2 == 0)
  22.             cout << test << " passed\n";
  23.         else
  24.         {
  25.             cout << "Problem with " << test<< ": " << test_1_1 << " seems to be wrong\n";
  26.             error_counter++;
  27.         }
  28.     }
  29.     //operator ++
  30.     vector<string> vals_2 { "Serpukhov", "Catch", "LV", "Fleetex", "HAL", "Indeks:"};
  31.     vector<int> ticks_1 {15, 22, 426, 831, 9000, 304523};
  32.     cout << "\nTesting operator++:" << endl;
  33.  
  34.     for (int i = 0; i < (int)vals_2.size(); i++)
  35.     {
  36.         entry test(vals_2[i]);
  37.         for (int j = 0; j < ticks_1[i]; j++)
  38.         {
  39.             test++;
  40.         }
  41.         string test_2_1 = *test;
  42.         int test_2_2 = int(test);
  43.  
  44.         if (test_2_1 == vals_2[i] && test_2_2 == ticks_1[i])
  45.             cout << test << " passed\n";
  46.         else
  47.         {
  48.             cout << "Problem with " << test << ": " << test_2_2 << "seems to be wrong t\n";
  49.             error_counter++;
  50.         }
  51.     }
  52.     //operator<
  53.     vector<string> vals_3 {"Major Major Major Major", "Chief White Halfoat", "Milo Minderbinder", "Scheisskopf" };
  54.     cout << "\nTesting operator<:" << endl;
  55.  
  56.     for (int i = 0; i < (int)vals_3.size() - 1; i++)
  57.     {
  58.         entry test(vals_3[i]);
  59.         entry test_1(vals_3[i + 1]);
  60.         if ((test < test_1) == (vals_3[i] < vals_3[i + 1]))
  61.             cout << test << " < " << test_1 << " passed\n";
  62.         else
  63.         {
  64.         cout << "Problem with " << test << " and " << test_1 << "\n";
  65.         error_counter++;
  66.         }
  67.  
  68.     }
  69.     //operator<<
  70.     vector<entry> vals_4 {{"Major"}, {"MiG"}, {"M"}, {"Million"}};
  71.     vector<int> ticks_2 {0, 29, 134, 1000000};
  72.     vector<string>result {"[Major 0]", "[MiG 29]", "[M 134]", "[Million 1000000]"};
  73.     cout << "\nTesting operator<<:" << endl;
  74.  
  75.     ostringstream ss;
  76.     for (int i = 0; i < (int)vals_4.size(); i++)
  77.     {
  78.     entry test(vals_4[i]);
  79.     for (int j = 0; j < ticks_2[i]; j++)
  80.     {
  81.         test++;
  82.     }
  83.     ss << test;
  84.     if(ss.str() == result[i])
  85.         cout << test << " passed\n";
  86.     else
  87.     {
  88.         cout << "Problem with " << test << "\n";
  89.         error_counter++;
  90.     }
  91.     ss.str("");
  92.     }
  93.     //operator>>
  94.     vector<string> entry_objects {"[Sukhoi 15]", "[Uranium 235]", "[Pip-Boy 3000]", "Error 1]", "[Error 2"};
  95.     vector<string> vals_5 {"Sukhoi", "Uranium", "Pip-Boy"};
  96.     vector<int> ticks_3 {15, 235, 3000, 1, 2};
  97.     cout << "\nTesting operator>>:" << endl;
  98.  
  99.     for (int i = 0; i < (int)entry_objects.size(); i++)
  100.     {
  101.         try
  102.         {
  103.             entry et("");
  104.             istringstream is;
  105.             is.str(entry_objects[i]);
  106.             is >> et;
  107.             if(*et == vals_5[i] && (int)et == ticks_3[i])
  108.                 cout << entry_objects[i] << " passed\n";
  109.              else
  110.             {
  111.                 cout << "Problem with " << entry_objects[i] << "\n";
  112.                 error_counter++;
  113.             }
  114.         }
  115.         catch(invalid_argument& err)
  116.         {
  117.             err.what();
  118.             cout << "Exception caught" << endl;
  119.         }
  120.     }
  121.  
  122.     cout << "\nErrors: " << error_counter;
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement