diff --git a/.gitignore b/.gitignore index 2bd99ec81..9c636d134 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ jnq* *.mod *.pro.user *.txt +!**/CMakeLists.txt cmake-build-debug cmake-build-release CMakeFiles diff --git a/CMakeLists.txt b/CMakeLists.txt index f160d6b82..17f863042 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -926,7 +926,9 @@ endif () if (WSJT_GENERATE_DOCS) add_subdirectory (doc) endif (WSJT_GENERATE_DOCS) - +if (EXISTS ${CMAKE_SOURCE_DIR}/tests AND IS_DIRECTORY ${CMAKE_SOURCE_DIR}/tests) + add_subdirectory (tests) +endif () # # Library building setup diff --git a/qt_helpers.cpp b/qt_helpers.cpp index 3f41d0f87..dd474e191 100644 --- a/qt_helpers.cpp +++ b/qt_helpers.cpp @@ -37,14 +37,14 @@ void update_dynamic_property (QWidget * widget, char const * property, QVariant widget->update (); } -QDateTime qt_round_date_time_to (QDateTime dt, int seconds) +QDateTime qt_round_date_time_to (QDateTime dt, int milliseconds) { - dt.setSecsSinceEpoch (dt.addSecs (seconds - 1).toSecsSinceEpoch () / seconds * seconds); + dt.setMSecsSinceEpoch (dt.addMSecs (milliseconds / 2).toMSecsSinceEpoch () / milliseconds * milliseconds); return dt; } -QDateTime qt_truncate_date_time_to (QDateTime dt, int seconds) +QDateTime qt_truncate_date_time_to (QDateTime dt, int milliseconds) { - dt.setSecsSinceEpoch (dt.toSecsSinceEpoch () / seconds * seconds); + dt.setMSecsSinceEpoch (dt.toMSecsSinceEpoch () / milliseconds * milliseconds); return dt; } diff --git a/qt_helpers.hpp b/qt_helpers.hpp index 638c50e2a..aae8c5f07 100644 --- a/qt_helpers.hpp +++ b/qt_helpers.hpp @@ -69,11 +69,11 @@ QString font_as_stylesheet (QFont const&); // conditional style sheet updates void update_dynamic_property (QWidget *, char const * property, QVariant const& value); -// round a QDateTime instance to an interval -QDateTime qt_round_date_time_to (QDateTime dt, int seconds); +// round a QDateTime instance to an integral interval of milliseconds +QDateTime qt_round_date_time_to (QDateTime dt, int milliseconds); -// truncate a QDateTime to an interval -QDateTime qt_truncate_date_time_to (QDateTime dt, int seconds); +// truncate a QDateTime to an integral interval of milliseconds +QDateTime qt_truncate_date_time_to (QDateTime dt, int milliseconds); template class VPtr diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 000000000..4b3462337 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,23 @@ +find_package (Qt5Test 5 REQUIRED) + +# +# Compiler options +# +set (CMAKE_CXX_STANDARD 11) +add_compile_options ("$<$:-Wall;-Wno-conversion;-fno-second-underscore;-fno-f2c>") +add_compile_options ("$<$,$>:-fbounds-check>") +add_compile_options ("$<$,$>>:-funroll-all-loops>") +add_compile_options ("$<$,$>,$,$,$>>:-Wall;-Wextra>") +add_compile_options ("$<$,$>:-Wno-pragmas>") +add_compile_options ("$<$,$>,$>>:-fdata-sections;-ffunction-sections>") +if (${OPENMP_FOUND} OR APPLE) + add_compile_options ("$<$,$>,$>:${OpenMP_C_FLAGS}>") +endif () + +# Tell CMake to run moc when necessary +set (CMAKE_AUTOMOC ON) +include_directories (${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}) + +add_executable (test_qt_helpers test_qt_helpers.cpp) +target_link_libraries (test_qt_helpers wsjt_qt Qt5::Test) +add_test (test_qt_helpers test_qt_helpers) diff --git a/tests/test_qt_helpers.cpp b/tests/test_qt_helpers.cpp new file mode 100644 index 000000000..cb6744df2 --- /dev/null +++ b/tests/test_qt_helpers.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + +#include "qt_helpers.hpp" + +class TestQtHelpers + : public QObject +{ + Q_OBJECT + +public: + +private: + Q_SLOT void round_15s_date_time_up () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 500}}; + QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 30))); + } + + Q_SLOT void truncate_15s_date_time_up () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 500}}; + QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15))); + } + + Q_SLOT void round_15s_date_time_down () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 499}}; + QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15))); + } + + Q_SLOT void truncate_15s_date_time_down () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 499}}; + QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15))); + } + + Q_SLOT void round_15s_date_time_on () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 15}}; + QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15))); + } + + Q_SLOT void truncate_15s_date_time_on () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 15}}; + QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15))); + } + + Q_SLOT void round_15s_date_time_under () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 14, 999}}; + QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15))); + } + + Q_SLOT void truncate_15s_date_time_under () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 14, 999}}; + QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15))); + } + + Q_SLOT void round_15s_date_time_over () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 15, 1}}; + QCOMPARE (qt_round_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15))); + } + + Q_SLOT void truncate_15s_date_time_over () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 15, 1}}; + QCOMPARE (qt_truncate_date_time_to (dt, 15000), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15))); + } + + Q_SLOT void round_7p5s_date_time_up () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 26, 250}}; + QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 30))); + } + + Q_SLOT void truncate_7p5s_date_time_up () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 26, 250}}; + QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500))); + } + + Q_SLOT void round_7p5s_date_time_down () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 26, 249}}; + QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500))); + } + + Q_SLOT void truncate_7p5s_date_time_down () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 26, 249}}; + QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500))); + } + + Q_SLOT void round_7p5s_date_time_on () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 500}}; + QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500))); + } + + Q_SLOT void truncate_7p5s_date_time_on () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 500}}; + QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500))); + } + + Q_SLOT void round_7p5s_date_time_under () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 499}}; + QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500))); + } + + Q_SLOT void truncate_7p5s_date_time_under () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 499}}; + QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 15))); + } + + Q_SLOT void round_7p5s_date_time_over () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 501}}; + QCOMPARE (qt_round_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500))); + } + + Q_SLOT void truncate_7p5s_date_time_over () + { + QDateTime dt {QDate {2020, 8, 6}, QTime {14, 15, 22, 501}}; + QCOMPARE (qt_truncate_date_time_to (dt, 7500), QDateTime (QDate (2020, 8, 6), QTime (14, 15, 22, 500))); + } +}; + +QTEST_MAIN (TestQtHelpers); + +#include "test_qt_helpers.moc" diff --git a/widgets/mainwindow.cpp b/widgets/mainwindow.cpp index 6c2a32c25..b91207089 100644 --- a/widgets/mainwindow.cpp +++ b/widgets/mainwindow.cpp @@ -8017,8 +8017,8 @@ void MainWindow::p1ReadFromStdout() //p1readFromStdout QString MainWindow::beacon_start_time (int n) { - auto bt = qt_truncate_date_time_to (QDateTime::currentDateTimeUtc ().addSecs (n), m_TRperiod); - if (m_TRperiod < 60) + auto bt = qt_truncate_date_time_to (QDateTime::currentDateTimeUtc ().addSecs (n), m_TRperiod * 1.e3); + if (m_TRperiod < 60.) { return bt.toString ("HHmmss"); }