libcruft-util/test/hash/checksum.cpp

55 lines
1.4 KiB
C++
Raw Normal View History

#include "hash/adler.hpp"
#include "hash/bsdsum.hpp"
2016-06-20 16:49:22 +10:00
#include "types.hpp"
2015-04-13 16:45:56 +10:00
#include "tap.hpp"
#include "debug.hpp"
#include <cstdlib>
#include <cstring>
2016-03-11 13:28:56 +11:00
///////////////////////////////////////////////////////////////////////////////
static const char *ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
2016-03-11 13:28:56 +11:00
//-----------------------------------------------------------------------------
static const struct {
uint32_t adler;
uint16_t bsd;
2014-09-11 15:44:03 +10:00
2016-06-20 16:49:22 +10:00
std::string msg;
const char *data;
} TESTS[] = {
2016-06-20 16:49:22 +10:00
{ 0x00000001, 0x0000, "empty", "" },
{ 0xDF5B150C, 0x52FB, "alphabet", ALPHABET },
{ 0x11E60398, 0x3DC8, "wikipedia", "Wikipedia" },
{ 0x97B61069, 0x5555, "digits", "12345678901234567890123456789012345678901234567890123456789012345678901234567890" },
};
2016-03-11 13:28:56 +11:00
///////////////////////////////////////////////////////////////////////////////
int
main (int, char**) {
2015-04-13 16:45:56 +10:00
util::TAP::logger tap;
util::hash::adler32 a;
util::hash::bsdsum b;
2016-06-20 16:49:22 +10:00
for (const auto &t: TESTS) {
tap.expect_eq (
t.adler,
a (util::view {t.data}.template cast<const uint8_t> ()),
2016-06-20 16:49:22 +10:00
"adler checksum: %s", t.msg
);
tap.expect_eq (
t.bsd,
b (util::view {t.data}.template cast<const uint8_t> ()),
2016-06-20 16:49:22 +10:00
"bsdsum checksum: %s", t.msg);
}
return tap.status ();
}