A short library to validate tests created for competitive programming problems
Copy the src/TestValidator.hpp
contents and change the main function to your requirements. That's it! Change the input and output stream according to your requirement. If the input is validated then it will output validated, else it will throw exception.
If the input is validated then it will output validated which as is not the expected output will cause Wrong Answer
, else it will throw exception causing Runtime Error
. So, we are expecting Wrong Answer
on all test cases.
Your OJ might skip test cases if it encounters WA. Contact OJ support to solve this issue.
Validator(std::istream &is, std::ostream &_os)
The Validator constructor initializes the validator, taking in input and output streams.
Validator(std::istream &is, std::ostream &_os)
Parameters:
- is: Input stream to read data from.
- _os: Output stream for validation messages.
Example:
Validator validator(std::cin, std::cout);
template<class T>
bool checkNumberIsBetween(T &x, T lowerLimit, T upperLimit, std::string msg = "")
Validates if a given number x lies between lowerLimit and upperLimit.
template<class T>
bool checkNumberIsBetween(T &x, T lowerLimit, T upperLimit, std::string msg = "")
Parameters:
- x: The number to validate.
- lowerLimit: Lower bound of the range.
- upperLimit: Upper bound of the range.
- msg: Custom error message if validation fails (optional).
Example:
int num = 5;
validator.checkNumberIsBetween(num, 1, 10, "Number out of range");
bool checkCharMatchesClass(char &x, std::string charClass, std::string msg = "")
Validates if the character x matches any character in the specified charClass.
bool checkCharMatchesClass(char &x, std::string charClass, std::string msg = "")
Parameters:
- x: Character to validate.
- charClass: String specifying valid characters.
- msg: Custom error message if validation fails (optional).
Example:
char ch = 'a';
validator.checkCharMatchesClass(ch, "abc", "Invalid character");
bool checkStringLengthBetween(std::string &x, size_t lowerLimit, size_t upperLimit, std::string msg = "")
Ensures the length of string x falls within the range [lowerLimit, upperLimit].
bool checkStringLengthBetween(std::string &x, size_t lowerLimit, size_t upperLimit, std::string msg = "")
Parameters:
- x: String to validate.
- lowerLimit: Minimum length.
- upperLimit: Maximum length.
- msg: Custom error message if validation fails (optional).
Example:
std::string str = "hello";
validator.checkStringLengthBetween(str, 3, 10);
template<class T>
bool checkInterval(T &start, T &end, T lowerLimit, T upperlimit, std::string msg = "")
Validates that start and end represent an interval within [lowerLimit, upperLimit].
template<class T>
bool checkInterval(T &start, T &end, T lowerLimit, T upperlimit, std::string msg = "")
Parameters:
- start: Interval start.
- end: Interval end.
- lowerLimit: Minimum allowable start.
- upperLimit: Maximum allowable end.
- msg: Custom error message if validation fails (optional).
Example:
int start = 1, end = 5;
validator.checkInterval(start, end, 1, 10);
bool checkCondition(bool condition, std::string msg = "")
Validates a custom condition.
bool checkCondition(bool condition, std::string msg = "")
Parameters:
- condition: Boolean condition to check.
- msg: Custom error message if validation fails (optional).
Example:
bool isValid = (x > 0);
validator.checkCondition(isValid, "Condition not met");
template<class T>
bool readIntegerBetween(T &x, T lowerLimit, T upperLimit, std::string msg = "")
readIntegerBetween
reads an integer x and validates if the integer lies within [lowerLimit, upperLimit].
template<class T>
bool readIntegerBetween(T &x, T lowerLimit, T upperLimit, std::string msg = "")
readIntegerBetween
reads an integer x and validates if the integer lies within [lowerLimit, upperLimit].Parameters:
- x: Variable to store the read integer.
- lowerLimit: Lower bound of the range.
- upperLimit: Upper bound of the range.
- msg: Custom error message if validation fails (optional).
Example:
int n;
validator.readIntegerBetween(n, 1, 500);
template<class T>
bool readIntervalBetween(T &start, T &end, T lowerLimit, T upperLimit, std::string msg = "")
readIntervalBetween
reads two integers, start and end, and ensures that the interval they form lies within [lowerLimit, upperLimit].
template<class T>
bool readIntervalBetween(T &start, T &end, T lowerLimit, T upperLimit, std::string msg = "")
readIntervalBetween
reads two integers, start and end, and ensures that the interval they form lies within [lowerLimit, upperLimit].Parameters:
- start: Variable to store the start of the interval.
- end: Variable to store the end of the interval.
- lowerLimit: Minimum allowable start value.
- upperLimit: Maximum allowable end value.
- msg: Custom error message if validation fails (optional).
Example:
int start, end;
validator.readIntervalBetween(start, end, 1, 10);
bool readCharAndMatchClass(char &x, std::string charClass, std::string msg = "")
readCharAndMatchClass
reads a character x and validates if it belongs to the specified charClass.
bool readCharAndMatchClass(char &x, std::string charClass, std::string msg = "")
readCharAndMatchClass
reads a character x and validates if it belongs to the specified charClass.Parameters:
- x: Variable to store the read character.
- charClass: String specifying valid characters.
- msg: Custom error message if validation fails (optional).
Example:
char ch;
validator.readCharAndMatchClass(ch, "abc", "Invalid character");
bool readStringBetweenLengthAndMatchClass(std::string &x, size_t lowerLimit, size_t upperLimit, std::string charClass, std::string msg = "")
readStringBetweenLengthAndMatchClass
reads a string x, ensures its length falls between [lowerLimit, upperLimit], and checks that each character matches the specified charClass.
bool readStringBetweenLengthAndMatchClass(std::string &x, size_t lowerLimit, size_t upperLimit, std::string charClass, std::string msg = "")
readStringBetweenLengthAndMatchClass
reads a string x, ensures its length falls between [lowerLimit, upperLimit], and checks that each character matches the specified charClass.Parameters:
- x: Variable to store the read string.
- lowerLimit: Minimum length of the string.
- upperLimit: Maximum length of the string.
- charClass: String specifying valid characters. msg: Custom error message if validation fails (optional).
Example:
std::string str;
validator.readStringBetweenLengthAndMatchClass(str, 1, 10, "abc123");
bool readSpaceChar(std::string msg = "")
readSpaceChar
reads a single whitespace character. If the next character is not a space, it reports an error.
bool readSpaceChar(std::string msg = "")
readSpaceChar
reads a single whitespace character. If the next character is not a space, it reports an error.Parameters:
- msg: Custom error message if validation fails (optional).
Example:
validator.readSpaceChar("Expected a space character");
bool readEndOfLine(std::string msg = "")
readEndOfLine
reads a newline character. If the next character is not a newline, it reports an error.
bool readEndOfLine(std::string msg = "")
readEndOfLine
reads a newline character. If the next character is not a newline, it reports an error.Parameters:
- msg: Custom error message if validation fails (optional).
Example:
validator.readEndOfLine("Expected end of line");
bool readEndOfFile(std::string msg = "")
readEndOfFile
checks if the end of the file has been reached. If not, it reports an error.
bool readEndOfFile(std::string msg = "")
readEndOfFile
checks if the end of the file has been reached. If not, it reports an error.Parameters:
- msg: Custom error message if validation fails (optional).
Example:
validator.readEndOfFile("Expected end of file");
- Test integer read
- Use exception
- Mistakes always consume