Back to index

lightning-sunbird  0.9+nobinonly
testprstrm.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "prinit.h"
00039 #include "prstrms.h"
00040 #include "prio.h"
00041 #include <string.h>
00042 #include <stdio.h>
00043 #if defined(XP_UNIX) || defined(XP_OS2_EMX)
00044 #include <sys/types.h>
00045 #include <sys/stat.h>
00046 #endif
00047 
00048 const unsigned int MaxCnt = 1;
00049 
00050 void threadwork(void *mytag);
00051 
00052 
00053 typedef struct threadarg {
00054     void *mytag;
00055 } threadarg;
00056 
00057 void 
00058 #ifdef XP_OS2_VACPP
00059 _Optlink
00060 #endif
00061 threadmain(void *mytag)
00062 {
00063     threadarg arg;
00064 
00065     arg.mytag = mytag;
00066 
00067     threadwork(&arg);
00068 }
00069 
00070 
00071 void
00072 threadwork(void *_arg)
00073 {
00074        threadarg *arg = (threadarg *)_arg;
00075        unsigned int i;
00076 
00077        char fname1[256];
00078        char fname2[256];
00079 
00080        strcpy(fname1, (char *)arg->mytag);
00081        strcpy(fname2, (char *)arg->mytag);
00082        strcat(fname2, "2");
00083        PR_Delete(fname1);
00084        PR_Delete(fname2);
00085 
00086        PRfilebuf *fb[MaxCnt];
00087        PRifstream *ifs[MaxCnt];
00088        PRofstream *ofs[MaxCnt];
00089        int mode = 0;
00090 #ifdef XP_UNIX
00091        mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IRGRP|S_IWOTH|S_IROTH;
00092 #endif
00093 
00094        //
00095        // Allocate a bunch
00096        cout << "Testing unused filebufs ----------------" << endl;
00097        for (i=0; i < MaxCnt; i++){
00098               fb[i] = new PRfilebuf;
00099        }
00100        // Delete them
00101        for (i=0; i < MaxCnt; i++){
00102               delete fb[i];
00103        }
00104        cout << "Unused filebufs complete ---------------" << endl;
00105 
00106        //
00107        // Allocate a bunch
00108        cout << "Testing unused ifstream -----------------" << endl;
00109        for (i=0; i < MaxCnt; i++){
00110          ifs[i] = new PRifstream;
00111        }
00112        //
00113        // Delete them
00114        for (i=0; i < MaxCnt; i++){
00115          delete ifs[i];
00116        }
00117        cout << "Unused ifstream complete ----------------" << endl;
00118        //
00119        // Allocate a bunch
00120        cout << "Testing unused ofstream -----------------" << endl;
00121        for (i=0; i < MaxCnt; i++){
00122               ofs[i] = new PRofstream;
00123        }
00124        for (i=0; i < MaxCnt; i++){
00125          *(ofs[i]) << "A"; // Write a bit
00126          delete ofs[i]; // Delete it.
00127        }
00128        cout << "Unused ofstream complete ----------------" << endl;
00129 
00130        cout << "Testing use of ofstream 1 (extra filebuf allocated) ---------" << endl;
00131        PRofstream *aos = new PRofstream(fname1, ios::out|ios::ate, mode);
00132        for (i=0; i < MaxCnt; i++){
00133          for (int j=0; j < 8192; j++)
00134               *aos << "AaBbCcDdEeFfGg" << endl;
00135               fb[i] = new PRfilebuf; // Allocate as we go to hack at the heap
00136        }
00137        //
00138        // Delete the extra foo we allocated
00139        for (i=0; i < MaxCnt; i++){
00140          delete fb[i];
00141        }
00142        aos->flush(); // Explicit flush
00143        delete aos;
00144        cout << "Testing use of ofstream 1 complete (extra filebuf deleted) --" << endl;
00145        cout << "Testing use of ofstream 2 (extra filebuf allocated) ---------" << endl;
00146        PRofstream *aos2 = new PRofstream(fname2, ios::out, mode);
00147 
00148        for (i=0; i < MaxCnt; i++){
00149            *aos2 << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
00150        }
00151        // Force flushing in the dtor
00152        delete aos2;
00153        cout << "Testing use of ofstream 2 complete (extra filebuf deleted) --" << endl;
00154        char line[1024];
00155        cout << "Testing use of ifstream 1 (stack allocation) -------------" << endl;
00156        PRifstream ais(fname1);
00157        for (i=0; i < MaxCnt; i++){
00158               ais >> line;
00159        }
00160        cout << "Testing use of ifstream 1 complete -----------------------" << endl;
00161        cout << "Testing use of ifstream 2 ----------------------" << endl;
00162        PRifstream *ais2 = new PRifstream(fname2);
00163        char achar;
00164        for (i=0; i < MaxCnt*10; i++){
00165               *ais2 >> achar;
00166        }
00167        delete ais2;
00168        cout << "Testing use of ifstream 2 complete -------------" << endl;
00169 }
00170 
00171 #define STACKSIZE 1024*1024
00172 int
00173 main(int argc, char **argv)
00174 {
00175        PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 256);
00176        threadmain("TestFile");
00177        PRThread *thr1 = PR_CreateThread(PR_SYSTEM_THREAD, 
00178                                     threadmain, 
00179                                     (void *)"TestFile1",
00180                                     PR_PRIORITY_NORMAL,
00181                                     PR_GLOBAL_THREAD,
00182                                     PR_JOINABLE_THREAD,
00183                                     STACKSIZE);
00184        PRThread *thr2 = PR_CreateThread(PR_SYSTEM_THREAD, 
00185                                     threadmain, 
00186                                     (void *)"TestFile2",
00187                                     PR_PRIORITY_NORMAL,
00188                                     PR_GLOBAL_THREAD,
00189                                     PR_JOINABLE_THREAD,
00190                                     STACKSIZE);
00191 
00192        PRThread *thr3 = PR_CreateThread(PR_SYSTEM_THREAD, 
00193                                     threadmain, 
00194                                     (void *)"TestFile3",
00195                                     PR_PRIORITY_NORMAL,
00196                                     PR_GLOBAL_THREAD,
00197                                     PR_JOINABLE_THREAD,
00198                                     STACKSIZE);
00199        PR_JoinThread(thr1);
00200        PR_JoinThread(thr2);
00201        PR_JoinThread(thr3);
00202        return 0;
00203 }
00204