/**********************************************

Programma di Test per Routines di bibl.
           last update 10/07/87
      AMIGA-Version by Frank Kremser
PC-Original-Version by Dr. Edgar Huckent
       (C) 1987  by Markt & Technik

************************************************

Test delle piu' importanti funzioni di biblioteca

************************************************/

#include <stdio.h>
#include <ctype.h>
#include <math.h>

int n,fno,iret,fd1;
long lret;
double fret,dv,pi;
char puffer[512],*cadr;
char car[10][10];
typedef struct test
{
  char dummy[1000];
  struct test *next;
} TSTRUCT;
TSTRUCT *badr,*start,*last;

void errexit(str)
char *str;
{
  printf("\n\t\t--- %s ---",str);
  exit(1);
}   /* fine */

void weiter()
{
  printf("\nPremi un tasto:");
  getchar();
}   /* end weiter */

void main()
{
  /* Dimensioni tipi dati */
  printf("\nsizeof char:     %d",sizeof(char));
  printf("\nsizeof short:    %d",sizeof(short));
  printf("\nsizeof int:      %d",sizeof(int));
  printf("\nsizeof long:     %d",sizeof(long));
  printf("\nsizeof float:    %d",sizeof(float));
  printf("\nsizeof double:   %d",sizeof(double));
  printf("\nsizeof (char *): %d",sizeof(char *));

  /* Conversioni */
  weiter();
  iret = atoi("   -123   ");
  printf("\nRisultato atoi: %d",iret);
  lret = atol("   +123123   ");
  printf("\nRisultato atol: %ld",lret);
  fret = atof("   -123.30e7   ");
  printf("\nRisultato atof: %f",fret);
  iret = isascii('A');
  printf("\nRisultato isascii per A: %d",iret);
  iret = isalpha('A');
  printf("\nRisultato isalpha per A: %d",iret);

  /* test per float e double in printf() */
  printf("\n4.5 float normale %f",4.5);
  printf("\n4.5 float scientifico %e",4.5);
  printf("\n4.5 Formato g %g",4.5);

  /* Routine matematiche */
  weiter();
  pi = 3.14159;
  dv = sin(45.0 * pi / 180.0);
  printf("\nsin(45)=%g",dv);
  dv = cos(45.0 * pi / 180.0);
  printf("\ncos(45)=%g",dv);
  dv = tan(45.0 * pi / 180.0);
  printf("\ntan(45)=%g",dv);
  dv = log(10.0);
  printf("\nlog(10.0)=%g",dv);
  dv = log10(10.0);
  printf("\nlog10(10.0)=%g",dv);
  dv = sqrt(2.0);
  printf("\nsqrt(2.0)=%g",dv);

  /* Test I/O di base */
  /* + controllo se write e' trasparente per default */

  weiter();
  printf("\nTest I/O di Base");
  /* Creazione File */
  fd1 = creat("xx.tst",0xffff);
  /* Scrittura di 4 caratteri */
  iret = write(fd1,"abc\n",4);
  /* Chiusura file */
  close(fd1);
  /* lettura file */
  fd1 = open("xx.tst",0);
  printf("\nRisultato open = %d",fd1);
  /* lettura dal file */
  iret = read(fd1,puffer,4);
  /* Output su Terminale con I/O di Base */
  write(1,"\nRisultato lettura file :",24);
  write(1,puffer,4);
  /* test su espansione di \n come LF+CR */
  iret = read(fd1,puffer,1);
  if (iret <= 0)
    printf("\nFunzioni di lettura/scrittura trasparenti");
  else  printf("\nFunzioni di lettura/scrittura NON trasparenti");
  close(fd1);

  /* Routines per gestione stringhe */
  weiter();
  printf("\nstrcmp: %d",strcmp("ABC","abc"));
  printf("\nstrncmp: %d",strncmp("ABC","abcdef",4));
  printf("\nstrcpy: %s",strcpy(puffer,"vwxyz"));
  printf("\nstrncpy: %s",strncpy(puffer,"abcdefg",3));
  cadr = strchr("ABCDEFG",'D');
  printf("\nstrchr: %lx",cadr);
  cadr = strchr("ABCDEFG",'X');
  printf("\nstrchr: %lx",cadr);

  /* Test di malloc e free */
  weiter();
  start = last = NULL;
  iret = 0;
  /* Allocazione blocchi finche' possibile */
  while (1)
    {
     badr =(TSTRUCT *) malloc(sizeof(TSTRUCT));
     if (badr == NULL) break;
     iret++;
     if (last != NULL) last->next = badr;
     else              start = badr;
     badr->next = NULL;
     last = badr;
    }
  /* libera blocchi */
  badr = start;
  while (badr != NULL)
    {
     last = badr;
     badr = badr->next;
     free(last);
    }
  printf("\n Blocchi di 1000 Bytes allocati = %d\n\n", iret);
}   /* end main */