/*
   file pi_test.c
   (C) 2003 Yann GUIDON <whygee@f-cpu.org>
   version Sat Dec 27 11:33:56 CET 2003

   Verifies that phasing-in.c and pi_opt.c
   work as expected. More useful than you'd think !

   compile with
     gcc -o pi_test pi_test.c
   or
     gcc -DOPT -o pi_test pi_test.c
*/

#ifndef OPT
#include "phasing-in.c"
#else
#include "pi_opt.c"
#endif

unsigned char s[50];

int main() {
  unsigned int i, j, range;

  stream = &s[0];

  /* encode everything */
  range=0;
  for (i=1; i<=10; i++) {
    printf("\n --- %d --- (range = %d) \n", i, range);

    for (j=0; j<i; j++)
      pi_put_bits(j, range, i);

    if ((1U<<range) <= (i + 1))
      range ++;
  }

  /* check then reinitialise the bitstream */
  printf("\nResult : %d bits\n\n",
      shift_count + (stream_length << 3));

  if (shift_count > 0)
    stream[stream_length++] = reservoir;

  stream_length = 0;
  shift_count = 0;
  reservoir = 0;

  /* check the result */
  range=0;
  for (i=1; i<=10; i++) {
    printf("\n --- %d --- (range = %d) \n", i, range);

    for (j=0; j<i; j++)
      if (pi_get_bits(range, i) != j) {
        printf("\n error !!!\n");
        return -1;
      }

    if ((1U<<range) <= (i + 1))
      range ++;
  }

  return 0;
}

