Skip to content

Commit

Permalink
refs #27: Add a minimal unit test scheme.
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol committed Jan 19, 2016
1 parent 7f9af6d commit 740fde9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
15 changes: 13 additions & 2 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ SUPPRESSED_CC_WARNINGS = (
CFLAGS = '-march=native -O2 -g -Wall -Wextra ' + ' '.join(map(lambda s: '-Wno-' + s, SUPPRESSED_CC_WARNINGS)) + ' -Iinclude'
if os.getenv('DEBUG', 0):
CFLAGS = '-march=native -Og -g3 -Wall -Wextra ' + ' '.join(map(lambda s: '-Wno-' + s, SUPPRESSED_CC_WARNINGS)) + ' -Iinclude -DDEBUG'
#LIBS = '-ltcmalloc_minimal -lnuma -lpthread -lpcre -lrt'
LIBS = '-lnuma -lpthread -lpcre -lrt'
LIBS = '-lnuma -pthread -lpcre -lrt'
if USE_CUDA: CFLAGS += ' -DUSE_CUDA'
if USE_PHI: CFLAGS += ' -DUSE_PHI'
if USE_OPENSSL_EVP: CFLAGS += ' -DUSE_OPENSSL_EVP'
Expand Down Expand Up @@ -232,6 +231,18 @@ _clean_cmds = '\n'.join(['rm -rf build bin/main `find . -path "lib/*_map.hh"`']
rule clean:
shell: _clean_cmds

_test_cases, = glob_wildcards('tests/test_{case}.cc')

rule test:
input: expand('tests/test_{case}', case=_test_cases)

for case in _test_cases:
includes = [f for f in compilelib.get_includes(fmt('tests/test_{case}.cc'), 'include')]
rule:
input: fmt('tests/test_{case}.cc'), includes
output: fmt('tests/test_{case}')
shell: '{CXX} {CXXFLAGS} -o {output} {input[0]} {LIBS}'

for srcfile in SOURCE_FILES:
# We generate build rules dynamically depending on the actual header
# dependencies to fully exploit automatic dependency checks.
Expand Down
16 changes: 16 additions & 0 deletions include/nba/core/assert.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __NBA_CORE_ASSERT_HH__
#define __NBA_CORE_ASSERT_HH__

#include <cstdio>
#include <cstdlib>

#define test_assert(cond, msg) {\
if (!(cond)) { \
fprintf(stderr, "Assertion failure: %s, %s\n", #cond, msg); \
exit(1); \
} \
}

#endif

// vim: ts=8 sts=4 sw=4 et
27 changes: 27 additions & 0 deletions include/nba/core/shiftedint.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef __NBA_CORE_SHIFTEDINT_HH__
#define __NBA_CORE_SHIFTEDINT_HH__

#include <cstdint>
#include <type_traits>

namespace nba {

template<typename Int, unsigned Shift>
class ShiftedInt
{
static_assert(std::is_integral<Int>::value, "Integer type required.");

Int i;

public:
ShiftedInt(Int initial) : i(initial) { }

ShiftedInt& operator= (Int v) { i = (v >> Shift); return *this; }
operator uint32_t() { return (uint32_t) (i << Shift); }
};

} /* endns(nba) */

#endif /* __NBA_CORE_SHIFTEDINT_HH__ */

// vim: ts=8 sts=4 sw=4 et
1 change: 0 additions & 1 deletion src/lib/elementgraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ void ElementGraph::send_offload_task_to_device(OffloadTask *task)
size_t num_batches = task->batches.size();
if (task->batches[0]->datablock_states == nullptr) {
struct datablock_tracker *dbstates[num_batches];
int bidx = 0;
assert(0 == rte_mempool_get_bulk(ctx->dbstate_pool,
(void **) &dbstates,
num_batches));
Expand Down
19 changes: 19 additions & 0 deletions tests/test_core_shiftedint.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <nba/core/assert.hh>
#include <nba/core/shiftedint.hh>
#include <cstdio>

using namespace nba;

int main() {
printf("TEST: core.shiftedint\n");
{
auto x = ShiftedInt<uint16_t, 2>(123);
test_assert((uint32_t) x == 120, "Assign-shift failed.");
}
{
auto x = ShiftedInt<uint16_t, 2>(120);
test_assert((uint32_t) x == 120, "Assign-shift failed.");
}
}

// vim: ts=8 sts=4 sw=4 et

0 comments on commit 740fde9

Please sign in to comment.