home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tstngn.zip / DISK1.ZIP / tstdemo.c < prev    next >
Text File  |  1994-04-23  |  9KB  |  255 lines

  1. /* Version of 94/04/23 by Peter Kanis */
  2. /************************************************************************
  3. *  Project     : TSTNGEN                     DATE    : 12.04.94         *
  4. *  Module      : DEMO                        VERSION : 1                *
  5. *  Filename    : tstdemo.c                   AUTHOR  : Peter J. Kanis   *
  6. *************************************************************************
  7. *                                                                       *
  8. *  Description : Demo Program for Test Engine.                          *
  9. *                                                                       *
  10. *  Export      :                                                        *
  11. *                                                                       *
  12. *  Defines     : Compile with define TST_NGEN for macros to work.       *
  13. *                                                                       *
  14. *************************************************************************
  15. *                                                                       *
  16. *  (c) ADD Consulting  All Rights Reserved                              *
  17. *                                                                       *
  18. *  The use of this code by third parties is licenced under contractual  *
  19. *  agreements.                                                          *
  20. *                                                                       *
  21. *************************************************************************/
  22.  
  23.  
  24.  
  25. #define     INCL_DOS
  26. #define     INCL_BASE
  27. #define     INCL_ERRORS
  28. #include    <os2.h>
  29. #include    <stdio.h>
  30. #include    <malloc.h>
  31. #include    <process.h>
  32. #include    <stddef.h>
  33. #include    <string.h>
  34. #include    <tstngen.h>
  35. #include    <files.h>
  36.  
  37.  
  38. HEV         sem1, sem2, sem3, sem4, sem5, sem6;
  39. ULONG       delay = 1000L;
  40. char        scr_path[256];
  41.  
  42. void testthread(void *);
  43.  
  44. int main(int argc, char *argv[])
  45.  
  46.     {
  47.     USHORT      i, *j, rc;
  48.     char        test_str[ 128];
  49.     HSCRIPT     psc;
  50.     ULONG       drv;
  51.     char        drv_str[ 4],
  52.                 path[ 128],
  53.                 fname[ 256];
  54.     
  55.     /***************** Initialize The test NGEN ******************/
  56.  
  57.     psc = TstInitTest( argc, argv);
  58.     if (!psc)
  59.         {
  60.         printf("Something went seriously wrong !!");
  61.         exit(1);
  62.         }
  63.     
  64.     /***** get the path for script files ********************************/
  65.     
  66.     FileParsePath( argv[ 1], &drv, drv_str, path, fname);
  67.     sprintf( scr_path, "%s%s\\", drv_str, path);
  68.     
  69.     /***** switch global dumping file on ********************************/
  70.     
  71.     TstDumpOnOff( TST_TRON);
  72.     
  73.     /***** Create semaphores for synchronisation ************************/
  74.     
  75.     DosCreateEventSem( NULL, &sem1, 0L, FALSE);
  76.     DosCreateEventSem( NULL, &sem2, 0L, FALSE);
  77.     DosCreateEventSem( NULL, &sem3, 0L, FALSE);
  78.     DosCreateEventSem( NULL, &sem4, 0L, FALSE);
  79.     DosCreateEventSem( NULL, &sem5, 0L, FALSE);
  80.     DosCreateEventSem( NULL, &sem6, 0L, FALSE);
  81.     
  82.     /***** Start 3 separate threads each with a script file *************/
  83.     
  84.     for (i = 2;i <= 4;i++) 
  85.         {
  86.         j = (PUSHORT) malloc( sizeof( USHORT));
  87.         *j = i;
  88.         if (_beginthread( testthread, NULL, 8192, (PVOID)j) < 0)
  89.             {
  90.             TstWriteTrace( " failed to start thread %u..\n", i);
  91.             exit( 4);
  92.             }
  93.  
  94.         }
  95.     
  96.     /***** Wait for the threads to have started *************************/
  97.     
  98.     DosWaitEventSem( sem1, SEM_INDEFINITE_WAIT);
  99.     DosWaitEventSem( sem2, SEM_INDEFINITE_WAIT);
  100.     DosWaitEventSem( sem3, SEM_INDEFINITE_WAIT);
  101.     
  102.     /***** read the script file line by line ****************************/
  103.     
  104.     rc = TstReadScriptLine( psc, test_str);
  105.     while( rc == 0)
  106.         {
  107.         
  108.         /***** Trace using function interface ***************************/
  109.         
  110.         TstWriteTrace("Read string '%s' from script file TEST.SPT, result = %u", test_str, rc);
  111.         DosSleep( delay);
  112.         
  113.         /***** read the next line from the script ***********************/
  114.         
  115.         rc = TstReadScriptLine( psc, test_str);
  116.         }
  117.     
  118.     /***** close the script & trace files *******************************/
  119.         
  120.     TstCloseScript( psc);
  121.     TstWriteTrace("Closed script file");   
  122.     TstCloseTrace();
  123.     TstWriteTrace("Closed Trace file");   
  124.     
  125.     /***** wait for the other threads to end ****************************/
  126.     
  127.     DosWaitEventSem( sem4, SEM_INDEFINITE_WAIT);
  128.     TstWriteTrace("sem4 Cleared -> Thread 2 terminated");
  129.     DosWaitEventSem( sem5, SEM_INDEFINITE_WAIT);
  130.     TstWriteTrace("sem5 Cleared -> Thread 3 terminated");
  131.     DosWaitEventSem( sem6, SEM_INDEFINITE_WAIT);
  132.     TstWriteTrace("sem6 Cleared -> Thread 4 terminated");
  133.     
  134.     /***** close global dumping *****************************************/
  135.         
  136.     TstDumpOnOff( TST_TROFF);
  137.     
  138.     /***** deregister the whole process *********************************/
  139.     
  140.     TstStopProcess();
  141.     exit(0);
  142.     return(0);
  143.     
  144.     }   /* end of main() */
  145.  
  146.  
  147. /************************************************************************/
  148. /* Thread function for reading other scripts.                           */
  149. /************************************************************************/
  150.  
  151. void testthread(void *p)
  152.  
  153.     {
  154.     USHORT      j;
  155.     USHORT      rc;
  156.     char        thr_name[128];
  157.     char        test_str[128];
  158.     char        scr_name[128];
  159.     HSCRIPT     psc;
  160.     
  161.     /***** Post the relevant semaphore **********************************/
  162.     
  163.     j = *(PUSHORT)p;
  164.     switch( j)
  165.         {
  166.         case 2 :
  167.             DosPostEventSem( sem1);
  168.             break;
  169.         
  170.         case 3 :
  171.             DosPostEventSem( sem2);
  172.             break;
  173.             
  174.         case 4 :
  175.             DosPostEventSem( sem3);
  176.             break;
  177.         }
  178.     
  179.     /***** Trace with macro *********************************************/
  180.     
  181.     TST_TRACE( "Thread %d started", j, 0, 0, 0);    
  182.     
  183.     /***** determine script file name ***********************************/
  184.     
  185.     sprintf( thr_name, "TEST%u", j);
  186.     sprintf( scr_name, "%s%s.SPT", scr_path, thr_name);
  187.     
  188.     /***** register the current thread with Test Engine/2 ***************/
  189.     
  190.     TST_REG( thr_name);
  191.     
  192.     /***** open the script file *****************************************/
  193.     
  194.     psc = TstOpenScriptFile( scr_name);
  195.     
  196.     /***** read script line by line *************************************/
  197.     
  198.     if (psc)
  199.         {
  200.         TST_TRACE( "Opened script file %s for thread %d", scr_name, j, 0, 0);
  201.         rc = TstReadScriptLine( psc, test_str);
  202.         while( rc == 0)
  203.             {
  204.         
  205.             /***** trace with macro *************************************/
  206.  
  207.             TST_TRACE( "Read string '%s' from script file TEST.SPT, result = %u", test_str, rc, 0, 0);
  208.             DosSleep( delay);
  209.             rc = TstReadScriptLine( psc, test_str);
  210.             }
  211.     
  212.         /***** close all the files **************************************/
  213.         
  214.         TstCloseScript( psc);
  215.         TST_TRACE( "Closed script file", 0, 0, 0, 0);   
  216.         TST_CLOSE();
  217.         TST_TRACE( "Closed Trace file", 0, 0, 0, 0);   
  218.         }
  219.     
  220.     /***** post the relevant semaphore **********************************/
  221.         
  222.     switch( j)
  223.         {
  224.         case 2 :
  225.             DosPostEventSem( sem4);
  226.             TST_TRACE( "Thread Terminates, cleared semaphore 'sem4'", 0, 0, 0, 0);
  227.             break;
  228.         
  229.         case 3 :
  230.             DosPostEventSem( sem5);
  231.             TST_TRACE( "Thread Terminates, cleared semaphore 'sem5'", 0, 0, 0, 0);
  232.             break;
  233.             
  234.         case 4 :
  235.             DosPostEventSem( sem6);
  236.             TST_TRACE( "Thread Terminates, cleared semaphore 'sem6'", 0, 0, 0, 0);
  237.             break;
  238.         }
  239.     
  240.     /***** Deregister the thread ****************************************/
  241.     
  242.     TST_END();
  243.     
  244.     }   /* end of testthread() */
  245.     
  246.  
  247. /***********************************************************************/
  248. /*                                                                     */
  249. /*                    (c) ADD Consulting 1993                          */
  250. /*                                                                     */
  251. /*                           END OF FILE.                              */
  252. /*                                                                     */
  253. /***********************************************************************/
  254.  
  255.