-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #27: Add a minimal unit test scheme.
- Loading branch information
Showing
5 changed files
with
75 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |