Skip to content

Commit

Permalink
Merge pull request #92 from mwoehlke-kitware/tests-cmake
Browse files Browse the repository at this point in the history
Add tests to CMake build
  • Loading branch information
ashuang authored Sep 5, 2016
2 parents 771b905 + 183d14a commit fa6b9a9
Show file tree
Hide file tree
Showing 19 changed files with 493 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ endif()
# .NET
# TODO

option(LCM_ENABLE_TESTS "Build unit tests" ON)
if(LCM_ENABLE_TESTS)
enable_testing()
add_subdirectory(test)
endif()

# Install rules
include(lcm-cmake/install.cmake)

Expand Down
6 changes: 5 additions & 1 deletion lcm-lua/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ set(lcm_lua_sources

add_library(lcm-lua MODULE ${lcm_lua_sources})
set_target_properties(lcm-lua PROPERTIES OUTPUT_NAME "lcm" PREFIX "")

target_include_directories(lcm-lua PRIVATE
${LUA_INCLUDE_DIR}
)

target_link_libraries(lcm-lua lcm ${LUA_LIBRARY})
include_directories(${LUA_INCLUDE_DIR})

install(TARGETS lcm-lua
DESTINATION lib${LIB_SUFFIX}/lua/${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
Expand Down
8 changes: 6 additions & 2 deletions lcm-python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ if(NOT EPYDOC_EXECUTABLE)
endif()

if(NOT "$ENV{PYTHONPATH}" STREQUAL "")
set(EXTRA_PYTHON_PATH ":$ENV{PYTHONPATH}")
if(WIN32)
set(EXTRA_PYTHON_PATH ";$ENV{PYTHONPATH}")
else()
set(EXTRA_PYTHON_PATH ":$ENV{PYTHONPATH}")
endif()
else()
set(EXTRA_PYTHON_PATH "")
endif()

add_custom_target(doc-python
COMMAND ${CMAKE_COMMAND} -E make_directory ${lcm_BINARY_DIR}/docs/html/python
COMMAND ${CMAKE_COMMAND} -E env
PYTHONPATH=${CMAKE_BINARY_DIR}/${PYTHON_SITE}${EXTRA_PYTHON_PATH}
"PYTHONPATH=${CMAKE_BINARY_DIR}/${PYTHON_SITE}${EXTRA_PYTHON_PATH}"
${EPYDOC_EXECUTABLE} --config ${lcm_SOURCE_DIR}/docs/epydoc.cfg
WORKING_DIRECTORY ${lcm_BINARY_DIR}/docs
DEPENDS doc-clean)
Expand Down
2 changes: 1 addition & 1 deletion lcm/lcm-cpp-impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ LogFile::writeEvent(LogEvent* event)
evt.timestamp = event->timestamp;
evt.channellen = event->channel.size();
evt.datalen = event->datalen;
evt.channel = (char*) event->channel.c_str();
evt.channel = const_cast<char*>(event->channel.c_str());
evt.data = event->data;
return lcm_eventlog_write_event(eventlog, &evt);
}
Expand Down
20 changes: 10 additions & 10 deletions lcm/lcm_coretypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static inline int __byte_decode_array(const void *_buf, int offset, int maxlen,
if (maxlen < elements)
return -1;

uint8_t *buf = (uint8_t*) _buf;
const uint8_t *buf = (const uint8_t*) _buf;
memcpy(p, &buf[offset], elements);

return elements;
Expand Down Expand Up @@ -109,7 +109,7 @@ static inline int __int8_t_decode_array(const void *_buf, int offset, int maxlen
if (maxlen < elements)
return -1;

int8_t *buf = (int8_t*) _buf;
const int8_t *buf = (const int8_t*) _buf;
memcpy(p, &buf[offset], elements);

return elements;
Expand Down Expand Up @@ -147,7 +147,7 @@ static inline int __int16_t_encode_array(void *_buf, int offset, int maxlen, con
// See Section 5.8 paragraph 3 of the standard
// http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf
// use uint for shifting instead if int
const uint16_t *unsigned_p = (uint16_t*)p;
const uint16_t *unsigned_p = (const uint16_t*)p;
for (element = 0; element < elements; element++) {
uint16_t v = unsigned_p[element];
buf[pos++] = (v>>8) & 0xff;
Expand All @@ -160,7 +160,7 @@ static inline int __int16_t_encode_array(void *_buf, int offset, int maxlen, con
static inline int __int16_t_decode_array(const void *_buf, int offset, int maxlen, int16_t *p, int elements)
{
int total_size = sizeof(int16_t) * elements;
uint8_t *buf = (uint8_t*) _buf;
const uint8_t *buf = (const uint8_t*) _buf;
int pos = offset;
int element;

Expand Down Expand Up @@ -207,7 +207,7 @@ static inline int __int32_t_encode_array(void *_buf, int offset, int maxlen, con
// See Section 5.8 paragraph 3 of the standard
// http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf
// use uint for shifting instead if int
const uint32_t* unsigned_p = (uint32_t*)p;
const uint32_t* unsigned_p = (const uint32_t*)p;
for (element = 0; element < elements; element++) {
const uint32_t v = unsigned_p[element];
buf[pos++] = (v>>24)&0xff;
Expand All @@ -222,7 +222,7 @@ static inline int __int32_t_encode_array(void *_buf, int offset, int maxlen, con
static inline int __int32_t_decode_array(const void *_buf, int offset, int maxlen, int32_t *p, int elements)
{
int total_size = sizeof(int32_t) * elements;
uint8_t *buf = (uint8_t*) _buf;
const uint8_t *buf = (const uint8_t*) _buf;
int pos = offset;
int element;

Expand Down Expand Up @@ -272,7 +272,7 @@ static inline int __int64_t_encode_array(void *_buf, int offset, int maxlen, con
// See Section 5.8 paragraph 3 of the standard
// http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf
// use uint for shifting instead if int
const uint64_t * unsigned_p = (uint64_t*)p;
const uint64_t * unsigned_p = (const uint64_t*)p;
for (element = 0; element < elements; element++) {
const uint64_t v = unsigned_p[element];
buf[pos++] = (v>>56)&0xff;
Expand All @@ -291,7 +291,7 @@ static inline int __int64_t_encode_array(void *_buf, int offset, int maxlen, con
static inline int __int64_t_decode_array(const void *_buf, int offset, int maxlen, int64_t *p, int elements)
{
int total_size = sizeof(int64_t) * elements;
uint8_t *buf = (uint8_t*) _buf;
const uint8_t *buf = (const uint8_t*) _buf;
int pos = offset;
int element;

Expand Down Expand Up @@ -333,7 +333,7 @@ static inline int __float_encoded_array_size(const float *p, int elements)

static inline int __float_encode_array(void *_buf, int offset, int maxlen, const float *p, int elements)
{
return __int32_t_encode_array(_buf, offset, maxlen, (int32_t*) p, elements);
return __int32_t_encode_array(_buf, offset, maxlen, (const int32_t*) p, elements);
}

static inline int __float_decode_array(const void *_buf, int offset, int maxlen, float *p, int elements)
Expand Down Expand Up @@ -362,7 +362,7 @@ static inline int __double_encoded_array_size(const double *p, int elements)

static inline int __double_encode_array(void *_buf, int offset, int maxlen, const double *p, int elements)
{
return __int64_t_encode_array(_buf, offset, maxlen, (int64_t*) p, elements);
return __int64_t_encode_array(_buf, offset, maxlen, (const int64_t*) p, elements);
}

static inline int __double_decode_array(const void *_buf, int offset, int maxlen, double *p, int elements)
Expand Down
6 changes: 3 additions & 3 deletions lcmgen/emit_cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,10 @@ static void _encode_recursive(lcmgen_t* lcm, FILE* f, lcm_member_t* lm, int dept
//
if(depth == g_ptr_array_size(lm->dimensions)) {
if(!strcmp(lm->type->lctypename, "string")) {
emit_start(indent, "char* __cstr = (char*) this->%s", lm->membername);
emit_start(indent, "char* __cstr = const_cast<char*>(this->%s", lm->membername);
for(int i=0; i<depth; i++)
emit_continue("[a%d]", i);
emit_end(".c_str();");
emit_end(".c_str());");
emit(indent, "tlen = __string_encode_array(buf, offset + pos, maxlen - pos, &__cstr, 1);");
} else {
emit_start(indent, "tlen = this->%s", lm->membername);
Expand Down Expand Up @@ -547,7 +547,7 @@ static void emit_encode_nohash(lcmgen_t *lcm, FILE *f, lcm_struct_t *ls)
if (0 == num_dims) {
if (lcm_is_primitive_type(lm->type->lctypename)) {
if(!strcmp(lm->type->lctypename, "string")) {
emit(1, "char* %s_cstr = (char*) this->%s.c_str();", lm->membername, lm->membername);
emit(1, "char* %s_cstr = const_cast<char*>(this->%s.c_str());", lm->membername, lm->membername);
emit(1, "tlen = __string_encode_array(buf, offset + pos, maxlen - pos, &%s_cstr, 1);",
lm->membername);
} else {
Expand Down
18 changes: 18 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
add_subdirectory(gtest)

add_subdirectory(types)

add_subdirectory(c)
add_subdirectory(cpp)

if(LCM_ENABLE_PYTHON)
add_subdirectory(python)
endif()

if(LCM_ENABLE_JAVA)
add_subdirectory(java)
endif()

if(LCM_ENABLE_LUA)
add_subdirectory(lua)
endif()
27 changes: 27 additions & 0 deletions test/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
set(test_c_libs lcm-test-types-c lcm gtest gtest_main)

add_executable(test-c-server server.c common.c)
target_link_libraries(test-c-server lcm-test-types-c lcm)

add_executable(test-c-client client.cpp common.c)
target_link_libraries(test-c-client ${test_c_libs})

add_executable(test-c-memq_test memq_test.cpp common.c)
target_link_libraries(test-c-memq_test ${test_c_libs})

add_executable(test-c-eventlog_test eventlog_test.cpp common.c)
target_link_libraries(test-c-eventlog_test ${test_c_libs})

add_executable(test-c-udpm_test udpm_test.cpp common.c)
target_link_libraries(test-c-udpm_test ${test_c_libs})

add_test(NAME C::memq_test COMMAND test-c-memq_test)
add_test(NAME C::eventlog_test COMMAND test-c-eventlog_test)

if(PYTHON_EXECUTABLE)
add_test(NAME C::client_server COMMAND
${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/../run_client_server_test.py
$<TARGET_FILE:test-c-server>
$<TARGET_FILE:test-c-client>)
endif()
4 changes: 2 additions & 2 deletions test/c/eventlog_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST(LCM_C, EventLogWriteRead) {

lcm_eventlog_event_t event;
event.channellen = channellen;
event.channel = (char*) channel;
event.channel = const_cast<char*>(channel);
event.datalen = datalen;
event.data = data;

Expand Down Expand Up @@ -98,7 +98,7 @@ TEST(LCM_C, EventLogCorrupt) {
lcm_eventlog_event_t event;
event.timestamp = 0;
event.channellen = channellen;
event.channel = (char*) channel;
event.channel = const_cast<char*>(channel);
event.datalen = datalen;
event.data = data;

Expand Down
24 changes: 24 additions & 0 deletions test/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set(test_cpp_libs lcm-test-types-cpp lcm gtest gtest_main)
if(CMAKE_VERSION VERSION_LESS 3.3) # TODO remove when we require >=3.3
# NOTE: with CMake 3.3 or later, this is added as a dependency of the
# lcm-test-types-cpp INTERFACE target, so is not needed; CMake prior to
# 3.3 does not support dependencies on INTERFACE targets, and we need to
# enforce the build order for obvious reasons
list(APPEND test_cpp_libs lcm-test-types-generate-cpp)
endif()

add_executable(test-cpp-client client.cpp common.cpp)
target_link_libraries(test-cpp-client ${test_cpp_libs})

add_executable(test-cpp-memq_test memq_test.cpp common.cpp)
target_link_libraries(test-cpp-memq_test ${test_cpp_libs})

add_test(NAME CPP::memq_test COMMAND test-cpp-memq_test)

if(PYTHON_EXECUTABLE)
add_test(NAME CPP::client_server COMMAND
${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/../run_client_server_test.py
$<TARGET_FILE:test-c-server>
$<TARGET_FILE:test-cpp-client>)
endif()
12 changes: 6 additions & 6 deletions test/cpp/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class EchoTest {
EchoTest()
: lcm_(),
num_trials_(100),
test_channel_("TEST_ECHO"),
response_count_(0),
echo_data_(NULL),
echo_msg_len_(0),
subscription_() {
echo_data_(NULL),
response_count_(0),
subscription_(),
test_channel_("TEST_ECHO") {
}

bool Run(void) {
Expand Down Expand Up @@ -79,10 +79,10 @@ class TypedTest {
public:
TypedTest(const std::string test_name, int num_trials)
: lcm_(),
test_channel_(test_name),
response_count_(0),
num_trials_(num_trials),
subscription_() {
subscription_(),
test_channel_(test_name) {
}

bool Run(void) {
Expand Down
10 changes: 10 additions & 0 deletions test/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project(gtest)

find_package(Threads)

add_library(gtest src/gtest-all.cc)
target_link_libraries(gtest ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(gtest SYSTEM PUBLIC ${PROJECT_SOURCE_DIR}/include)

add_library(gtest_main src/gtest_main.cc)
target_link_libraries(gtest_main gtest ${CMAKE_THREAD_LIBS_INIT})
29 changes: 29 additions & 0 deletions test/java/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
include(UseJava)

add_subdirectory(hamcrest-core-1.3)
add_subdirectory(junit-4.11)

add_jar(lcm-test-java
OUTPUT_NAME lcm-test
INCLUDE_JARS
lcm-test-types-java
lcm-java
junit
SOURCES
lcmtest/LcmTestClient.java
lcmtest/TestUDPMulticastProvider.java)

set(lcm-test-java_CLASSPATH)
foreach(jar lcm-test-java lcm-test-types-java lcm-java hamcrest-core junit)
get_target_property(${jar}_JAR ${jar} JAR_FILE)
list(APPEND lcm-test-java_CLASSPATH ${${jar}_JAR})
endforeach()
if(NOT WIN32)
string(REPLACE ";" ":" lcm-test-java_CLASSPATH "${lcm-test-java_CLASSPATH}")
endif()

add_test(NAME Java::client_server COMMAND
${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/../run_client_server_test.py
$<TARGET_FILE:test-c-server>
${Java_JAVA_EXECUTABLE} -cp "${lcm-test-java_CLASSPATH}" LcmTestClient)
47 changes: 47 additions & 0 deletions test/java/hamcrest-core-1.3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
set(hamcrest-core_version 1.3)

set(hamcrest-core_sources
org/hamcrest/BaseDescription.java
org/hamcrest/BaseMatcher.java
org/hamcrest/Condition.java
org/hamcrest/core/AllOf.java
org/hamcrest/core/AnyOf.java
org/hamcrest/core/CombinableMatcher.java
org/hamcrest/core/DescribedAs.java
org/hamcrest/core/Every.java
org/hamcrest/core/IsAnything.java
org/hamcrest/core/IsCollectionContaining.java
org/hamcrest/core/IsEqual.java
org/hamcrest/core/IsInstanceOf.java
org/hamcrest/core/Is.java
org/hamcrest/core/IsNot.java
org/hamcrest/core/IsNull.java
org/hamcrest/core/IsSame.java
org/hamcrest/CoreMatchers.java
org/hamcrest/core/ShortcutCombination.java
org/hamcrest/core/StringContains.java
org/hamcrest/core/StringEndsWith.java
org/hamcrest/core/StringStartsWith.java
org/hamcrest/core/SubstringMatcher.java
org/hamcrest/CustomMatcher.java
org/hamcrest/CustomTypeSafeMatcher.java
org/hamcrest/Description.java
org/hamcrest/DiagnosingMatcher.java
org/hamcrest/Factory.java
org/hamcrest/FeatureMatcher.java
org/hamcrest/internal/ArrayIterator.java
org/hamcrest/internal/ReflectiveTypeFinder.java
org/hamcrest/internal/SelfDescribingValueIterator.java
org/hamcrest/internal/SelfDescribingValue.java
org/hamcrest/MatcherAssert.java
org/hamcrest/Matcher.java
org/hamcrest/SelfDescribing.java
org/hamcrest/StringDescription.java
org/hamcrest/TypeSafeDiagnosingMatcher.java
org/hamcrest/TypeSafeMatcher.java
)

add_jar(hamcrest-core
OUTPUT_NAME hamcrest-core-${hamcrest-core_version}
SOURCES ${hamcrest-core_sources}
)
Loading

0 comments on commit fa6b9a9

Please sign in to comment.