[testlib] Validators

Правка en12, от I_love_Hoang_Yen, 2015-06-09 14:06:24

If you have written some tasks, and have prepared test cases, you will probably experience the terrible feeling that some test cases may be invalid (meaning it does not agree with the constraints in problem statement): upper bound can be violated, your graph not satisfied connectivity requirements or is not at tree... It is reasonable to feel that way. Even experienced problem setters make mistakes sometimes (for example, in the pretigious ACM ICPC World final 2007).

The validator in testlib is a good tool to help you prevent such mistakes. It is very easy to use.

Example

Following is the validator of validator, which I used in a problem I prepared: 100541A - Stock Market:

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

int main() {
    registerValidation();
    int test = inf.readInt(1, 10);
    inf.readEoln();
    
    while (test--) {
        int n = inf.readInt(1, 100);
        inf.readChar(' ');
        int w = inf.readInt(1, 1000000);
        inf.readEoln();

        for(int i = 0; i < n; ++i) {
            inf.readInt(1, 1000);
            if (i < n-1) inf.readChar(' ');
            else inf.readEoln();
        }
    }

    inf.readEof();
    return 0;
}

More examples can be found at the Github repo

Available methods

The first line of your code should be registerValidation() which does some magic in the background, so that you can use the necessary methods. Most methods for validators start with prefix "read" and it does the same thing: moves input stream pointer to next suitable place after reading something. It also detect violations (input does not match what you are trying to read), and then throw error.

Notes:

  • Validator is strict. It cares about correct placing of spaces. For exmple, when you're trying to read an integer and the next character is a space (and then an integer), the validator will throw error.
  • Some method has "regex" feature. It is not a full-featured regex as you may have used in many programming languages. It is a very simple version, which supports the following:
    • Set of character, e.g: [a-z] is a lowercase latin letter, [^a-z] matches anything but a lowercase latin letter
    • Range, e.g. [a-z]{1,5} is a string of length 1 to 5 consists of only lowercase latin letter
    • Or operator, e.g. mike|john is either mike or john
    • Optional character, e.g. -?[1-9][0-9]{0,3} will match non-zero integers from -9999 to 9999 (note the optional minus sign)
    • Repetition, e.g. [0-9]* will match sequences (empty or non-empty) of digits, and [0-9]+ will match non-empty sequences of digits.
  • Also regarding regex, very simple greedy algorithm is used. For example, pattern [0-9]?1 will not match 1, because of greedy nature of matching.

Following is full list of methods available:

Method What it does
void registerValidation() This method must be called at the beginning of your code in order to use validator.
After calling this method, you can access input stream by variable named inf.
char readChar() Returns current character and moves pointer one character forward.
char readChar(char c) Same as readChar() but ensure that the readCharacter is 'c'.
char readSpace() Same as readChar(' ').
void unreadChar(char c) Puts back character c to input stream.
string readToken() Reads a new token.
string readToken(string regex) Same as readToken() but ensure that it matches given regex.
long long readLong() Read a long (long long in C/C++ and long in Java)
long long readLong(int L, int R) Same as readLong() but ensure that the value is in range [L, R] (inclusively)
int readInt(),
int readInteger()
Read an integer (int type in both Java and C/C++)
int readInt(int L, int R),
int readInteger(L, R)
Same as readInt() but ensure that the value is in range [L, R] (inclusively)
double readReal(),
double readDouble()
Read a double.
double readReal(double L, double R),
double readDouble(double L, double R)
Same as readReal(), readDouble() but ensure that the value is in range [L, R].
double readStrictReal(double L, double R, int minPrecision, int maxPrecision),
double readStrictDouble(double L, double R, int minPrecision, int maxPrecision)
Same as readReal(L, R), readDouble(L, R), but additionally ensure that the number of digits after decimal point is between [minPrecision, maxPrecision].
string readString(),
string readLine()
Read a line from current position to EOLN. Move input stream pointer to first character of new line (if exist).
string readString(string regex),
string readLine(string regex)
Same as readString() and readLine(), but ensure that the string matches given regex.
void readEoln() Read EOLN or fails. Note that this method magically works on both Windows and Linux. On Windows it reads #13#10 and on Linux it reads #10.
void readEof() Read EOF or fails.

