2015-04-13 16:45:56 +10:00
|
|
|
#include "bitwise.hpp"
|
|
|
|
#include "tap.hpp"
|
2013-03-11 20:41:19 +11:00
|
|
|
|
2014-09-01 16:23:46 +10:00
|
|
|
|
2015-04-13 16:45:56 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-09-01 16:23:46 +10:00
|
|
|
static void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_rotate (cruft::TAP::logger &tap)
|
2015-04-13 16:45:56 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::rotatel (uint8_t (0x0F), 0), 0x0Fu, "rotate-left u8");
|
|
|
|
tap.expect_eq (cruft::rotatel (uint8_t (0x0F), 4), 0xF0u, "rotate-left u8");
|
|
|
|
tap.expect_eq (cruft::rotatel (uint8_t (0xF0), 4), 0x0Fu, "rotate-left u8");
|
|
|
|
tap.expect_eq (cruft::rotatel (uint8_t (0x0F), 8), 0x0Fu, "rotate-left u8");
|
2015-04-13 16:45:56 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::rotater (uint8_t (0x0F), 0), 0x0Fu, "rotate-right u8");
|
|
|
|
tap.expect_eq (cruft::rotater (uint8_t (0x0F), 4), 0xF0u, "rotate-right u8");
|
|
|
|
tap.expect_eq (cruft::rotater (uint8_t (0xF0), 4), 0x0Fu, "rotate-right u8");
|
|
|
|
tap.expect_eq (cruft::rotater (uint8_t (0x0F), 8), 0x0Fu, "rotate-right u8");
|
2015-04-13 16:45:56 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::rotatel (uint16_t (0xABCD), 0), 0xABCDu, "rotate-left u16");
|
|
|
|
tap.expect_eq (cruft::rotatel (uint16_t (0xABCD), 4), 0xBCDAu, "rotate-left u16");
|
|
|
|
tap.expect_eq (cruft::rotatel (uint16_t (0xABCD), 8), 0xCDABu, "rotate-left u16");
|
|
|
|
tap.expect_eq (cruft::rotatel (uint16_t (0xABCD), 12), 0xDABCu, "rotate-left u16");
|
|
|
|
tap.expect_eq (cruft::rotatel (uint16_t (0xABCD), 16), 0xABCDu, "rotate-left u16");
|
2015-04-13 16:45:56 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::rotater (uint16_t (0xABCD), 0), 0xABCDu, "rotate-right u16");
|
|
|
|
tap.expect_eq (cruft::rotater (uint16_t (0xABCD), 4), 0xDABCu, "rotate-right u16");
|
|
|
|
tap.expect_eq (cruft::rotater (uint16_t (0xABCD), 8), 0xCDABu, "rotate-right u16");
|
|
|
|
tap.expect_eq (cruft::rotater (uint16_t (0xABCD), 12), 0xBCDAu, "rotate-right u16");
|
|
|
|
tap.expect_eq (cruft::rotater (uint16_t (0xABCD), 16), 0xABCDu, "rotate-right u16");
|
2015-04-13 16:45:56 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::rotatel (uint32_t (0x12345670), 12), 0x45670123u, "rotate-left u32");
|
|
|
|
tap.expect_eq (cruft::rotater (uint32_t (0x12345670), 12), 0x67012345u, "rotate-right u32");
|
2015-04-13 16:45:56 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::rotatel (uint64_t (0x1234567890ABCDEF), 12), 0x4567890ABCDEF123u, "rotate-left u64");
|
|
|
|
tap.expect_eq (cruft::rotater (uint64_t (0x1234567890ABCDEF), 12), 0xDEF1234567890ABCu, "rotate-right u64");
|
2014-09-01 16:23:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-13 16:45:56 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-09-01 16:23:46 +10:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_reverse (cruft::TAP::logger &tap)
|
2015-04-13 16:45:56 +10:00
|
|
|
{
|
|
|
|
size_t matches = 0;
|
2014-09-01 16:23:46 +10:00
|
|
|
for (unsigned i = 0; i < 256; ++i) {
|
2018-08-05 14:42:02 +10:00
|
|
|
auto first = cruft::reverse<uint8_t> (i);
|
|
|
|
auto last = cruft::reverse<uint8_t> (first);
|
2015-04-13 16:45:56 +10:00
|
|
|
if (last == i)
|
|
|
|
matches++;
|
2014-09-01 16:23:46 +10:00
|
|
|
}
|
2015-04-13 16:45:56 +10:00
|
|
|
|
2015-11-13 17:10:10 +11:00
|
|
|
tap.expect_eq (matches, 256u, "exhaustive 8 bit reverse");
|
2014-09-01 16:23:46 +10:00
|
|
|
}
|
|
|
|
|
2015-04-13 16:45:56 +10:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-09-01 16:23:46 +10:00
|
|
|
int
|
2015-04-13 16:45:56 +10:00
|
|
|
main (int, char**)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2015-04-13 16:45:56 +10:00
|
|
|
|
|
|
|
test_rotate (tap);
|
|
|
|
test_reverse (tap);
|
2013-03-11 20:41:19 +11:00
|
|
|
|
2017-05-22 13:55:21 +10:00
|
|
|
return tap.status ();
|
2013-03-11 20:41:19 +11:00
|
|
|
}
|