nsprpub/pr/tests/fileio.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial