1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "prstrms.h"
7
8 #include "prinit.h"
9 #include "prio.h"
10 #include "prthread.h"
11
12 #include <cstring>
13 #include <iostream>
14
15 #ifdef XP_UNIX
16 #include <sys/stat.h>
17 #endif
18
19 using std::cout;
20 using std::endl;
21 using std::ios;
22
23 const unsigned int MaxCnt = 1;
24
25 typedef struct threadarg {
26 const char *mytag;
27 } threadarg;
28
29 void threadwork(threadarg *arg);
30
31 void
threadmain(void * mytag)32 threadmain(void *mytag)
33 {
34 threadarg arg;
35
36 arg.mytag = static_cast<const char *>(mytag);
37
38 threadwork(&arg);
39 }
40
41 void
threadwork(threadarg * arg)42 threadwork(threadarg *arg)
43 {
44 unsigned int i;
45
46 char fname1[256];
47 char fname2[256];
48
49 strcpy(fname1, arg->mytag);
50 strcpy(fname2, arg->mytag);
51 strcat(fname2, "2");
52 PR_Delete(fname1);
53 PR_Delete(fname2);
54
55 PRfilebuf *fb[MaxCnt];
56 PRifstream *ifs[MaxCnt];
57 PRofstream *ofs[MaxCnt];
58 int mode = 0;
59 #ifdef XP_UNIX
60 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
61 #endif
62
63 //
64 // Allocate a bunch
65 cout << "Testing unused filebufs ----------------" << endl;
66 for (i=0; i < MaxCnt; i++) {
67 fb[i] = new PRfilebuf;
68 }
69 // Delete them
70 for (i=0; i < MaxCnt; i++) {
71 delete fb[i];
72 }
73 cout << "Unused filebufs complete ---------------" << endl;
74
75 //
76 // Allocate a bunch
77 cout << "Testing unused ifstream -----------------" << endl;
78 for (i=0; i < MaxCnt; i++) {
79 ifs[i] = new PRifstream;
80 }
81 //
82 // Delete them
83 for (i=0; i < MaxCnt; i++) {
84 delete ifs[i];
85 }
86 cout << "Unused ifstream complete ----------------" << endl;
87 //
88 // Allocate a bunch
89 cout << "Testing unused ofstream -----------------" << endl;
90 for (i=0; i < MaxCnt; i++) {
91 ofs[i] = new PRofstream;
92 }
93 for (i=0; i < MaxCnt; i++) {
94 *(ofs[i]) << "A"; // Write a bit
95 delete ofs[i]; // Delete it.
96 }
97 cout << "Unused ofstream complete ----------------" << endl;
98
99 cout << "Testing use of ofstream 1 (extra filebuf allocated) ---------" << endl;
100 PRofstream *aos = new PRofstream(fname1, ios::out|ios::ate, mode);
101 for (i=0; i < MaxCnt; i++) {
102 for (int j=0; j < 8192; j++) {
103 *aos << "AaBbCcDdEeFfGg" << endl;
104 }
105 fb[i] = new PRfilebuf; // Allocate as we go to hack at the heap
106 }
107 //
108 // Delete the extra foo we allocated
109 for (i=0; i < MaxCnt; i++) {
110 delete fb[i];
111 }
112 aos->flush(); // Explicit flush
113 delete aos;
114 cout << "Testing use of ofstream 1 complete (extra filebuf deleted) --" << endl;
115 cout << "Testing use of ofstream 2 (extra filebuf allocated) ---------" << endl;
116 PRofstream *aos2 = new PRofstream(fname2, ios::out, mode);
117
118 for (i=0; i < MaxCnt; i++) {
119 *aos2 << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
120 }
121 // Force flushing in the dtor
122 delete aos2;
123 cout << "Testing use of ofstream 2 complete (extra filebuf deleted) --" << endl;
124 char line[1024];
125 cout << "Testing use of ifstream 1 (stack allocation) -------------" << endl;
126 PRifstream ais(fname1);
127 for (i=0; i < MaxCnt; i++) {
128 ais >> line;
129 }
130 cout << "Testing use of ifstream 1 complete -----------------------" << endl;
131 cout << "Testing use of ifstream 2 ----------------------" << endl;
132 PRifstream *ais2 = new PRifstream(fname2);
133 char achar;
134 for (i=0; i < MaxCnt*10; i++) {
135 *ais2 >> achar;
136 }
137 delete ais2;
138 cout << "Testing use of ifstream 2 complete -------------" << endl;
139 }
140
141 #define STACKSIZE 1024*1024
142 int
main()143 main()
144 {
145 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 256);
146 threadmain(const_cast<char *>("TestFile"));
147 PRThread *thr1 = PR_CreateThread(PR_SYSTEM_THREAD,
148 threadmain,
149 const_cast<char *>("TestFile1"),
150 PR_PRIORITY_NORMAL,
151 PR_GLOBAL_THREAD,
152 PR_JOINABLE_THREAD,
153 STACKSIZE);
154 PRThread *thr2 = PR_CreateThread(PR_SYSTEM_THREAD,
155 threadmain,
156 const_cast<char *>("TestFile2"),
157 PR_PRIORITY_NORMAL,
158 PR_GLOBAL_THREAD,
159 PR_JOINABLE_THREAD,
160 STACKSIZE);
161 PRThread *thr3 = PR_CreateThread(PR_SYSTEM_THREAD,
162 threadmain,
163 const_cast<char *>("TestFile3"),
164 PR_PRIORITY_NORMAL,
165 PR_GLOBAL_THREAD,
166 PR_JOINABLE_THREAD,
167 STACKSIZE);
168 PR_JoinThread(thr1);
169 PR_JoinThread(thr2);
170 PR_JoinThread(thr3);
171 return 0;
172 }
173