diff --git a/.gitignore b/.gitignore index c795b05..567609b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -build \ No newline at end of file +build/ diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index e0e7d2a..76a2d2e 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -1,25 +1,38 @@ { "configurations": [ - { - "name": "MinGW", - "compilerPath": "C:/mingw64/bin/g++.exe", - "cStandard": "c11", - "cppStandard": "c++17", - "intelliSenseMode": "windows-gcc-x64", - - "includePath": [ - "${workspaceFolder}/**", - "C:/Qt/6.9.0/mingw_64/include", - "C:/Qt/6.9.0/mingw_64/include/QtCore", - "C:/Qt/6.9.0/mingw_64/include/QtGui", - "C:/Qt/6.9.0/mingw_64/include/QtWidgets", - "C:/Qt/6.9.0/mingw_64/include/QtNetwork", - "C:/Qt/6.9.0/mingw_64/include/QtWebSockets", - "C:/Qt/6.9.0/mingw_64/include/QtSerialPort" - ] - } + { + "name": "Linux", + "compilerPath": "/usr/bin/g++", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include", + "/usr/include/x86_64-linux-gnu", + "/usr/include/qt6", + "/usr/include/qt6/QtCore", + "/usr/include/qt6/QtGui", + "/usr/include/qt6/QtWidgets" + ] + }, + { + "name": "Windows", + "compilerPath": "C:/mingw64/bin/g++.exe", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**", + "C:/Qt/6.9.0/mingw_64/include", + "C:/Qt/6.9.0/mingw_64/include/QtCore", + "C:/Qt/6.9.0/mingw_64/include/QtGui", + "C:/Qt/6.9.0/mingw_64/include/QtWidgets", + "C:/Qt/6.9.0/mingw_64/include/QtNetwork", + "C:/Qt/6.9.0/mingw_64/include/QtWebSockets", + "C:/Qt/6.9.0/mingw_64/include/QtSerialPort" + ] + } ], - "version": 4, - - } - \ No newline at end of file + "version": 4 +} diff --git a/.vscode/launch.json b/.vscode/launch.json index 407ede1..f2f9c63 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -2,7 +2,26 @@ "version": "0.2.0", "configurations": [ { - "name": "Debug engine", + "name": "Debug engine (Linux)", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/engine", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "MIMode": "gdb", + "miDebuggerPath": "/usr/bin/gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + }, + { + "name": "Debug engine (Windows)", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/engine.exe", diff --git a/.vscode/settings.json b/.vscode/settings.json index c517793..b276301 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,7 @@ { "cmake.configureSettings": { - "CMAKE_PREFIX_PATH": "C:/Qt/6.9.0/mingw_64/lib/cmake" + "CMAKE_PREFIX_PATH": "C:/Qt/6.9.0/mingw_64/lib/cmake" }, - "files.associations": { "qudpsocket": "cpp", "qhostaddress": "cpp", @@ -99,4 +98,4 @@ "text_encoding": "cpp", "qtmath": "cpp" } -} \ No newline at end of file +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 08d9005..1eed6a3 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,28 +1,27 @@ { + "version": "2.0.0", "tasks": [ { - "type": "cppbuild", - "label": "C/C++: gcc build active file", - "command": "/usr/bin/gcc", - "args": [ - "-fdiagnostics-color=always", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}" - ], + "label": "build (Linux)", + "type": "shell", + "command": "cmake", + "args": ["--build", "build", "--target", "engine"], + "group": "build", + "problemMatcher": [] + }, + { + "label": "build (Windows)", + "type": "shell", + "command": "cmake", + "args": ["--build", "build", "--target", "engine"], "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true + "shell": { + "executable": "C:\\Windows\\System32\\cmd.exe", + "args": ["/d", "/c"] + } }, - "detail": "Task generated by Debugger." + "group": "build", + "problemMatcher": [] } - ], - "version": "2.0.0" -} \ No newline at end of file + ] +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 7510ec3..127de97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,13 @@ # The CMakeRoot handles all the libraries necessary to run the code cmake_minimum_required(VERSION 3.16) -project(engine VERSION 1.0.0 LANGUAGES CXX) +project(engine VERSION 1.0.0 LANGUAGES CXX C) set(TARGET engine) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Setup Qt6 -find_package(Qt6 REQUIRED COMPONENTS Core Network Gui Widgets WebSockets SerialPort) +find_package(Qt6 REQUIRED COMPONENTS Core Network Gui Widgets WebSockets SerialPort Test) qt_standard_project_setup() # Build protobuf @@ -51,3 +51,6 @@ FetchContent_MakeAvailable(sol2) add_subdirectory(src) +enable_testing() +add_subdirectory(tests) + diff --git a/src/radio/grsim.cpp b/src/radio/grsim.cpp index 8f309d0..c66cf42 100644 --- a/src/radio/grsim.cpp +++ b/src/radio/grsim.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include Grsim::Grsim() { sendSocket = new QUdpSocket(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..04dc8c7 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,14 @@ +add_executable(robotstate_test robotstate_test.cpp) +target_include_directories(robotstate_test PRIVATE + ${CMAKE_SOURCE_DIR}/src/utilities +) +target_link_libraries(robotstate_test Qt6::Test utilities) +add_test(NAME robotstate_test COMMAND robotstate_test) + +add_executable(world_test world_test.cpp) +target_include_directories(world_test PRIVATE + ${CMAKE_SOURCE_DIR}/src/utilities + ${CMAKE_SOURCE_DIR}/src/world +) +target_link_libraries(world_test Qt6::Test utilities world) +add_test(NAME world_test COMMAND world_test) diff --git a/tests/robotstate_test.cpp b/tests/robotstate_test.cpp new file mode 100644 index 0000000..bfafdf9 --- /dev/null +++ b/tests/robotstate_test.cpp @@ -0,0 +1,31 @@ +#include +#include "robotstate.hpp" + +class RobotStateTest : public QObject { + Q_OBJECT +private slots: + void defaultConstruction(); + void updateFunctions(); +}; + +void RobotStateTest::defaultConstruction() { + RobotState rs; + QCOMPARE(rs.getId(), 0); + QCOMPARE(rs.getTeam(), 0); + QCOMPARE(rs.getPosition(), QVector2D(0, 0)); + QCOMPARE(rs.getOrientation(), 0.0f); +} + +void RobotStateTest::updateFunctions() { + RobotState rs(1, 0); + rs.setPosition({1.0f, 2.0f}); + rs.setOrientation(0.5f); + rs.setVelocity({0.3f, -0.2f}); + + QCOMPARE(rs.getPosition(), QVector2D(1.0f, 2.0f)); + QCOMPARE(rs.getOrientation(), 0.5f); + QCOMPARE(rs.getVelocity(), QVector2D(0.3f, -0.2f)); +} + +QTEST_MAIN(RobotStateTest) +#include "robotstate_test.moc" diff --git a/tests/world_test.cpp b/tests/world_test.cpp new file mode 100644 index 0000000..28679c9 --- /dev/null +++ b/tests/world_test.cpp @@ -0,0 +1,32 @@ +#include +#include "world.hpp" + +class WorldTest : public QObject { + Q_OBJECT +private slots: + void robotUpdates(); + void jsonSerialization(); +}; + +void WorldTest::robotUpdates() { + World w(1,1); + QVector2D p(2.0f,3.0f); + w.updateRobot(0,0,p,1.0f); + RobotState r = w.getRobotState(0,0); + QCOMPARE(r.getPosition(), p); + QVERIFY(r.isActive()); +} + +void WorldTest::jsonSerialization() { + World w(1,0); + w.updateRobot(0,0,{1.0f,1.0f},0.0f); + QJsonObject obj = w.toJson(); + QVERIFY(obj.contains("robots")); + QJsonArray arr = obj["robots"].toArray(); + QCOMPARE(arr.size(), 1); + QJsonObject r = arr.at(0).toObject(); + QCOMPARE(r["id"].toInt(), 0); +} + +QTEST_MAIN(WorldTest) +#include "world_test.moc"