-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
200 lines (167 loc) · 6.18 KB
/
Copy pathtest.cpp
File metadata and controls
200 lines (167 loc) · 6.18 KB
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
#include <random>
#include <stdexcept>
#include "MyMatrix.hpp"
int main(){
std::random_device rd;
std::mt19937 gen(rd());
std::cout << "Copy tests" << std::endl;
for (int i = 1; i < 5; i++) {
std::uniform_int_distribution<int> distribution_int(1, 5);
std::uniform_real_distribution<double> distribution_real(-20.0, 20.0);
int x1 = distribution_int(gen);
int x2 = distribution_int(gen);
MyMatrix<double> matrix1(3, 3);
MyMatrix<double> matrix2(2, 2);
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < 3; j++) {
matrix1[i][j] = distribution_real(gen);
}
}
for (size_t i = 0; i < 2; i++) {
for (size_t j = 0; j < 2; j++) {
matrix2[i][j] = distribution_real(gen);
}
}
matrix1 = matrix2;
for (size_t i = 0; i < 2; i++) {
for (size_t j = 0; j < 2; j++) {
std::cout << matrix2[i][j] << " ";
}
std:: cout << std::endl;
}
matrix1 = matrix2;
for (size_t i = 0; i < 2; i++) {
for (size_t j = 0; j < 2; j++) {
std::cout << matrix2[i][j] << " ";
}
std:: cout << std::endl;
}
std::cout << std::endl;
}
std::cout << "Test for intenger numbers:" << std::endl;
std::uniform_int_distribution<int> distribution_int(-10, 10);
for (size_t kol = 1; kol < 100; kol++){
MyMatrix<int> matrix1(100,100);
double det_correct1 = 1.0;
for (size_t j = 0; j < 99; j++){
for (size_t k = 0; k < 99; k++ ){
if (j == k) {
matrix1[j][k] = distribution_int(gen);
det_correct1 = det_correct1 * matrix1[j][k];
}
else matrix1[j][k] = 0;
}
}
for (size_t i = 0; i < 99; i++) {
for(size_t j = 0; j < 99; j++) {
matrix1[i + 1][j] = matrix1[i + 1][j] + matrix1[i][j];
}
}
for (size_t i = 0; i < 99; i--){
for (size_t j = 0; j < 99; j++) {
matrix1[i][j] = matrix1[i][j] + matrix1[99][j] * 2;
}
}
double det1 = matrix1.determinant() ;
if (det_correct1 == 0){
if (det1 == 0){
std::cout << "Test" << " " << kol << " " << "passed" << std::endl;
}
else {
std::cout << "Test" << " " << kol << " " << "failed. Expected answer ="
<< " " << det_correct1 << "Result of the program =" << " "
<< det1 << std::endl;
}
}
else {
double result = det1 / det_correct1;
if ((result - 1) < 0.000001) {
std::cout << "Test" << " " << kol << " " << "passed" << std::endl;
} else {
std::cout << "Test" << " " << kol << " " << "failed. Expected answer ="
<< " " << det_correct1 << "Result of the program =" << " "
<< det1 << std::endl;
}
}
}
std::cout << std::endl;
std::cout << "Test for real numbers:" << std::endl;
std::uniform_real_distribution<double> distribution_real(-20.0, 20.0);
for (size_t kol = 1; kol <= 100; kol++){
MyMatrix<double> matrix2(100,100);
double det_correct2 = 1.0;
for (size_t j = 0; j < 100; j++){
for (size_t k = 0; k < 100; k++ ){
if (j == k) {
matrix2[j][k] = distribution_real(gen);
det_correct2 = det_correct2 * matrix2[j][k];
}
else matrix2[j][k] = 0.0;
}
}
for (size_t i = 0; i < 99; i++){
for (size_t j = 0; j < 99; j++) {
matrix2[i + 1][j] = matrix2[i + 1][j] + matrix2[i][j];
}
}
for (size_t i = 0; i < 99; i--){
for (size_t j = 0; j < 99; j++) {
matrix2[i][j] = matrix2[i][j] + matrix2[99][j] * 2;
}
}
double det2 = matrix2.determinant() ;
if (det_correct2 == 0){
if (det2 == 0){
std::cout << "Test" << " " << kol << " " << "passed" << std::endl;
}
else {
std::cout << "Test" << " " << kol << " " << "failed. Expected answer ="
<< " " << det_correct2 << "Result of the program =" << " "
<< det2 << std::endl;
}
}
else {
double result = det2 / det_correct2;
if ((result - 1) < 0.000001) {
std::cout << "Test" << " " << kol << " " << "passed" << std::endl;
} else {
std::cout << "Test" << " " << kol << " " << "failed. Expected answer ="
<< " " << det_correct2 << "Result of the program =" << " "
<< det2 << std::endl;
}
}
}
std::cout << std::endl << "exeptions tests: " << std::endl;
MyMatrix<double> matrix1(3, 5);
MyMatrix<double> matrix2(6, 9);
try {
MyMatrix<double> result = matrix1 + matrix2;
} catch (const std::runtime_error &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
try {
MyMatrix<double> result = matrix1 - matrix2;
} catch (const std::runtime_error &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
try {
MyMatrix<double> result = matrix1 * matrix2;
} catch (const std::runtime_error &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
try {
MyMatrix<double> result = matrix2 * matrix1;
} catch (const std::runtime_error &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
try {
double det = matrix2.determinant();
} catch (const std::runtime_error &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
try {
double det = matrix1.determinant();
} catch (const std::runtime_error &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
}