-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
142 lines (136 loc) · 4.08 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <cstdint>
#include <iostream>
#include <limits>
#include <random>
#include <type_traits>
template <typename T> class Test final {
T fib(T X) {
if (X <= 1)
return 1;
return fib(X - 1) + fib(X - 2);
}
T gcd(T A, T B) {
if (B == 0)
return A;
return gcd(B, A % B);
}
// std::minstd_rand RNG;
// std::uniform_int_distribution<T> Distrib{std::numeric_limits<T>::min()};
T Idx = 0;
T generate() {
Idx = Idx * 97 + 1;
return Idx;
// return Distrib(RNG);
}
public:
void run() {
using U = std::conditional_t<
std::is_same_v<int8_t, T> || std::is_same_v<uint8_t, T>,
std::conditional_t<std::is_same_v<int8_t, T>, int16_t, uint16_t>, T>;
constexpr int Count = 1000;
for (int I = 0; I < Count; ++I) {
T V = generate() % 20;
std::cout << "fib(" << U(V) << ") = " << U(T(fib(V))) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
std::cout << "gcd(" << U(A) << ", " << U(B) << ") = " << U(T(gcd(A, B)))
<< std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
std::cout << U(A) << " + " << U(B) << " = " << U(T(A + B)) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
std::cout << U(A) << " - " << U(B) << " = " << U(T(A - B)) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
std::cout << U(A) << " * " << U(B) << " = " << U(T(A * B)) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
if (B)
std::cout << U(A) << " / " << U(B) << " = " << U(T(A / B)) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
if (B)
std::cout << U(A) << " % " << U(B) << " = " << U(T(A % B)) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate() % 5;
T B = generate() % 5;
std::cout << U(A) << " == " << U(B) << " = " << (A == B) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate() % 5;
T B = generate() % 5;
std::cout << U(A) << " != " << U(B) << " = " << (A != B) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
std::cout << U(A) << " < " << U(B) << " = " << (A < B) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate() % 5;
T B = generate() % 5;
std::cout << U(A) << " <= " << U(B) << " = " << (A <= B) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
std::cout << U(A) << " > " << U(B) << " = " << (A > B) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate() % 5;
T B = generate() % 5;
std::cout << U(A) << " >= " << U(B) << " = " << (A >= B) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
std::cout << U(A) << " & " << U(B) << " = " << U(T(A & B)) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
std::cout << U(A) << " | " << U(B) << " = " << U(T(A | B)) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate();
std::cout << U(A) << " ^ " << U(B) << " = " << U(T(A ^ B)) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate() % (sizeof(T) * 8);
std::cout << U(A) << " << " << U(B) << " = " << U(T(A << B)) << std::endl;
}
for (int I = 0; I < Count; ++I) {
T A = generate();
T B = generate() % (sizeof(T) * 8);
std::cout << U(A) << " >> " << U(B) << " = " << U(T(A >> B)) << std::endl;
}
}
};
int main() {
puts("hello");
Test<int8_t>{}.run();
Test<uint8_t>{}.run();
Test<int16_t>{}.run();
Test<uint16_t>{}.run();
Test<int32_t>{}.run();
Test<uint32_t>{}.run();
Test<int64_t>{}.run();
Test<uint64_t>{}.run();
return 0;
}