Skip to content

Commit

Permalink
BFSDK missing unit tests (#477)
Browse files Browse the repository at this point in the history
Signed-off-by: Rian Quinn <“rianquinn@gmail.com”>
  • Loading branch information
rianquinn committed Aug 28, 2017
1 parent 3d33cf3 commit ef33f57
Show file tree
Hide file tree
Showing 27 changed files with 485 additions and 135 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -682,18 +682,21 @@ if(ENABLE_TIDY AND NOT WIN32)

add_custom_target(tidy
# COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfdriver/build --target tidy
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfelf_loader/build --target tidy
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfm/build --target tidy
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfsdk/build --target tidy
)

add_custom_target(tidy-all
# COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfdriver/build --target tidy-all
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfelf_loader/build --target tidy-all
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfm/build --target tidy-all
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfsdk/build --target tidy-all
)

if(BUILD_SHARED_LIBS)
add_custom_command(TARGET tidy COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfelf_loader/build --target tidy)
add_custom_command(TARGET tidy-all COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfelf_loader/build --target tidy-all)
endif()

if(NOT ENABLE_UNITTESTING)
add_custom_command(TARGET tidy COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfvmm/build --target tidy)
add_custom_command(TARGET tidy COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfsysroot/build --target tidy)
Expand Down
5 changes: 5 additions & 0 deletions bfsdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ExternalProject_Add(
DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/json/download
SOURCE_DIR ${CMAKE_BINARY_DIR}/json/src
BINARY_DIR ${CMAKE_BINARY_DIR}/json/build
UPDATE_DISCONNECTED 1
)

# ------------------------------------------------------------------------------
Expand All @@ -45,6 +46,7 @@ ExternalProject_Add(
DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/gsl/download
SOURCE_DIR ${CMAKE_BINARY_DIR}/gsl/src
BINARY_DIR ${CMAKE_BINARY_DIR}/gsl/build
UPDATE_DISCONNECTED 1
)

# ------------------------------------------------------------------------------
Expand All @@ -69,6 +71,7 @@ if(ENABLE_UNITTESTING)
DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/catch/download
SOURCE_DIR ${CMAKE_BINARY_DIR}/catch/src
BINARY_DIR ${CMAKE_BINARY_DIR}/catch/build
UPDATE_DISCONNECTED 1
)

endif()
Expand All @@ -95,6 +98,7 @@ if(ENABLE_UNITTESTING)
DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/hippomocks/download
SOURCE_DIR ${CMAKE_BINARY_DIR}/hippomocks/src
BINARY_DIR ${CMAKE_BINARY_DIR}/hippomocks/build
UPDATE_DISCONNECTED 1
)

endif()
Expand Down Expand Up @@ -122,6 +126,7 @@ if(ENABLE_ASTYLE)
DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/astyle/download
SOURCE_DIR ${CMAKE_BINARY_DIR}/astyle/src
BINARY_DIR ${CMAKE_BINARY_DIR}/astyle/build
UPDATE_DISCONNECTED 1
)

endif()
Expand Down
1 change: 0 additions & 1 deletion bfsdk/cmake/CMakeOption_ASan.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ if(ENABLE_DYNAMIC_ASAN AND NOT WIN32)
message(STATUS "Testing Config: Dynamic Analysis")

set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -g")
set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -O1")
set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fuse-ld=gold")
set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fno-omit-frame-pointer")
set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fsanitize=address")
Expand Down
12 changes: 6 additions & 6 deletions bfsdk/include/bfbenchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ uint64_t benchmark(T func)
size_t g_page_allocs = 0;
size_t g_nonpage_allocs = 0;