Reference: Github page of testlib.h

Теги testlib, validator, validators, polygon

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
ru11 Русский KAN 2022-07-07 20:21:26 2128
en24 Английский KAN 2022-07-07 20:16:48 4201 Tiny change: 'alidation()` which d' -> 'alidation(argc, argv)` which d'
ru10 Русский KAN 2022-07-07 19:00:36 2066 added global methods
ru9 Русский KAN 2022-07-07 18:43:02 2171 updated functions
ru8 Русский MikeMirzayanov 2018-12-29 14:28:59 20 Мелкая правка: ' readLong(int L, int R)|Аналог' -> ' readLong(long long L, long long R)|Аналог'
ru7 Русский arsijo 2018-11-02 01:08:48 527
en23 Английский arsijo 2018-11-02 01:01:28 400
ru6 Русский Zlobober 2016-10-01 20:36:24 32
en22 Английский Zlobober 2016-02-24 02:54:05 32
en21 Английский I_love_Hoang_Yen 2015-06-18 14:09:58 20 Tiny change: ' readLong(int L, int R)|Same a' -> ' readLong(long long L, long long R)|Same a'
ru5 Русский riadwaw 2015-06-10 22:35:00 20
en20 Английский I_love_Hoang_Yen 2015-06-10 21:00:28 20
ru4 Русский MikeMirzayanov 2015-06-10 12:29:16 118
en19 Английский MikeMirzayanov 2015-06-10 12:27:46 410
en18 Английский PrinceOfPersia 2015-06-10 08:57:32 218
ru3 Русский MikeMirzayanov 2015-06-10 02:25:07 67
en17 Английский MikeMirzayanov 2015-06-10 02:24:34 67
ru2 Русский MikeMirzayanov 2015-06-10 02:20:24 3
ru1 Русский elena 2015-06-10 02:18:13 7940 Первая редакция перевода на Русский
en16 Английский elena 2015-06-10 01:13:44 30 Tiny change: 'inus sign), note that it will match '-0'.\n - *' -> 'inus sign).\n - *'
en15 Английский MikeMirzayanov 2015-06-09 17:33:29 1546
en14 Английский PrinceOfPersia 2015-06-09 17:16:10 56
en13 Английский I_love_Hoang_Yen 2015-06-09 14:08:48 125
en12 Английский I_love_Hoang_Yen 2015-06-09 14:06:24 246 Tiny change: '| Same as readChar(' ').|\n|void ' -> '| Same as `readChar(' ')`.|\n|void '
en11 Английский I_love_Hoang_Yen 2015-06-09 13:56:28 86 Update styling
en10 Английский I_love_Hoang_Yen 2015-06-09 13:53:23 1840 (published)
en9 Английский I_love_Hoang_Yen 2015-06-09 13:38:13 291
en8 Английский I_love_Hoang_Yen 2015-06-09 13:34:33 28 Tiny change: ' C/C++)|\n\n|readInt' -
en7 Английский I_love_Hoang_Yen 2015-06-09 13:01:14 3 Tiny change: 'readInt(), readIntege' -> 'readInt(),\nreadIntege'
en6 Английский I_love_Hoang_Yen 2015-06-09 12:54:26 2 Tiny change: ' in range [L, R].|\n|readS' -> ' in range $[L, R]$.|\n|readS'
en5 Английский I_love_Hoang_Yen 2015-06-09 12:54:01 60
en4 Английский I_love_Hoang_Yen 2015-06-09 12:52:57 1888
en3 Английский I_love_Hoang_Yen 2015-06-09 12:34:31 206
en2 Английский I_love_Hoang_Yen 2015-06-09 12:04:31 331
en1 Английский I_love_Hoang_Yen 2015-06-09 12:01:01 1478 Initial revision (saved to drafts)