regex_test.cpp
Go to the documentation of this file.00001
00017 #include "string_manip.h"
00018
00019 #include "op_regex.h"
00020
00021 #include <iostream>
00022 #include <fstream>
00023
00024 #include <cstdlib>
00025
00026 using namespace std;
00027
00028 static int nr_error = 0;
00029
00030 static void do_test(istream& fin)
00031 {
00032 regular_expression_replace rep;
00033
00034 setup_regex(rep, "../stl.pat");
00035
00036 string test, expect, last;
00037 bool first = true;
00038 while (getline(fin, last)) {
00039 last = trim(last);
00040 if (last.length() == 0 || last[0] == '#')
00041 continue;
00042
00043 if (first) {
00044 test = last;
00045 first = false;
00046 } else {
00047 expect = last;
00048 first = true;
00049 string str(test);
00050 rep.execute(str);
00051 if (str != expect) {
00052 cerr << "mistmatch: test, expect, returned\n"
00053 << '"' << test << '"' << endl
00054 << '"' << expect << '"' << endl
00055 << '"' << str << '"' << endl;
00056 ++nr_error;
00057 }
00058 }
00059 }
00060
00061 if (!first)
00062 cerr << "input file ill formed\n";
00063 }
00064
00065 int main(int argc, char * argv[])
00066 {
00067 try {
00068 if (argc > 1) {
00069 for (int i = 1; i < argc; ++i) {
00070 ifstream fin(argv[i]);
00071 do_test(fin);
00072 }
00073 } else {
00074 ifstream fin("mangled-name");
00075 if (!fin) {
00076 cerr << "Unable to open input test "
00077 << "\"mangled_name\"\n" << endl;
00078 exit(EXIT_FAILURE);
00079 }
00080 do_test(fin);
00081 }
00082 }
00083 catch (bad_regex const & e) {
00084 cerr << "bad_regex " << e.what() << endl;
00085 return EXIT_FAILURE;
00086 }
00087 catch (exception const & e) {
00088 cerr << "exception: " << e.what() << endl;
00089 return EXIT_FAILURE;
00090 }
00091
00092 return nr_error ? EXIT_FAILURE : EXIT_SUCCESS;
00093 }
00094