#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* #include "posbb_tests.h"  // this would be needed by Test_qsort,not finished,yet
*/
#define TRUE -1
#define FALSE 0



/****** posbb_tests.c/Test_IMath ********************************************
*
*   NAME
*     Test_IMath -- Tests the speed of some int Math operations.
*
*   SYNOPSIS
*     time = Test_IMath( void )
*     BYTE Test_IMath( void );
*
*   FUNCTION
*     Does 1 million of additions,differences,multiplications and divisions
*     and returns the time it took.
*
*   RESULT
*     time - number of seconds taken. No error can be returned.
*
*   EXAMPLE
*     See posbb.c/Perform_Tests()
**
****************************************************************************
*/

int Test_IMath( precision )
int precision;
{
  int c,i=0;
  long *a=0,*b=0,*d=0;
  time_t time1,time2;

  a = calloc(sizeof(long),100*precision);
  b = calloc(sizeof(long),100*precision);
  d = calloc(sizeof(long),100*precision);
  /* Initializing b and d table with random values */
  for(i =0;i<(100*precision);i++)
  {
    b[i] = rand();
    d[i] = rand();
  }
  time1 = time(NULL);
  for (c = 0;c<10000;c++)
  {
    for ( i = 0; i<(100 * precision)-3; i+=3 )
    {
      a[i] /= b[i] + d[i];
      a[i+1] *= b[i+1] - d[i+1];
      a[i+2] += b[i+2] * d[i+2];
      a[i+3] -= b[i+3] / d[i+3];
    }
  }
  time2 = time(NULL);

  free(a);
  free(b);
  free(d);
  return (time2-time1);
}


int Test_FPMath( precision )
int precision;
{
    int c,i=0;
    double *a=0,*b,*d;
    time_t time1,time2;

    a = calloc(sizeof(double),100*precision);
    b = calloc(sizeof(double),100*precision);
    d = calloc(sizeof(double),100*precision);
    /* Initializing b and d table with random values; */
    for(c =0;c<(100*precision);c++) {
      b[c] = (double) rand();
      d[c] = (double) rand();
    }
  time1 = time(NULL);
  for (c = 0;c<10000;c++)
  {
    for ( i = 0; i<(100 * precision)-3; i+=3 )
    {
      a[i] /= b[i] + d[i];
      a[i+1] *= b[i+1] - d[i+1];
      a[i+2] += b[i+2] * d[i+2];
      a[i+3] -= b[i+3] / d[i+3];
    }
  }
  time2 = time(NULL);
  free(a);
  free(b);
  free(d);
  return (time2-time1);

}

int Test_TMath(precision)
int precision;
{
  time_t time1,time2;
  int c=0;
  double *cosine=0,*sine=0,*tangent=0,angle = 0;

  cosine = calloc(sizeof(double),360*precision);
  sine = calloc(sizeof(double),360*precision);
  tangent = calloc(sizeof(double),360*precision);

  time1 = time(NULL);
  for(c=0;c<100;c++)
  {
    for(angle=0;angle<360*precision;angle++)
    {
      sine[(int)angle] = sin(angle);
      cosine[(int)angle] = cos(angle);
      tangent[(int)angle] = tan(angle);
    }
  }
  time2 = time(NULL);
  free(sine);
  free(cosine);
  free(tangent);
  return (time2-time1);
}


