#include #include #include #include "unrolled.h" using std::cout; using std::endl; using std::list; // =================================================================== // This function compares an unrolled linked list to an STL list. It // returns true iff the lists are the same size, and contain the same // elements in the same order. template bool SAME(UnrolledLL& a, list &b) { if (a.size() != b.size()) return false; typename UnrolledLL::iterator a_iter = a.begin(); list::iterator b_iter = b.begin(); while (a_iter != a.end() && b_iter != b.end()) { if (*a_iter != *b_iter) return false; a_iter++; b_iter++; } assert (a_iter == a.end() && b_iter == b.end()); return true; } // =================================================================== void BasicTests(); void MoreTests(); int main() { BasicTests(); MoreTests(); } // =================================================================== // NOTE: Your internal data structure may be different (& this print // differently), depending on how you implement your internal member // functions. That's ok! void BasicTests() { // make two matching list of integers, one using an unrolled list, // one using an STL list. They should stay the "SAME" throughout // these tests. UnrolledLL a; list b; for (int i = 10; i < 30; ++i) { a.push_back(i); b.push_back(i); } // iterate through the integers and print them out cout << "the integers from 10->29" << endl; for (UnrolledLL::iterator itr = a.begin(); itr != a.end(); itr++) { cout << " " << *itr; } cout << endl << endl; assert (SAME(a,b)); // use the output operator to print the underlying representation cout << "initial" << endl << a << endl; // testing some basic functions in the class cout << "some editing with pop & push" << endl; assert (a.size() == 20); assert (a.front() == 10); assert (a.back() == 29); a.pop_front(); b.pop_front(); assert (a.size() == 19); assert (a.front() == 11); a.pop_back(); b.pop_back(); assert (a.size() == 18); assert (a.back() == 28); cout << a << endl; assert (SAME(a,b)); // more editing cout << "more editing with pop & push" << endl; a.pop_front(); a.pop_front(); a.pop_front(); a.pop_front(); a.pop_front(); b.pop_front(); b.pop_front(); b.pop_front(); b.pop_front(); b.pop_front(); cout << a; a.pop_back(); b.pop_back(); cout << a; assert (a.size() == 12); assert (a.front() == 16); assert (a.back() == 27); a.push_front(90); a.push_front(91); a.push_front(92); a.push_front(93); b.push_front(90); b.push_front(91); b.push_front(92); b.push_front(93); cout << a << endl; assert (a.size() == 16); assert (a.front() == 93); assert (SAME(a,b)); // erase the multiples of 3 cout <<"erase the multiples of 3" << endl; UnrolledLL::iterator a_iter = a.begin(); while (a_iter != a.end()) { if (*a_iter % 3 == 0) { a_iter = a.erase(a_iter); } else { a_iter++; } } list::iterator b_iter = b.begin(); while (b_iter != b.end()) { if (*b_iter % 3 == 0) { b_iter = b.erase(b_iter); } else { b_iter++; } } cout << a << endl; assert (a.size() == 10); assert (SAME(a,b)); // inserting elements cout << "do some inserts" << endl; // insert some stuff for (UnrolledLL::iterator itr = a.begin(); itr != a.end(); itr++) { if (*itr == 92 || *itr == 16 || *itr == 19 || *itr == 26) { itr = a.insert(itr,77); itr++; } } for (list::iterator itr = b.begin(); itr != b.end(); itr++) { if (*itr == 92 || *itr == 16 || *itr == 19 || *itr == 26) { itr = b.insert(itr,77); itr++; } } cout << a << endl; assert (a.size() == 14); assert (SAME(a,b)); // overflowing an insert cout << "insert that overflows the node" << endl; for (UnrolledLL::iterator itr = a.begin(); itr != a.end(); itr++) { if (*itr == 17) { itr = a.insert(itr,88); itr++; } } for (list::iterator itr = b.begin(); itr != b.end(); itr++) { if (*itr == 17) { itr = b.insert(itr,88); itr++; } } cout << a << endl; assert (a.size() == 15); assert (SAME(a,b)); // more erasing cout << "erasing that removes a node" << endl; a_iter = a.begin(); while (a_iter != a.end()) { if (*a_iter == 77 || *a_iter == 16 || *a_iter == 88) { a_iter = a.erase(a_iter); } else { a_iter++; } } b_iter = b.begin(); while (b_iter != b.end()) { if (*b_iter == 77 || *b_iter == 16 || *b_iter == 88) { b_iter = b.erase(b_iter); } else { b_iter++; } } cout << a << endl; assert (a.size() == 9); assert (SAME(a,b)); } // =================================================================== void MoreTests() { // // // ADD YOUR OWN TEST CASES HERE // // }