-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.h
More file actions
269 lines (239 loc) · 7.77 KB
/
test.h
File metadata and controls
269 lines (239 loc) · 7.77 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
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
#ifndef TEST_H
#define TEST_H
#include "thing.h"
#include <float.h>
#include <iostream>
#include <QRandomGenerator>
#include <QDateTime>
#include <QStringList>
#include "list.h"
#include "iterator.h"
#include <QtDebug>
namespace Random
{
double realNumber(double from = -DBL_MAX/2, double to = DBL_MAX/2) {
return from + (to - from) * QRandomGenerator::global()->generateDouble();
}
int integer(int from = INT_MIN, int to = INT_MAX) {
return QRandomGenerator::global()->bounded(from, to);
}
QDateTime dateTime(QDateTime from, QDateTime to) {
return from.addSecs(static_cast<qint64>(QRandomGenerator::global()->
generate64()) % from.secsTo(to));
}
const QString& itemFrom(const QStringList &list) {
return list.at(QRandomGenerator::global()->bounded(list.length()));
}
QString ingredient() {
static QStringList ingredients = {"Вода", "Яйцо", "Сахар", "Молоко",
"Картофель", "Соль", "Кефир", "Яблоко",
"Мука", "Масло", "Сосиска", "Хлопья"};
return itemFrom(ingredients);
}
QString material() {
static QStringList woodTypes = {"Дуб", "Вишня", "Клён", "Орех",
"Палисандр", "Сосна", "Бук",
"Ясень", "Берёза", "Кедр"};
return itemFrom(woodTypes);
}
QString aminoacidName() {
static QStringList aminoacids = {"Глицин", "Аланин", "Валин", "Изолейцин",
"Лейцин", "Пролин", "Серин", "Треонин",
"Цистеин", "Метионин",
"Аспарагиновая кислота", "Аспарагин",
"Глутаминовая кислота", "Глутамин",
"Лизин", "Аргинин", "Гистидин",
"Фенилаланин", "Тирозин", "Триптофан"};
return itemFrom(aminoacids);
}
QChar aminoacidLetter() {
static QString letters = "GAVILPSTCMDNEQKRHFYW";
return letters.at(QRandomGenerator::global()->bounded(letters.length()));
}
QString studentFIO() {
static QStringList names = {"Алексей", "Анатолий", "Вячеслав", "Георгий",
"Евгений", "Кирилл", "Матвей", "Мухаммад",
"Никита", "Роман", "Сергей"},
surnames = {"Игнатьев", "Кабисов", "Кожевников", "Лернер",
"Лясковский", "Сафронов", "Умбрас", "Шадько",
"Штырлин", "Юсупов", "Якубов"},
patronymic = {"Александрович", "Андреевич", "Даниилович",
"Иванович", "Ильич", "Максимович",
"Павлович", "Романович", "Сергеевич", "Юрьевич", ""};
auto randomGenerator = QRandomGenerator::global();
return surnames.at(randomGenerator->bounded(surnames.size())) + " " +
names.at(randomGenerator->bounded(names.size())) + " " +
patronymic.at(randomGenerator->bounded(patronymic.size()));
}
};
inline std::string stdString(const QString &qstring) {
return qstring.toLocal8Bit().toStdString();
}
inline char stdChar(const QChar &qchar) {
return qchar.toLatin1();
}
/*
class YourClass {
private:
int _param1;
double _param2;
public:
int param3;
double param4;
YourClass();
YourClass(int value1, double value2, int value3, double value4);
YourClass(const YourClass &other);
int getParameter1() const;
double getParameter2() const;
void setParameter1(int newValue1);
void setParameter2(double newValue2);
};
*/
#define MIN_VOLUME 0.1
#define MIN_PRICE 0
namespace Test
{
void assertValidity(Thing &object)
{
assert(object.getVolume() >= MIN_VOLUME);
assert(object.getPrice() >= MIN_PRICE);
}
void assertEquality(Thing &first,Thing &second)
{
assert(first.getName() == second.getName());
assert(first.getVolume() == second.getVolume());
assert(first.getPrice() == second.getPrice());
}
void assertStability(Thing &object)
{
int value1 = Random::integer(MIN_VOLUME);
object.setVolume(value1);
assert(object.getVolume() == value1);
int error1 = Random::integer(-999, MIN_VOLUME);
object.setVolume(error1);
assert(object.getVolume() != error1);
assert(object.getVolume() == value1);
QString name = Random::material();
object.setName(name);
assert(object.getName() == name);
double value2 = Random::realNumber(MIN_PRICE);
object.setPrice(value2);
assert(object.getPrice() == value2);
double error2 = Random::realNumber(-100, MIN_PRICE);
object.setPrice(error2);
assert(object.getPrice() == value2);
assert(object.getPrice() != error2);
}
void assertListCopying(){
List list1;
Thing a;
a.setName(QString("a"));
a.setVolume(1);
a.setPrice(1);
list1.add(a);
List list2(list1);
assert(list1 == list2);
}
void assertAddingAndDeleting(){
List list1;
Thing a;
a.setName(QString("a"));
a.setVolume(1);
a.setPrice(1);
Thing b;
b.setName(QString("b"));
b.setVolume(2);
b.setPrice(3);
int x = list1.len();
list1.add(a);
list1.add(b);
int y = list1.len();
assert(x == (y-2));
list1.deleteElement(b);
assert(list1.len() == (y-1));
list1.deleteElement(b);
assert(list1.len() == (y-1));
}
void assertClearing(){
List list1;
Thing a;
a.setName(QString("a"));
a.setVolume(1);
a.setPrice(1);
Thing b;
b.setName(QString("b"));
b.setVolume(2);
b.setPrice(3);
list1.add(a);
list1.add(b);
int x = list1.len();
list1.clearList();
List list2;
assert(list1.len() == 0);
assert(list2 == list1);
assert(list1.len() != x);
}
void printList(List &list){
Iterator itr = list.begin();
int i = 0;
for (;itr != list.end(); ++itr){
if((*itr)->how()){
Thing *temp = dynamic_cast<Thing*>(*itr);
qDebug() << i << temp->getInfo();
}
else {
CoinPile *temp = dynamic_cast<CoinPile*>(*itr);
qDebug() << i << temp->getInfo();
}
++i;
}
}
void assertIterator(){
List list1;
Thing a;
a.setName(QString("a"));
a.setVolume(1);
a.setPrice(1);
Thing b;
b.setName(QString("b"));
b.setVolume(2);
b.setPrice(3);
list1.add(a);
list1.add(b);
printList(list1);
}
void assertFileWork(){
List list1;
Thing a(QString("a"), 1, 1);
Thing b(QString("b"), 2, 3);
list1.add(a);
list1.add(b);
list1.writeToFile(QString("1"));
List list2;
list2.readListFromFile(QString("1"));
assert(list1 == list2);
}
void run() {
Thing defaultObject;
assertValidity(defaultObject);
for (int i = 0; i < 1000; i++)
{
Thing randomObject(Random::material(),
Random::realNumber(MIN_PRICE - 10),
Random::realNumber(-1e9, 1e9));
assertValidity(randomObject);
Thing copyObject(randomObject);
assertValidity(copyObject);
assertEquality(randomObject, copyObject);
assertStability(copyObject);
assertValidity(copyObject);
}
assertListCopying();
assertClearing();
assertAddingAndDeleting();
assertIterator();
assertFileWork();
std::cout << "\nВсе тесты успешно пройдены!\n";
}
};
#endif // TEST_H