initial commit

This commit is contained in:
Blue 2018-08-05 00:46:25 +03:00 committed by Юрий Губич
commit 4b60ece582
327 changed files with 28286 additions and 0 deletions

29
test/CMakeLists.txt Normal file
View file

@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 2.8.12)
project(test)
find_package(CxxTest)
if(CXXTEST_FOUND)
include_directories(${CXXTEST_INCLUDE_DIR})
enable_testing()
CXXTEST_ADD_TEST(testtypes testtypes.cpp ${CMAKE_CURRENT_SOURCE_DIR}/testTypes.h)
target_link_libraries(testtypes wType)
CXXTEST_ADD_TEST(testtools testtools.cpp ${CMAKE_CURRENT_SOURCE_DIR}/testTools.h)
target_link_libraries(testtools tools)
target_link_libraries(testtools tag)
find_package(Qt5Core REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
CXXTEST_ADD_TEST(testsocket testsocket.cpp ${CMAKE_CURRENT_SOURCE_DIR}/testSocket.h)
target_link_libraries(testsocket Qt5::Core)
target_link_libraries(testsocket wSocket)
CXXTEST_ADD_TEST(testdispatcher testdispatcher.cpp ${CMAKE_CURRENT_SOURCE_DIR}/testDispatcher.h)
target_link_libraries(testdispatcher Qt5::Core)
target_link_libraries(testdispatcher wDispatcher)
target_link_libraries(testdispatcher wType)
endif()

92
test/testDispatcher.h Normal file
View file

@ -0,0 +1,92 @@
#ifndef TESTDISPATCHER_H
#define TESTDISPATCHER_H
#include <cxxtest/TestSuite.h>
#include <QtCore/QCoreApplication>
#include <QtCore/QTimer>
#include <wDispatcher/dispatcher.h>
#include <wDispatcher/handler.h>
#include <wType/event.h>
class TestObject: public QObject
{
Q_OBJECT
public:
TestObject(const W::Address& addr, W::Dispatcher* p_dp):
QObject(),
right_h(0),
wrong_h(0),
dp(p_dp)
{
right_h = W::Handler::create(addr + W::Address({u"right"}), this, &TestObject::right);
wrong_h = W::Handler::create(addr + W::Address({u"wrong"}), this, &TestObject::wrong);
dp->registerHandler(right_h);
dp->registerHandler(wrong_h);
}
~TestObject()
{
dp->unregisterHandler(right_h);
dp->unregisterHandler(wrong_h);
delete right_h;
delete wrong_h;
}
void right(const W::Object& data)
{
emit success();
}
void wrong(const W::Object& data)
{
}
W::Handler* right_h;
W::Handler* wrong_h;
W::Dispatcher* dp;
signals:
void success();
public slots:
void launch()
{
W::Event ev(W::Address({u"client", u"123", u"some_hop", u"main", u"right"}), W::String(u"hello!"));
dp->pass(ev);
}
};
class TestDispatcher : public CxxTest::TestSuite
{
public:
void testEventPassing()
{
char a1[] = "nothing";
char* argv[] = {a1};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
QCoreApplication app (argc, argv);
W::Dispatcher* root_dp = new W::Dispatcher();
TestObject *test_object = new TestObject(W::Address({u"client", u"123", u"some_hop", u"main"}), root_dp);
QObject::connect(test_object, SIGNAL(success()), &app, SLOT(quit()));
QTimer::singleShot(0, test_object, SLOT(launch()));
app.exec();
delete test_object;
delete root_dp;
}
};
#endif //TESTDISPATCHER_H

98
test/testSocket.h Normal file
View file

@ -0,0 +1,98 @@
#ifndef TESTSOCKET_H
#define TESTSOCKET_H
#include <cxxtest/TestSuite.h>
#include <QtCore/QCoreApplication>
#include <wSocket/socket.h>
#include <wSocket/server.h>
class TestServer : public QObject
{
Q_OBJECT
public:
TestServer(QObject* parent):
QObject(parent),
server(new W::Server(W::String(u"test_server"), this))
{
connect(server, SIGNAL(newConnection(const W::Socket&)), SLOT(onNewConnection(const W::Socket&)));
server->listen(8080);
}
private:
W::Server *server;
signals:
void success();
private slots:
void onNewConnection(const W::Socket& socket)
{
connect(&socket, SIGNAL(message(const W::Event&)), SLOT(onSocketMessage(const W::Event&)));
}
void onSocketMessage(const W::Event& event)
{
W::Socket* socket = static_cast<W::Socket*>(sender());
W::Address addr({socket->getName()});
const W::String& msg = static_cast<const W::String&>(event.getData());
TS_ASSERT_EQUALS(addr, event.getDestination());
TS_ASSERT_EQUALS(msg, u"Hello, dear test server!");
emit success();
}
};
class TestClient : public QObject
{
Q_OBJECT
public:
TestClient(QObject* parent):
QObject(parent),
socket(new W::Socket(W::String(u"test_client"), this))
{
connect(socket, SIGNAL(connected()), SLOT(onConnected()));
socket->open(W::String(u"localhost"), W::Uint64(8080));
}
private:
W::Socket *socket;
private slots:
void onConnected()
{
W::Address addr({socket->getRemoteName()});
W::String message(u"Hello, dear test server!");
W::Event ev(addr, message);
ev.setSenderId(socket->getId());
socket->send(ev);
}
};
class TestSocket : public CxxTest::TestSuite
{
public:
void testHandshake()
{
char a1[] = "nothing";
char* argv[] = {a1};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
QCoreApplication* app = new QCoreApplication(argc, argv);
TestServer* srv = new TestServer(app);
TestClient* cli = new TestClient(app);
QObject::connect(srv, SIGNAL(success()), app, SLOT(quit()));
app->exec();
delete app;
}
};
#endif //TESTSOCKET_H

26
test/testTools.h Normal file
View file

@ -0,0 +1,26 @@
#ifndef TESTUTILS_H
#define TESTUTILS_H
#include <cxxtest/TestSuite.h>
#include <tools/file.h>
#include <wType/string.h>
#include <taglib/fileref.h>
#include <iostream>
class TestUtils : public CxxTest::TestSuite
{
public:
void testFile() {
TagLib::FileRef ref("/home/betrayer/Music/Disturbed/Indestructible/Façade.mp3");
TS_ASSERT_EQUALS(ref.tag()->title().to8Bit(true), "Façade");
W::String wPath(u"/home/betrayer/Music/Disturbed/Indestructible/Façade.mp3");
W::String wTitle(u"Façade");
TagLib::FileRef ref2(wPath.toString().c_str());
TS_ASSERT_EQUALS(W::String(ref.tag()->title().to8Bit(true)), wTitle);
}
};
#endif //TESTUTILS_H

317
test/testTypes.h Normal file
View file

@ -0,0 +1,317 @@
#ifndef TESTTYPES_H
#define TESTTYPES_H
#include <cxxtest/TestSuite.h>
#include <string>
#include <wType/object.h>
#include <wType/bytearray.h>
#include <wType/string.h>
#include <wType/vocabulary.h>
#include <wType/uint64.h>
#include <wType/address.h>
#include <wType/boolean.h>
#include <wType/event.h>
#include <wType/vector.h>
class TestTypes : public CxxTest::TestSuite
{
public:
void testStringSize()
{
W::String str(u"hey!");
TS_ASSERT_EQUALS(str.length(), 4);
}
void testStringToString()
{
W::String str(u"hello world!");
TS_ASSERT_EQUALS(str.toString(), "hello world!");
}
void testStringToString2()
{
W::String str(u"Сраные стандарты стингов!");
TS_ASSERT_EQUALS(str.toString(), "Сраные стандарты стингов!");
}
void testStringCopying()
{
W::String str(u"string");
W::String str2 = str;
}
void testStringSerialization()
{
W::String str(u"serialization");
int testSize = 13*2 + 4; //16 bits for each symbol and 32 bytes for length
int size = str.size();
TS_ASSERT_EQUALS(size, testSize);
W::ByteArray bytes(size + 1); //one more byte for type
bytes.push8(str.getType());
str.serialize(bytes);
TS_ASSERT_EQUALS(bytes.size(), testSize + 1);
W::Object *obj = W::Object::fromByteArray(bytes);
W::String *str2 = static_cast<W::String*>(obj);
TS_ASSERT_EQUALS(bytes.size(), 0);
TS_ASSERT_EQUALS(str.toString(), "serialization");
TS_ASSERT_EQUALS(str2->toString(), "serialization");
delete obj;
}
void testStringSerialization2()
{
W::String str(u"разве сложно сразу сделать все нормально?!");
int testSize = 42*2 + 4; //16 bits for each symbol and 32 bytes for length
int size = str.size();
TS_ASSERT_EQUALS(size, testSize);
W::ByteArray bytes(size + 1); //one more byte for type
bytes.push8(str.getType());
str.serialize(bytes);
TS_ASSERT_EQUALS(bytes.size(), testSize + 1);
W::Object *obj = W::Object::fromByteArray(bytes);
W::String *str2 = static_cast<W::String*>(obj);
TS_ASSERT_EQUALS(bytes.size(), 0);
TS_ASSERT_EQUALS(str.toString(), "разве сложно сразу сделать все нормально?!");
TS_ASSERT_EQUALS(str2->toString(), "разве сложно сразу сделать все нормально?!");
TS_ASSERT_EQUALS(str, *str2);
delete obj;
}
void testUint64Serialization()
{
W::Uint64 a(895458745634);
W::ByteArray bytes(a.size() + 1);
bytes.push8(a.getType());
a.serialize(bytes);
TS_ASSERT_EQUALS(bytes.size(), 1 + 8);
W::Object *obj = W::Object::fromByteArray(bytes);
W::Uint64 *b = static_cast<W::Uint64*>(obj);
TS_ASSERT_EQUALS(a, *b);
delete obj;
}
void testVCSerialization()
{
W::String key1(u"foo");
W::String val1(u"bar");
W::Vocabulary vc;
vc.insert(key1, val1);
W::ByteArray bytes(vc.size() + 1);
TS_ASSERT_EQUALS(vc.length(), 1);
TS_TRACE(vc.toString());
bytes.push8(vc.getType());
vc.serialize(bytes);
W::Object* deserialized = W::Object::fromByteArray(bytes);
W::Vocabulary* dvc = static_cast<W::Vocabulary*>(deserialized);
TS_ASSERT_EQUALS(vc.length(), dvc->length());
W::String val2 = static_cast<const W::String&>(dvc->at(key1));
TS_ASSERT_EQUALS(val2, val1);
delete deserialized;
}
void testVectorSerialization()
{
W::String str1(u"foo");
W::String str2(u"bar");
W::Vector vec;
vec.push(str1);
vec.push(str2);
vec.push(str2);
vec.push(str2);
vec.push(str1);
W::ByteArray bytes(vec.size() + 1);
TS_ASSERT_EQUALS(vec.length(), 5);
TS_TRACE(vec.toString());
bytes.push8(vec.getType());
vec.serialize(bytes);
W::Object* deserialized = W::Object::fromByteArray(bytes);
W::Vector* dvec = static_cast<W::Vector*>(deserialized);
TS_ASSERT_EQUALS(vec.length(), dvec->length());
W::String str22 = static_cast<const W::String&>(dvec->at(3));
TS_ASSERT_EQUALS(str2, str22);
delete deserialized;
}
void testAddressOperators()
{
W::Address a1({u"hey"});
W::Address a2({u"hey", u"you"});
W::Address a3({u"hey1", u"you"});
W::Address a4({u"hey", u"you1"});
TS_ASSERT_EQUALS(a1, a1);
TS_ASSERT_DIFFERS(a1, a2);
TS_ASSERT_LESS_THAN(a1, a2);
TS_ASSERT_LESS_THAN(a2, a3);
TS_ASSERT_LESS_THAN(a2, a4);
TS_ASSERT_LESS_THAN(a4, a3);
}
void testAddressFunctions()
{
W::Address a1({u"1st", u"2nd", u"3rd", u"4th"});
W::Address a2 = a1 >> 1;
W::Address a3 = a1 << 1;
W::Address ae;
W::Address a4({u"1st"});
W::Address a5({u"1st", u"2nd"});
W::Address a6({u"1st", u"3rd"});
W::Address a7({u"3rd", u"4th"});
W::Address a8({u"4th"});
W::Address a2c({u"1st", u"2nd", u"3rd"});
W::Address a3c({u"2nd", u"3rd", u"4th"});
TS_ASSERT_EQUALS(a2, a2c);
TS_ASSERT_EQUALS(a3, a3c);
TS_ASSERT(a4.begins(ae));
TS_ASSERT(a4.ends(ae));
TS_ASSERT(a1.begins(ae));
TS_ASSERT(a1.ends(ae));
TS_ASSERT(a1.begins(a4));
TS_ASSERT(a1.begins(a5));
TS_ASSERT(!a1.begins(a6));
TS_ASSERT(a1.ends(a7));
TS_ASSERT(a1.ends(a8));
TS_ASSERT(!a1.ends(a6));
TS_ASSERT(a1.begins(a2c));
TS_ASSERT(a1.ends(a3c));
}
void testAddressSerialization()
{
W::Address addr({u"hello", u"world"});
W::ByteArray bytes(addr.size() + 1);
bytes.push8(addr.getType());
addr.serialize(bytes);
W::Object *obj = W::Object::fromByteArray(bytes);
W::Address *addrd = static_cast<W::Address*>(obj);
TS_ASSERT_EQUALS(addr, *addrd);
TS_TRACE(addr.toString());
TS_TRACE(addrd->toString());
delete addrd;
}
void testBooleanSerialization()
{
W::Boolean a(true);
W::Boolean b(false);
W::Boolean c;
W::Boolean d;
c = false;
d = true;
TS_ASSERT_EQUALS(a, true);
TS_ASSERT_EQUALS(b, false);
TS_ASSERT_EQUALS(c, false);
TS_ASSERT_EQUALS(d, true);
W::ByteArray bytes(a.size() + b.size() + c.size() + d.size() + 4);
bytes.push8(a.getType());
a.serialize(bytes);
bytes.push8(b.getType());
b.serialize(bytes);
bytes.push8(c.getType());
c.serialize(bytes);
bytes.push8(d.getType());
d.serialize(bytes);
W::Object *a_o = W::Object::fromByteArray(bytes);
W::Object *b_o = W::Object::fromByteArray(bytes);
W::Object *c_o = W::Object::fromByteArray(bytes);
W::Object *d_o = W::Object::fromByteArray(bytes);
W::Boolean *ad = static_cast<W::Boolean*>(a_o);
W::Boolean *bd = static_cast<W::Boolean*>(b_o);
W::Boolean *cd = static_cast<W::Boolean*>(c_o);
W::Boolean *dd = static_cast<W::Boolean*>(d_o);
TS_ASSERT_EQUALS(*ad, a);
TS_ASSERT_EQUALS(*bd, b);
TS_ASSERT_EQUALS(*cd, c);
TS_ASSERT_EQUALS(*dd, d);
TS_TRACE(ad->toString());
TS_TRACE(bd->toString());
delete ad;
delete bd;
delete cd;
delete dd;
}
void testEventSerialization()
{
W::Address dest({u"to", u"somebody"});
W::Uint64 id(5);
W::Vocabulary dat;
W::String val(u"some value");
W::Uint64 val2(7887198479813);
dat.insert(u"key1", val);
dat.insert(u"key2", val2);
W::Event ev(dest, dat);
ev.setSenderId(id);
W::ByteArray bytes(ev.size() + 1);
bytes.push8(ev.getType());
ev.serialize(bytes);
W::Object *obj = W::Object::fromByteArray(bytes);
W::Event *evd = static_cast<W::Event*>(obj);
TS_ASSERT_EQUALS(evd->isSystem(), false);
TS_ASSERT_EQUALS(evd->getDestination(), dest);
const W::Vocabulary vcd = static_cast<const W::Vocabulary&>(evd->getData());
TS_ASSERT_EQUALS(static_cast<const W::String&>(vcd.at(u"key1")), val);
TS_ASSERT_EQUALS(static_cast<const W::Uint64&>(vcd.at(u"key2")), val2);
TS_ASSERT_EQUALS(evd->getSenderId(), id);
delete obj;
}
};
#endif