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