/* settest.cpp -- Matt Mahoney, mmahoney@cs.fit.edu Test code for Set in assignment 4 of http://cs.fit.edu/~mmahoney/cse2050/ Usage: settest [file [word]] When run with no arguments, tests all set operations. With a file name argument, it also reads the words of a file into a Set and the word lengths into a Set and prints them. When a word is given as the second argument, tests whether that word is in the file. For example, if the file "test.txt" contains "this is a test", then settest test.txt this should produce the following output: Words in test.txt = {a,is,test,this} Word lengths = {1,2,4} 3 lengths and 4 different words this is in test.txt Output should be as follows: {}{1}{1,2}{1,2,3}{1,2,3,4,5} {1}{2,3}{1,2,3}{1}{}{}{} {}{1,2}{2}{2} Actual output is: {}{1}{1,2}{1,2,3}{1,2,3,4,5} {1}{2,3}{1,2,3}{1}{}{}{} {}{1,2}{2}{2} Next 7 tests should pass Test 1 passes Test 2 passes Test 3 passes Test 4 passes Test 5 passes Test 6 passes Test 7 passes */ #include #include #include #include "set.h" // Your file goes here using namespace std; // Pass test n if x < y #define test(x, y, n) \ if (x <= y && x < y && x != y && !(x == y) && !(x >= y) && !(x > y) \ && !(y <= x) && !(y < x) && y != x && !(y == x) && y >= x && y > x) \ cout << "Test " << n << " passes\n"; \ else \ cout << "Test " << n << " fails\n"; \ // Pass test n if x == y #define testeq(x, y, n) \ if (x == y && !(y != x) && x <= y && x >= y && !(x < y) && !(x > y)) \ cout << "Test " << n << " passes\n"; \ else \ cout << "Test " << n << " fails\n"; \ // Usage: settest filename word int main(int argc, char** argv) { // Read a file into sets of words and their lengths if (argc>1) { ifstream f(argv[1]); if (f) { Set ss; Set si; string s; while (f >> s) { ss += s; si = int(s.size()) + si; } // Print the sets and their sizes cout << "Words in " << argv[1] << " = " << ss << endl; cout << "Word lengths = " << si << endl; cout << si.size() << " lengths and " << ss.size() << " different words\n"; // Print whether the word is found if (argc>2) { if (ss.in(argv[2])) cout << argv[2] << " is in " << argv[1] << endl; else cout << argv[2] << " is not in " << argv[1] << endl; } // Test set subtraction ss -= ss; si = si - si; if (ss.size()!=0 || si.size()!=0) cout << "Set subtraction test fails\nss=" << ss << " si=" << si << endl; } else cout << "File not found: " << argv[1] << endl; } // Test constructors, +, -, * cout << "\nOutput should be as follows:\n" "{}{1}{1,2}{1,2,3}{1,2,3,4,5}\n" "{1}{2,3}{1,2,3}{1}{}{}{}\n" "{}{1,2}{2}{2}\n\n" "Actual output is:\n"; const Set null, s1=1, s12=s1+2, s123(3+s12); // Test union cout << null << s1 << s12 << s123 << 5+null+4+s123 << endl; // {}{1}{1,2}{1,2,3}{1,2,3,4,5} cout << s12-2 << s123-s1 << s123-null << 1-null << null-null << null-s1 << null-4 << endl; // {1}{2,3}{1,2,3}{1}{}{}{} cout << null*s1 << s123*s12 << s123*2 << 2*s123 << endl; // {}{1,2}{2}{2} // Test comparison operators and some more expressions cout << "\nNext 7 tests should pass\n"; test(s1, s12, 1); test(null, 1, 2); test(1, s12, 3); test(1-null, s123-2, 4); test(3*s123, s123-1, 5); testeq(s12+3, s123+4-4, 6); testeq((Set(4)+3+2+1-4+5)*(6+s12), s12, 7) return 0; }