nsprpub/lib/prstreams/tests/testprstrm/testprstrm.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     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/. */
     6 #include "prstrms.h"
     8 #include "prinit.h"
     9 #include "prio.h"
    10 #include "prthread.h"
    12 #include <cstring>
    13 #include <iostream>
    15 #ifdef XP_UNIX
    16 #include <sys/stat.h>
    17 #endif
    19 using std::cout;
    20 using std::endl;
    21 using std::ios;
    23 const unsigned int MaxCnt = 1;
    25 typedef struct threadarg {
    26     const char *mytag;
    27 } threadarg;
    29 void threadwork(threadarg *arg);
    31 void 
    32 threadmain(void *mytag)
    33 {
    34     threadarg arg;
    36     arg.mytag = static_cast<const char *>(mytag);
    38     threadwork(&arg);
    39 }
    41 void
    42 threadwork(threadarg *arg)
    43 {
    44 	unsigned int i;
    46 	char fname1[256];
    47 	char fname2[256];
    49 	strcpy(fname1, arg->mytag);
    50 	strcpy(fname2, arg->mytag);
    51 	strcat(fname2, "2");
    52 	PR_Delete(fname1);
    53 	PR_Delete(fname2);
    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
    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;
    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;
    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 		fb[i] = new PRfilebuf; // Allocate as we go to hack at the heap
   105 	}
   106 	//
   107 	// Delete the extra foo we allocated
   108 	for (i=0; i < MaxCnt; i++){
   109 	  delete fb[i];
   110 	}
   111 	aos->flush(); // Explicit flush
   112 	delete aos;
   113 	cout << "Testing use of ofstream 1 complete (extra filebuf deleted) --" << endl;
   114 	cout << "Testing use of ofstream 2 (extra filebuf allocated) ---------" << endl;
   115 	PRofstream *aos2 = new PRofstream(fname2, ios::out, mode);
   117 	for (i=0; i < MaxCnt; i++){
   118 	    *aos2 << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
   119 	}
   120 	// Force flushing in the dtor
   121 	delete aos2;
   122 	cout << "Testing use of ofstream 2 complete (extra filebuf deleted) --" << endl;
   123 	char line[1024];
   124 	cout << "Testing use of ifstream 1 (stack allocation) -------------" << endl;
   125 	PRifstream ais(fname1);
   126 	for (i=0; i < MaxCnt; i++){
   127 		ais >> line;
   128 	}
   129 	cout << "Testing use of ifstream 1 complete -----------------------" << endl;
   130 	cout << "Testing use of ifstream 2 ----------------------" << endl;
   131 	PRifstream *ais2 = new PRifstream(fname2);
   132 	char achar;
   133 	for (i=0; i < MaxCnt*10; i++){
   134 		*ais2 >> achar;
   135 	}
   136 	delete ais2;
   137 	cout << "Testing use of ifstream 2 complete -------------" << endl;
   138 }
   140 #define STACKSIZE 1024*1024
   141 int
   142 main()
   143 {
   144 	PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 256);
   145 	threadmain(const_cast<char *>("TestFile"));
   146 	PRThread *thr1 = PR_CreateThread(PR_SYSTEM_THREAD, 
   147 					 threadmain, 
   148 					 const_cast<char *>("TestFile1"),
   149 					 PR_PRIORITY_NORMAL,
   150 					 PR_GLOBAL_THREAD,
   151 					 PR_JOINABLE_THREAD,
   152 					 STACKSIZE);
   153 	PRThread *thr2 = PR_CreateThread(PR_SYSTEM_THREAD, 
   154 					 threadmain, 
   155 					 const_cast<char *>("TestFile2"),
   156 					 PR_PRIORITY_NORMAL,
   157 					 PR_GLOBAL_THREAD,
   158 					 PR_JOINABLE_THREAD,
   159 					 STACKSIZE);
   160 	PRThread *thr3 = PR_CreateThread(PR_SYSTEM_THREAD, 
   161 					 threadmain, 
   162 					 const_cast<char *>("TestFile3"),
   163 					 PR_PRIORITY_NORMAL,
   164 					 PR_GLOBAL_THREAD,
   165 					 PR_JOINABLE_THREAD,
   166 					 STACKSIZE);
   167 	PR_JoinThread(thr1);
   168 	PR_JoinThread(thr2);
   169 	PR_JoinThread(thr3);
   170 	return 0;
   171 }

mercurial