/* Test code for slist.h. To use: g++ slistest.cpp a.out < file Input is any arbitrary text file (not empty). Output should always be: Test 1 passes Test 2 passes Test 3 passes Test 1 passes Test 2 passes Test 3 passes Test 1 passes Test 2 passes Test 3 passes Test 1 passes Test 2 passes Test 3 passes */ #include "slist.h" // Your file // #define Slist list // Or use instead of the above to verify the test code #include #include #include #include using namespace std; // Test 2 arbitrary Slists template void test(const Slist& a, const Slist& b) { // Test 1: copy, assignment, comparison, size(), iterators Slist c=a, d, e; d=b; if (a.size()==c.size() && b.size()==d.size() && a==c && b==d && !(a!=c) && !(bc) && !(d>b) && (c<=a) && (b>=d) && ((ab))==1 && ((c<=d)+(d!=c)+(c>=d))==2 && equal(a.begin(), a.end(), c.begin()) && equal(d.begin(), d.end(), b.begin())) cout << "Test 1 passes\n"; else cout << "Test 1 fails\n"; // Test 2: assignment, front(), push_front(), empty() Slist f; bool c1, c2, c3, c4; e=Slist(); // empty e.push_front(a.front()); f=e; f.push_front(a.front()); c1 = (e.size()==1 && f.size()==2 && e.front()==f.front() && e!=f); f.pop_front(); c2 = (e==f && f.size()==1 && e.front()==f.front()); f.front()=b.front(); c3 = (f.size()==1 && f.front()==b.front()); while (!e.empty()) e.pop_front(); const Slist g=e; c4 = (e.empty() && g.empty()); if (c1 && c2 && c3 && c4) cout << "Test 2 passes\n"; else cout << "Test 2 fails\n"; // Test 3: iterators Slist::iterator p=c.begin(), q; e=c; q=e.begin(); c1=true; while (p!=c.end() && !(e.end()==q)) { // compare c and e c1 &= (*p++ == *q); ++q; } if (c1) cout << "Test 3 passes\n"; else cout << "Test 3 fails\n"; } int main() { Slist a, b; Slist c; string s; while (cin >> s) { a.push_front(s); // Read a list of words c.push_front(a.begin()->size()); // and their sizes (and test -> ) } b = a; // Assignment Slist d = c; // Copy test(a, b); // Test identical lists test(c, d); a.push_front("foo"); test(a, b); // Test different lists b.push_front("bar"); test(b, a); // Test different lists of the same size return 0; }