forked from ifsmirnov/jngen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcases.h
113 lines (91 loc) · 2.27 KB
/
testcases.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
#pragma once
#include "array.h"
#include "common.h"
#include "math_jngen.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
namespace jngen {
inline int getInitialTestNo() {
char *envvar = std::getenv("TESTNO");
int testno;
if (!envvar || 1 != std::sscanf(envvar, "%d", &testno)) {
return 1;
}
return testno;
}
#ifdef JNGEN_DECLARE_ONLY
extern int nextTestNo;
#else
int nextTestNo = -1;
#endif // JNGEN_DECLARE_ONLY
void startTest(int testNo);
void startTest();
void setNextTestNumber(int testNo);
Array64 randomTestSizes(
long long totalSize,
int count,
long long minSize,
long long maxSize,
const Array64& predefined);
Array randomTestSizes(
int totalSize,
int count,
int minSize,
int maxSize,
const Array& predefined);
#ifndef JNGEN_DECLARE_ONLY
void startTest(int testNo) {
nextTestNo = testNo + 1;
char filename[10];
std::sprintf(filename, "%d", testNo);
if (!std::freopen(filename, "w", stdout)) {
ensure(false, format("Cannot open the file `%s'", filename));
}
}
void startTest() {
if (nextTestNo == -1) {
nextTestNo = getInitialTestNo();
}
startTest(nextTestNo);
}
void setNextTestNumber(int testNo) {
nextTestNo = testNo;
}
Array64 randomTestSizes(
long long totalSize,
int count,
long long minSize,
long long maxSize,
const Array64& predefined)
{
for (auto x: predefined) {
totalSize -= x;
}
ensure(totalSize >= 0, "Sum of predefined test sizes exceeds total size");
ensure(count * minSize <= totalSize, "minSize is too large");
ensure(count * maxSize >= totalSize, "maxSize is too small");
ensure(minSize <= maxSize);
return (rndm.partition(totalSize, count, minSize, maxSize) +
predefined).shuffle();
}
Array randomTestSizes(
int totalSize,
int count,
int minSize,
int maxSize,
const Array& predefined)
{
return arrayCast<int>(randomTestSizes(
static_cast<long long>(totalSize),
count,
static_cast<long long>(minSize),
static_cast<long long>(maxSize),
arrayCast<long long>(predefined)
));
}
#endif // JNGEN_DECLARE_ONLY
} // namespace jngen
using jngen::startTest;
using jngen::setNextTestNumber;
using jngen::randomTestSizes;