michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "prstrms.h" michael@0: michael@0: #include "prinit.h" michael@0: #include "prio.h" michael@0: #include "prthread.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #ifdef XP_UNIX michael@0: #include michael@0: #endif michael@0: michael@0: using std::cout; michael@0: using std::endl; michael@0: using std::ios; michael@0: michael@0: const unsigned int MaxCnt = 1; michael@0: michael@0: typedef struct threadarg { michael@0: const char *mytag; michael@0: } threadarg; michael@0: michael@0: void threadwork(threadarg *arg); michael@0: michael@0: void michael@0: threadmain(void *mytag) michael@0: { michael@0: threadarg arg; michael@0: michael@0: arg.mytag = static_cast(mytag); michael@0: michael@0: threadwork(&arg); michael@0: } michael@0: michael@0: void michael@0: threadwork(threadarg *arg) michael@0: { michael@0: unsigned int i; michael@0: michael@0: char fname1[256]; michael@0: char fname2[256]; michael@0: michael@0: strcpy(fname1, arg->mytag); michael@0: strcpy(fname2, arg->mytag); michael@0: strcat(fname2, "2"); michael@0: PR_Delete(fname1); michael@0: PR_Delete(fname2); michael@0: michael@0: PRfilebuf *fb[MaxCnt]; michael@0: PRifstream *ifs[MaxCnt]; michael@0: PRofstream *ofs[MaxCnt]; michael@0: int mode = 0; michael@0: #ifdef XP_UNIX michael@0: mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; michael@0: #endif michael@0: michael@0: // michael@0: // Allocate a bunch michael@0: cout << "Testing unused filebufs ----------------" << endl; michael@0: for (i=0; i < MaxCnt; i++){ michael@0: fb[i] = new PRfilebuf; michael@0: } michael@0: // Delete them michael@0: for (i=0; i < MaxCnt; i++){ michael@0: delete fb[i]; michael@0: } michael@0: cout << "Unused filebufs complete ---------------" << endl; michael@0: michael@0: // michael@0: // Allocate a bunch michael@0: cout << "Testing unused ifstream -----------------" << endl; michael@0: for (i=0; i < MaxCnt; i++){ michael@0: ifs[i] = new PRifstream; michael@0: } michael@0: // michael@0: // Delete them michael@0: for (i=0; i < MaxCnt; i++){ michael@0: delete ifs[i]; michael@0: } michael@0: cout << "Unused ifstream complete ----------------" << endl; michael@0: // michael@0: // Allocate a bunch michael@0: cout << "Testing unused ofstream -----------------" << endl; michael@0: for (i=0; i < MaxCnt; i++){ michael@0: ofs[i] = new PRofstream; michael@0: } michael@0: for (i=0; i < MaxCnt; i++){ michael@0: *(ofs[i]) << "A"; // Write a bit michael@0: delete ofs[i]; // Delete it. michael@0: } michael@0: cout << "Unused ofstream complete ----------------" << endl; michael@0: michael@0: cout << "Testing use of ofstream 1 (extra filebuf allocated) ---------" << endl; michael@0: PRofstream *aos = new PRofstream(fname1, ios::out|ios::ate, mode); michael@0: for (i=0; i < MaxCnt; i++){ michael@0: for (int j=0; j < 8192; j++) michael@0: *aos << "AaBbCcDdEeFfGg" << endl; michael@0: fb[i] = new PRfilebuf; // Allocate as we go to hack at the heap michael@0: } michael@0: // michael@0: // Delete the extra foo we allocated michael@0: for (i=0; i < MaxCnt; i++){ michael@0: delete fb[i]; michael@0: } michael@0: aos->flush(); // Explicit flush michael@0: delete aos; michael@0: cout << "Testing use of ofstream 1 complete (extra filebuf deleted) --" << endl; michael@0: cout << "Testing use of ofstream 2 (extra filebuf allocated) ---------" << endl; michael@0: PRofstream *aos2 = new PRofstream(fname2, ios::out, mode); michael@0: michael@0: for (i=0; i < MaxCnt; i++){ michael@0: *aos2 << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; michael@0: } michael@0: // Force flushing in the dtor michael@0: delete aos2; michael@0: cout << "Testing use of ofstream 2 complete (extra filebuf deleted) --" << endl; michael@0: char line[1024]; michael@0: cout << "Testing use of ifstream 1 (stack allocation) -------------" << endl; michael@0: PRifstream ais(fname1); michael@0: for (i=0; i < MaxCnt; i++){ michael@0: ais >> line; michael@0: } michael@0: cout << "Testing use of ifstream 1 complete -----------------------" << endl; michael@0: cout << "Testing use of ifstream 2 ----------------------" << endl; michael@0: PRifstream *ais2 = new PRifstream(fname2); michael@0: char achar; michael@0: for (i=0; i < MaxCnt*10; i++){ michael@0: *ais2 >> achar; michael@0: } michael@0: delete ais2; michael@0: cout << "Testing use of ifstream 2 complete -------------" << endl; michael@0: } michael@0: michael@0: #define STACKSIZE 1024*1024 michael@0: int michael@0: main() michael@0: { michael@0: PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 256); michael@0: threadmain(const_cast("TestFile")); michael@0: PRThread *thr1 = PR_CreateThread(PR_SYSTEM_THREAD, michael@0: threadmain, michael@0: const_cast("TestFile1"), michael@0: PR_PRIORITY_NORMAL, michael@0: PR_GLOBAL_THREAD, michael@0: PR_JOINABLE_THREAD, michael@0: STACKSIZE); michael@0: PRThread *thr2 = PR_CreateThread(PR_SYSTEM_THREAD, michael@0: threadmain, michael@0: const_cast("TestFile2"), michael@0: PR_PRIORITY_NORMAL, michael@0: PR_GLOBAL_THREAD, michael@0: PR_JOINABLE_THREAD, michael@0: STACKSIZE); michael@0: PRThread *thr3 = PR_CreateThread(PR_SYSTEM_THREAD, michael@0: threadmain, michael@0: const_cast("TestFile3"), michael@0: PR_PRIORITY_NORMAL, michael@0: PR_GLOBAL_THREAD, michael@0: PR_JOINABLE_THREAD, michael@0: STACKSIZE); michael@0: PR_JoinThread(thr1); michael@0: PR_JoinThread(thr2); michael@0: PR_JoinThread(thr3); michael@0: return 0; michael@0: }