libcruft-util/test/hash/checksum.cpp

54 lines
1.3 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 <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**) {
cruft::TAP::logger tap;
2015-04-13 16:45:56 +10:00
cruft::hash::adler32 a;
cruft::hash::bsdsum b;
2016-06-20 16:49:22 +10:00
for (const auto &t: TESTS) {
tap.expect_eq (
t.adler,
a (cruft::view {t.data}.template cast<const uint8_t*> ()),
2021-04-13 16:05:08 +10:00
"adler checksum: {:s}", t.msg
2016-06-20 16:49:22 +10:00
);
tap.expect_eq (
t.bsd,
b (cruft::view {t.data}.template cast<const uint8_t*> ()),
2021-04-13 16:05:08 +10:00
"bsdsum checksum: {:s}", t.msg);
}
return tap.status ();
}