home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.7 KB  |  232 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /***********************************************************************
  20. **
  21. ** Name: fileio.c
  22. **
  23. ** Description: Program to copy one file to another. 
  24. **
  25. ** Modification History:
  26. ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  27. **             The debug mode will print all of the printfs associated with this test.
  28. **             The regress mode will be the default mode. Since the regress tool limits
  29. **           the output to a one line status:PASS or FAIL,all of the printf statements
  30. **             have been handled with an if (debug_mode) statement.
  31. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  32. **            recognize the return code from tha main program.
  33. ** 12-June-97 Revert to return code 0 and 1, remove debug option (obsolete).
  34. ***********************************************************************/
  35.  
  36. /***********************************************************************
  37. ** Includes
  38. ***********************************************************************/
  39. #include "prinit.h"
  40. #include "prthread.h"
  41. #include "prlock.h"
  42. #include "prcvar.h"
  43. #include "prmon.h"
  44. #include "prmem.h"
  45. #include "prio.h"
  46. #include "prlog.h"
  47.  
  48. #include <stdio.h>
  49.  
  50. #ifdef XP_MAC
  51. #include "prsem.h"
  52. #include "prlog.h"
  53. #define printf PR_LogPrint
  54. extern void SetupMacPrintfLog(char *logFile);
  55. #else
  56. #include "obsolete/prsem.h"
  57. #endif
  58.  
  59.  
  60. #define TBSIZE 1024
  61.  
  62. static PRUint8 tbuf[TBSIZE];
  63.  
  64. static PRFileDesc *t1, *t2;
  65.  
  66. PRIntn failed_already=0;
  67. PRIntn debug_mode;
  68. static void InitialSetup(void)
  69. {
  70.     PRUintn    i;
  71.     PRInt32 nWritten, rv;
  72.     
  73.     t1 = PR_Open("t1.tmp", PR_CREATE_FILE | PR_RDWR, 0);
  74.     PR_ASSERT(t1 != NULL);    
  75.     
  76.     for (i=0; i<TBSIZE; i++)
  77.         tbuf[i] = i;
  78.         
  79.     nWritten = PR_Write((PRFileDesc*)t1, tbuf, TBSIZE);
  80.     PR_ASSERT(nWritten == TBSIZE);    
  81.            
  82.     rv = PR_Seek(t1,0,PR_SEEK_SET);
  83.     PR_ASSERT(rv == 0);    
  84.  
  85.        t2 = PR_Open("t2.tmp", PR_CREATE_FILE | PR_RDWR, 0);
  86.     PR_ASSERT(t2 != NULL);    
  87. }
  88.  
  89.  
  90. static void VerifyAndCleanup(void)
  91. {
  92.     PRUintn    i;
  93.     PRInt32 nRead, rv;
  94.     
  95.     for (i=0; i<TBSIZE; i++)
  96.         tbuf[i] = 0;
  97.         
  98.     rv = PR_Seek(t2,0,PR_SEEK_SET);
  99.     PR_ASSERT(rv == 0);    
  100.  
  101.     nRead = PR_Read((PRFileDesc*)t2, tbuf, TBSIZE);
  102.     PR_ASSERT(nRead == TBSIZE);    
  103.            
  104.     for (i=0; i<TBSIZE; i++)
  105.         if (tbuf[i] != (PRUint8)i) {
  106.             if (debug_mode) printf("data mismatch for index= %d \n", i);
  107.             else failed_already=1;
  108.         }
  109.        PR_Close(t1);
  110.        PR_Close(t2);
  111.  
  112.        PR_Delete("t1.tmp");
  113.        PR_Delete("t2.tmp");
  114.  
  115.     if (debug_mode) printf("fileio test passed\n");
  116. }
  117.  
  118.  
  119. /*------------------ Following is the real test program ---------*/
  120. /*
  121.     Program to copy one file to another.  Two temporary files get
  122.     created.  First one gets written in one write call.  Then,
  123.     a reader thread reads from this file into a double buffer.
  124.     The writer thread writes from double buffer into the other
  125.     temporary file.  The second temporary file gets verified
  126.     for accurate data.
  127. */
  128.  
  129. PRSemaphore    *emptyBufs;    /* number of empty buffers */
  130. PRSemaphore *fullBufs;    /* number of buffers that are full */
  131.  
  132. #define BSIZE    100
  133.  
  134. struct {
  135.     char data[BSIZE];
  136.     PRUintn nbytes;        /* number of bytes in this buffer */
  137. } buf[2];
  138.  
  139. static void PR_CALLBACK reader(void *arg)
  140. {
  141.     PRUintn    i = 0;
  142.     PRInt32    nbytes;
  143.     
  144.     do {
  145.         (void) PR_WaitSem(emptyBufs);
  146.         nbytes = PR_Read((PRFileDesc*)arg, buf[i].data, BSIZE);
  147.         if (nbytes >= 0) {
  148.             buf[i].nbytes = nbytes;
  149.             PR_PostSem(fullBufs);
  150.             i = (i + 1) % 2;
  151.         }
  152.     } while (nbytes > 0);
  153. }
  154.  
  155. static void PR_CALLBACK writer(void *arg)
  156. {
  157.     PRUintn    i = 0;
  158.     PRInt32    nbytes;
  159.     
  160.     do {
  161.         (void) PR_WaitSem(fullBufs);
  162.         nbytes = buf[i].nbytes;
  163.         if (nbytes > 0) {
  164.             nbytes = PR_Write((PRFileDesc*)arg, buf[i].data, nbytes);
  165.             PR_PostSem(emptyBufs);
  166.             i = (i + 1) % 2;
  167.         }
  168.     } while (nbytes > 0);
  169. }
  170.  
  171. int main(int argc, char **argv)
  172. {
  173.     PRThread *r, *w;
  174.  
  175.  
  176.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  177.     PR_STDIO_INIT();
  178.  
  179. #ifdef XP_MAC
  180.     SetupMacPrintfLog("fileio.log");
  181.     debug_mode = 1;
  182. #endif
  183.  
  184.     emptyBufs = PR_NewSem(2);    /* two empty buffers */
  185.  
  186.     fullBufs = PR_NewSem(0);    /* zero full buffers */
  187.  
  188.     /* Create initial temp file setup */
  189.     InitialSetup();
  190.     
  191.     /* create the reader thread */
  192.     
  193.     r = PR_CreateThread(PR_USER_THREAD,
  194.                       reader, t1, 
  195.                       PR_PRIORITY_NORMAL,
  196.                       PR_LOCAL_THREAD,
  197.                       PR_JOINABLE_THREAD,
  198.                       0);
  199.  
  200.     w = PR_CreateThread(PR_USER_THREAD,
  201.                       writer, t2, 
  202.                       PR_PRIORITY_NORMAL,
  203.                       PR_LOCAL_THREAD,
  204.                       PR_JOINABLE_THREAD,
  205.                       0);
  206.  
  207.     /* Do the joining for both threads */
  208.     (void) PR_JoinThread(r);
  209.     (void) PR_JoinThread(w);
  210.  
  211.     /* Do the verification and clean up */
  212.     VerifyAndCleanup();
  213.  
  214.     PR_DestroySem(emptyBufs);
  215.     PR_DestroySem(fullBufs);
  216.  
  217.     PR_Cleanup();
  218.  
  219.     if(failed_already)
  220.     {
  221.         printf("Fail\n");
  222.         return 1;
  223.     }
  224.     else
  225.     {
  226.         printf("PASS\n");
  227.         return 0;
  228.     }
  229.  
  230.  
  231. }
  232.