OSDN Git Service

2003-07-04 Paolo Carlini <pcarlini@unitus.it>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / performance / filebuf_unbuf_sputn.cc
1 // 2003-07-04  Petur Runolfsson <peturr02@ru.is>
2 //
3 // Copyright (C) 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 #include <cstdio>
31 #include <cstring>
32 #include <fstream>
33 #include <iostream>
34
35 // Contributed as part of libstdc++/11378.
36 int main(int argc, char** argv)
37 {
38   using namespace std;
39   
40   if (argc < 3)
41     {
42       cerr << "Usage: " << argv[0] << " type iters [chunksize]\n";
43       return -1;
44     }
45
46   enum { type_stdio, type_iostream } type;
47   
48   if (!strcmp(argv[1], "stdio"))
49     type = type_stdio;
50   else if (!strcmp(argv[1], "iostream"))
51     type = type_iostream;
52   else
53     {
54       cerr << "Type must be one of {stdio, iostream}\n";
55       return -1;
56     }
57
58   int iters = atoi(argv[2]);
59   if (iters < 1)
60     {
61       cerr << "Iters must be an positive integer\n";
62       return -1;
63     }
64   
65   int chunksize = 1;
66   if (argc > 3)
67     chunksize = atoi(argv[3]);
68   
69   if (iters < 1)
70     {
71       cerr << "Chunksize must be an positive integer\n";
72       return -1;
73     }
74   
75   char* chunk = 0;
76   if (chunksize > 1)
77     {
78       chunk = new char[chunksize];
79       memset(chunk, 'a', chunksize);
80     }
81
82   switch (type)
83     {
84     case type_stdio:
85       {
86         FILE* f = fopen("tmp", "w");
87         setvbuf(f, 0, _IONBF, 0);
88         
89         if (chunksize > 1)
90           {
91             for (int i = 0; i < iters; ++i)
92               fwrite_unlocked(chunk, 1, chunksize, f);
93           }
94         else
95           {
96             for (int i = 0; i < iters; ++i)
97               putc_unlocked('a', f);
98           }
99         
100         fclose(f);
101         break;
102       }
103
104     case type_iostream:
105       {
106         filebuf f;
107         f.pubsetbuf(0, 0);
108         
109         f.open("tmp", ios_base::out);
110
111         if (chunksize > 1)
112           {
113             for (int i = 0; i < iters; ++i)
114               f.sputn(chunk, chunksize);
115           }
116         else
117           {
118             for (int i = 0; i < iters; ++i)
119               f.sputc('a');
120           }
121         
122         f.close();
123         
124         break;
125       }
126     }
127
128   delete[] chunk;
129   return 0;
130 }