Commit c7a11a48 authored by Mathieu Valois's avatar Mathieu Valois
Browse files

Nicer tests

parent 6ba59d60
Loading
Loading
Loading
Loading
Loading
+36 −10
Original line number Diff line number Diff line
@@ -7,13 +7,21 @@

using namespace std;

uint64_t count(const std::string& filename, const std::string& regex){
// compute regex matches occurrences and stores it in pair vector
vector<pair<uint64_t, uint64_t>> count(const std::string& filename, const vector<pair<uint64_t, string>>& regexs_str){
	vector<regex> regexs;
	vector<pair<uint64_t, uint64_t>> matches;
	for(pair<uint64_t, string> s : regexs_str){
		matches.push_back(make_pair(s.first, 0));
		regexs.push_back(regex(s.second, regex::egrep));
	}
	std::ifstream file(filename);
	uint64_t matches = 0;
	std::string line;
	while(getline(file, line)){
		if(regex_match(line, std::regex(regex))){
			++matches;
		for(uint i = 0; i < regexs.size(); ++i){
			if(regex_match(line, regexs[i])){
				++matches[i].second;
			}
		}
	}
	return matches;
@@ -27,12 +35,30 @@ int main(){
	s4.generate_stats();
	assert(s0 == s4);
	Statistics r = s4.getResults();
	for(int i=1; i <= 10; ++i){
		assert(r.length[i] == count(TESTFILE, "^.{" + to_string(i) + "}$"));

	vector<pair<uint64_t, string>> regexs;
	// add tests here, like [computed by cppack], [regex]
	for(int i = 0; i <= 20; ++i){
		regexs.push_back(make_pair(r.length[i], "^.{" + to_string(i) + "}$"));
	}

	assert(r.charactersets["loweralpha"] == count(TESTFILE, "^[a-z]+$"));
	assert(r.charactersets["upperalpha"] == count(TESTFILE, "^[A-Z]+$"));
	assert(r.charactersets["numeric"] == count(TESTFILE, "^[0-9]+$"));
	return 0;
	regexs.push_back(make_pair(r.charactersets["loweralpha"], "^[a-z]+$"));
	regexs.push_back(make_pair(r.charactersets["upperalpha"], "^[A-Z]+$"));
	regexs.push_back(make_pair(r.charactersets["numeric"], 	  "^[0-9]+$"));
	regexs.push_back(make_pair(r.charactersets["special"], 	  "^[^a-zA-Z0-9]+$"));
	regexs.push_back(make_pair(r.charactersets["loweralphanum"], "^([a-z]+[0-9]|[0-9]+[a-z])[a-z0-9]*$"));
	regexs.push_back(make_pair(r.charactersets["upperalphanum"], "^([A-Z]+[0-9]|[0-9]+[A-Z])[A-Z0-9]*$"));

	vector<pair<uint64_t, uint64_t>>  matches = count(TESTFILE, regexs);

	// verification
	bool passed = true;
	for(uint i = 0; i < regexs.size(); ++i){
		bool equals = matches[i].first == matches[i].second;
		if(!equals){
			cerr << "Test failed for " << regexs[i].second << " (" << regexs[i].first << " != " << matches[i].second << ")" << endl;
			passed = false;
		}
	}
	return !passed;
}
 No newline at end of file