libcruft-util/test/checksum.cpp

56 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 "hash/simple.hpp"
#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;
2016-06-20 16:49:22 +10:00
for (const auto &t: TESTS) {
tap.expect_eq (
t.adler,
util::hash::simple<util::hash::adler32> (
(const void*)t.data,
(const void*)(t.data + strlen (t.data))
),
"adler checksum: %s", t.msg
);
tap.expect_eq (
t.bsd,
util::hash::simple<util::hash::bsdsum> (t.data, t.data + strlen (t.data)),
"bsdsum checksum: %s", t.msg);
}
return EXIT_SUCCESS;
}