添加单元测试
This commit is contained in:
65
test/test_main.cpp
Normal file
65
test/test_main.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
// 版权所有 (c) ling 保留所有权利。
|
||||
// 除非另行说明,否则仅允许在Transmission中使用此文件中的代码。
|
||||
//
|
||||
// 由 ling 创建于 24-4-18.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include "Transmit.h"
|
||||
|
||||
static bool isExec = false;
|
||||
static const char *shortStr = "Client Hello";
|
||||
static const char *LongStr = "Client HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient HelloClient Hello";
|
||||
|
||||
class Transmit : public Transmission::Transmit {
|
||||
private:
|
||||
SOCKET fd;
|
||||
public:
|
||||
explicit Transmit(SOCKET fd, std::string ip) : Transmission::Transmit(fd, ip) {
|
||||
this->fd = fd;
|
||||
}
|
||||
|
||||
void packetReady(int type, std::shared_ptr<unsigned char> data, size_t size) override {
|
||||
if (type == 1)
|
||||
ASSERT_TRUE(strcmp((char *) data.get(), shortStr) == 0);
|
||||
else if (type == 2)
|
||||
ASSERT_TRUE(strcmp((char *) data.get(), LongStr) == 0);
|
||||
else
|
||||
ASSERT_TRUE(false);
|
||||
isExec = true;
|
||||
}
|
||||
|
||||
void pushData(unsigned char *data, int32_t size) const override {
|
||||
::write(this->fd, &size, sizeof(int32_t));
|
||||
::write(this->fd, data, size);
|
||||
::write(this->fd, &DATA_STOP, sizeof(DATA_STOP));
|
||||
}
|
||||
|
||||
void read() {
|
||||
int32_t size = 0;
|
||||
::read(this->fd, &size, sizeof(size));
|
||||
auto temp = new unsigned char[size];
|
||||
::read(this->fd, temp, size);
|
||||
int32_t stop = 0;
|
||||
::read(this->fd, &stop, sizeof(stop));
|
||||
dataArrives((unsigned char *) &size, sizeof(size));
|
||||
dataArrives(temp, size);
|
||||
dataArrives((unsigned char *) &stop, sizeof(stop));
|
||||
delete[] temp;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Transmit测试, 1) {
|
||||
int fds[2];
|
||||
pipe(fds);
|
||||
int read = fds[0];
|
||||
int write = fds[1];
|
||||
Transmit transmitRead(read, "");
|
||||
Transmit transmitWrite(write, "");
|
||||
transmitWrite.sendData((unsigned char *) shortStr, strlen(shortStr) + 1, 1);
|
||||
transmitRead.read();
|
||||
ASSERT_TRUE(isExec);
|
||||
isExec = false;
|
||||
transmitWrite.sendData((unsigned char *) LongStr, strlen(LongStr) + 1, 2);
|
||||
transmitRead.read();
|
||||
ASSERT_TRUE(isExec);
|
||||
}
|
||||
Reference in New Issue
Block a user