home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / lib / prstreams / tests / testprstrm / testprstrm.cpp < prev   
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.7 KB  |  183 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "prinit.h"
  20. #include "prstrms.h"
  21. #include "prio.h"
  22. #include <string.h>
  23. #include <stdio.h>
  24. #ifdef XP_UNIX
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #endif
  28.  
  29. const unsigned int MaxCnt = 1;
  30.  
  31. void threadwork(void *mytag);
  32.  
  33.  
  34. typedef struct threadarg {
  35.     void *mytag;
  36. } threadarg;
  37.  
  38. void 
  39. threadmain(void *mytag)
  40. {
  41.     threadarg arg;
  42.  
  43.     arg.mytag = mytag;
  44.  
  45.     threadwork(&arg);
  46. }
  47.  
  48.  
  49. void
  50. threadwork(void *_arg)
  51. {
  52.     threadarg *arg = (threadarg *)_arg;
  53.     unsigned int i;
  54.  
  55.     char fname1[256];
  56.     char fname2[256];
  57.  
  58.     strcpy(fname1, (char *)arg->mytag);
  59.     strcpy(fname2, (char *)arg->mytag);
  60.     strcat(fname2, "2");
  61.     PR_Delete(fname1);
  62.     PR_Delete(fname2);
  63.  
  64.     PRfilebuf *fb[MaxCnt];
  65.     PRifstream *ifs[MaxCnt];
  66.     PRofstream *ofs[MaxCnt];
  67.     int mode = 0;
  68. #ifdef XP_UNIX
  69.     mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IRGRP|S_IWOTH|S_IROTH;
  70. #endif
  71.  
  72.     //
  73.     // Allocate a bunch
  74.     cout << "Testing unused filebufs ----------------" << endl;
  75.     for (i=0; i < MaxCnt; i++){
  76.         fb[i] = new PRfilebuf;
  77.     }
  78.     // Delete them
  79.     for (i=0; i < MaxCnt; i++){
  80.         delete fb[i];
  81.     }
  82.     cout << "Unused filebufs complete ---------------" << endl;
  83.  
  84.     //
  85.     // Allocate a bunch
  86.     cout << "Testing unused ifstream -----------------" << endl;
  87.     for (i=0; i < MaxCnt; i++){
  88.       ifs[i] = new PRifstream;
  89.     }
  90.     //
  91.     // Delete them
  92.     for (i=0; i < MaxCnt; i++){
  93.       delete ifs[i];
  94.     }
  95.     cout << "Unused ifstream complete ----------------" << endl;
  96.     //
  97.     // Allocate a bunch
  98.     cout << "Testing unused ofstream -----------------" << endl;
  99.     for (i=0; i < MaxCnt; i++){
  100.         ofs[i] = new PRofstream;
  101.     }
  102.     for (i=0; i < MaxCnt; i++){
  103.       *(ofs[i]) << "A"; // Write a bit
  104.       delete ofs[i]; // Delete it.
  105.     }
  106.     cout << "Unused ofstream complete ----------------" << endl;
  107.  
  108.     cout << "Testing use of ofstream 1 (extra filebuf allocated) ---------" << endl;
  109.     PRofstream *aos = new PRofstream(fname1, ios::out|ios::ate, mode);
  110.     for (i=0; i < MaxCnt; i++){
  111.       for (int j=0; j < 8192; j++)
  112.         *aos << "AaBbCcDdEeFfGg" << endl;
  113.         fb[i] = new PRfilebuf; // Allocate as we go to hack at the heap
  114.     }
  115.     //
  116.     // Delete the extra foo we allocated
  117.     for (i=0; i < MaxCnt; i++){
  118.       delete fb[i];
  119.     }
  120.     aos->flush(); // Explicit flush
  121.     delete aos;
  122.     cout << "Testing use of ofstream 1 complete (extra filebuf deleted) --" << endl;
  123.     cout << "Testing use of ofstream 2 (extra filebuf allocated) ---------" << endl;
  124.     PRofstream *aos2 = new PRofstream(fname2, ios::out, mode);
  125.  
  126.     for (i=0; i < MaxCnt; i++){
  127.         *aos2 << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
  128.     }
  129.     // Force flushing in the dtor
  130.     delete aos2;
  131.     cout << "Testing use of ofstream 2 complete (extra filebuf deleted) --" << endl;
  132.     char line[1024];
  133.     cout << "Testing use of ifstream 1 (stack allocation) -------------" << endl;
  134.     PRifstream ais(fname1);
  135.     for (i=0; i < MaxCnt; i++){
  136.         ais >> line;
  137.     }
  138.     cout << "Testing use of ifstream 1 complete -----------------------" << endl;
  139.     cout << "Testing use of ifstream 2 ----------------------" << endl;
  140.     PRifstream *ais2 = new PRifstream(fname2);
  141.     char achar;
  142.     for (i=0; i < MaxCnt*10; i++){
  143.         *ais2 >> achar;
  144.     }
  145.     delete ais2;
  146.     cout << "Testing use of ifstream 2 complete -------------" << endl;
  147. }
  148.  
  149. #define STACKSIZE 1024*1024
  150. int
  151. main(int argc, char **argv)
  152. {
  153.     PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 256);
  154.     threadmain("TestFile");
  155.     PRThread *thr1 = PR_CreateThread(PR_SYSTEM_THREAD, 
  156.                      threadmain, 
  157.                      (void *)"TestFile1",
  158.                      PR_PRIORITY_NORMAL,
  159.                      PR_GLOBAL_THREAD,
  160.                      PR_JOINABLE_THREAD,
  161.                      STACKSIZE);
  162.     PRThread *thr2 = PR_CreateThread(PR_SYSTEM_THREAD, 
  163.                      threadmain, 
  164.                      (void *)"TestFile2",
  165.                      PR_PRIORITY_NORMAL,
  166.                      PR_GLOBAL_THREAD,
  167.                      PR_JOINABLE_THREAD,
  168.                      STACKSIZE);
  169.  
  170.     PRThread *thr3 = PR_CreateThread(PR_SYSTEM_THREAD, 
  171.                      threadmain, 
  172.                      (void *)"TestFile3",
  173.                      PR_PRIORITY_NORMAL,
  174.                      PR_GLOBAL_THREAD,
  175.                      PR_JOINABLE_THREAD,
  176.                      STACKSIZE);
  177.     PR_JoinThread(thr1);
  178.     PR_JoinThread(thr2);
  179.     PR_JoinThread(thr3);
  180.     return 0;
  181. }
  182.  
  183.