static void *
inline void *
custom_new(std::size_t size)
{
if ((size & 0xFFF) == 0) {
Expand All @@ -57,7 +57,7 @@ custom_new(std::size_t size)
return malloc(size);
}

static void
inline void
custom_delete(void *ptr, std::size_t size)
{
bfignored(size);
Expand All @@ -78,17 +78,15 @@ operator delete (void *ptr, std::size_t size) throw()

void
operator delete (void *ptr) throw()
{ custom_delete(ptr, 0); }
{ operator delete (ptr, static_cast<std::size_t>(0)); }

void
operator delete[](void *ptr, std::size_t size) throw()
{ custom_delete(ptr, size); }

void
operator delete[](void *ptr) throw()
{ custom_delete(ptr, 0); }

#endif
{ operator delete[](ptr, static_cast<std::size_t>(0)); }

inline void
print_memory_stats()
Expand All @@ -107,3 +105,5 @@ clear_memory_stats()
g_page_allocs = 0;
g_nonpage_allocs = 0;
}

#endif
28 changes: 14 additions & 14 deletions bfsdk/include/bfdebugringinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define BFDEBUGRINGINTERFACE_H

#include <bftypes.h>
#include <bfdebug.h>
#include <bfconstants.h>
#include <bferrorcodes.h>

Expand Down Expand Up @@ -130,31 +131,30 @@ debug_ring_read(struct debug_ring_resources_t *drr, char *str, uint64_t len)
{
uint64_t i;
uint64_t spos;
uint64_t count;
uint64_t content;

if (drr == 0 || str == 0 || len == 0)
{ return 0; }
if (drr == 0 || str == 0 || len == 0) {
return 0;
}

if (drr->spos > drr->epos) {
return 0;
}

spos = drr->spos % DEBUG_RING_SIZE;
content = drr->epos - drr->spos;

for (i = 0; i < content && i < len - 1; i++) {
if (spos == DEBUG_RING_SIZE)
{ spos = 0; }

if (drr->buf[spos] != '\0')
{ str[i] = drr->buf[spos]; }
else {
i--;
content--;
for (i = 0, count = 0; i < content && i < len - 1; i++) {
if (drr->buf[spos] != '\0') {
str[count++] = drr->buf[spos];
}

spos++;
spos = ((spos + 1) % DEBUG_RING_SIZE);
}

str[i] = '\0';

return content;
return count;
}

#ifdef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions bfsdk/include/bfexception.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ guard_exceptions(int64_t error_code, T func)
return BF_BAD_ALLOC;
}
catch (std::exception &e) {
bfdebug_transaction(1, [&](std::string * msg) {
bfdebug_transaction(0, [&](std::string * msg) {
bferror_lnbr(0, msg);
bferror_brk1(0, msg);
bferror_info(0, typeid(e).name(), msg);
Expand All @@ -66,7 +66,7 @@ guard_exceptions(int64_t error_code, T func)
});
}
catch (...) {
bfdebug_transaction(1, [&](std::string * msg) {
bfdebug_transaction(0, [&](std::string * msg) {
bferror_lnbr(0, msg);
bferror_brk1(0, msg);
bferror_info(0, "unknown exception", msg);
Expand Down
11 changes: 9 additions & 2 deletions bfsdk/include/bfgsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
#include <gsl/gsl>
#else

#ifdef NEED_STD_LITE
#include <bfstd.h>
#endif

/// @cond

Expand All @@ -54,13 +56,17 @@
#define gsl_unlikely(x) (x)
#endif

#ifndef GSL_ABORT
#define GSL_ABORT abort
#endif

#define expects(cond) \
if (gsl_unlikely(!(cond))) { \
std::terminate(); \
GSL_ABORT(); \
}
#define ensures(cond) \
if (gsl_unlikely(!(cond))) { \
std::terminate(); \
GSL_ABORT(); \
}

/// @endcond
Expand Down Expand Up @@ -159,4 +165,5 @@ at(const T *arr, size_t N, I index)
}

#endif

#endif
25 changes: 14 additions & 11 deletions bfsdk/include/bfnewdelete.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ size_t g_new_throws_bad_alloc = 0;
#define aligned_alloc _aligned_malloc
#endif

static void *
inline void *
custom_new(std::size_t size)
{
if (size == g_new_throws_bad_alloc || size == 0xFFFFFFFFFFFFFFFF) {
Expand All @@ -52,9 +52,12 @@ custom_new(std::size_t size)
return malloc(size);
}

static void
custom_delete(void *ptr)
{ free(ptr); }
inline void
custom_delete(void *ptr, std::size_t size)
{
bfignored(size);
free(ptr);
}

void *
operator new[](std::size_t size)
Expand All @@ -65,19 +68,19 @@ operator new (std::size_t size)
{ return custom_new(size); }

void
operator delete (void *ptr, std::size_t /* size */) throw()
{ custom_delete(ptr); }
operator delete (void *ptr, std::size_t size) throw()
{ custom_delete(ptr, size); }

void
operator delete (void *ptr) throw()
{ custom_delete(ptr); }
{ operator delete (ptr, static_cast<std::size_t>(0)); }

void
operator delete[](void *ptr) throw()
{ custom_delete(ptr); }
operator delete[](void *ptr, std::size_t size) throw()
{ custom_delete(ptr, size); }

void
operator delete[](void *ptr, std::size_t /* size */) throw()
{ custom_delete(ptr); }
operator delete[](void *ptr) throw()
{ operator delete[](ptr, static_cast<std::size_t>(0)); }

#endif
54 changes: 20 additions & 34 deletions bfsdk/scripts/bareflank_clang_tidy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ OUTPUT=$PWD/.clang_tidy_results.txt
NUM_CORES=$(grep -c ^processor /proc/cpuinfo)

get_changed_files() {
pushd $1 > /dev/null
files=$(git diff --relative --name-only --diff-filter=ACM HEAD^ $PWD | grep -Ee "\.(cpp|h|c)$" || true)
pushd $2 > /dev/null
if [[ "$1" == "all" ]]; then
files=$(git ls-files | grep -Ee "\.(cpp|h|c)$" || true)
else
files=$(git diff --relative --name-only --diff-filter=ACM HEAD^ $PWD | grep -Ee "\.(cpp|h|c)$" || true)
fi
popd > /dev/null
}

Expand All @@ -89,33 +93,22 @@ verify_analysis() {
fi
}

run_clang_tidy_all() {
run-clang-tidy-4.0.py \
-clang-tidy-binary clang-tidy-4.0 \
-header-filter="*.h" \
-j=$NUM_CORES \
-checks=$1 > $OUTPUT 2>&1
}

run_clang_tidy_diff() {
run-clang-tidy-4.0.py \
-clang-tidy-binary clang-tidy-4.0 \
-header-filter="*.h" \
-j=$NUM_CORES \
-checks=$1 \
files $2 > $OUTPUT 2>&1
run_clang_tidy_script() {
if [[ ! $2 == *"pthread.cpp" ]] && [[ ! $2 == *"syscall.cpp" ]]; then
run-clang-tidy-4.0.py \
-clang-tidy-binary clang-tidy-4.0 \
-header-filter="*.h" \
-j=$NUM_CORES \
-checks=$1 \
files $2 > $OUTPUT 2>&1
fi
}

analyze() {
if [[ "$1" == "all" ]]; then
run_clang_tidy_all $2
verify_analysis "$3"
else
for f in $files; do
run_clang_tidy_diff $2 $f
verify_analysis "$3: $f"
done
fi
for f in $files; do
run_clang_tidy_script $2 $f
verify_analysis "$3: $f"
done
}

if [[ "$#" -lt 2 ]]; then
Expand All @@ -139,14 +132,7 @@ if [[ ! "$1" == "all" ]] && [[ ! "$1" == "diff" ]]; then
exit 1
fi

if [[ "$1" == "diff" ]]; then
get_changed_files $2

echo "Files undergoing static analysis:"
for f in $files; do
echo " - $f"
done
fi
get_changed_files $1 $2

#
# Perform Checks
Expand Down

0 comments on commit ef33f57

Please sign in to comment.