Skip to content

Instantly share code, notes, and snippets.

@nvbn
Created April 5, 2017 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvbn/a91b13d6e15c9cc03e376a374ab4df50 to your computer and use it in GitHub Desktop.
Save nvbn/a91b13d6e15c9cc03e376a374ab4df50 to your computer and use it in GitHub Desktop.
validator
#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