1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/fileio.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,206 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/*********************************************************************** 1.10 +** 1.11 +** Name: fileio.c 1.12 +** 1.13 +** Description: Program to copy one file to another. 1.14 +** 1.15 +** Modification History: 1.16 +** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. 1.17 +** The debug mode will print all of the printfs associated with this test. 1.18 +** The regress mode will be the default mode. Since the regress tool limits 1.19 +** the output to a one line status:PASS or FAIL,all of the printf statements 1.20 +** have been handled with an if (debug_mode) statement. 1.21 +** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to 1.22 +** recognize the return code from tha main program. 1.23 +** 12-June-97 Revert to return code 0 and 1, remove debug option (obsolete). 1.24 +***********************************************************************/ 1.25 + 1.26 +/*********************************************************************** 1.27 +** Includes 1.28 +***********************************************************************/ 1.29 +#include "prinit.h" 1.30 +#include "prthread.h" 1.31 +#include "prlock.h" 1.32 +#include "prcvar.h" 1.33 +#include "prmon.h" 1.34 +#include "prmem.h" 1.35 +#include "prio.h" 1.36 +#include "prlog.h" 1.37 + 1.38 +#include <stdio.h> 1.39 + 1.40 +#include "obsolete/prsem.h" 1.41 + 1.42 + 1.43 +#define TBSIZE 1024 1.44 + 1.45 +static PRUint8 tbuf[TBSIZE]; 1.46 + 1.47 +static PRFileDesc *t1, *t2; 1.48 + 1.49 +PRIntn failed_already=0; 1.50 +PRIntn debug_mode; 1.51 +static void InitialSetup(void) 1.52 +{ 1.53 + PRUintn i; 1.54 + PRInt32 nWritten, rv; 1.55 + 1.56 + t1 = PR_Open("t1.tmp", PR_CREATE_FILE | PR_RDWR, 0); 1.57 + PR_ASSERT(t1 != NULL); 1.58 + 1.59 + for (i=0; i<TBSIZE; i++) 1.60 + tbuf[i] = i; 1.61 + 1.62 + nWritten = PR_Write((PRFileDesc*)t1, tbuf, TBSIZE); 1.63 + PR_ASSERT(nWritten == TBSIZE); 1.64 + 1.65 + rv = PR_Seek(t1,0,PR_SEEK_SET); 1.66 + PR_ASSERT(rv == 0); 1.67 + 1.68 + t2 = PR_Open("t2.tmp", PR_CREATE_FILE | PR_RDWR, 0); 1.69 + PR_ASSERT(t2 != NULL); 1.70 +} 1.71 + 1.72 + 1.73 +static void VerifyAndCleanup(void) 1.74 +{ 1.75 + PRUintn i; 1.76 + PRInt32 nRead, rv; 1.77 + 1.78 + for (i=0; i<TBSIZE; i++) 1.79 + tbuf[i] = 0; 1.80 + 1.81 + rv = PR_Seek(t2,0,PR_SEEK_SET); 1.82 + PR_ASSERT(rv == 0); 1.83 + 1.84 + nRead = PR_Read((PRFileDesc*)t2, tbuf, TBSIZE); 1.85 + PR_ASSERT(nRead == TBSIZE); 1.86 + 1.87 + for (i=0; i<TBSIZE; i++) 1.88 + if (tbuf[i] != (PRUint8)i) { 1.89 + if (debug_mode) printf("data mismatch for index= %d \n", i); 1.90 + else failed_already=1; 1.91 + } 1.92 + PR_Close(t1); 1.93 + PR_Close(t2); 1.94 + 1.95 + PR_Delete("t1.tmp"); 1.96 + PR_Delete("t2.tmp"); 1.97 + 1.98 + if (debug_mode) printf("fileio test passed\n"); 1.99 +} 1.100 + 1.101 + 1.102 +/*------------------ Following is the real test program ---------*/ 1.103 +/* 1.104 + Program to copy one file to another. Two temporary files get 1.105 + created. First one gets written in one write call. Then, 1.106 + a reader thread reads from this file into a double buffer. 1.107 + The writer thread writes from double buffer into the other 1.108 + temporary file. The second temporary file gets verified 1.109 + for accurate data. 1.110 +*/ 1.111 + 1.112 +PRSemaphore *emptyBufs; /* number of empty buffers */ 1.113 +PRSemaphore *fullBufs; /* number of buffers that are full */ 1.114 + 1.115 +#define BSIZE 100 1.116 + 1.117 +struct { 1.118 + char data[BSIZE]; 1.119 + PRUintn nbytes; /* number of bytes in this buffer */ 1.120 +} buf[2]; 1.121 + 1.122 +static void PR_CALLBACK reader(void *arg) 1.123 +{ 1.124 + PRUintn i = 0; 1.125 + PRInt32 nbytes; 1.126 + 1.127 + do { 1.128 + (void) PR_WaitSem(emptyBufs); 1.129 + nbytes = PR_Read((PRFileDesc*)arg, buf[i].data, BSIZE); 1.130 + if (nbytes >= 0) { 1.131 + buf[i].nbytes = nbytes; 1.132 + PR_PostSem(fullBufs); 1.133 + i = (i + 1) % 2; 1.134 + } 1.135 + } while (nbytes > 0); 1.136 +} 1.137 + 1.138 +static void PR_CALLBACK writer(void *arg) 1.139 +{ 1.140 + PRUintn i = 0; 1.141 + PRInt32 nbytes; 1.142 + 1.143 + do { 1.144 + (void) PR_WaitSem(fullBufs); 1.145 + nbytes = buf[i].nbytes; 1.146 + if (nbytes > 0) { 1.147 + nbytes = PR_Write((PRFileDesc*)arg, buf[i].data, nbytes); 1.148 + PR_PostSem(emptyBufs); 1.149 + i = (i + 1) % 2; 1.150 + } 1.151 + } while (nbytes > 0); 1.152 +} 1.153 + 1.154 +int main(int argc, char **argv) 1.155 +{ 1.156 + PRThread *r, *w; 1.157 + 1.158 + 1.159 + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 1.160 + PR_STDIO_INIT(); 1.161 + 1.162 + emptyBufs = PR_NewSem(2); /* two empty buffers */ 1.163 + 1.164 + fullBufs = PR_NewSem(0); /* zero full buffers */ 1.165 + 1.166 + /* Create initial temp file setup */ 1.167 + InitialSetup(); 1.168 + 1.169 + /* create the reader thread */ 1.170 + 1.171 + r = PR_CreateThread(PR_USER_THREAD, 1.172 + reader, t1, 1.173 + PR_PRIORITY_NORMAL, 1.174 + PR_LOCAL_THREAD, 1.175 + PR_JOINABLE_THREAD, 1.176 + 0); 1.177 + 1.178 + w = PR_CreateThread(PR_USER_THREAD, 1.179 + writer, t2, 1.180 + PR_PRIORITY_NORMAL, 1.181 + PR_LOCAL_THREAD, 1.182 + PR_JOINABLE_THREAD, 1.183 + 0); 1.184 + 1.185 + /* Do the joining for both threads */ 1.186 + (void) PR_JoinThread(r); 1.187 + (void) PR_JoinThread(w); 1.188 + 1.189 + /* Do the verification and clean up */ 1.190 + VerifyAndCleanup(); 1.191 + 1.192 + PR_DestroySem(emptyBufs); 1.193 + PR_DestroySem(fullBufs); 1.194 + 1.195 + PR_Cleanup(); 1.196 + 1.197 + if(failed_already) 1.198 + { 1.199 + printf("Fail\n"); 1.200 + return 1; 1.201 + } 1.202 + else 1.203 + { 1.204 + printf("PASS\n"); 1.205 + return 0; 1.206 + } 1.207 + 1.208 + 1.209 +}