Skip to content

Commit adc9ad3

Browse files
Added googletest as submodule
- Googletest submodule added below Resources/thirdParty/googletest - Added Resources/test/OperatingSystem/test_MDDRealtimeSynchronize.cpp as googletest usage example (all test_MDDRealtimeSynchronize_* as unit tests) - Related to cmake versioning discussion in modelica-3rdparty#286
1 parent 166694d commit adc9ad3

File tree

8 files changed

+196
-25
lines changed

8 files changed

+196
-25
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "gtest"]
2+
path = Modelica_DeviceDrivers/Resources/thirdParty/googletest
3+
url = ../../google/googletest.git

.travis.yml

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
language: c
1+
language: cpp
22

33
git:
44
depth: 5
@@ -9,6 +9,7 @@ addons:
99
- ubuntu-toolchain-r-test
1010
packages:
1111
- gcc-4.8-multilib
12+
- g++-4.8-multilib
1213

1314
before_install:
1415
- sudo apt-get update -qq
@@ -22,8 +23,8 @@ compiler:
2223

2324
env:
2425
matrix:
25-
- CFLAGS="-O3 -DNDEBUG -fPIC -msse2 -m32"
26-
- CFLAGS="-O3 -DNDEBUG -fPIC"
26+
- CFLAGS="-O3 -DNDEBUG -fPIC -msse2 -m32" CXXFLAGS="-O3 -DNDEBUG -fPIC -msse2 -m32"
27+
- CFLAGS="-O3 -DNDEBUG -fPIC" CXXFLAGS="-O3 -DNDEBUG -fPIC"
2728

2829
before_script:
2930
- cd Modelica_DeviceDrivers/Resources
@@ -41,6 +42,7 @@ script:
4142
- ./test_MDDRealtimeSynchronize_idle
4243
- ./test_MDDRealtimeSynchronize_low
4344
- ./test_MDDRealtimeSynchronize_normal
45+
- sudo ./test_MDDRealtimeSynchronize
4446
- cd ../Communication
4547
- ./test_EndiannessConversion
4648
- ./test_MDDSerialPackager
@@ -49,6 +51,8 @@ script:
4951
- ./test_MDDLCM
5052
- ./test_MDDMQTT
5153
- cd ../..
54+
# Alternatively, instead of individual binary execution, use ctest:
55+
# - sudo /opt/cmake-2.8.12.2-Linux-i386/bin/ctest --output-on-failure -E "MDDSpaceMouse|MDDTCPIPSocketClient|MDDSerialPort|MDDBeep|MDDSocketCAN"
5256

5357
notifications:
5458
email: false

Modelica_DeviceDrivers/Resources/CMakeLists.txt

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
project(Modelica_DeviceDrivers C)
2-
cmake_minimum_required(VERSION 2.8.12)
3-
enable_testing()
4-
# copy CTestCustom.cmake to build dir to disable interactive tests in 'make test'
5-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake ${CMAKE_CURRENT_BINARY_DIR})
1+
project(Modelica_DeviceDrivers C CXX)
2+
cmake_minimum_required(VERSION 2.8.12...3.12.0)
63
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
74
#set(CMAKE_VERBOSE_MAKEFILE on)
85
#set(CMAKE_BUILD_TYPE DEBUG)
@@ -81,22 +78,14 @@ add_subdirectory(thirdParty)
8178
# Library providing some utility functions
8279
add_subdirectory(src/Util)
8380

84-
85-
# Various little C test programs:
86-
# Operating System module
87-
add_subdirectory(test/OperatingSystem)
88-
# Input Devices module
89-
add_subdirectory(test/InputDevices)
90-
# Communication module
91-
add_subdirectory(test/Communication)
92-
# Softing CAN module
93-
add_subdirectory(test/SoftingCAN)
94-
# Linux comedi daq interface
95-
add_subdirectory(test/HardwareIO)
96-
# Linux socket CAN interface
97-
add_subdirectory(test/SocketCAN)
98-
# Utility module
99-
add_subdirectory(test/Util)
81+
# Various little C tests for the different modules
82+
option(PACKAGE_TESTS "Build the tests" ON)
83+
if(PACKAGE_TESTS)
84+
enable_testing()
85+
add_subdirectory(test)
86+
# copy CTestCustom.cmake to build dir to disable interactive tests in 'make test'
87+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake ${CMAKE_CURRENT_BINARY_DIR})
88+
endif()
10089

10190

10291
# Forget about everything under this line
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
message(STATUS "WRITING BUILD FILES FOR test")
2+
3+
4+
5+
# Google Test
6+
# https://cliutils.gitlab.io/modern-cmake/chapters/testing/googletest.html
7+
# Use shared (DLL) run-time lib even when Google Test is built as static lib
8+
set(gtest_force_shared_crt TRUE CACHE BOOL "Use shared (DLL) run-time lib even when Google Test is built as static lib")
9+
set(INSTALL_GMOCK FALSE CACHE BOOL "")
10+
set(INSTALL_GTEST FALSE CACHE BOOL "")
11+
add_subdirectory("${PROJECT_SOURCE_DIR}/thirdParty/googletest" "thirdParty/googletest")
12+
set_property(TARGET gtest PROPERTY FOLDER "googletest")
13+
set_property(TARGET gtest_main PROPERTY FOLDER "googletest")
14+
set_property(TARGET gmock PROPERTY FOLDER "googletest")
15+
set_property(TARGET gmock_main PROPERTY FOLDER "googletest")
16+
17+
# Keep CACHE cleaner
18+
mark_as_advanced(
19+
BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS
20+
INSTALL_GMOCK INSTALL_GTEST
21+
gmock_build_tests gtest_build_samples gtest_build_tests
22+
gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols
23+
)
24+
25+
if (NOT (${CMAKE_VERSION} VERSION_LESS "3.10.0"))
26+
# https://cmake.org/cmake/help/latest/module/GoogleTest.html
27+
include(GoogleTest)
28+
endif()
29+
30+
macro(package_add_test TESTNAME)
31+
add_executable(${TESTNAME} ${ARGN})
32+
target_link_libraries(${TESTNAME} gtest gmock gtest_main)
33+
if (NOT (${CMAKE_VERSION} VERSION_LESS "3.10.0"))
34+
gtest_discover_tests(${TESTNAME} PROPERTIES TIMEOUT 10)
35+
else()
36+
add_test(NAME ${TESTNAME} COMMAND ${TESTNAME})
37+
endif()
38+
endmacro()
39+
40+
41+
# Operating System module
42+
add_subdirectory(OperatingSystem)
43+
# Input Devices module
44+
add_subdirectory(InputDevices)
45+
# Communication module
46+
add_subdirectory(Communication)
47+
# Softing CAN module
48+
add_subdirectory(SoftingCAN)
49+
# Linux comedi daq interface
50+
add_subdirectory(HardwareIO)
51+
# Linux socket CAN interface
52+
add_subdirectory(SocketCAN)
53+
# Utility module
54+
add_subdirectory(Util)

Modelica_DeviceDrivers/Resources/test/OperatingSystem/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,12 @@ ELSE (MSVC OR MINGW OR CYGWIN)
6565
target_link_libraries(test_MDDRealtimeSynchronize_realtime m rt)
6666
ENDIF (MSVC OR MINGW OR CYGWIN)
6767
add_test(test_MDDRealtimeSynchronize_realtime ${EXECUTABLE_OUTPUT_PATH}/test_MDDRealtimeSynchronize_realtime)
68+
69+
70+
package_add_test(test_MDDRealtimeSynchronize test_MDDRealtimeSynchronize.cpp ../Util/ModelicaUtilities.c)
71+
set_property(TARGET test_MDDRealtimeSynchronize PROPERTY FOLDER "test/OperatingSystem")
72+
if (MSVC OR MINGW OR CYGWIN)
73+
target_link_libraries(test_MDDRealtimeSynchronize Winmm)
74+
else()
75+
target_link_libraries(test_MDDRealtimeSynchronize m rt)
76+
endif()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <stdio.h>
2+
#include "../../Include/MDDRealtimeSynchronize.h"
3+
4+
#include "gtest/gtest.h"
5+
6+
namespace {
7+
TEST(MDDRealtimeSynchronize, realtime) {
8+
9+
double simTime, availableTime, calculationTime;
10+
double Ts = 0.1;
11+
int alwaysInTime = 1;
12+
void* procPrio = MDD_ProcessPriorityConstructor();
13+
void* rtSync = MDD_realtimeSynchronizeConstructor();
14+
15+
printf("Priority \"Realtime\" with Ts=%lf...\n", Ts);
16+
MDD_setPriority(procPrio, 2);
17+
for (simTime=Ts; simTime < 1.0; simTime += Ts) {
18+
calculationTime = MDD_realtimeSynchronize(rtSync, simTime, 0, 1, &availableTime);
19+
printf("simTime: %lf, availableTime: %lf, calculationTime: %lf\n", simTime, availableTime, calculationTime);
20+
alwaysInTime = alwaysInTime && availableTime > 0;
21+
}
22+
printf("Priority \"Realtime\", alwaysInTime: %s\n", alwaysInTime ? "true" : "false");
23+
MDD_realtimeSynchronizeDestructor(rtSync);
24+
MDD_ProcessPriorityDestructor(procPrio);
25+
EXPECT_GT(alwaysInTime, 0);
26+
}
27+
28+
TEST(MDDRealtimeSynchronize, high) {
29+
30+
double simTime, availableTime, calculationTime;
31+
double Ts = 0.1;
32+
int alwaysInTime = 1;
33+
void* procPrio = MDD_ProcessPriorityConstructor();
34+
void* rtSync = MDD_realtimeSynchronizeConstructor();
35+
36+
printf("Priority \"high\" with Ts=%lf...\n", Ts);
37+
MDD_setPriority(procPrio, 1);
38+
for (simTime = Ts; simTime < 1.0; simTime += Ts) {
39+
calculationTime = MDD_realtimeSynchronize(rtSync, simTime, 0, 1, &availableTime);
40+
printf("simTime: %lf, availableTime: %lf, calculationTime: %lf\n", simTime, availableTime, calculationTime);
41+
alwaysInTime = alwaysInTime && availableTime > 0;
42+
}
43+
printf("Priority \"high\", alwaysInTime: %s\n", alwaysInTime ? "true" : "false");
44+
MDD_realtimeSynchronizeDestructor(rtSync);
45+
MDD_ProcessPriorityDestructor(procPrio);
46+
EXPECT_GT(alwaysInTime, 0);
47+
}
48+
49+
TEST(MDDRealtimeSynchronize, normal) {
50+
51+
double simTime, availableTime, calculationTime;
52+
double Ts = 0.1;
53+
int alwaysInTime = 1;
54+
void* procPrio = MDD_ProcessPriorityConstructor();
55+
void* rtSync = MDD_realtimeSynchronizeConstructor();
56+
57+
printf("Priority \"Normal\" with Ts=%lf...\n", Ts);
58+
MDD_setPriority(procPrio, 0);
59+
for (simTime = Ts; simTime < 1.0; simTime += Ts) {
60+
calculationTime = MDD_realtimeSynchronize(rtSync, simTime, 0, 1, &availableTime);
61+
printf("simTime: %lf, availableTime: %lf, calculationTime: %lf\n", simTime, availableTime, calculationTime);
62+
alwaysInTime = alwaysInTime && availableTime > 0;
63+
}
64+
printf("Priority \"Normal\", alwaysInTime: %s\n", alwaysInTime ? "true" : "false");
65+
MDD_realtimeSynchronizeDestructor(rtSync);
66+
MDD_ProcessPriorityDestructor(procPrio);
67+
EXPECT_GT(alwaysInTime, 0);
68+
}
69+
70+
TEST(MDDRealtimeSynchronize, low) {
71+
double simTime, availableTime, calculationTime;
72+
double Ts = 0.1;
73+
int alwaysInTime = 1;
74+
void* procPrio = MDD_ProcessPriorityConstructor();
75+
void* rtSync = MDD_realtimeSynchronizeConstructor();
76+
77+
printf("Priority \"below normal\" with Ts=%lf...\n", Ts);
78+
MDD_setPriority(procPrio, -1);
79+
for (simTime = Ts; simTime < 1.0; simTime += Ts) {
80+
calculationTime = MDD_realtimeSynchronize(rtSync, simTime, 0, 1, &availableTime);
81+
printf("simTime: %lf, availableTime: %lf, calculationTime: %lf\n", simTime, availableTime, calculationTime);
82+
alwaysInTime = alwaysInTime && availableTime > 0;
83+
}
84+
printf("Priority \"below normal\", alwaysInTime: %s\n", alwaysInTime ? "true" : "false");
85+
MDD_realtimeSynchronizeDestructor(rtSync);
86+
MDD_ProcessPriorityDestructor(procPrio);
87+
EXPECT_GT(alwaysInTime, 0);
88+
}
89+
90+
TEST(MDDRealtimeSynchronize, idle) {
91+
double simTime, availableTime, calculationTime;
92+
double Ts = 0.1;
93+
int alwaysInTime = 1;
94+
void* procPrio = MDD_ProcessPriorityConstructor();
95+
void* rtSync = MDD_realtimeSynchronizeConstructor();
96+
97+
printf("Priority \"Idle\" with Ts=%lf...\n", Ts);
98+
MDD_setPriority(procPrio, -2);
99+
for (simTime = Ts; simTime < 1.0; simTime += Ts) {
100+
calculationTime = MDD_realtimeSynchronize(rtSync, simTime, 0, 1, &availableTime);
101+
printf("simTime: %lf, availableTime: %lf, calculationTime: %lf\n", simTime, availableTime, calculationTime);
102+
alwaysInTime = alwaysInTime && availableTime > 0;
103+
}
104+
printf("Priority \"Idle\", alwaysInTime: %s\n", alwaysInTime ? "true" : "false");
105+
MDD_realtimeSynchronizeDestructor(rtSync);
106+
MDD_ProcessPriorityDestructor(procPrio);
107+
EXPECT_GT(alwaysInTime, 0);
108+
}
109+
110+
}

Modelica_DeviceDrivers/Resources/thirdParty/Readme.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
Third party sources / libraries and tools that are not licensed under the BSD 3-Clause License
1+
Third party sources / libraries and tools that are (mostly) not licensed under the BSD-3-Clause License
22
conditions. These sources / libraries are developed by other authors / organisations.
33

44
FOR LEGAL REASONS SOME MIGHT NOT BE INCLUDED WITHIN THIS MODELICA_DEVICEDRIVERS DISTRIBUTION.
55

66
- 3DconnexionSDK: 3Dconnexion Software Developer Kit for Linux (http://www.3dconnexion.com/)
77
- boost: Boost library macros for machine type Endianness (http://www.boost.org/)
88
- glib: low-level system libraries, LGPL (https://github.com/GNOME/glib/)
9+
- googletest: Google Testing and Mocking Framework, BSD-3-Clause (https://github.com/google/googletest)
910
- hdisdi: Microsoft Device Driver files used for space mouse (http://www.microsoft.com/)
1011
- ITI: ModelicaExternalC.lib version for SimulationX by ESI ITI (https://github.com/ESI-ITI-GmbH/ModelicaExternalC/)
1112
- lcm: Lightweight Communications and Marshalling, LGPL (https://github.com/lcm-proj/)

0 commit comments

Comments
 (0)