diff --git a/sea_transport/entities/IEntity.h b/sea_transport/entities/IEntity.h index a1dbca3..9e577c9 100644 --- a/sea_transport/entities/IEntity.h +++ b/sea_transport/entities/IEntity.h @@ -8,7 +8,7 @@ typedef unsigned long long entity_id; class IEntity : public ISerializable { public: - virtual entity_id id() = 0; + virtual entity_id id() const = 0; }; #endif // IENTITY_H diff --git a/sea_transport/entities/cargo_entity.cpp b/sea_transport/entities/cargo_entity.cpp index 4a689f6..4104050 100644 --- a/sea_transport/entities/cargo_entity.cpp +++ b/sea_transport/entities/cargo_entity.cpp @@ -8,15 +8,15 @@ cargo_entity::cargo_entity(const QString &title, unsigned int volume) : _title(t } } -entity_id cargo_entity::id() { +entity_id cargo_entity::id() const { return this->_id; } -QString cargo_entity::title() { +QString cargo_entity::title() const { return this->_title; } -unsigned int cargo_entity::volume() { +unsigned int cargo_entity::volume() const { return this->_volume; } diff --git a/sea_transport/entities/cargo_entity.h b/sea_transport/entities/cargo_entity.h index 07d151b..8d84b3a 100644 --- a/sea_transport/entities/cargo_entity.h +++ b/sea_transport/entities/cargo_entity.h @@ -17,9 +17,9 @@ public: cargo_entity() = default; cargo_entity(const QString &title, unsigned int volume); - entity_id id(); - QString title(); - unsigned int volume(); + entity_id id() const; + QString title() const; + unsigned int volume() const; void serialize(QDataStream &output); void deserialize(QDataStream &input); diff --git a/sea_transport/entities/dpoint_entity.cpp b/sea_transport/entities/dpoint_entity.cpp index 2a2d48d..abc256a 100644 --- a/sea_transport/entities/dpoint_entity.cpp +++ b/sea_transport/entities/dpoint_entity.cpp @@ -8,11 +8,11 @@ dpoint_entity::dpoint_entity(const QString &title) : _title(title) { } } -entity_id dpoint_entity::id() { +entity_id dpoint_entity::id() const { return this->_id; } -QString dpoint_entity::title() { +QString dpoint_entity::title() const { return this->_title; } diff --git a/sea_transport/entities/dpoint_entity.h b/sea_transport/entities/dpoint_entity.h index 23aebb6..037f8ff 100644 --- a/sea_transport/entities/dpoint_entity.h +++ b/sea_transport/entities/dpoint_entity.h @@ -19,8 +19,8 @@ public: dpoint_entity() = default; dpoint_entity(const QString &title); - entity_id id(); - QString title(); + entity_id id() const; + QString title() const; const QVector storages(); void serialize(QDataStream &output); diff --git a/sea_transport/entities/storage_entity.cpp b/sea_transport/entities/storage_entity.cpp index 3e9aae3..5e55f4c 100644 --- a/sea_transport/entities/storage_entity.cpp +++ b/sea_transport/entities/storage_entity.cpp @@ -8,11 +8,11 @@ storage_entity::storage_entity(unsigned int capacity) : _capacity(capacity) { } -entity_id storage_entity::id() { +entity_id storage_entity::id() const { return this->_id; } -unsigned int storage_entity::capacity() { +unsigned int storage_entity::capacity() const { return this->_capacity; } diff --git a/sea_transport/entities/storage_entity.h b/sea_transport/entities/storage_entity.h index 0bc9f61..3b8abf5 100644 --- a/sea_transport/entities/storage_entity.h +++ b/sea_transport/entities/storage_entity.h @@ -20,8 +20,8 @@ public: storage_entity() = default; storage_entity(unsigned int capacity); - entity_id id(); - unsigned int capacity(); + entity_id id() const; + unsigned int capacity() const; const QVector cargo(); void add_cargo(cargo_entity object, bool &success); diff --git a/sea_transport/entities/user_entity.cpp b/sea_transport/entities/user_entity.cpp index 4a84f67..5cd56a7 100644 --- a/sea_transport/entities/user_entity.cpp +++ b/sea_transport/entities/user_entity.cpp @@ -4,15 +4,15 @@ user_entity::user_entity(const QString &login, const QString &password, UserRole this->_pwd_hash = QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha3_256); } -entity_id user_entity::id() { +entity_id user_entity::id() const { return this->_id; } -const QString user_entity::login() { +const QString user_entity::login() const { return this->_login; } -UserRole user_entity::role() { +UserRole user_entity::role() const { return this->_role; } diff --git a/sea_transport/entities/user_entity.h b/sea_transport/entities/user_entity.h index 3926527..890279c 100644 --- a/sea_transport/entities/user_entity.h +++ b/sea_transport/entities/user_entity.h @@ -23,9 +23,9 @@ public: user_entity() = default; user_entity(const QString &login, const QString &password, UserRole role); - entity_id id(); - const QString login(); - UserRole role(); + entity_id id() const; + const QString login() const; + UserRole role() const; bool verify_password(const QString &password) const; void serialize(QDataStream &output); diff --git a/sea_transport/entities/vessel_entity.cpp b/sea_transport/entities/vessel_entity.cpp index 9dc49aa..b4ccdc7 100644 --- a/sea_transport/entities/vessel_entity.cpp +++ b/sea_transport/entities/vessel_entity.cpp @@ -7,15 +7,15 @@ vessel_entity::vessel_entity(const dpoint_entity &harbor, unsigned int capacity) this->_id = ++vessel_entity::__global_id; } -entity_id vessel_entity::id() { +entity_id vessel_entity::id() const { return this->_id; } -const dpoint_entity vessel_entity::harbor() { +const dpoint_entity vessel_entity::harbor() const { return this->_harbor; } -unsigned int vessel_entity::capacity() { +unsigned int vessel_entity::capacity() const { return this->_capacity; } diff --git a/sea_transport/entities/vessel_entity.h b/sea_transport/entities/vessel_entity.h index 9a28810..f381f37 100644 --- a/sea_transport/entities/vessel_entity.h +++ b/sea_transport/entities/vessel_entity.h @@ -19,9 +19,9 @@ public: vessel_entity() = default; vessel_entity(const dpoint_entity &harbor, unsigned int capacity); - entity_id id(); - const dpoint_entity harbor(); - unsigned int capacity(); + entity_id id() const; + const dpoint_entity harbor() const; + unsigned int capacity() const; const QVector cargo(); void serialize(QDataStream &output); diff --git a/sea_transport/system/apparatus.cpp b/sea_transport/system/apparatus.cpp index 409037d..43747f5 100644 --- a/sea_transport/system/apparatus.cpp +++ b/sea_transport/system/apparatus.cpp @@ -4,16 +4,9 @@ apparatus *apparatus::_instance = nullptr; const QString apparatus::filename = "data.bin"; -void apparatus::open_reading_stream() { +void apparatus::open_stream() { this->_bin_file = new QFile(apparatus::filename); - this->_bin_file->open(QIODevice::ReadOnly); - - stream.setDevice(_bin_file); -} - -void apparatus::open_writing_stream() { - this->_bin_file = new QFile(apparatus::filename); - this->_bin_file->open(QIODevice::WriteOnly); + this->_bin_file->open(QIODevice::ReadWrite); stream.setDevice(_bin_file); } @@ -27,35 +20,27 @@ void apparatus::close_stream() { } apparatus::apparatus() { - this->open_reading_stream(); - this->loadGIDS(); - this->deserialize_data(); - this->close_stream(); + } apparatus::~apparatus() { - if (this->_bin_file) { - this->_bin_file->flush(); - this->_bin_file->close(); - delete this->_bin_file; - this->_bin_file = nullptr; - } + this->shutdown(); } -apparatus& apparatus::instance() { +apparatus* apparatus::instance() { if (apparatus::_instance == nullptr) { - apparatus::init(); + throw std::runtime_error("System non initialized!"); } - return *apparatus::_instance; + return apparatus::_instance; } bool apparatus::isFirstRun() { - return QFile(apparatus::filename).exists(); + return QFile().exists("init"); } void apparatus::generate_empty_data() { - this->open_writing_stream(); + this->open_stream(); this->writeGIDS(); this->serialize_data(); this->close_stream(); @@ -71,15 +56,18 @@ const object_system& apparatus::get_object_subsystem() { void apparatus::init() { apparatus::_instance = new apparatus(); + + apparatus::instance()->open_stream(); + apparatus::instance()->loadGIDS(); + apparatus::instance()->deserialize_data(); + apparatus::instance()->close_stream(); } void apparatus::shutdown() { - if (apparatus::_instance != nullptr) { - apparatus::_instance->open_writing_stream(); - apparatus::_instance->writeGIDS(); - apparatus::_instance->serialize_data(); - apparatus::_instance->close_stream(); - } + apparatus::instance()->open_stream(); + apparatus::instance()->writeGIDS(); + apparatus::instance()->serialize_data(); + apparatus::instance()->close_stream(); } void apparatus::writeGIDS() { @@ -101,6 +89,7 @@ void apparatus::serialize_data() { } void apparatus::deserialize_data() { + QFile("init").open(QIODevice::ReadWrite); this->_auth_system.shutdown(this->stream); this->_object_system.shutdown(this->stream); } diff --git a/sea_transport/system/apparatus.h b/sea_transport/system/apparatus.h index 6ebc132..3493115 100644 --- a/sea_transport/system/apparatus.h +++ b/sea_transport/system/apparatus.h @@ -4,13 +4,13 @@ #include "auth_system.h" #include "object_system.h" +#include "../entities/vessel_entity.h" +#include "../entities/storage_entity.h" + #include #include #include -#include -#include - class apparatus { @@ -24,8 +24,7 @@ private: auth_system _auth_system; object_system _object_system; - void open_reading_stream(); - void open_writing_stream(); + void open_stream(); void close_stream(); void writeGIDS(); @@ -43,7 +42,7 @@ public: const auth_system& get_auth_subsystem(); const object_system& get_object_subsystem(); - static apparatus& instance(); + static apparatus* instance(); static void init(); static void shutdown(); }; diff --git a/sea_transport/system/auth_system.h b/sea_transport/system/auth_system.h index b0f7cab..52849e1 100644 --- a/sea_transport/system/auth_system.h +++ b/sea_transport/system/auth_system.h @@ -3,7 +3,7 @@ #include -#include +#include "../entities/user_entity.h" class auth_system diff --git a/sea_transport/system/object_system.h b/sea_transport/system/object_system.h index 9ee0f99..18285bb 100644 --- a/sea_transport/system/object_system.h +++ b/sea_transport/system/object_system.h @@ -3,8 +3,8 @@ #include -#include -#include +#include "../entities/dpoint_entity.h" +#include "../entities/vessel_entity.h" class object_system diff --git a/sea_transport_project.pro b/sea_transport_project.pro index 18ba504..16d97ce 100644 --- a/sea_transport_project.pro +++ b/sea_transport_project.pro @@ -1,4 +1,5 @@ TEMPLATE = subdirs SUBDIRS += \ - sea_transport + sea_transport \ + st_test diff --git a/sea_transport_project.pro.user b/sea_transport_project.pro.user index 06ce466..62fefe9 100644 --- a/sea_transport_project.pro.user +++ b/sea_transport_project.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -19,7 +19,7 @@ Cpp - CppGlobal + qt2 @@ -65,24 +65,20 @@ true - Checked - Checked - Checked - Checked - Checked - Checked - Checked - Checked - Checked - Checked - Checked - Checked - Checked - Checked - Checked + Checked + Checked + Checked + Checked + Checked + Checked 0 true + + -fno-delayed-template-parsing + + true + Builtin.Questionable true Builtin.DefaultTidyAndClazy @@ -103,7 +99,7 @@ qt.qt5.5150.win64_mingw81_kit 0 0 - 0 + 1 true 0 @@ -336,9 +332,8 @@ 2 - sea_transport2 - Qt4ProjectManager.Qt4RunConfiguration:C:/Users/Admin/Documents/repos/sea_transport/sea_transport/sea_transport.pro - C:/Users/Admin/Documents/repos/sea_transport/sea_transport/sea_transport.pro + Qt4ProjectManager.Qt4RunConfiguration:C:/Users/Admin/Documents/repos/sea_transport_project/sea_transport/sea_transport.pro + C:/Users/Admin/Documents/repos/sea_transport_project/sea_transport/sea_transport.pro false @@ -351,7 +346,82 @@ C:/Users/Admin/Documents/repos/build-sea_transport_project-Desktop_Qt_5_15_0_MinGW_64_bit-Debug/sea_transport - 1 + + dwarf + + cpu-cycles + + + 250 + + -e + cpu-cycles + --call-graph + dwarf,4096 + -F + 250 + + -F + true + 4096 + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + kcachegrind + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + + 2 + + Qt4ProjectManager.Qt4RunConfiguration:C:/Users/Admin/Documents/repos/sea_transport_project/st_test/st_test.pro + C:/Users/Admin/Documents/repos/sea_transport_project/st_test/st_test.pro + + false + + false + true + true + false + false + true + + C:/Users/Admin/Documents/repos/build-sea_transport_project-Desktop_Qt_5_15_0_MinGW_64_bit-Debug/st_test + + 2 diff --git a/st_test/st_test.pro b/st_test/st_test.pro new file mode 100644 index 0000000..b99df5e --- /dev/null +++ b/st_test/st_test.pro @@ -0,0 +1,29 @@ +QT += testlib +QT -= gui + +CONFIG += qt console warn_on depend_includepath testcase +CONFIG -= app_bundle + +TEMPLATE = app + +SOURCES += tst_st_test.cpp \ + ../sea_transport/entities/cargo_entity.cpp \ + ../sea_transport/entities/storage_entity.cpp \ + ../sea_transport/entities/dpoint_entity.cpp \ + ../sea_transport/entities/vessel_entity.cpp \ + ../sea_transport/entities/user_entity.cpp \ + ../sea_transport/system/auth_system.cpp \ + ../sea_transport/system/object_system.cpp \ + ../sea_transport/system/apparatus.cpp + +HEADERS += \ + ../sea_transport/entities/ISerializable.h \ + ../sea_transport/entities/IEntity.h \ + ../sea_transport/entities/cargo_entity.h \ + ../sea_transport/entities/storage_entity.h \ + ../sea_transport/entities/dpoint_entity.h \ + ../sea_transport/entities/vessel_entity.h \ + ../sea_transport/entities/user_entity.h \ + ../sea_transport/system/auth_system.h \ + ../sea_transport/system/object_system.h \ + ../sea_transport/system/apparatus.h diff --git a/st_test/tst_st_test.cpp b/st_test/tst_st_test.cpp new file mode 100644 index 0000000..8a21a9f --- /dev/null +++ b/st_test/tst_st_test.cpp @@ -0,0 +1,287 @@ +#include + +#include "../sea_transport/entities/cargo_entity.h" +#include "../sea_transport/entities/storage_entity.h" +#include "../sea_transport/entities/dpoint_entity.h" +#include "../sea_transport/entities/vessel_entity.h" +#include "../sea_transport/entities/user_entity.h" + +#include "../sea_transport/system/apparatus.h" + +#include +#include + +class st_test : public QObject +{ + Q_OBJECT + +public: + st_test(); + ~st_test(); + +private slots: + void initTestCase(); + void cleanupTestCase(); + + void cargo_entity_serialization_test(); + void storage_entity_serialization_test(); + void dpoint_entity_serialization_test(); + void vessel_entity_serialization_test(); + void user_entity_serialization_test(); + + void user_entity_password_verification_test(); + void apparatus_check_null_throws_error(); + void apparatus_check_first_run(); + void apparatus_check_auth_subsystem(); + void apparatus_check_object_subsystem(); +}; + +st_test::st_test() { + +} + +st_test::~st_test() { + +} + +//================================================= +void st_test::initTestCase() { + QVERIFY2( + !QFile("data.bin").exists(), + "There should be no data file!" + ); +} + +void st_test::cleanupTestCase() { + QVERIFY2( + QFile("data.bin").exists(), + "There should be a data file!" + ); + QFile().remove("data.bin"); +} + +//================================================= +void st_test::cargo_entity_serialization_test() { + QDataStream stream; + cargo_entity ent1; + cargo_entity ent2; + + { + QFile f("test_file.dat"); + f.open(QIODevice::WriteOnly); + stream.setDevice(&f); + + ent1 = cargo_entity("test title", 256); + ent1.serialize(stream); + + stream.setDevice(nullptr); + } + + { + QFile f("test_file.dat"); + f.open(QIODevice::ReadOnly); + stream.setDevice(&f); + + ent2.deserialize(stream); + f.remove(); + } + + QVERIFY2( + ent1.id() == ent2.id() && ent1.title() == ent2.title() && ent1.volume() == ent2.volume(), + "Cargo entity not serialized properly" + ); +} + +void st_test::storage_entity_serialization_test() { + QDataStream stream; + storage_entity ent1; + storage_entity ent2; + + { + QFile f("test_file.dat"); + f.open(QIODevice::WriteOnly); + stream.setDevice(&f); + + ent1 = storage_entity(128); + ent1.serialize(stream); + + stream.setDevice(nullptr); + } + + { + QFile f("test_file.dat"); + f.open(QIODevice::ReadOnly); + stream.setDevice(&f); + + ent2.deserialize(stream); + f.remove(); + } + + QVERIFY2( + ent1.id() == ent2.id() && ent1.capacity() == ent2.capacity(), + "Storage entity not serialized properly" + ); +} + +void st_test::dpoint_entity_serialization_test() { + QDataStream stream; + dpoint_entity ent1; + dpoint_entity ent2; + + { + QFile f("test_file.dat"); + f.open(QIODevice::WriteOnly); + stream.setDevice(&f); + + ent1 = dpoint_entity("some_test_point"); + ent1.serialize(stream); + + stream.setDevice(nullptr); + } + + { + QFile f("test_file.dat"); + f.open(QIODevice::ReadOnly); + stream.setDevice(&f); + + ent2.deserialize(stream); + f.remove(); + } + + QVERIFY2( + ent1.id() == ent2.id() && ent1.title() == ent2.title(), + "Delivery Point entity not serialized properly" + ); +} + +void st_test::vessel_entity_serialization_test() { + QDataStream stream; + vessel_entity ent1; + vessel_entity ent2; + + { + QFile f("test_file.dat"); + f.open(QIODevice::WriteOnly); + stream.setDevice(&f); + + dpoint_entity test_harbor("test_harbor_for_vessel"); + ent1 = vessel_entity(test_harbor, 256); + ent1.serialize(stream); + + stream.setDevice(nullptr); + } + + { + QFile f("test_file.dat"); + f.open(QIODevice::ReadOnly); + stream.setDevice(&f); + + ent2.deserialize(stream); + f.remove(); + } + + QVERIFY2( + ent1.id() == ent2.id() && ent1.harbor().id() == ent2.harbor().id() && ent1.capacity() == ent2.capacity(), + "Delivery Point entity not serialized properly" + ); +} + +void st_test::user_entity_serialization_test() { + QDataStream stream; + user_entity ent1; + user_entity ent2; + QString test_password = "test_password"; + + { + QFile f("test_file.dat"); + f.open(QIODevice::WriteOnly); + stream.setDevice(&f); + + ent1 = user_entity("test_login", test_password, UserRole::ADMINISTRATOR); + ent1.serialize(stream); + + stream.setDevice(nullptr); + } + + { + QFile f("test_file.dat"); + f.open(QIODevice::ReadOnly); + stream.setDevice(&f); + + ent2.deserialize(stream); + f.remove(); + } + + QVERIFY2( + ent1.id() == ent2.id() && ent1.id() == ent2.id() && ent1.role() == ent2.role() && ent2.verify_password(test_password), + "User entity not serialized properly" + ); +} + +//================================================= +void st_test::user_entity_password_verification_test() { + QString test_password = "test_password"; + QString wrong_password = "wrong_password"; + user_entity ent1("test_login", test_password, UserRole::ADMINISTRATOR); + + QVERIFY2( + ent1.verify_password(test_password) || ent1.verify_password(wrong_password), + "Password verification failed!" + ); +} + +void st_test::apparatus_check_null_throws_error() { + QVERIFY_EXCEPTION_THROWN(apparatus::instance(), std::runtime_error); +} + +void st_test::apparatus_check_first_run() { + apparatus::init(); + QVERIFY2( + apparatus::instance()->isFirstRun(), + "Not a first run!" + ); + apparatus::shutdown(); +} + +void st_test::apparatus_check_auth_subsystem() { + apparatus::init(); + auth_system as = apparatus::instance()->get_auth_subsystem(); + { + bool test = as.register_user("testor", "passwd", UserRole::ADMINISTRATOR); + QVERIFY(test); + } + { + bool test; + as.get_user("testor", test); + QVERIFY(test); + } + { + bool test = as.remove_user("testor"); + QVERIFY(test); + } + apparatus::shutdown(); +} + +void st_test::apparatus_check_object_subsystem() { + apparatus::init(); + object_system os = apparatus::instance()->get_object_subsystem(); + dpoint_entity p("test"); + { + bool test = os.add_dpoint(p); + QVERIFY(test); + } + { + bool test; + os.get_dpoint(p.id(), test); + QVERIFY(test); + } + { + bool test = os.remove_dpoint(p.id()); + QVERIFY(test); + } + apparatus::shutdown(); +} + +QTEST_APPLESS_MAIN(st_test) + +#include "tst_st_test.moc"