lfsr_test.c revision dafcb997e390efa4423883dafd100c975c4095d6
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews/*
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999-2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt * purpose with or without fee is hereby granted, provided that the above
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt * copyright notice and this permission notice appear in all copies.
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt *
428dd5c588cfe0dc1519af728e6e75c10aeb4439Curtis Blackburn * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt * PERFORMANCE OF THIS SOFTWARE.
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt */
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt/* $Id: lfsr_test.c,v 1.12 2004/03/05 04:58:38 marka Exp $ */
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt#include <config.h>
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt#include <stdio.h>
cfd262045c23cadb8415f0111f56995258f17361Evan Hunt
#include <isc/lfsr.h>
#include <isc/util.h>
isc_uint32_t state[1024 * 64];
int
main(int argc, char **argv) {
isc_lfsr_t lfsr1, lfsr2;
int i;
isc_uint32_t temp;
UNUSED(argc);
UNUSED(argv);
/*
* Verify that returned values are reproducable.
*/
isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
for (i = 0; i < 32; i++) {
isc_lfsr_generate(&lfsr1, &state[i], 4);
printf("lfsr1: state[%2d] = %08x\n", i, state[i]);
}
isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
for (i = 0; i < 32; i++) {
isc_lfsr_generate(&lfsr1, &temp, 4);
if (state[i] != temp)
printf("lfsr1: state[%2d] = %08x, "
"but new state is %08x\n",
i, state[i], temp);
}
/*
* Now do the same with skipping.
*/
isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
for (i = 0; i < 32; i++) {
isc_lfsr_generate(&lfsr1, &state[i], 4);
isc_lfsr_skip(&lfsr1, 32);
printf("lfsr1: state[%2d] = %08x\n", i, state[i]);
}
isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
for (i = 0; i < 32; i++) {
isc_lfsr_generate(&lfsr1, &temp, 4);
isc_lfsr_skip(&lfsr1, 32);
if (state[i] != temp)
printf("lfsr1: state[%2d] = %08x, "
"but new state is %08x\n",
i, state[i], temp);
}
/*
* Try to find the period of the LFSR.
*
* x^16 + x^5 + x^3 + x^2 + 1
*/
isc_lfsr_init(&lfsr2, 0, 16, 0x00008016U, 0, NULL, NULL);
for (i = 0; i < 32; i++) {
isc_lfsr_generate(&lfsr2, &state[i], 4);
printf("lfsr2: state[%2d] = %08x\n", i, state[i]);
}
isc_lfsr_init(&lfsr2, 0, 16, 0x00008016U, 0, NULL, NULL);
for (i = 0; i < 32; i++) {
isc_lfsr_generate(&lfsr2, &temp, 4);
if (state[i] != temp)
printf("lfsr2: state[%2d] = %08x, "
"but new state is %08x\n",
i, state[i], temp);
}
return (0);
}