VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/lib/prstreams/tests/testprstrm/testprstrm.cpp@ 20481

Last change on this file since 20481 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include "prinit.h"
39#include "prstrms.h"
40#include "prio.h"
41#include <string.h>
42#include <stdio.h>
43#if defined(XP_UNIX) || defined(XP_OS2_EMX)
44#include <sys/types.h>
45#include <sys/stat.h>
46#endif
47
48const unsigned int MaxCnt = 1;
49
50void threadwork(void *mytag);
51
52
53typedef struct threadarg {
54 void *mytag;
55} threadarg;
56
57void
58#ifdef XP_OS2_VACPP
59_Optlink
60#endif
61threadmain(void *mytag)
62{
63 threadarg arg;
64
65 arg.mytag = mytag;
66
67 threadwork(&arg);
68}
69
70
71void
72threadwork(void *_arg)
73{
74 threadarg *arg = (threadarg *)_arg;
75 unsigned int i;
76
77 char fname1[256];
78 char fname2[256];
79
80 strcpy(fname1, (char *)arg->mytag);
81 strcpy(fname2, (char *)arg->mytag);
82 strcat(fname2, "2");
83 PR_Delete(fname1);
84 PR_Delete(fname2);
85
86 PRfilebuf *fb[MaxCnt];
87 PRifstream *ifs[MaxCnt];
88 PRofstream *ofs[MaxCnt];
89 int mode = 0;
90#ifdef XP_UNIX
91 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IRGRP|S_IWOTH|S_IROTH;
92#endif
93
94 //
95 // Allocate a bunch
96 cout << "Testing unused filebufs ----------------" << endl;
97 for (i=0; i < MaxCnt; i++){
98 fb[i] = new PRfilebuf;
99 }
100 // Delete them
101 for (i=0; i < MaxCnt; i++){
102 delete fb[i];
103 }
104 cout << "Unused filebufs complete ---------------" << endl;
105
106 //
107 // Allocate a bunch
108 cout << "Testing unused ifstream -----------------" << endl;
109 for (i=0; i < MaxCnt; i++){
110 ifs[i] = new PRifstream;
111 }
112 //
113 // Delete them
114 for (i=0; i < MaxCnt; i++){
115 delete ifs[i];
116 }
117 cout << "Unused ifstream complete ----------------" << endl;
118 //
119 // Allocate a bunch
120 cout << "Testing unused ofstream -----------------" << endl;
121 for (i=0; i < MaxCnt; i++){
122 ofs[i] = new PRofstream;
123 }
124 for (i=0; i < MaxCnt; i++){
125 *(ofs[i]) << "A"; // Write a bit
126 delete ofs[i]; // Delete it.
127 }
128 cout << "Unused ofstream complete ----------------" << endl;
129
130 cout << "Testing use of ofstream 1 (extra filebuf allocated) ---------" << endl;
131 PRofstream *aos = new PRofstream(fname1, ios::out|ios::ate, mode);
132 for (i=0; i < MaxCnt; i++){
133 for (int j=0; j < 8192; j++)
134 *aos << "AaBbCcDdEeFfGg" << endl;
135 fb[i] = new PRfilebuf; // Allocate as we go to hack at the heap
136 }
137 //
138 // Delete the extra foo we allocated
139 for (i=0; i < MaxCnt; i++){
140 delete fb[i];
141 }
142 aos->flush(); // Explicit flush
143 delete aos;
144 cout << "Testing use of ofstream 1 complete (extra filebuf deleted) --" << endl;
145 cout << "Testing use of ofstream 2 (extra filebuf allocated) ---------" << endl;
146 PRofstream *aos2 = new PRofstream(fname2, ios::out, mode);
147
148 for (i=0; i < MaxCnt; i++){
149 *aos2 << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
150 }
151 // Force flushing in the dtor
152 delete aos2;
153 cout << "Testing use of ofstream 2 complete (extra filebuf deleted) --" << endl;
154 char line[1024];
155 cout << "Testing use of ifstream 1 (stack allocation) -------------" << endl;
156 PRifstream ais(fname1);
157 for (i=0; i < MaxCnt; i++){
158 ais >> line;
159 }
160 cout << "Testing use of ifstream 1 complete -----------------------" << endl;
161 cout << "Testing use of ifstream 2 ----------------------" << endl;
162 PRifstream *ais2 = new PRifstream(fname2);
163 char achar;
164 for (i=0; i < MaxCnt*10; i++){
165 *ais2 >> achar;
166 }
167 delete ais2;
168 cout << "Testing use of ifstream 2 complete -------------" << endl;
169}
170
171#define STACKSIZE 1024*1024
172int
173main(int argc, char **argv)
174{
175 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 256);
176 threadmain("TestFile");
177 PRThread *thr1 = PR_CreateThread(PR_SYSTEM_THREAD,
178 threadmain,
179 (void *)"TestFile1",
180 PR_PRIORITY_NORMAL,
181 PR_GLOBAL_THREAD,
182 PR_JOINABLE_THREAD,
183 STACKSIZE);
184 PRThread *thr2 = PR_CreateThread(PR_SYSTEM_THREAD,
185 threadmain,
186 (void *)"TestFile2",
187 PR_PRIORITY_NORMAL,
188 PR_GLOBAL_THREAD,
189 PR_JOINABLE_THREAD,
190 STACKSIZE);
191
192 PRThread *thr3 = PR_CreateThread(PR_SYSTEM_THREAD,
193 threadmain,
194 (void *)"TestFile3",
195 PR_PRIORITY_NORMAL,
196 PR_GLOBAL_THREAD,
197 PR_JOINABLE_THREAD,
198 STACKSIZE);
199 PR_JoinThread(thr1);
200 PR_JoinThread(thr2);
201 PR_JoinThread(thr3);
202 return 0;
203}
204
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette