Skip to content

Commit

Permalink
Build test on debug only (#45)
Browse files Browse the repository at this point in the history
* Build tests in debug mode only, fix gcc warnings
* strncpy fix in thread err
  • Loading branch information
tezc committed Feb 16, 2021
1 parent ed5bcab commit 950d6f1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
77 changes: 41 additions & 36 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ endif ()
message(STATUS "Build type ${CMAKE_BUILD_TYPE}")

option(SC_BUILD_TEST "Build tests" ON)
if(NOT SC_BUILD_TEST)
message(STATUS "Turned off building tests because SC is a subproject.")
message(STATUS "Turned off coverage because SC is a subproject.")
endif()
if (NOT SC_BUILD_TEST)
message(STATUS "Turned off building tests because SC is a subproject.")
message(STATUS "Turned off coverage because SC is a subproject.")
endif ()

if (NOT ${CMAKE_BUILD_TYPE} MATCHES "Debug" AND NOT ${CMAKE_BUILD_TYPE} MATCHES "Coverage")
SET(SC_BUILD_TEST OFF CACHE BOOL "Turn off sc_lib tests" FORCE)
message(STATUS "Not building tests, SC tests are supported in Debug build only.")
endif ()

add_subdirectory(array)
add_subdirectory(buffer)
Expand Down Expand Up @@ -43,47 +48,47 @@ add_subdirectory(uri)
# --------------------------------------------------------------------------- #
# --------------------- Test Configuration Start ---------------------------- #
# --------------------------------------------------------------------------- #
if(SC_BUILD_TEST)
# ----------------------- - Code Coverage Start ----------------------------- #
if (SC_BUILD_TEST)
# ----------------------- - Code Coverage Start ----------------------------- #

if (${CMAKE_BUILD_TYPE} MATCHES "Coverage")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(--coverage)
link_libraries(gcov)
else()
message(FATAL_ERROR "Only GCC is supported for coverage")
endif()
endif ()
if (${CMAKE_BUILD_TYPE} MATCHES "Coverage")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(--coverage)
link_libraries(gcov)
else ()
message(FATAL_ERROR "Only GCC is supported for coverage")
endif ()
endif ()

add_custom_target(coverage)
add_custom_command(
TARGET coverage
COMMAND lcov --capture --directory .
--output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
COMMAND lcov --remove coverage.info '/usr/*' '*example*' '*test*'
--output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
COMMAND lcov --list coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
)
add_custom_target(coverage)
add_custom_command(
TARGET coverage
COMMAND lcov --capture --directory .
--output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
COMMAND lcov --remove coverage.info '/usr/*' '*example*' '*test*'
--output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
COMMAND lcov --list coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert'
)

# -------------------------- Code Coverage End ------------------------------ #
# -------------------------- Code Coverage End ------------------------------ #

SET(MEMORYCHECK_COMMAND_OPTIONS
"-q --log-fd=2 --trace-children=yes --track-origins=yes \
SET(MEMORYCHECK_COMMAND_OPTIONS
"-q --log-fd=2 --trace-children=yes --track-origins=yes \
--leak-check=full --show-leak-kinds=all \
--error-exitcode=255")

add_custom_target(valgrind ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG>
--overwrite MemoryCheckCommandOptions=${MEMORYCHECK_COMMAND_OPTIONS}
--verbose -T memcheck WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_target(valgrind ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG>
--overwrite MemoryCheckCommandOptions=${MEMORYCHECK_COMMAND_OPTIONS}
--verbose -T memcheck WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

add_custom_target(check ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG> --verbose
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_target(check ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG> --verbose
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

add_dependencies(coverage check)
add_dependencies(coverage check)

endif ()
# ----------------------- Test Configuration End ---------------------------- #
2 changes: 2 additions & 0 deletions condition/sc_cond.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ void sc_cond_signal(struct sc_cond *cond, void *data)
// This won't fail as long as we pass correct params.
rc = pthread_cond_signal(&cond->cond);
assert(rc == 0);
(void) rc;

pthread_mutex_unlock(&cond->mtx);
}
Expand All @@ -174,6 +175,7 @@ void *sc_cond_wait(struct sc_cond *cond)
// This won't fail as long as we pass correct params.
rc = pthread_cond_wait(&cond->cond, &cond->mtx);
assert(rc == 0);
(void) rc;
}

data = cond->data;
Expand Down
3 changes: 3 additions & 0 deletions mutex/sc_mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ int sc_mutex_init(struct sc_mutex *mtx)
// This won't fail as long as we pass correct param.
rv = pthread_mutexattr_destroy(&attr);
assert(rv == 0);
(void) rv;

return rc != 0 ? -1 : 0;
}
Expand All @@ -98,6 +99,7 @@ void sc_mutex_lock(struct sc_mutex *mtx)
// This won't fail as long as we pass correct param.
rc = pthread_mutex_lock(&mtx->mtx);
assert(rc == 0);
(void) rc;
}

void sc_mutex_unlock(struct sc_mutex *mtx)
Expand All @@ -107,6 +109,7 @@ void sc_mutex_unlock(struct sc_mutex *mtx)
// This won't fail as long as we pass correct param.
rc = pthread_mutex_unlock(&mtx->mtx);
assert(rc == 0);
(void) rc;
}

#endif
5 changes: 3 additions & 2 deletions signal/sc_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,13 @@ int sc_signal_init()

void sc_signal_log(int fd, char *buf, size_t len, char *fmt, ...)
{
int written;
int written, rc;
va_list args;

va_start(args, fmt);
written = sc_signal_vsnprintf(buf, len, fmt, args);
va_end(args);

(void) write(fd, buf, (size_t) written);
rc = write(fd, buf, (size_t) written);
(void) rc;
}
6 changes: 3 additions & 3 deletions thread/sc_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int sc_thread_start(struct sc_thread *thread, void *(*fn)(void *), void *arg)

rc = pthread_attr_init(&attr);
if (rc != 0) {
strncpy(thread->err, strerror(rc), sizeof(thread->err));
strncpy(thread->err, strerror(rc), sizeof(thread->err) - 1);
return -1;
}

Expand All @@ -124,7 +124,7 @@ int sc_thread_start(struct sc_thread *thread, void *(*fn)(void *), void *arg)

rc = pthread_create(&thread->id, &attr, fn, arg);
if (rc != 0) {
strncpy(thread->err, strerror(rc), sizeof(thread->err));
strncpy(thread->err, strerror(rc), sizeof(thread->err) - 1);
}

// This may only fail with EINVAL.
Expand All @@ -144,7 +144,7 @@ int sc_thread_join(struct sc_thread *thread, void **ret)

rc = pthread_join(thread->id, &val);
if (rc != 0) {
strncpy(thread->err, strerror(rc), sizeof(thread->err));
strncpy(thread->err, strerror(rc), sizeof(thread->err) - 1);
}

thread->id = 0;
Expand Down
4 changes: 4 additions & 0 deletions time/sc_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ uint64_t sc_time_ms()

rc = clock_gettime(CLOCK_REALTIME, &ts);
assert(rc == 0);
(void) rc;

return ts.tv_sec * 1000 + (uint64_t)(ts.tv_nsec / 10e6);
#endif
Expand All @@ -76,6 +77,7 @@ uint64_t sc_time_ns()

rc = clock_gettime(CLOCK_REALTIME, &ts);
assert(rc == 0);
(void) rc;

return ts.tv_sec * (uint64_t)1000000000 + ts.tv_nsec;
#endif
Expand All @@ -101,6 +103,7 @@ uint64_t sc_time_mono_ms()

rc = clock_gettime(CLOCK_MONOTONIC, &ts);
assert(rc == 0);
(void) rc;

return (uint64_t)((uint64_t) ts.tv_sec * 1000 +
(uint64_t) ts.tv_nsec / 1000000);
Expand All @@ -126,6 +129,7 @@ uint64_t sc_time_mono_ns()

rc = clock_gettime(CLOCK_MONOTONIC, &ts);
assert(rc == 0);
(void) rc;

return ((uint64_t) ts.tv_sec * 1000000000 + (uint64_t) ts.tv_nsec);
#endif
Expand Down

0 comments on commit 950d6f1

Please sign in to comment.