-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestlib.h
2133 lines (1770 loc) · 54.8 KB
/
testlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* It is strictly recommended to include "testlib.h" before any other include
* in your code. In this case testlib overrides compiler specific "random()".
*
* If you can't compile your code and compiler outputs something about
* ambiguous call of "random_shuffle", "rand" or "srand" it means that
* you shouldn't use them. Use "shuffle", and "rnd.next()" instead of them
* because these calls produce stable result for any C++ compiler. Read
* sample generator sources for clarification.
*
* Please read the documentation for class "random_t" and use "rnd" instance in
* generators. Probably, these sample calls will be usefull for you:
* rnd.next(); rnd.next(100); rnd.next(1, 2);
* rnd.next(3.14); rnd.next("[a-z]{1,100}").
*
* Also read about wnext() to generate off-center random distribution.
*
* See http://code.google.com/p/testlib/ to get latest version or bug tracker.
*/
#ifndef _TESTLIB_H_
#define _TESTLIB_H_
/*
* Copyright (c) 2005-2012
*/
#define VERSION "0.7.4"
/*
* Mike Mirzayanov
*
* This material is provided "as is", with absolutely no warranty expressed
* or implied. Any use is at your own risk.
*
* Permission to use or copy this software for any purpose is hereby granted
* without fee, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*/
/* NOTE: This file contains testlib library for C++.
*
* Check, using testlib running format:
* check.exe <Input_File> <Output_File> <Answer_File> [<Result_File> [-appes]],
* If result file is specified it will contain results.
*
* Validator, using testlib running format:
* validator.exe < input.txt,
* It will return non-zero exit code and writes message to standard output.
*
* Generator, using testlib running format:
* gen.exe [parameter-1] [parameter-2] [... paramerter-n],
* You can write generated test(s) into standard output or into the file(s).
*/
const char* latestFeatures[] = {
"Fixed to be compilable on Mac",
"PC_BASE_EXIT_CODE=50 in case of defined TESTSYS",
"Fixed issues 19-21, added __attribute__ format printf",
"Some bug fixes",
"ouf.readInt(1, 100) and similar calls return WA",
"Modified random_t to avoid integer overflow",
"Truncated checker output [patch by Stepan Gatilov]",
"Renamed class random -> class random_t",
"Supported name parameter for read-and-validation methods, like readInt(1, 2, \"n\")",
"Fixed bug in readDouble()",
"Improved ensuref(), fixed nextLine to work in case of EOF, added startTest()",
"Supported \"partially correct\", example: quitf(_pc(13), \"result=%d\", result)",
"Added shuffle(begin, end), use it instead of random_shuffle(begin, end)",
"Added readLine(const string& ptrn), fixed the logic of readLine() in the validation mode",
"Package extended with samples of generators and validators",
"Written the documentation for classes and public methods in testlib.h",
"Implemented random routine to support generators, use registerGen() to switch it on",
"Implemented strict mode to validate tests, use registerValidation() to switch it on",
"Now ncmp.cpp and wcmp.cpp are return WA if answer is suffix or prefix of the output",
"Added InStream::readLong() and removed InStream::readLongint()",
"Now no footer added to each report by default (use directive FOOTER to switch on)",
"Now every checker has a name, use setName(const char* format, ...) to set it",
"Now it is compatible with TTS (by Kittens Computing)",
"Added \'ensure(condition, message = \"\")\' feature, it works like assert()",
"Fixed compatibility with MS C++ 7.1",
"Added footer with exit code information",
"Added compatibility with EJUDGE (compile with EJUDGE directive)"
};
#ifdef _MSC_VER
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#endif
/* Overrides random() for Borland C++. */
#define random __random_deprecated
#include <stdlib.h>
#include <cstdlib>
#include <climits>
#include <algorithm>
#undef random
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cmath>
#include <sstream>
#include <cstring>
#include <stdarg.h>
#include <fcntl.h>
#if !defined(unix) && !defined(__APPLE__)
#include <io.h>
#endif
#if ( _WIN32 || __WIN32__ || _WIN64 || __WIN64__ )
#include <windows.h>
#define ON_WINDOWS
#else
#define WORD unsigned short
#endif
#ifndef LLONG_MIN
#define LLONG_MIN (-9223372036854775807LL - 1)
#endif
#define MAX_FORMAT_BUFFER_SIZE (8388608)
#define LF ((char)10)
#define CR ((char)13)
#define TAB ((char)9)
#define SPACE ((char)' ')
#define EOFC ((char)26)
#ifndef EJUDGE
#define OK_EXIT_CODE 0
#define WA_EXIT_CODE 1
#define PE_EXIT_CODE 2
#define FAIL_EXIT_CODE 3
#define DIRT_EXIT_CODE 4
#define PC_BASE_EXIT_CODE 0
#else
#define OK_EXIT_CODE 0
#define WA_EXIT_CODE 5
#define PE_EXIT_CODE 4
#define FAIL_EXIT_CODE 6
#define DIRT_EXIT_CODE 6
#define PC_BASE_EXIT_CODE 0
#endif
#ifdef TESTSYS
#undef PC_BASE_EXIT_CODE
#define PC_BASE_EXIT_CODE 50
#endif
#define __TESTLIB_STATIC_ASSERT(condition) typedef void* __testlib_static_assert_type[((condition) != 0) * 2 - 1];
const long long __TESTLIB_LONGLONG_MAX = 9223372036854775807LL;
template<typename T>
static inline T __testlib_abs(const T& x)
{
return x > 0 ? x : -x;
}
template<typename T>
static inline T __testlib_min(const T& a, const T& b)
{
return a < b ? a : b;
}
template<typename T>
static inline T __testlib_max(const T& a, const T& b)
{
return a > b ? a : b;
}
static void __testlib_fail(const std::string& message);
/*
* Very simple regex-like pattern.
* It used for two purposes: validation and generation.
*
* For example, pattern("[a-z]{1,5}").next(rnd) will return
* random string from lowercase latin letters with length
* from 1 to 5. It is easier to call rnd.next("[a-z]{1,5}")
* for the same effect.
*
* Another samples:
* "mike|john" will generate (match) "mike" or "john";
* "-?[1-9][0-9]{0,3}" will generate (match) non-zero integers from -9999 to 9999;
* "id-([ac]|b{2})" will generate (match) "id-a", "id-bb", "id-c";
* "[^0-9]*" will match sequences (empty or non-empty) without digits, you can't
* use it for generations.
*
* You can't use pattern for generation if it contains meta-symbol '*'. Also it
* is not recommended to use it for char-sets with meta-symbol '^' like [^a-z].
*
* For matching very simple greedy algorithm is used. For example, pattern
* "[0-9]?1" will not match "1", because of greedy nature of matching.
* Alternations (meta-symbols "|") are processed with brute-force algorithm, so
* do not use many alternations in one expression.
*
* If you want to use one expression many times it is better to compile it into
* a single pattern like "pattern p("[a-z]+")". Later you can use
* "p.matches(std::string s)" or "p.next(random_t& rd)" to check matching or generate
* new string by pattern.
*
* Simpler way to read token and check it for pattern matching is "inf.readToken("[a-z]+")".
*/
class random_t;
class pattern
{
public:
/* Create pattern instance by string. */
pattern(std::string s);
/* Generate new string by pattern and given random_t. */
std::string next(random_t& rnd) const;
/* Checks if given string match the pattern. */
bool matches(const std::string& s) const;
private:
bool matches(const std::string& s, size_t pos) const;
std::vector<pattern> children;
std::vector<char> chars;
int from;
int to;
};
/*
* Use random_t instances to generate random values. It is preffered
* way to use randoms instead of rand() function or self-written
* randoms.
*
* Testlib defines global variable "rnd" of random_t class.
* Use registerGen(argc, argv) to setup random_t seed be command
* line.
*
* Random generates uniformly distributed values if another strategy is
* not specified explicitly.
*/
class random_t
{
private:
long long seed;
static const long long multiplier;
static const long long addend;
static const long long mask;
static const int lim;
long long nextBits(int bits)
{
if (bits <= 48)
{
seed = (seed * multiplier + addend) & mask;
return (long long)(seed >> (48 - bits));
}
else
{
if (bits > 63)
__testlib_fail("random_t::nextBits(int bits): n must be less than 64");
return ((nextBits(31) << 32) ^ nextBits(31));
}
}
public:
/* New random_t with fixed seed. */
random_t()
: seed(3905348978240129619LL)
{
}
/* Sets seed by command line. */
void setSeed(int argc, char* argv[])
{
random_t p;
seed = 3905348978240129619LL;
for (int i = 1; i < argc; i++)
{
std::size_t le = std::strlen(argv[i]);
for (std::size_t j = 0; j < le; j++)
seed = seed * multiplier + (unsigned int)(argv[i][j]) + addend;
seed += multiplier / addend;
}
seed = seed & mask;
}
/* Sets seed by given value. */
void setSeed(long long _seed)
{
_seed = (_seed ^ multiplier) & mask;
seed = _seed;
}
/* Random value in range [0, n-1]. */
int next(int n)
{
if (n <= 0)
__testlib_fail("random_t::next(int n): n must be positive");
if ((n & -n) == n) // n is a power of 2
return (int)((n * (long long)nextBits(31)) >> 31);
const long long limit = INT_MAX / n * n;
long long bits;
do {
bits = nextBits(31);
} while (bits >= limit);
return bits % n;
}
/* Random value in range [0, n-1]. */
int next(unsigned int n)
{
if (n >= INT_MAX)
__testlib_fail("random_t::next(unsigned int n): n must be less INT_MAX");
return next(int(n));
}
/* Random value in range [0, n-1]. */
long long next(long long n)
{
if (n <= 0)
__testlib_fail("random_t::next(long long n): n must be positive");
const long long limit = __TESTLIB_LONGLONG_MAX / n * n;
long long bits;
do {
bits = nextBits(63);
} while (bits >= limit);
return bits % n;
}
/* Random value in range [0, n-1]. */
int next(unsigned long long n)
{
if (n >= (unsigned long long)(__TESTLIB_LONGLONG_MAX))
__testlib_fail("random_t::next(unsigned long long n): n must be less LONGLONG_MAX");
return (int)next((long long)(n));
}
/* Returns random value in range [from,to]. */
int next(int from, int to)
{
return int(next((long long)to - from + 1) + from);
}
/* Returns random value in range [from,to]. */
unsigned int next(unsigned int from, unsigned int to)
{
return (unsigned int)(next((long long)to - from + 1) + from);
}
/* Returns random value in range [from,to]. */
long long next(long long from, long long to)
{
return next(to - from + 1) + from;
}
/* Random double value in range [0, 1). */
double next()
{
return (((long long)(nextBits(26)) << 27) + nextBits(27)) / (double)(1LL << 53);
}
/* Random double value in range [0, n). */
double next(double n)
{
return n * next();
}
/* Random double value in range [from, to). */
double next(double from, double to)
{
return next(to - from) + from;
}
/* Random string value by given pattern (see pattern documentation). */
std::string next(const std::string& ptrn)
{
pattern p(ptrn);
return p.next(*this);
}
/* Random string value by given pattern (see pattern documentation). */
#ifdef __GNUC__
__attribute__ ((format (printf, 2, 3)))
#endif
std::string next(const char* format, ...)
{
char* buffer = new char [MAX_FORMAT_BUFFER_SIZE];
va_list ap;
va_start(ap, format);
std::vsprintf(buffer, format, ap);
va_end(ap);
std::string ptrn(buffer);
delete[] buffer;
return next(ptrn);
}
/*
* Weighted next. If type == 0 than it is usual "next()".
*
* If type = 1, than it returns "max(next(), next())"
* (the number of "max" functions equals to "type").
*
* If type < 0, than "max" function replaces with "min".
*/
int wnext(int n, int type)
{
if (n <= 0)
__testlib_fail("random_t::wnext(int n, int type): n must be positive");
if (abs(type) < random_t::lim)
{
int result = next(n);
for (int i = 0; i < +type; i++)
result = __testlib_max(result, next(n));
for (int i = 0; i < -type; i++)
result = __testlib_min(result, next(n));
return result;
}
else
{
double p;
if (type > 0)
p = std::pow(next() + 0.0, 1.0 / (type + 1));
else
p = 1 - std::pow(next() + 0.0, 1.0 / (-type + 1));
return int(n * p);
}
}
/* See wnext(int, int). It uses the same algorithms. */
int wnext(unsigned int n, int type)
{
if (n >= INT_MAX)
__testlib_fail("random_t::wnext(unsigned int n, int type): n must be less INT_MAX");
return wnext(int(n), type);
}
/* See wnext(int, int). It uses the same algorithms. */
long long wnext(long long n, int type)
{
if (n <= 0)
__testlib_fail("random_t::wnext(long long n, int type): n must be positive");
if (abs(type) < random_t::lim)
{
long long result = next(n);
for (int i = 0; i < +type; i++)
result = __testlib_max(result, next(n));
for (int i = 0; i < -type; i++)
result = __testlib_min(result, next(n));
return result;
}
else
{
double p;
if (type > 0)
p = std::pow(next() + 0.0, 1.0 / (type + 1));
else
p = std::pow(next() + 0.0, - type + 1);
return (long long)(n * p);
}
}
/* See wnext(int, int). It uses the same algorithms. */
double wnext(int type)
{
if (abs(type) < random_t::lim)
{
double result = next();
for (int i = 0; i < +type; i++)
result = __testlib_max(result, next());
for (int i = 0; i < -type; i++)
result = __testlib_min(result, next());
return result;
}
else
{
double p;
if (type > 0)
p = std::pow(next() + 0.0, 1.0 / (type + 1));
else
p = std::pow(next() + 0.0, - type + 1);
return p;
}
}
/* See wnext(int, int). It uses the same algorithms. */
double wnext(double n, int type)
{
if (n <= 0)
__testlib_fail("random_t::wnext(double n, int type): n must be positive");
if (abs(type) < random_t::lim)
{
double result = next();
for (int i = 0; i < +type; i++)
result = __testlib_max(result, next());
for (int i = 0; i < -type; i++)
result = __testlib_min(result, next());
return n * result;
}
else
{
double p;
if (type > 0)
p = std::pow(next() + 0.0, 1.0 / (type + 1));
else
p = std::pow(next() + 0.0, - type + 1);
return n * p;
}
}
/* Returns weighted random value in range [from, to]. */
int wnext(int from, int to, int type)
{
return wnext(to - from + 1, type) + from;
}
/* Returns weighted random value in range [from, to]. */
int wnext(unsigned int from, unsigned int to, int type)
{
return wnext(to - from + 1, type) + from;
}
/* Returns weighted random value in range [from, to]. */
long long wnext(long long from, long long to, int type)
{
return wnext(to - from + 1, type) + from;
}
/* Returns weighted random double value in range [from, to). */
double wnext(double from, double to, int type)
{
return wnext(to - from, type) + from;
}
};
const int random_t::lim = 25;
const long long random_t::multiplier = 0x5DEECE66DLL;
const long long random_t::addend = 0xBLL;
const long long random_t::mask = (1LL << 48) - 1;
/* Pattern implementation */
bool pattern::matches(const std::string& s) const
{
return matches(s, 0);
}
static bool __pattern_isSlash(const std::string& s, size_t pos)
{
return s[pos] == '\\';
}
static bool __pattern_isCommandChar(const std::string& s, size_t pos, char value)
{
if (pos >= s.length())
return false;
int slashes = 0;
int before = pos - 1;
while (before >= 0 && s[before] == '\\')
before--, slashes++;
return slashes % 2 == 0 && s[pos] == value;
}
static char __pattern_getChar(const std::string& s, size_t& pos)
{
if (__pattern_isSlash(s, pos))
pos += 2;
else
pos++;
return s[pos - 1];
}
static int __pattern_greedyMatch(const std::string& s, size_t pos, const std::vector<char> chars)
{
int result = 0;
while (pos < s.length())
{
char c = s[pos++];
if (!std::binary_search(chars.begin(), chars.end(), c))
break;
else
result++;
}
return result;
}
bool pattern::matches(const std::string& s, size_t pos) const
{
std::string result;
if (to > 0)
{
int size = __pattern_greedyMatch(s, pos, chars);
if (size < from)
return false;
if (size > to)
size = to;
pos += size;
}
if (children.size() > 0)
{
for (size_t child = 0; child < children.size(); child++)
if (children[child].matches(s, pos))
return true;
return false;
}
else
return pos == s.length();
}
std::string pattern::next(random_t& rnd) const
{
std::string result;
if (to == INT_MAX)
__testlib_fail("pattern::next(random_t& rnd): can't process character '*' for generation");
if (to > 0)
{
int count = rnd.next(to - from + 1) + from;
for (int i = 0; i < count; i++)
result += chars[rnd.next(int(chars.size()))];
}
if (children.size() > 0)
{
int child = rnd.next(int(children.size()));
result += children[child].next(rnd);
}
return result;
}
static void __pattern_scanCounts(const std::string& s, size_t& pos, int& from, int& to)
{
if (pos >= s.length())
{
from = to = 1;
return;
}
if (__pattern_isCommandChar(s, pos, '{'))
{
std::vector<std::string> parts;
std::string part;
pos++;
while (pos < s.length() && !__pattern_isCommandChar(s, pos, '}'))
{
if (__pattern_isCommandChar(s, pos, ','))
parts.push_back(part), part = "", pos++;
else
part += __pattern_getChar(s, pos);
}
if (part != "")
parts.push_back(part);
if (!__pattern_isCommandChar(s, pos, '}'))
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
pos++;
if (parts.size() < 1 || parts.size() > 2)
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
std::vector<int> numbers;
for (size_t i = 0; i < parts.size(); i++)
{
if (parts[i].length() == 0)
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
int number;
if (std::sscanf(parts[i].c_str(), "%d", &number) != 1)
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
numbers.push_back(number);
}
if (numbers.size() == 1)
from = to = numbers[0];
else
from = numbers[0], to = numbers[1];
if (from > to)
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
}
else
{
if (__pattern_isCommandChar(s, pos, '?'))
{
from = 0, to = 1, pos++;
return;
}
if (__pattern_isCommandChar(s, pos, '*'))
{
from = 0, to = INT_MAX, pos++;
return;
}
if (__pattern_isCommandChar(s, pos, '+'))
{
from = 1, to = INT_MAX, pos++;
return;
}
from = to = 1;
}
}
static std::vector<char> __pattern_scanCharSet(const std::string& s, size_t& pos)
{
if (pos >= s.length())
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
std::vector<char> result;
if (__pattern_isCommandChar(s, pos, '['))
{
pos++;
bool negative = __pattern_isCommandChar(s, pos, '^');
char prev = 0;
while (pos < s.length() && !__pattern_isCommandChar(s, pos, ']'))
{
if (__pattern_isCommandChar(s, pos, '-') && prev != 0)
{
pos++;
if (pos + 1 == s.length())
{
result.push_back(prev);
prev = '-';
continue;
}
char next = __pattern_getChar(s, pos);
if (prev > next)
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
for (char c = prev; c != next; c++)
result.push_back(c);
result.push_back(next);
prev = 0;
}
else
{
if (prev != 0)
result.push_back(prev);
prev = __pattern_getChar(s, pos);
}
}
if (prev != 0)
result.push_back(prev);
if (!__pattern_isCommandChar(s, pos, ']'))
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
pos++;
if (negative)
{
std::sort(result.begin(), result.end());
std::vector<char> actuals;
for (int code = 0; code < 255; code++)
{
char c = char(code);
if (!std::binary_search(result.begin(), result.end(), c))
actuals.push_back(c);
}
result = actuals;
}
std::sort(result.begin(), result.end());
}
else
result.push_back(__pattern_getChar(s, pos));
return result;
}
pattern::pattern(std::string s): from(0), to(0)
{
std::string t;
for (size_t i = 0; i < s.length(); i++)
if (!__pattern_isCommandChar(s, i, ' '))
t += s[i];
s = t;
int opened = 0;
int firstClose = -1;
std::vector<int> seps;
for (size_t i = 0; i < s.length(); i++)
{
if (__pattern_isCommandChar(s, i, '('))
{
opened++;
continue;
}
if (__pattern_isCommandChar(s, i, ')'))
{
opened--;
if (opened == 0 && firstClose == -1)
firstClose = i;
continue;
}
if (opened < 0)
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
if (__pattern_isCommandChar(s, i, '|') && opened == 0)
seps.push_back(i);
}
if (opened != 0)
__testlib_fail("pattern: Illegal pattern (or part) \"" + s + "\"");
if (seps.size() == 0 && firstClose + 1 == (int)s.length()
&& __pattern_isCommandChar(s, 0, '(') && __pattern_isCommandChar(s, s.length() - 1, ')'))
{
children.push_back(pattern(s.substr(1, s.length() - 2)));
}
else
{
if (seps.size() > 0)
{
seps.push_back(s.length());
int last = 0;
for (size_t i = 0; i < seps.size(); i++)
{
children.push_back(pattern(s.substr(last, seps[i] - last)));
last = seps[i] + 1;
}
}
else
{
size_t pos = 0;
chars = __pattern_scanCharSet(s, pos);
__pattern_scanCounts(s, pos, from, to);
if (pos < s.length())
children.push_back(pattern(s.substr(pos)));
}
}
}
/* End of pattern implementation */
inline bool isEof(char c)
{
return (c == EOF || c == EOFC);
}
inline bool isEoln(char c)
{
return (c == LF || c == CR);
}
inline bool isBlanks(char c)
{
return (c == LF || c == CR || c == SPACE || c == TAB);
}
enum TMode
{
_input, _output, _answer
};
enum TResult
{
_ok, _wa, _pe, _fail, _dirt, _partially
};
#define _pc(exitCode) (TResult(_partially + (exitCode)))
const std::string outcomes[] =
{"accepted", "wrong-answer", "presentation-error", "fail", "fail", "partially-correct"};
/*
* Streams to be used for reading data in checkers or validators.
* Each read*() method moves pointer to the next character after the
* read value.
*/
struct InStream
{
/* Do not use it. */
InStream();
std::FILE * file;
std::string name;
TMode mode;
bool opened;
bool stdfile;
bool strict;
void init(std::string fileName, TMode mode);
void init(std::FILE* f, TMode mode);
/* Moves stream pointer to the first non-white-space character or EOF. */
void skipBlanks();
/* Returns current character in the stream. Doesn't remove it from stream. */
char curChar();
/* Moves stream pointer one character forward. */
void skipChar();
/* Returns current character and moves pointer one character forward. */
char nextChar();
/* Returns current character and moves pointer one character forward. */
char readChar();
/* As "readChar()" but ensures that the result is equal to given parameter. */
char readChar(char c);
/* As "readChar()" but ensures that the result is equal to the space (code=32). */
char readSpace();
/* Puts back the character into the stream. */
void unreadChar(char c);
/* Reopens stream, you should not use it. */
void reset();
/* Checks that current position is EOF. If not it doesn't move stream pointer. */
bool eof();
/* Moves pointer to the first non-white-space character and calls "eof()". */
bool seekEof();
/*
* Checks that current position contains EOLN.
* If not it doesn't move stream pointer.
* In strict mode expects "#13#10" for windows or "#10" for other platforms.
*/
bool eoln();
/* Moves pointer to the first non-space and non-tab character and calls "eoln()". */
bool seekEoln();
/* Moves stream pointer to the first character of the next line (if exists). */
void nextLine();
/*
* Reads new token. Ignores white-spaces into the non-strict mode
* (strict mode is used in validators usually).
*/
std::string readWord();
/* The same as "readWord()", it is preffered to use "readToken()". */
std::string readToken();
/* The same as "readWord()", but ensures that token matches to given pattern. */
std::string readWord(const std::string& ptrn, const std::string& variableName = "");
/* The same as "readToken()", but ensures that token matches to given pattern. */
std::string readToken(const std::string& ptrn, const std::string& variableName = "");
/*