-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.cpp
122 lines (104 loc) · 2.99 KB
/
Test.cpp
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
#include "include/Test.h"
#include "include/IO.h"
#include "include/RSA.h"
#include "include/Converter.h"
#include "include/Random.h"
#include "include/Algorithm.h"
using std::tie;
using std::to_string;
void Test::startClock()
{
start = std::chrono::steady_clock::now();
}
void Test::stopClock()
{
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
io.writeOutput("Execution time: " + std::to_string(duration.count() / 1e6) + " seconds\n\n");
}
void Test::setBase(int base)
{
Test::base = base;
}
void Test::testOperators()
{
BigInt a, b;
for (tuple<BigInt, BigInt> test : testCases)
{
tie(a, b) = test;
#if 1
io.writeOutputs(a, b, a - b, " - ", base);
io.writeOutputs(a, b, a + b, " + ", base);
io.writeOutputs(a, b, a == b, " == ", base);
io.writeOutputs(a, b, a != b, " != ", base);
io.writeOutputs(a, b, a < b, " < ", base);
io.writeOutputs(a, b, a > b, " > ", base);
io.writeOutputs(a, 10, a >> 10, " >> ", base);
io.writeOutputs(a, 5, a << 5, " << ", base);
io.writeOutputs(a, b, a / b, " / ", base);
io.writeOutputs(a, b, a % b, " % ", base);
io.writeOutputs(a, b, a * b, " * ", base);
#endif
}
}
void Test::testIO()
{
BigInt a, b;
for (tuple<BigInt, BigInt> test : testCases)
{
tie(a, b) = test;
io.writeOutput("Binary form of a: ", a, BigIntBase::BASE_2);
io.writeOutput("Decimal form of a: ", a, BigIntBase::BASE_10);
io.writeOutput("Binary form of b: ", b, BigIntBase::BASE_2);
io.writeOutput("Decimal form of b: ", b, BigIntBase::BASE_10);
}
}
void Test::testRandom()
{
for (int i = 0; i < Test::testCount; i++)
{
BigInt firstRandomNumber = random.next(BigInt::maxByteCount);
BigInt secondRandomNumber = random.next(2, firstRandomNumber - 2);
io.writeOutput("First random number: ", firstRandomNumber, base);
io.writeOutput("Second random number: ", secondRandomNumber, base);
}
}
void Test::testAlgorithm()
{
BigInt a, b;
for (tuple<BigInt, BigInt> test : testCases)
{
tie(a, b) = test;
BigInt m = converter.decimalStrToBigInt("93033494171281019060146861548143953189380057555617");
BigInt g, x, y; tie(g, x, y) = extendedEuclidean(a, b);
io.writeOutputs(a, b, powMod(a, b, m), " powMod ", base);
io.writeOutputs(a, b, gcd(a, b), " gcd ", base);
io.writeOutputs(a, b, g, " extended gcd ", base);
io.writeOutputs(a, b, inverseMod(a, b), " invMod ", base);
}
}
void Test::testRSA()
{
if (BigInt::maxByteCount <= 0) return;
RSA rsa(BigInt::maxByteCount);
io.clearFile("cipher.txt");
io.clearFile("decrypted.txt");
rsa.encryptFile("plain.txt", "cipher.txt");
rsa.decryptFile("cipher.txt", "decrypted.txt");
}
void Test::runTest(string type)
{
io.clearFile("resources/log.txt");
io.clearFile("resources/output.txt");
io.readInputs(testCases, base);
if (type == "operators")
testOperators();
else if (type == "random")
testRandom();
else if (type == "io")
testIO();
else if (type == "algorithm")
testAlgorithm();
else if (type == "rsa")
testRSA();
}