Created
April 5, 2017 13:11
-
-
Save nvbn/a91b13d6e15c9cc03e376a374ab4df50 to your computer and use it in GitHub Desktop.
validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
#include <regex> | |
using namespace std; | |
class Validator { | |
public: | |
Validator(istream &in) : in_(in) {} | |
bool validate() { | |
std::ostringstream buffer; | |
buffer << in_.rdbuf(); | |
auto input = buffer.str(); | |
std::regex pattern( | |
"^\\[\\(\\d\\d?\\.?(\\s\\d\\d?'\\d\\d?\")? (N|S), \\d\\d?\\.?(\\s\\d\\d?'\\d\\d?\")? (W|E)\\)\\]$"); | |
return std::regex_match(input.begin(), input.end(), pattern); | |
} | |
private: | |
istream &in_; | |
}; | |
void testValidator(int &test_nr, const char *input, bool expected) { | |
cout << "test #" << test_nr++ << endl; | |
istringstream in(input); | |
Validator v(in); | |
if (v.validate() != expected) { | |
cout << " failed:" << endl | |
<< "input:" << endl | |
<< input << endl | |
<< "expected: " << boolalpha << expected << endl | |
<< "got: " << boolalpha << !expected << endl; | |
} else | |
cout << " OK" << endl; | |
} | |
int main() { | |
int test_nr = 1; | |
testValidator(test_nr, "[(0. N, 0. E)]", true); | |
testValidator(test_nr, "[(0. S, 0. W)]", true); | |
testValidator(test_nr, "[(0 0'0\" N, 0 0'0\" W)]", true); | |
testValidator(test_nr, "[(89 59'50\" N, 89 59'0\" E)]", true); | |
// Tests for false: | |
testValidator(test_nr, "[(89 5950\" N, 89 59'0\" E)]", false); | |
testValidator(test_nr, "(89 N, 89 E)", false); | |
testValidator(test_nr, "[]", false); | |
testValidator(test_nr, "[(0. E, 0. N)]", false); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment