1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include "sl_list.h"
#include "sl_node.h"
#include <iostream>
#include <sstream>
using std::cout;
using std::endl;
using std::string;
// For testing (DO NOT ALTER)
#include <cctype>
#include <vector>
void UnitTest();
void Test(bool test, int line_number, string more_info = "", string yours = "!",
string actual = "!");
void OutputFailedTests();
unsigned int ut_passed = 0, ut_failed = 0, ut_total = 0, num_of_tests = 46;
std::vector<int> failed_tests;
// Program Execution Starts Here
int main() {
// To test your code (DO NOT ALTER)
UnitTest();
// This ends program execution
return 0;
}
// For testing (DO NOT ALTER)
void UnitTest() {
cout << string(40, '-') << endl;
cout << "UNIT TEST:\n" << string(40, '-') << endl;
if (num_of_tests != 0)
cout << "Total Number of Tests: " << num_of_tests << endl;
string yours = "", actual = "";
// Tests
SLList list;
Test(list.size() == 0, __LINE__, "Default Constructor & size()");
yours = list.ToString();
actual = "";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
Test(list.GetHead() == 0, __LINE__, "GetHead()");
Test(list.GetTail() == 0, __LINE__, "GetTail()");
list.Insert(10);
Test(list.size() == 1, __LINE__, "Insert(10) & size()");
yours = list.ToString();
actual = "10";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
list.Insert(50);
Test(list.size() == 2, __LINE__, "Insert(50) & size()");
yours = list.ToString();
actual = "10, 50";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
list.Insert(30);
Test(list.size() == 3, __LINE__, "Insert(30) & size()");
yours = list.ToString();
actual = "10, 30, 50";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
Test(list.GetHead() == 10, __LINE__, "GetHead()");
Test(list.GetTail() == 50, __LINE__, "GetTail()");
list.Insert(5);
Test(list.size() == 4, __LINE__, "Insert(5) & size()");
yours = list.ToString();
actual = "5, 10, 30, 50";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
list.Insert(55);
Test(list.size() == 5, __LINE__, "Insert(55) & size()");
yours = list.ToString();
actual = "5, 10, 30, 50, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
list.Insert(20);
Test(list.size() == 6, __LINE__, "Insert(20) & size()");
yours = list.ToString();
actual = "5, 10, 20, 30, 50, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
list.Insert(40);
Test(list.size() == 7, __LINE__, "Insert(40) & size()");
yours = list.ToString();
actual = "5, 10, 20, 30, 40, 50, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
list.Insert(30);
Test(list.size() == 8, __LINE__, "Insert(30) & size()");
yours = list.ToString();
actual = "5, 10, 20, 30, 30, 40, 50, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
list.Insert(5);
Test(list.size() == 9, __LINE__, "Insert(5) & size()");
yours = list.ToString();
actual = "5, 5, 10, 20, 30, 30, 40, 50, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
list.Insert(55);
Test(list.size() == 10, __LINE__, "Insert(55) & size()");
yours = list.ToString();
actual = "5, 5, 10, 20, 30, 30, 40, 50, 55, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
cout << "\nYour List: " << list.ToString() << endl << endl;
Test(list.RemoveFirstOccurence(1) == false, __LINE__,
"RemoveFirstOccurence(1)");
Test(list.RemoveFirstOccurence(5) == true, __LINE__,
"RemoveFirstOccurence(5)");
Test(list.size() == 9, __LINE__, "size()");
yours = list.ToString();
actual = "5, 10, 20, 30, 30, 40, 50, 55, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
Test(list.RemoveFirstOccurence(30) == true, __LINE__,
"RemoveFirstOccurence(30)");
Test(list.size() == 8, __LINE__, "size()");
yours = list.ToString();
actual = "5, 10, 20, 30, 40, 50, 55, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
Test(list.RemoveFirstOccurence(30) == true, __LINE__,
"RemoveFirstOccurence(30)");
Test(list.size() == 7, __LINE__, "size()");
yours = list.ToString();
actual = "5, 10, 20, 40, 50, 55, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
Test(list.RemoveFirstOccurence(55) == true, __LINE__,
"RemoveFirstOccurence(55)");
Test(list.size() == 6, __LINE__, "size()");
yours = list.ToString();
actual = "5, 10, 20, 40, 50, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
Test(list.RemoveFirstOccurence(10) == true, __LINE__,
"RemoveFirstOccurence(10)");
Test(list.size() == 5, __LINE__, "size()");
yours = list.ToString();
actual = "5, 20, 40, 50, 55";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
Test(list.GetHead() == 5, __LINE__, "GetHead()");
Test(list.GetTail() == 55, __LINE__, "GetTail()");
list.Clear();
Test(list.size() == 0, __LINE__, "Clear() & size()");
yours = list.ToString();
actual = "";
Test(yours == actual, __LINE__, "ToString()", yours, actual);
cout << string(40, '-') << endl;
cout << "Passed: " << ut_passed << " / " << ut_total << endl;
OutputFailedTests();
cout << string(40, '-') << endl;
cout << "END OF UNIT TEST!\n";
cout << string(40, '-') << endl;
cout << "Be sure to run 'make style' to check for any style errors.\n"
<< "Please note that 'make style' does NOT check variable names or"
<< " indentation" << endl << endl;
}
// For testing (DO NOT ALTER)
void Test(bool test, int line_number, string more_info, string yours,
string actual) {
ut_total++;
if (test) {
cout << "PASSED TEST ";
ut_passed++;
} else {
cout << "FAILED TEST ";
ut_failed++;
failed_tests.push_back(ut_total);
}
cout << ut_total << " " << more_info << "!" << endl;
if (!test) {
if (yours != "!")
cout << "Yours: \"" << yours << '"' << endl;
if (actual != "!")
cout << "Actual: \"" << actual << '"' << endl;
cout << " Check Line " << line_number << " for more info" << endl;
}
}
void OutputFailedTests() {
if (failed_tests.size()) {
cout << "Failed test number(s): ";
for (unsigned int i = 0; i < failed_tests.size() - 1; i++)
cout << failed_tests.at(i) << ", ";
cout << failed_tests.at(failed_tests.size() - 1) << endl;
}
}
|