Home | History | Annotate | Download | only in tests
      1 /**
      2  * @file regex_test.cpp
      3  *
      4  * A simple test for libregex. Run it through:
      5  * $ regex_test
      6  * or
      7  * $ regex_test filename(s)
      8  * when no argument is provided "mangled-name" is used,
      9  * see it for the input file format
     10  *
     11  * @remark Copyright 2003 OProfile authors
     12  * @remark Read the file COPYING
     13  *
     14  * @author Philippe Elie
     15  */
     16 
     17 #include "string_manip.h"
     18 
     19 #include "op_regex.h"
     20 
     21 #include <iostream>
     22 #include <fstream>
     23 
     24 #include <cstdlib>
     25 
     26 using namespace std;
     27 
     28 static int nr_error = 0;
     29 
     30 static void do_test(istream& fin)
     31 {
     32 	regular_expression_replace rep;
     33 
     34 	setup_regex(rep, "../stl.pat");
     35 
     36 	string test, expect, last;
     37 	bool first = true;
     38 	while (getline(fin, last)) {
     39 		last = trim(last);
     40 		if (last.length() == 0 || last[0] == '#')
     41 			continue;
     42 
     43 		if (first) {
     44 			test = last;
     45 			first = false;
     46 		} else {
     47 			expect = last;
     48 			first = true;
     49 			string str(test);
     50 			rep.execute(str);
     51 			if (str != expect) {
     52 				cerr << "mistmatch: test, expect, returned\n"
     53 				     << '"' << test << '"' << endl
     54 				     << '"' << expect << '"' << endl
     55 				     << '"' << str << '"' << endl;
     56 				++nr_error;
     57 			}
     58 		}
     59 	}
     60 
     61 	if (!first)
     62 		cerr << "input file ill formed\n";
     63 }
     64 
     65 int main(int argc, char * argv[])
     66 {
     67 	try {
     68 		if (argc > 1) {
     69 			for (int i = 1; i < argc; ++i) {
     70 				ifstream fin(argv[i]);
     71 				do_test(fin);
     72 			}
     73 		} else {
     74 			ifstream fin("mangled-name");
     75 			if (!fin) {
     76 				cerr << "Unable to open input test "
     77 				     << "\"mangled_name\"\n" << endl;
     78 				exit(EXIT_FAILURE);
     79 			}
     80 			do_test(fin);
     81 		}
     82 	}
     83 	catch (bad_regex const & e) {
     84 		cerr << "bad_regex " << e.what() << endl;
     85 		return EXIT_FAILURE;
     86 	}
     87 	catch (exception const & e) {
     88 		cerr << "exception: " << e.what() << endl;
     89 		return EXIT_FAILURE;
     90 	}
     91 
     92 	return nr_error ? EXIT_FAILURE : EXIT_SUCCESS;
     93 }
     94 
     95