// Non-Deterministic Finite Automata Unit Test // // File: ndfa_test.cc // Author: course // Version: 2 #include "ndfa.h" int test1 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `abc'" << endl; cout << endl; ndfa a = single_character ('a'); ndfa b = single_character ('b'); ndfa c = single_character ('c'); ndfa n = concatenate (a, concatenate (b, c)); // Print cout << the_state_table; // Tests cout << endl << "Test \"abc\"" << endl; n.run ("abc", true); cout << endl << "Test \"abx\"" << endl; n.run ("abx", true); cout << endl << "Test \"xbc\"" << endl; n.run ("xbc", true); cout << endl << "Test \"ab\"" << endl; n.run ("ab", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test2 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `a|b|c'" << endl; cout << endl; ndfa a = single_character ('a'); ndfa b = single_character ('b'); ndfa c = single_character ('c'); ndfa n = alternate (a, alternate (b, c)); // Print cout << the_state_table; // Tests cout << endl << "Test \"a\"" << endl; n.run ("a", true); cout << endl << "Test \"b\"" << endl; n.run ("b", true); cout << endl << "Test \"c\"" << endl; n.run ("c", true); cout << endl << "Test \"x\"" << endl; n.run ("x", true); cout << endl << "Test \"ab\"" << endl; n.run ("ab", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test3 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `ab*c'" << endl; cout << endl; ndfa a = single_character ('a'); ndfa b = single_character ('b'); ndfa c = single_character ('c'); ndfa n = concatenate (a, concatenate (star(b), c)); // Print cout << the_state_table; // Tests cout << endl << "Test \"ac\"" << endl; n.run ("ac", true); cout << endl << "Test \"abc\"" << endl; n.run ("abc", true); cout << endl << "Test \"abbc\"" << endl; n.run ("abbc", true); cout << endl << "Test \"axc\"" << endl; n.run ("axc", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test4 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `ab+c'" << endl; cout << endl; ndfa a = single_character ('a'); ndfa b = single_character ('b'); ndfa c = single_character ('c'); ndfa n = concatenate (a, concatenate (plus(b), c)); // Print cout << the_state_table; // Tests cout << endl << "Test \"ac\"" << endl; n.run ("ac", true); cout << endl << "Test \"abc\"" << endl; n.run ("abc", true); cout << endl << "Test \"abbc\"" << endl; n.run ("abbc", true); cout << endl << "Test \"axc\"" << endl; n.run ("axc", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test5 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `ab(c|#)d'" << endl; cout << endl; ndfa a = single_character ('a'); ndfa b = single_character ('b'); ndfa c = single_character ('c'); ndfa d = single_character ('d'); ndfa e = single_character ('#'); ndfa n = concatenate (a, concatenate (b, concatenate (alternate (c, e), d))); // Print cout << the_state_table; // Tests cout << endl << "Test \"abcd\"" << endl; n.run ("abcd", true); cout << endl << "Test \"abd\"" << endl; n.run ("abd", true); cout << endl << "Test \"abxd\"" << endl; n.run ("abxd", true); cout << endl << "Test \"abcx\"" << endl; n.run ("abcx", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test6 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `a?b'" << endl; cout << endl; ndfa a = single_character ('a'); ndfa q = single_character ('?'); ndfa b = single_character ('b'); ndfa n = concatenate (a, concatenate (q, b)); // Print cout << the_state_table; // Tests cout << endl << "Test \"abb\"" << endl; n.run ("abb", true); cout << endl << "Test \"a3b\"" << endl; n.run ("a3b", true); cout << endl << "Test \"xbb\"" << endl; n.run ("xbb", true); cout << endl << "Test \"axbb\"" << endl; n.run ("axbb", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test7 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `axb|ayb'" << endl; cout << endl; ndfa a1 = single_character ('a'); ndfa a2 = single_character ('a'); ndfa x = single_character ('x'); ndfa y = single_character ('y'); ndfa b1 = single_character ('b'); ndfa b2 = single_character ('b'); ndfa n = alternate ( concatenate ( concatenate ( a1, x ), b1 ), concatenate ( concatenate ( a2, y ), b2 )); // Print cout << the_state_table; // Tests cout << endl << "Test \"axb\"" << endl; n.run ("axb", true); cout << endl << "Test \"ayb\"" << endl; n.run ("ayb", true); cout << endl << "Test \"axc\"" << endl; n.run ("axc", true); cout << endl << "Test \"ax\"" << endl; n.run ("ax", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test8 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `a*a'" << endl; cout << endl; ndfa a1 = single_character ('a'); ndfa a2 = single_character ('a'); ndfa n = concatenate (star (a1), a2); // Print cout << the_state_table; // Tests cout << endl << "Test \"a\"" << endl; n.run ("a", true); cout << endl << "Test \"aa\"" << endl; n.run ("aa", true); cout << endl << "Test \"aab\"" << endl; n.run ("aab", true); cout << endl << "Test \"b\"" << endl; n.run ("b", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test9 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `(ab*)*'" << endl; cout << endl; ndfa a = single_character ('a'); ndfa b = single_character ('b'); ndfa n = star (concatenate (a, star (b))); // Print cout << the_state_table; // Tests cout << endl << "Test \"a\"" << endl; n.run ("a", true); cout << endl << "Test \"ab\"" << endl; n.run ("ab", true); cout << endl << "Test \"abbbbbb\"" << endl; n.run ("abbbbbb", true); cout << endl << "Test \"abababa\"" << endl; n.run ("abababa", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test10 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `a(a|b)*b'" << endl; cout << endl; ndfa a1 = single_character ('a'); ndfa a2 = single_character ('a'); ndfa b1 = single_character ('b'); ndfa b2 = single_character ('b'); ndfa n = concatenate (a1, concatenate (star (alternate (a2, b2)), b1)); // Print cout << the_state_table; // Tests cout << endl << "Test \"ab\"" << endl; n.run ("ab", true); cout << endl << "Test \"abbbcsde\"" << endl; n.run ("abbbcsde", true); cout << endl << "Test \"abbbbbb\"" << endl; n.run ("abbbbbb", true); cout << endl << "Test \"abababa\"" << endl; n.run ("abababa", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test11 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `a*b*a*'" << endl; cout << endl; ndfa a1 = single_character ('a'); ndfa a2 = single_character ('a'); ndfa b = single_character ('b'); ndfa n = concatenate (star (a1), concatenate (star (b), star (a2))); // Print cout << the_state_table; // Tests cout << endl << "Test \"a\"" << endl; n.run ("a", true); cout << endl << "Test \"b\"" << endl; n.run ("b", true); cout << endl << "Test \"bba\"" << endl; n.run ("bba", true); cout << endl << "Test \"aabba\"" << endl; n.run ("aabba", true); cout << endl << "Test \"abab\"" << endl; n.run ("abab", true); cout << endl << "Test \"aaa\"" << endl; n.run ("aaa", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test12 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" " `a*b+a*'" << endl; cout << endl; ndfa a1 = single_character ('a'); ndfa a2 = single_character ('a'); ndfa b = single_character ('b'); ndfa n = concatenate (star (a1), concatenate (plus (b), star (a2))); // Print cout << the_state_table; // Tests cout << endl << "Test \"a\"" << endl; n.run ("a", true); cout << endl << "Test \"b\"" << endl; n.run ("b", true); cout << endl << "Test \"bba\"" << endl; n.run ("bba", true); cout << endl << "Test \"aabba\"" << endl; n.run ("aabba", true); cout << endl << "Test \"abab\"" << endl; n.run ("abab", true); cout << endl << "Test \"aaa\"" << endl; n.run ("aaa", true); cout << endl << "Test \"\"" << endl; n.run ("", true); } int test13 () { the_state_table.init(); cout << endl; cout << endl; cout << "State table for regular expression" "\n\t`(a | bc | d)* (? | #)'" << endl; cout << endl; ndfa a = single_character ('a'); ndfa b = single_character ('b'); ndfa c = single_character ('c'); ndfa d = single_character ('d'); ndfa qm = single_character ('?'); ndfa em = single_character ('#'); ndfa n = concatenate (star (alternate (a, alternate (concatenate (b, c), d))), alternate ( qm, em )); // Print cout << the_state_table; // Tests cout << endl << "Test \"ax\"" << endl; n.run ("ax", true); cout << endl << "Test \"adbcbca\"" << endl; n.run ("ax", true); cout << endl << "Test \"adb\"" << endl; n.run ("adb", true); cout << endl << "Test \"adbcea\"" << endl; n.run ("adbcea", true); } main () { test1(); test2(); test3(); test4(); test5(); test6(); test7(); test8(); test9(); test10(); test11(); test12(); test13(); return 0; }