1/*
2 * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: lfsr_test.c,v 1.16 2007/06/19 23:46:59 tbox Exp $ */
19
20/*! \file */
21#include <config.h>
22
23#include <stdio.h>
24
25#include <isc/lfsr.h>
26#include <isc/util.h>
27
28isc_uint32_t state[1024 * 64];
29
30int
31main(int argc, char **argv) {
32	isc_lfsr_t lfsr1, lfsr2;
33	int i;
34	isc_uint32_t temp;
35
36	UNUSED(argc);
37	UNUSED(argv);
38
39	/*
40	 * Verify that returned values are reproducable.
41	 */
42	isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
43	for (i = 0; i < 32; i++) {
44		isc_lfsr_generate(&lfsr1, &state[i], 4);
45		printf("lfsr1:  state[%2d] = %08x\n", i, state[i]);
46	}
47	isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
48	for (i = 0; i < 32; i++) {
49		isc_lfsr_generate(&lfsr1, &temp, 4);
50		if (state[i] != temp)
51			printf("lfsr1:  state[%2d] = %08x, "
52			       "but new state is %08x\n",
53			       i, state[i], temp);
54	}
55
56	/*
57	 * Now do the same with skipping.
58	 */
59	isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
60	for (i = 0; i < 32; i++) {
61		isc_lfsr_generate(&lfsr1, &state[i], 4);
62		isc_lfsr_skip(&lfsr1, 32);
63		printf("lfsr1:  state[%2d] = %08x\n", i, state[i]);
64	}
65	isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
66	for (i = 0; i < 32; i++) {
67		isc_lfsr_generate(&lfsr1, &temp, 4);
68		isc_lfsr_skip(&lfsr1, 32);
69		if (state[i] != temp)
70			printf("lfsr1:  state[%2d] = %08x, "
71			       "but new state is %08x\n",
72			       i, state[i], temp);
73	}
74
75	/*
76	 * Try to find the period of the LFSR.
77	 *
78	 *	x^16 + x^5 + x^3 + x^2 + 1
79	 */
80	isc_lfsr_init(&lfsr2, 0, 16, 0x00008016U, 0, NULL, NULL);
81	for (i = 0; i < 32; i++) {
82		isc_lfsr_generate(&lfsr2, &state[i], 4);
83		printf("lfsr2:  state[%2d] = %08x\n", i, state[i]);
84	}
85	isc_lfsr_init(&lfsr2, 0, 16, 0x00008016U, 0, NULL, NULL);
86	for (i = 0; i < 32; i++) {
87		isc_lfsr_generate(&lfsr2, &temp, 4);
88		if (state[i] != temp)
89			printf("lfsr2:  state[%2d] = %08x, "
90			       "but new state is %08x\n",
91			       i, state[i], temp);
92	}
93
94	return (0);
95}
96