home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / glass / glass.lha / GLASS / libtmc / testprog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-06  |  6.3 KB  |  308 lines

  1. /* 
  2.    Copyright (C) 1990 C van Reewijk, email: dutentb.uucp!reeuwijk
  3.  
  4. This file is part of GLASS.
  5.  
  6. GLASS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GLASS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GLASS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* File: testprog.c
  21.  *
  22.  * Test of library programs.
  23.  */
  24.  
  25. /* Unix libraries */
  26. #include <stdio.h>
  27.  
  28. /* the test library */
  29. #include "config.h"
  30. #include "tmc.h"
  31.  
  32. #if defined( __STDC__ ) && __STDC__ >0
  33. void testprintopt ( void );
  34. void testbrac ( void );
  35. void testcons ( void );
  36. void testunsigned ( void );
  37. void testint ( void );
  38. void testdouble ( void );
  39. void teststring ( void );
  40. void testtmbool ( void );
  41. void ftestint ( FILE *f );
  42. void ftestunsigned ( FILE *f );
  43. void fteststring ( FILE *f );
  44. void main ( void );
  45. #endif
  46.  
  47. /* testprintopt: test printopt routines */
  48. void testprintopt()
  49. {
  50.     int k;
  51.     int i;
  52.     int j;
  53.  
  54.     openlist();
  55.     closelist();
  56.     opentuple();
  57.     printword( "tuple1" );
  58.     printword( "24" );
  59.     closetuple();
  60.     opencons();
  61.     printword( "Cons1" );
  62.     closecons();
  63.     for( k=0; k<12; k+=3 ){
  64.     opencons();
  65.     printword( "Cons2" );
  66.     opencons();
  67.     printword( "Cons3" );
  68.     for( i=1; i<k; i++ ){
  69.         openlist();
  70.         for( j=0; j<i; j++ ) printword( "42" );
  71.         closelist();
  72.     }
  73.     closecons();
  74.     opentuple();
  75.     printword( "tuple" );
  76.     for( i=1; i<k; i++ ){
  77.         openlist();
  78.         for( j=0; j<i; j++ ) printword( "42" );
  79.         closelist();
  80.     }
  81.     closetuple();
  82.     closecons();
  83.     }
  84.     printword( "Top" );
  85.     printword( "level" );
  86.     printword( "printword()." );
  87. }
  88.  
  89. /* Test of fscanopenbrac() and fscanclosebrac().
  90.  * Read open brackets and match with close brackets until
  91.  * a group of 0 pairs is encountered. Write the brackets
  92.  * to the output file with the opencons() and closecons()
  93.  * functions.
  94.  */
  95. void testbrac()
  96. {
  97.     int bracs;
  98.     int i;
  99.  
  100.     do{
  101.     bracs = fscanopenbrac( stdin );
  102.     for( i=0; i<bracs; i++ ){
  103.         opencons();
  104.         printword( "Bractest" );
  105.         printword( "42" );
  106.     }
  107.     if( fscanclosebrac( stdin, bracs ) ){
  108.         fprintf( stderr, "*** Error: %s\n", tmerrmsg );
  109.     }
  110.     for( i=0; i<bracs; i++ ) closecons();
  111.     } while( bracs != 0 );
  112. }
  113.  
  114. /* Test of fscancons(). Read and write constructors until one
  115.    with name "StopFscanCons" is encountered.
  116.  */
  117. void testcons()
  118. {
  119.     char buf[1000];
  120.  
  121.     opencons();
  122.     printword( "Constest" );
  123.     openlist();
  124.     do{
  125.     if( fscancons( stdin, buf ) ){
  126.         fprintf( stderr, "*** Error: %s\n", tmerrmsg );
  127.     }
  128.     printword( buf );
  129.     } while( strcmp( buf, "StopFscanCons" ) != 0 );
  130.     closelist();
  131.     closecons();
  132. }
  133.  
  134. /* Test of print_unsigned() and fscan_unsigned(): read and write
  135.    unsigned's until one with value 42 is encountered.
  136.  */
  137. void testunsigned(){
  138.     unsigned int u;
  139.  
  140.     opencons();
  141.     printword( "Unsignedtest" );
  142.     do {
  143.     if( fscan_unsigned( stdin, &u ) ){
  144.         fprintf( stderr, "*** Error: %s\n", tmerrmsg );
  145.     }
  146.     print_unsigned( u );
  147.     } while( u != 42 );
  148.     closecons();
  149. }
  150.  
  151. /* Test of print_int() and fscan_int(): read and write int's until
  152.    one with value -1 is encountered.
  153.  */
  154. void testint(){
  155.     int i;
  156.  
  157.     opencons();
  158.     printword( "Inttest" );
  159.     do {
  160.     if( fscan_int( stdin, &i ) ){
  161.         fprintf( stderr, "*** Error: %s\n", tmerrmsg );
  162.     }
  163.     print_int( i );
  164.     } while( i != -1 );
  165.     closecons();
  166. }
  167.  
  168. /* test cmp_double() */
  169. void testdouble()
  170. {
  171.     int res;
  172.     double a = 1.2e3;
  173.  
  174.     res = cmp_double( (double) 1.0, (double) -1.0 );
  175.     if( res <= 0 ){
  176.     fprintf(
  177.         stderr,
  178.         "*** Error: cmp_double( (double) 1.0, (double) -1.0 ) = %d\n",
  179.         res
  180.     );
  181.     }
  182.     res = cmp_double( (double) -42.0, (double) -1.0 );
  183.     if( res >= 0 ){
  184.     fprintf(
  185.         stderr,
  186.         "*** Error: cmp_double( (double) -42.0, (double) -1.0 ) = %d\n",
  187.         res
  188.     );
  189.     }
  190.     res = cmp_double( a, a );
  191.     if( res != 0 ){
  192.     fprintf(
  193.         stderr,
  194.         "*** Error: cmp_double( a , a ) = %d\n",
  195.         res
  196.     );
  197.     }
  198. }
  199.  
  200. void teststring()
  201. {
  202.     char *s;
  203.     int stop;
  204.     int i;
  205.     char *sbuf[10];
  206.  
  207.     for( i=0; i<10; i++ ){
  208.     sbuf[i] = new_string( "testing 1,2,3...." );
  209.     }
  210.     for( i=0; i<10; i++ ){
  211.     fre_string( sbuf[i] );
  212.     }
  213.     opencons();
  214.     printword( "Stringouttest" );
  215.     print_string( "" );
  216.     print_string( "Test" );
  217.     closecons();
  218.     opencons();
  219.     printword( "Stringintest" );
  220.     stop = 0;
  221.     do {
  222.     if( fscan_string( stdin, &s ) ){
  223.         fprintf( stderr, "*** Error: %s\n", tmerrmsg );
  224.     }
  225.     print_string( s );
  226.     if( strcmp( s, "STOP"  ) == 0 ) stop = 1;
  227.     fre_string( s );
  228.     } while( stop == 0 );
  229.     closecons();
  230. }
  231.  
  232. void testtmbool()
  233. {
  234.     tmbool b;
  235.  
  236.     opencons();
  237.     printword( "Tmboolouttest" );
  238.     print_tmbool( TMTRUE );
  239.     print_tmbool( TMFALSE );
  240.     do {
  241.     if( fscan_tmbool( stdin, &b ) ){
  242.         fprintf( stderr, "*** Error: %s\n", tmerrmsg );
  243.     }
  244.     print_tmbool( b );
  245.     } while( b );
  246.     closecons();
  247. }
  248.  
  249. /* Test of fprint_int() */
  250. void ftestint( f )
  251.  FILE *f;
  252. {
  253.     fputs( "fprint_int: [", f );
  254.     fprint_int( f, 12 );
  255.     fputs( " ", f );
  256.     fprint_int( f, 0 );
  257.     fputs( " ", f );
  258.     fprint_int( f, -42 );
  259.     fputs( "]\n", f );
  260. }
  261.  
  262. /* Test of fprint_unsigned() */
  263. void ftestunsigned( f )
  264.  FILE *f;
  265. {
  266.     fputs( "fprint_unsigned: [", f );
  267.     fprint_unsigned( f, 12 );
  268.     fputs( " ", f );
  269.     fprint_unsigned( f, 0 );
  270.     fputs( " ", f );
  271.     fprint_unsigned( f, 42 );
  272.     fputs( "]\n", f );
  273. }
  274.  
  275. void fteststring( f )
  276.  FILE *f;
  277. {
  278.     fputs( "fprint_string: [", f );
  279.     fprint_string( f, "blabla" );
  280.     fputs( " ", f );
  281.     fprint_string( f,  "" );
  282.     fputs( " ", f );
  283.     fprint_string( f,  "reutel" );
  284.     fputs( "]\n", f );
  285. }
  286.  
  287. int main()
  288. {
  289.     tmlineno = 1;
  290.     setprint( stdout, 4, 80 );
  291.     testprintopt();
  292.     testbrac();
  293.     testcons();
  294.     testint();
  295.     testunsigned();
  296.     testdouble();
  297.     teststring();
  298.     testtmbool();
  299.     ftestint( stdout );
  300.     ftestunsigned( stdout );
  301.     fteststring( stdout );
  302.     stat_string( stderr );
  303.     fprintf( stdout, "tmlineno=%d\n", tmlineno );
  304.     tmfatal( __FILE__, __LINE__, "test of 'tmfatal'" );
  305.     exit( 0 );
  306.     return( 0 );
  307. }
  308.