1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/lib/prstreams/tests/testprstrm/testprstrm.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,171 @@ 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 +#include "prstrms.h" 1.10 + 1.11 +#include "prinit.h" 1.12 +#include "prio.h" 1.13 +#include "prthread.h" 1.14 + 1.15 +#include <cstring> 1.16 +#include <iostream> 1.17 + 1.18 +#ifdef XP_UNIX 1.19 +#include <sys/stat.h> 1.20 +#endif 1.21 + 1.22 +using std::cout; 1.23 +using std::endl; 1.24 +using std::ios; 1.25 + 1.26 +const unsigned int MaxCnt = 1; 1.27 + 1.28 +typedef struct threadarg { 1.29 + const char *mytag; 1.30 +} threadarg; 1.31 + 1.32 +void threadwork(threadarg *arg); 1.33 + 1.34 +void 1.35 +threadmain(void *mytag) 1.36 +{ 1.37 + threadarg arg; 1.38 + 1.39 + arg.mytag = static_cast<const char *>(mytag); 1.40 + 1.41 + threadwork(&arg); 1.42 +} 1.43 + 1.44 +void 1.45 +threadwork(threadarg *arg) 1.46 +{ 1.47 + unsigned int i; 1.48 + 1.49 + char fname1[256]; 1.50 + char fname2[256]; 1.51 + 1.52 + strcpy(fname1, arg->mytag); 1.53 + strcpy(fname2, arg->mytag); 1.54 + strcat(fname2, "2"); 1.55 + PR_Delete(fname1); 1.56 + PR_Delete(fname2); 1.57 + 1.58 + PRfilebuf *fb[MaxCnt]; 1.59 + PRifstream *ifs[MaxCnt]; 1.60 + PRofstream *ofs[MaxCnt]; 1.61 + int mode = 0; 1.62 +#ifdef XP_UNIX 1.63 + mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; 1.64 +#endif 1.65 + 1.66 + // 1.67 + // Allocate a bunch 1.68 + cout << "Testing unused filebufs ----------------" << endl; 1.69 + for (i=0; i < MaxCnt; i++){ 1.70 + fb[i] = new PRfilebuf; 1.71 + } 1.72 + // Delete them 1.73 + for (i=0; i < MaxCnt; i++){ 1.74 + delete fb[i]; 1.75 + } 1.76 + cout << "Unused filebufs complete ---------------" << endl; 1.77 + 1.78 + // 1.79 + // Allocate a bunch 1.80 + cout << "Testing unused ifstream -----------------" << endl; 1.81 + for (i=0; i < MaxCnt; i++){ 1.82 + ifs[i] = new PRifstream; 1.83 + } 1.84 + // 1.85 + // Delete them 1.86 + for (i=0; i < MaxCnt; i++){ 1.87 + delete ifs[i]; 1.88 + } 1.89 + cout << "Unused ifstream complete ----------------" << endl; 1.90 + // 1.91 + // Allocate a bunch 1.92 + cout << "Testing unused ofstream -----------------" << endl; 1.93 + for (i=0; i < MaxCnt; i++){ 1.94 + ofs[i] = new PRofstream; 1.95 + } 1.96 + for (i=0; i < MaxCnt; i++){ 1.97 + *(ofs[i]) << "A"; // Write a bit 1.98 + delete ofs[i]; // Delete it. 1.99 + } 1.100 + cout << "Unused ofstream complete ----------------" << endl; 1.101 + 1.102 + cout << "Testing use of ofstream 1 (extra filebuf allocated) ---------" << endl; 1.103 + PRofstream *aos = new PRofstream(fname1, ios::out|ios::ate, mode); 1.104 + for (i=0; i < MaxCnt; i++){ 1.105 + for (int j=0; j < 8192; j++) 1.106 + *aos << "AaBbCcDdEeFfGg" << endl; 1.107 + fb[i] = new PRfilebuf; // Allocate as we go to hack at the heap 1.108 + } 1.109 + // 1.110 + // Delete the extra foo we allocated 1.111 + for (i=0; i < MaxCnt; i++){ 1.112 + delete fb[i]; 1.113 + } 1.114 + aos->flush(); // Explicit flush 1.115 + delete aos; 1.116 + cout << "Testing use of ofstream 1 complete (extra filebuf deleted) --" << endl; 1.117 + cout << "Testing use of ofstream 2 (extra filebuf allocated) ---------" << endl; 1.118 + PRofstream *aos2 = new PRofstream(fname2, ios::out, mode); 1.119 + 1.120 + for (i=0; i < MaxCnt; i++){ 1.121 + *aos2 << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; 1.122 + } 1.123 + // Force flushing in the dtor 1.124 + delete aos2; 1.125 + cout << "Testing use of ofstream 2 complete (extra filebuf deleted) --" << endl; 1.126 + char line[1024]; 1.127 + cout << "Testing use of ifstream 1 (stack allocation) -------------" << endl; 1.128 + PRifstream ais(fname1); 1.129 + for (i=0; i < MaxCnt; i++){ 1.130 + ais >> line; 1.131 + } 1.132 + cout << "Testing use of ifstream 1 complete -----------------------" << endl; 1.133 + cout << "Testing use of ifstream 2 ----------------------" << endl; 1.134 + PRifstream *ais2 = new PRifstream(fname2); 1.135 + char achar; 1.136 + for (i=0; i < MaxCnt*10; i++){ 1.137 + *ais2 >> achar; 1.138 + } 1.139 + delete ais2; 1.140 + cout << "Testing use of ifstream 2 complete -------------" << endl; 1.141 +} 1.142 + 1.143 +#define STACKSIZE 1024*1024 1.144 +int 1.145 +main() 1.146 +{ 1.147 + PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 256); 1.148 + threadmain(const_cast<char *>("TestFile")); 1.149 + PRThread *thr1 = PR_CreateThread(PR_SYSTEM_THREAD, 1.150 + threadmain, 1.151 + const_cast<char *>("TestFile1"), 1.152 + PR_PRIORITY_NORMAL, 1.153 + PR_GLOBAL_THREAD, 1.154 + PR_JOINABLE_THREAD, 1.155 + STACKSIZE); 1.156 + PRThread *thr2 = PR_CreateThread(PR_SYSTEM_THREAD, 1.157 + threadmain, 1.158 + const_cast<char *>("TestFile2"), 1.159 + PR_PRIORITY_NORMAL, 1.160 + PR_GLOBAL_THREAD, 1.161 + PR_JOINABLE_THREAD, 1.162 + STACKSIZE); 1.163 + PRThread *thr3 = PR_CreateThread(PR_SYSTEM_THREAD, 1.164 + threadmain, 1.165 + const_cast<char *>("TestFile3"), 1.166 + PR_PRIORITY_NORMAL, 1.167 + PR_GLOBAL_THREAD, 1.168 + PR_JOINABLE_THREAD, 1.169 + STACKSIZE); 1.170 + PR_JoinThread(thr1); 1.171 + PR_JoinThread(thr2); 1.172 + PR_JoinThread(thr3); 1.173 + return 0; 1.174 +}