summaryrefslogtreecommitdiff
path: root/ccan/crcsync/test/run-roll.c
blob: 95c50c1abe93a9c4deee4200291d3df9998c39e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <ccan/crcsync/crcsync.h>
#include <ccan/crcsync/crcsync.c>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <stdbool.h>

/* FIXME: ccanize. */
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

static void test_roll(unsigned int wsize)
{
	uint8_t data[wsize * 2];
	uint64_t uncrc_tab[256];
	unsigned int i;

	init_uncrc_tab(uncrc_tab, wsize);

	for (i = 0; i < ARRAY_SIZE(data); i++)
		data[i] = random();

	for (i = 1; i < ARRAY_SIZE(data) - wsize; i++) {
		uint64_t rollcrc, crc;

		crc = crc64_iso(0, data+i, wsize);
		rollcrc = crc_roll(crc64_iso(0, data+i-1, wsize),
				   data[i-1], data[i+wsize-1], uncrc_tab);

		ok(crc == rollcrc, "wsize %u, i %u", wsize, i);
	}
}

int main(int argc, char *argv[])
{
	plan_tests(100 - 1 + 128 - 1);
	test_roll(100);
	test_roll(128);
	return exit_status();
}