Back to index

glibc  2.9
Functions | Variables
tst-random.c File Reference
#include <stdlib.h>
#include <stdio.h>

Go to the source code of this file.

Functions

void fail (const char *msg, int s, int i) __attribute__((__noreturn__))
int main (void)

Variables

const int degree = 128
const int nseq = 3
const int nrnd = 50
const unsigned int seed [3] = { 0x12344321U, 0xEE11DD22U, 0xFEDCBA98 }

Function Documentation

void fail ( const char *  msg,
int  s,
int  i 
)

Definition at line 114 of file tst-random.c.

{
  printf ("\nTest FAILED: ");
  printf ("%s (seq %d, pos %d).\n", msg, s, i);
  exit (1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( void  )

Definition at line 50 of file tst-random.c.

{
  long int rnd[nseq][nrnd]; /* pseudorandom numbers */
  char* state[nseq];        /* state for PRNG */
  char* oldstate[nseq];            /* old PRNG state */
  int s;                    /* sequence index */
  int i;                    /* element index */

  printf ("Begining random package test using %d sequences of length %d.\n",
         nseq, nrnd);

  /* 1. Generate and store the sequences.  */
  printf ("Generating random sequences.\n");
  for (s = 0; s < nseq; ++s)
    {
      srandom ( seed[s] );
      for (i = 0; i < nrnd; ++i)
       rnd[s][i] = random ();
    }

  /* 2. Regenerate and check.  */
  printf ("Regenerating and checking sequences.\n");
  for (s = 0; s < nseq; ++s)
    {
      srandom (seed[s]);
      for (i = 0; i < nrnd; ++i)
       if (rnd[s][i] != random ())
         fail ("first regenerate test", s, i);
    }

  /* 3. Create state vector, one for each sequence.
       First state is random's internal state; others are malloced.  */
  printf ("Creating and checking state vector for each sequence.\n");
  srandom (seed[0]);               /* reseed with first seed */
  for (s = 1; s < nseq; ++s)
    {
      state[s] = (char*) malloc (degree);
      oldstate[s] = initstate (seed[s], state[s], degree);
    }
  state[0] = oldstate[1];

  /* Check returned values.  */
  for (s = 1; s < nseq - 1; ++s)
    if (state[s] != oldstate[s + 1])
      fail ("bad initstate() return value", s, i);

  /* 4. Regenerate sequences interleaved and check.  */
  printf ("Regenerating and checking sequences in interleaved order.\n");
  for (i = 0; i < nrnd; ++i)
    {
      for (s = 0; s < nseq; ++s)
       {
         char *oldstate = (char *) setstate (state[s]);
         if (oldstate != state[(s + nseq - 1) % nseq])
           fail ("bad setstate() return value", s, i);
         if (rnd[s][i] != random ())
           fail ("bad value generated in interleave test", s, i);
       }
    }
  printf ("All tests passed!\n");
  return 0;
}

Here is the call graph for this function:


Variable Documentation

const int degree = 128

Definition at line 41 of file tst-random.c.

const int nrnd = 50

Definition at line 44 of file tst-random.c.

const int nseq = 3

Definition at line 43 of file tst-random.c.

const unsigned int seed[3] = { 0x12344321U, 0xEE11DD22U, 0xFEDCBA98 }

Definition at line 45 of file tst-random.c.