languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

assignment_3_unit_test.cpp (14062B)


      1 /*
      2  * Name        : assignment_3_unit_test.cpp
      3  * Author      : Luke Sathrum
      4  * Description : Unit Test for class TodoItem and TodoList.
      5  *               THIS FILE SHOUD NOT BE ALTERED, UNLESS DEBUGGING IN MAIN
      6  */
      7 
      8 #include <iostream>
      9 #include <sstream>
     10 #include <streambuf>
     11 #include "todo_list.h"
     12 #include "todo_item.h"
     13 using std::cout;
     14 using std::endl;
     15 using std::string;
     16 using std::stringstream;
     17 
     18 // For testing (DO NOT ALTER)
     19 #include <cctype>
     20 #include <vector>
     21 void UnitTest();
     22 template <typename T>
     23 void Test(bool test, string more_info = "", T yours = T(),
     24           T expected = T());
     25 void OutputFailedTests();
     26 unsigned int ut_passed = 0, ut_failed = 0, ut_total = 0, num_of_tests = 53;
     27 std::vector<int> failed_tests;
     28 
     29 // Program Execution Starts Here
     30 int main() {
     31   // START DEBUGGING CODE
     32 
     33   // END DEBUGINH CODE
     34   // To test your code (DO NOT ALTER)
     35   UnitTest();
     36   // This ends program execution
     37   return 0;
     38 }
     39 
     40 
     41 // For testing (DO NOT ALTER)
     42 void UnitTest() {
     43   cout << string(40, '-') << endl;
     44   cout << "UNIT TEST:\n" << string(40, '-') << endl;
     45   if (num_of_tests != 0)
     46     cout << "Total Number of Tests: " << num_of_tests << endl;
     47 
     48   // Tests
     49   string desc1 = "Go to Store";
     50   string desc2 = "Brush Teeth @ 8PM";
     51   string expected, yours;
     52   string to_file;
     53 
     54   cout << "*****TodoItem(\"Go to Store\")*****\n";
     55   TodoItem item(desc1);
     56   Test(item.description() == desc1, "description()", item.description(), desc1);
     57   Test(item.priority() == 1, "priority()", item.priority(), 1);
     58   Test(item.completed() == false, "completed()", item.completed(), false);
     59   to_file = "Go to Store@1@0";
     60   Test(item.ToFile() == to_file, "ToFile()", item.ToFile(), to_file);
     61 
     62   cout << "\n*****TodoItem(\"Go to Store\", 3)*****\n";
     63   TodoItem item2(desc1, 3);
     64   Test(item2.description() == desc1, "description()", item2.description(),
     65        desc1);
     66   Test(item2.priority() == 3, "priority()", item2.priority(), 3);
     67   Test(item2.completed() == false, "completed()", item2.completed(), false);
     68   to_file = "Go to Store@3@0";
     69   Test(item2.ToFile() == to_file, "ToFile()", item2.ToFile(), to_file);
     70 
     71   cout << "\n*****TodoItem(\"Go to Store\", 2, true)*****\n";
     72   TodoItem item3(desc1, 2, true);
     73   Test(item3.description() == desc1, "description()", item3.description(),
     74        desc1);
     75   Test(item3.priority() == 2, "priority()", item3.priority(), 2);
     76   Test(item3.completed() == true, "completed()", item3.completed(), true);
     77   to_file = "Go to Store@2@1";
     78   Test(item3.ToFile() == to_file, "ToFile()", item3.ToFile(), to_file);
     79 
     80   cout << "\n*****TodoItem Mutators*****\n";
     81   item.set_description(desc2);
     82   Test(item.description() == desc2,
     83        "set_description(\"Brush Teeth @ 8PM\") / description()",
     84        item.description(), desc2);
     85   item.set_priority(4);
     86   Test(item.priority() == 4, "set_priority(4) / priority()", item.priority(),
     87        4);
     88   item.set_priority(0);
     89   Test(item.priority() == 5, "set_priority(0) / priority()", item.priority(),
     90        5);
     91   item.set_priority(6);
     92   Test(item.priority() == 5, "set_priority(6) / priority()", item.priority(),
     93        5);
     94   item.set_completed(true);
     95   Test(item.completed() == true, "set_completed(true) / completed()",
     96        item.completed(), true);
     97   to_file = "Brush Teeth ` 8PM@5@1";
     98   Test(item.ToFile() == to_file, "ToFile()", item.ToFile(), to_file);
     99 
    100   cout << "\n*****TodoList Constructor*****\n";
    101   TodoList list;
    102   Test(list.GetSize() == 0, "GetSize()", list.GetSize(),
    103        static_cast<unsigned int>(0));
    104   Test(list.GetCapacity() == 25, "GetCapacity()", list.GetCapacity(),
    105        static_cast<unsigned int>(25));
    106   Test(list.ToFile() == "", "ToFile()", list.ToFile(), string(""));
    107 
    108   cout << "\n*****TodoList Member Functions with 1 Item*****\n";
    109   list.AddItem(new TodoItem(desc1));
    110   Test(list.GetSize() == 1, "AddItem(TodoItem(\"Go to Store\")) / GetSize()",
    111        list.GetSize(), static_cast<unsigned int>(1));
    112   Test(list.GetCapacity() == 25, "GetCapacity()", list.GetCapacity(),
    113        static_cast<unsigned int>(25));
    114   Test(list.GetItem(1)->ToFile() == string("Go to Store@1@0"),
    115        "GetItem(1)->ToFile()", list.GetItem(1)->ToFile(),
    116        string("Go to Store@1@0"));
    117   Test(list.ToFile() == "Go to Store@1@0\n", "ToFile()", list.ToFile(),
    118        string("Go to Store@1@0\n"));
    119   cout << "Testing Overloaded <<:\n" << list << endl;
    120 
    121   cout << "\n*****TodoList Member Functions with 2 Items*****\n";
    122   list.AddItem(new TodoItem(desc1, 2, true));
    123   Test(list.GetSize() == 2,
    124        "AddItem(TodoItem(\"Go to Store\", 2, true)) / GetSize()",
    125        list.GetSize(), static_cast<unsigned int>(2));
    126   Test(list.GetCapacity() == 25, "GetCapacity()", list.GetCapacity(),
    127        static_cast<unsigned int>(25));
    128   yours = list.GetItem(1)->ToFile();
    129   expected = "Go to Store@1@0";
    130   Test(yours == expected, "GetItem(1)->ToFile()", yours, expected);
    131   yours = list.GetItem(2)->ToFile();
    132   expected = "Go to Store@2@1";
    133   Test(yours == expected, "GetItem(2)->ToFile()", yours, expected);
    134   yours = list.ToFile();
    135   expected = "Go to Store@1@0\nGo to Store@2@1\n";
    136   Test(yours == expected, "ToFile()", yours, expected);
    137   list.DeleteItem(1);
    138   Test(list.GetSize() == 1, "DeleteItem(1) / GetSize()", list.GetSize(),
    139        static_cast<unsigned int>(1));
    140   Test(list.GetCapacity() == 25, "GetCapacity()", list.GetCapacity(),
    141        static_cast<unsigned int>(25));
    142   cout << "Testing Overloaded <<:\n" << list << endl;
    143 
    144   cout << "\n*****TodoList Member Functions with 25 Items*****\n";
    145   list.AddItem(new TodoItem(desc2, 4, true));
    146   stringstream ss;
    147   for (int i = 0; i < 23; i++) {
    148     ss << "Description #" << i + 3;
    149     list.AddItem(new TodoItem(ss.str(), i % 5 + 1));
    150     ss.str("");
    151   }
    152   Test(list.GetSize() == 25, "AddItem(Adding 24 More Items) / GetSize()",
    153        list.GetSize(), static_cast<unsigned int>(25));
    154   Test(list.GetCapacity() == 25, "GetCapacity()", list.GetCapacity(),
    155        static_cast<unsigned int>(25));
    156   yours = list.GetItem(17)->ToFile();
    157   expected = "Description #17@5@0";
    158   Test(yours == expected, "GetItem(17)->ToFile()", yours, expected);
    159   yours = list.GetItem(25)->ToFile();
    160   expected = "Description #25@3@0";
    161   Test(yours == expected, "GetItem(25)->ToFile()", yours, expected);
    162   yours = list.ToFile();
    163   expected =
    164       "Go to Store@2@1\nBrush Teeth ` 8PM@4@1\nDescription #3@1@0\n"
    165           "Description #4@2@0\nDescription #5@3@0\nDescription #6@4@0\n"
    166           "Description #7@5@0\nDescription #8@1@0\nDescription #9@2@0\n"
    167           "Description #10@3@0\nDescription #11@4@0\nDescription #12@5@0\n"
    168           "Description #13@1@0\nDescription #14@2@0\nDescription #15@3@0\n"
    169           "Description #16@4@0\nDescription #17@5@0\nDescription #18@1@0\n"
    170           "Description #19@2@0\nDescription #20@3@0\nDescription #21@4@0\n"
    171           "Description #22@5@0\nDescription #23@1@0\nDescription #24@2@0\n"
    172           "Description #25@3@0\n";
    173   Test(yours == expected, "ToFile()", yours, expected);
    174   cout << "Testing Overloaded <<:\n" << list << endl;
    175 
    176   list.DeleteItem(15);
    177   Test(list.GetSize() == 24, "DeleteItem(15) / GetSize()", list.GetSize(),
    178        static_cast<unsigned int>(24));
    179   Test(list.GetCapacity() == 25, "GetCapacity()", list.GetCapacity(),
    180        static_cast<unsigned int>(25));
    181   yours = list.GetItem(10)->ToFile();
    182   expected = "Description #10@3@0";
    183   Test(yours == expected, "GetItem(10)->ToFile()", yours, expected);
    184   yours = list.GetItem(20)->ToFile();
    185   expected = "Description #21@4@0";
    186   Test(yours == expected, "GetItem(20)->ToFile()", yours, expected);
    187   yours = list.ToFile();
    188   expected = "Go to Store@2@1\nBrush Teeth ` 8PM@4@1\nDescription #3@1@0\n"
    189       "Description #4@2@0\nDescription #5@3@0\nDescription #6@4@0\n"
    190       "Description #7@5@0\nDescription #8@1@0\nDescription #9@2@0\n"
    191       "Description #10@3@0\nDescription #11@4@0\nDescription #12@5@0\n"
    192       "Description #13@1@0\nDescription #14@2@0\n"
    193       "Description #16@4@0\nDescription #17@5@0\nDescription #18@1@0\n"
    194       "Description #19@2@0\nDescription #20@3@0\nDescription #21@4@0\n"
    195       "Description #22@5@0\nDescription #23@1@0\nDescription #24@2@0\n"
    196       "Description #25@3@0\n";
    197   Test(yours == expected, "ToFile()", yours, expected);
    198   cout << "Testing Overloaded <<:\n" << list << endl;
    199 
    200   cout << "\n*****TodoList Member Functions with 30 Items*****\n";
    201   for (int i = 25; i <= 30; i++) {
    202     ss << "New Description #" << i;
    203     list.AddItem(new TodoItem(ss.str(), i % 5 + 1, true));
    204     ss.str("");
    205   }
    206   Test(list.GetSize() == 30, "AddItem(Adding 6 More Items) / GetSize()",
    207        list.GetSize(), static_cast<unsigned int>(30));
    208   Test(list.GetCapacity() == 35, "GetCapacity()", list.GetCapacity(),
    209        static_cast<unsigned int>(35));
    210   yours = list.GetItem(11)->ToFile();
    211   expected = "Description #11@4@0";
    212   Test(yours == expected, "GetItem(11)->ToFile()", yours, expected);
    213   yours = list.GetItem(30)->ToFile();
    214   expected = "New Description #30@1@1";
    215   Test(yours == expected, "GetItem(30)->ToFile()", yours, expected);
    216   yours = list.ToFile();
    217   expected = "Go to Store@2@1\nBrush Teeth ` 8PM@4@1\nDescription #3@1@0\n"
    218       "Description #4@2@0\nDescription #5@3@0\nDescription #6@4@0\n"
    219       "Description #7@5@0\nDescription #8@1@0\nDescription #9@2@0\n"
    220       "Description #10@3@0\nDescription #11@4@0\nDescription #12@5@0\n"
    221       "Description #13@1@0\nDescription #14@2@0\n"
    222       "Description #16@4@0\nDescription #17@5@0\nDescription #18@1@0\n"
    223       "Description #19@2@0\nDescription #20@3@0\nDescription #21@4@0\n"
    224       "Description #22@5@0\nDescription #23@1@0\nDescription #24@2@0\n"
    225       "Description #25@3@0\nNew Description #25@1@1\nNew Description #26@2@1\n"
    226       "New Description #27@3@1\nNew Description #28@4@1\n"
    227       "New Description #29@5@1\nNew Description #30@1@1\n";
    228   Test(yours == expected, "ToFile()", yours, expected);
    229   cout << "Testing Overloaded <<:\n" << list << endl;
    230 
    231   cout << "\n*****Sorting TodoList with 30 Items*****\n";
    232   list.Sort();
    233   bool sorted = true;
    234   for (int i = 1; i <= 30; i++) {
    235     if ((i <= 7) && (list.GetItem(i)->priority()) != 1)
    236       sorted = false;
    237     else if ((i > 7 && i <= 14) && (list.GetItem(i)->priority()) != 2)
    238       sorted = false;
    239     else if ((i > 14 && i <= 19) && (list.GetItem(i)->priority()) != 3)
    240       sorted = false;
    241     else if ((i > 19 && i <= 25) && (list.GetItem(i)->priority()) != 4)
    242       sorted = false;
    243     else if ((i > 25 && i <= 30) && (list.GetItem(i)->priority()) != 5)
    244       sorted = false;
    245   }
    246   yours = list.ToFile();
    247   expected = "Description #3@1@0\nDescription #8@1@0\n"
    248               "Description #13@1@0\nDescription #18@1@0\n"
    249               "Description #23@1@0\nNew Description #25@1@1\n"
    250               "New Description #30@1@1\nGo to Store@2@1\n"
    251               "Description #4@2@0\nDescription #9@2@0\n"
    252               "Description #14@2@0\nDescription #19@2@0\n"
    253               "Description #24@2@0\nNew Description #26@2@1\n"
    254               "Description #5@3@0\nDescription #10@3@0\n"
    255               "Description #20@3@0\nDescription #25@3@0\n"
    256               "New Description #27@3@1\nBrush Teeth ` 8PM@4@1\n"
    257               "Description #6@4@0\nDescription #11@4@0\n"
    258               "Description #16@4@0\nDescription #21@4@0\n"
    259               "New Description #28@4@1\nDescription #7@5@0\n"
    260               "Description #12@5@0\nDescription #17@5@0\n"
    261               "Description #22@5@0\nNew Description #29@5@1\n";
    262 
    263   Test(sorted && yours == expected, "Sort()", yours, expected);
    264   cout << "Testing Overloaded <<:\n" << list << endl;
    265 
    266   cout << "\n*****TodoList Member Functions with 1000 Items*****\n";
    267   for (int i = 0; i < 970; i++)
    268     list.AddItem(new TodoItem("A", 1, true));
    269   Test(list.GetSize() == 1000, "AddItem(Adding 970 More Items) / GetSize()",
    270        list.GetSize(), static_cast<unsigned int>(1000));
    271   Test(list.GetCapacity() == 1005, "GetCapacity()", list.GetCapacity(),
    272        static_cast<unsigned int>(1005));
    273   cout << "**Deleting All Items**\n";
    274   for (int i = 1000; i >= 1; i--)
    275     list.DeleteItem(i);
    276   Test(list.GetSize() == 0, "DeleteItem() / GetSize()", list.GetSize(),
    277        static_cast<unsigned int>(0));
    278   Test(list.GetCapacity() == 1005, "GetCapacity()", list.GetCapacity(),
    279        static_cast<unsigned int>(1005));
    280 
    281   // Testing Destructors
    282   cout << "\n*****Testing Destructors*****" << endl
    283        << "If the next line is the \"END Testing Destructors\" then you passed!"
    284        << endl;
    285   TodoItem *dynamic_item = new TodoItem("testing");
    286   delete dynamic_item;
    287   dynamic_item = new TodoItem("testing2", 5);
    288   delete dynamic_item;
    289   dynamic_item = new TodoItem("Testing3", 3, true);
    290   TodoList *dynamic_list = new TodoList();
    291   delete dynamic_list;
    292   dynamic_list = new TodoList();
    293   for (int i = 0; i < 50; i++)
    294     dynamic_list->AddItem(new TodoItem("testing"));
    295   delete dynamic_list;
    296   cout << "*****END Testing Destructors*****" << endl;
    297   Test(true, "Destructors", 0, 0);
    298 
    299   cout << string(40, '-') << endl;
    300   cout << "Passed: " << ut_passed << " / " << ut_total << endl;
    301   OutputFailedTests();
    302   cout << string(40, '-') << endl;
    303   cout << "END OF UNIT TEST!\n";
    304   cout << string(40, '-') << endl;
    305   cout << "Be sure to run 'make style' to check for any style errors.\n"
    306        << "Please note that 'make style' does NOT check variable names or"
    307        << " indentation" << endl << endl;
    308 }
    309 
    310 // For testing (DO NOT ALTER)
    311 template <typename T>
    312 void Test(bool test, string more_info, T yours, T expected) {
    313   ut_total++;
    314   if (test) {
    315     cout << "PASSED TEST ";
    316     ut_passed++;
    317   } else {
    318     cout << "FAILED TEST ";
    319     ut_failed++;
    320     failed_tests.push_back(ut_total);
    321   }
    322   cout << ut_total << " " << more_info << "!" << endl;
    323   if (!test && yours != T()) {
    324       cout << "Expected: " << std::boolalpha << expected << endl;
    325       cout << "Yours   : " << std::boolalpha << yours  << endl << endl;
    326   }
    327 }
    328 
    329 void OutputFailedTests() {
    330   if (failed_tests.size()) {
    331     cout << "Failed test number(s): ";
    332     for (unsigned int i = 0; i < failed_tests.size() - 1; i++)
    333       cout << failed_tests.at(i) << ", ";
    334     cout << failed_tests.at(failed_tests.size() - 1) << endl;
    335   }
    336 }
    337