-
Notifications
You must be signed in to change notification settings - Fork 11
/
AutoTest.hpp
280 lines (222 loc) · 6.14 KB
/
AutoTest.hpp
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
#ifndef _SNARKLIB_AUTO_TEST_HPP_
#define _SNARKLIB_AUTO_TEST_HPP_
#include <cassert>
#include <cstdint>
#include <gmp.h>
#include <iostream>
#include <memory>
#include <ostream>
#include <random>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>
#include /*libsnark*/ "algebra/fields/bigint.hpp"
#include "snarklib/AuxSTL.hpp"
#include "snarklib/Pairing.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// autotest base class
//
class AutoTest
{
public:
~AutoTest() = default;
std::string testName() const {
std::stringstream ss;
ss << typeid(*this).name() << m_testNameSuffix;
return ss.str();
}
std::size_t testNumber() const {
return m_testCounter;
}
virtual void runTest() = 0;
bool testPass() const {
return m_testPass;
}
void testLog(std::ostream& out) const {
out << testNumber() << "\t"
<< (testPass() ? "PASS" : "FAIL") << "\t"
<< testName() << std::endl;
}
protected:
AutoTest()
: m_testPass(true)
{
static std::size_t testCounter = 0;
m_testCounter = testCounter++;
}
template <typename A>
AutoTest(const A& a)
: AutoTest{}
{
std::stringstream ss;
ss << " " << a;
m_testNameSuffix = ss.str();
}
template <typename A, typename B>
AutoTest(const A& a, const B& b)
: AutoTest{}
{
std::stringstream ss;
ss << " " << a << " " << b;
m_testNameSuffix = ss.str();
}
template <typename A, typename B, typename C>
AutoTest(const A& a, const B& b, const C& c)
: AutoTest{}
{
std::stringstream ss;
ss << " " << a << " " << b << " " << c;
m_testNameSuffix = ss.str();
}
template <typename A, typename B, typename C, typename D>
AutoTest(const A& a, const B& b, const C& c, const D& d)
: AutoTest{}
{
std::stringstream ss;
ss << " " << a << " " << b << " " << c << " " << d;
m_testNameSuffix = ss.str();
}
bool checkPass(const bool v) {
m_testPass &= v;
return v;
}
// vector<>
template <typename T>
void printData(std::ostream& out,
const std::string& prefix,
const std::vector<T>& v) const
{
for (std::size_t i = 0; i < v.size(); ++i) {
out << prefix << "[" << i << "] = " << v[i] << std::endl;
}
}
private:
std::string m_testNameSuffix;
std::size_t m_testCounter;
bool m_testPass;
};
////////////////////////////////////////////////////////////////////////////////
// battery of autotests
//
class AutoTestBattery
{
public:
AutoTestBattery()
: m_os(nullptr)
{}
AutoTestBattery(std::ostream& os)
: m_os(std::addressof(os))
{}
std::size_t testCount() const {
return m_testVector.size();
}
void addTest(AutoTest* ptr) {
if (m_os) {
*m_os << ".";
}
m_testVector.push_back(std::unique_ptr<AutoTest>(ptr));
}
// returns number of failed tests
std::size_t runTest() {
std::size_t failCount = 0;
for (const auto& a : m_testVector) {
if (m_os) {
*m_os << std::endl
<< "Running test " << a->testNumber() << " - " << a->testName()
<< std::endl;
}
a->runTest();
if (! a->testPass()) ++failCount;
}
return failCount;
}
// returns true if test passes, false if test fails
bool runTest(const std::size_t indexNumber) {
m_testVector[indexNumber]->runTest();
return m_testVector[indexNumber]->testPass();
}
void testLog(std::ostream& out) const {
for (const auto& a : m_testVector) {
a->testLog(out);
}
}
void failLog(std::ostream& out) const {
for (const auto& a : m_testVector) {
if (! a->testPass()) a->testLog(out);
}
}
void testLog(std::ostream& out, const std::size_t indexNumber) const {
m_testVector[indexNumber]->testLog(out);
}
private:
std::ostream* m_os;
std::vector<std::unique_ptr<AutoTest>> m_testVector;
};
////////////////////////////////////////////////////////////////////////////////
// convenient functions
//
std::string randomBase10(std::random_device& rd, const mp_size_t N) {
std::stringstream ss;
for (size_t i = 0; i < N; ++i) {
ss << rd();
}
return ss.str();
}
std::string uniformBase10(const unsigned long low, const unsigned long high) {
assert(low <= high);
std::default_random_engine generator;
std::uniform_int_distribution<unsigned long> distribution(low, high);
std::stringstream ss;
ss << distribution(generator);
return ss.str();
}
std::string sparseUniformBase10(const unsigned long low, const unsigned long high) {
assert(low <= high);
std::default_random_engine generator;
std::uniform_int_distribution<unsigned long>
selector(0, 2),
distribution(low, high);
std::stringstream ss;
switch (selector(generator)) {
case (0) :
ss << 0;
break;
case (1) :
ss << 1;
break;
default:
ss << distribution(generator);
break;
}
return ss.str();
}
template <mp_size_t N>
libsnark::bigint<N> to_bigint(const std::string& base10) {
return libsnark::bigint<N>(base10.c_str());
}
template <typename GA, typename GB>
void randomSparseVector(SparseVector<Pairing<GA, GB>>& a,
const std::size_t numberElems,
const std::size_t startIndex)
{
a.reserve(numberElems);
for (std::size_t i = 0; i < numberElems; ++i) {
a.pushBack(
startIndex + i,
Pairing<GA, GB>(GA::random(), GB::random()));
}
}
template <typename T>
void randomVector(std::vector<T>& a,
const std::size_t numberElems)
{
a.reserve(numberElems);
for (std::size_t i = 0; i < numberElems; ++i) {
a.emplace_back(
T::random());
}
}
} // namespace snarklib
#endif