-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
113 lines (96 loc) · 4.1 KB
/
Copy pathmain.cpp
File metadata and controls
113 lines (96 loc) · 4.1 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
#include <QCoreApplication>
#include <QRandomGenerator>
#include <stdio.h>
#include <string.h>
#include <QDebug>
#include <QThread>
extern "C" {
#include "../../../Libs/sheller/Source/sheller.h"
}
sheller_t shell;
#define TEST_START_BYTE 0x23
#define TEST_DATA_LENGTH 16
#define TEST_RX_BUFF_LENGTH 256
void test_full();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "App start";
test_full();
qDebug() << "\nApp end";
return a.exec();
}
void test_full()
{
qDebug() << "\nStart full test";
bool sheller_init_result = sheller_init(&shell, TEST_START_BYTE, TEST_DATA_LENGTH, TEST_RX_BUFF_LENGTH);
if (sheller_init_result == false) {
qDebug() << "sheller init result return false";
qDebug() << "Cannot start the test";
return;
}
uint64_t iteration_counter = 0;
while(1)
{
qDebug() << "Begin test iteration " << QString::number(iteration_counter++) << "--------------------------------";
uint8_t random_state = QRandomGenerator::global()->bounded(2); //0, 1, 2
qDebug() << "Test case " << QString::number(random_state);
uint8_t user_message[TEST_DATA_LENGTH] = {0};
for (int i = 0; i < TEST_DATA_LENGTH; ++i) {
user_message[i] = QRandomGenerator::global()->bounded(256);
}
qDebug() << "Generated user message: " << QByteArray((char*)user_message, TEST_DATA_LENGTH).toHex('.');
uint8_t user_message_wrapered[128] = {0};
uint8_t sheller_wrap_result = sheller_wrap(&shell, user_message, TEST_DATA_LENGTH, user_message_wrapered);
if (sheller_wrap_result == SHELLER_ERROR) {
qDebug() << "[ ERROR ] sheller_wrap return false";
qDebug() << "Stopping test...";
return;
}
qDebug() << "Wrappered user message: " << QByteArray((char*)user_message_wrapered, 11).toHex('.');
//Emulate normal packages
if (random_state == 1) {
for (int i = 0; i < sheller_get_package_length(&shell); ++i) {
if (sheller_push(&shell, user_message_wrapered[i]) == SHELLER_ERROR) {
qDebug() << "[ ERROR ] Sheller circular buffer overflow";
qDebug() << "Stopping test...";
return;
}
}
uint8_t received_message[TEST_DATA_LENGTH] = {0};
while(sheller_read(&shell, received_message) == SHELLER_ERROR) {
qDebug() << "Wait read in main";
}
uint8_t sheller_read_result = SHELLER_OK; //Костыль
if (sheller_read_result == SHELLER_OK) {
qDebug() << "Receive message: " << QByteArray((char*)received_message, TEST_DATA_LENGTH).toHex('.');
if(strncmp((const char*)user_message, (const char*)received_message, TEST_DATA_LENGTH) == 0) {
qDebug() << "Behaviour ok";
} else {
qDebug() << "\nBefore\nSheller buff: " << QByteArray((char*)shell.rx_buff, TEST_RX_BUFF_LENGTH).toHex('.');
qDebug() << "[ ERROR ] Not equil generated and received data";
qDebug() << "Stopping test...";
return;
}
} else {
qDebug() << "sheller_read return false in normal case";
qDebug() << "Stopping test...";
return;
}
}
//Emulate damaged packages
else if (random_state == 0) {
uint8_t count = QRandomGenerator::global()->bounded(1, TEST_DATA_LENGTH - 1); //!!!!!!!!!!!
for (uint8_t i = 0; i < count; ++i) {
if (sheller_push(&shell, user_message[i]) == SHELLER_ERROR) {
qDebug() << "[ ERROR ] Sheller circular buffer overflow";
qDebug() << "Stopping test...";
return;
}
}
}
//QThread().currentThread()->msleep(1);
qDebug() << "End iteration\n";
}
qDebug() << "\nEnd full test --------------------------------------------------------------------------";
}