Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Mem_Map / IO-tests / IO_Test.cpp
blob1d1167cf6736343df810dd90f6ce8247f69683c7
1 #include "ace/OS_NS_string.h"
2 #include "ace/OS_NS_unistd.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/Mem_Map.h"
5 #include "ace/Log_Msg.h"
6 #include "IO_Test.h"
8 #if !defined(ACE_WIN32)
11 IO_Test::IO_Test (const char *name,
12 ACE_Profile_Timer &tm)
13 : name_ (name), tm_ (tm)
17 IO_Test::~IO_Test ()
21 const char *
22 IO_Test::name ()
24 return this->name_;
27 Slow_Read_Write_Test::Slow_Read_Write_Test (const char *name,
28 ACE_Profile_Timer &tm)
29 : IO_Test (name, tm)
33 int
34 Slow_Read_Write_Test::run_test (int iterations,
35 FILE *input_fp,
36 FILE *output_fp)
38 ACE_HANDLE ifd = ACE_OS::fileno (input_fp);
39 ACE_HANDLE ofd = ACE_OS::fileno (output_fp);
41 this->tm_.start ();
43 while (--iterations >= 0)
45 char c;
47 while (ACE_OS::read (ifd, &c, sizeof c) > 0)
48 ACE_OS::write (ofd, &c, sizeof c);
50 ACE_OS::lseek (ifd, 0, SEEK_SET);
51 ACE_OS::lseek (ofd, 0, SEEK_SET);
54 this->tm_.stop ();
55 return 0;
58 Stdio_Test::Stdio_Test (const char *name,
59 ACE_Profile_Timer &tm)
60 : IO_Test (name, tm)
64 int
65 Stdio_Test::run_test (int iterations,
66 FILE *input_fp,
67 FILE *output_fp)
69 this->tm_.start ();
71 while (--iterations >= 0)
73 int c;
75 while ((c = ACE_OS::getc (input_fp)) != EOF)
76 ACE_OS::putc (c, output_fp);
78 ACE_OS::rewind (input_fp);
79 ACE_OS::rewind (output_fp);
81 this->tm_.stop ();
82 return 0;
85 Block_Read_Write_Test::Block_Read_Write_Test (const char *name,
86 ACE_Profile_Timer &tm)
87 : IO_Test (name, tm)
91 int
92 Block_Read_Write_Test::run_test (int iterations,
93 FILE *input_fp,
94 FILE *output_fp)
96 ACE_HANDLE ifd = ACE_OS::fileno (input_fp);
97 ACE_HANDLE ofd = ACE_OS::fileno (output_fp);
99 this->tm_.start ();
101 while (--iterations >= 0)
103 char buf[BUFSIZ];
104 ssize_t n;
106 while ((n = ACE_OS::read (ifd,
107 buf,
108 sizeof buf)) > 0)
109 ACE_OS::write (ofd, buf, n);
111 ACE_OS::lseek (ifd, 0, SEEK_SET);
112 ACE_OS::lseek (ofd, 0, SEEK_SET);
115 this->tm_.stop ();
116 return 0;
119 Block_Fread_Fwrite_Test::Block_Fread_Fwrite_Test (const char *name,
120 ACE_Profile_Timer &tm)
121 : IO_Test (name, tm)
126 Block_Fread_Fwrite_Test::run_test (int iterations,
127 FILE *input_fp,
128 FILE *output_fp)
130 this->tm_.start ();
132 while (--iterations >= 0)
134 char buf[BUFSIZ];
135 ssize_t n;
137 while ((n = ACE_OS::fread (buf,
139 sizeof buf,
140 input_fp)) != 0)
141 ACE_OS::fwrite (buf, n, 1, output_fp);
143 ACE_OS::rewind (input_fp);
144 ACE_OS::rewind (output_fp);
147 this->tm_.stop ();
148 return 0;
151 Mmap1_Test::Mmap1_Test (const char *name,
152 ACE_Profile_Timer &tm)
153 : IO_Test (name, tm)
158 Mmap1_Test::run_test (int iterations,
159 FILE *input_fp,
160 FILE *output_fp)
162 ACE_Mem_Map map_input (ACE_OS::fileno (input_fp));
163 void *src = map_input.addr ();
165 if (src == MAP_FAILED)
166 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%C"), this->name ()), -1);
167 else
169 this->tm_.start ();
171 while (--iterations >= 0)
173 if (ACE_OS::write (ACE_OS::fileno (output_fp),
174 src,
175 map_input.size ()) == -1)
176 ACE_ERROR_RETURN ((LM_ERROR,
177 ACE_TEXT ("%C"),
178 this->name ()),
179 -1);
180 ACE_OS::lseek (ACE_OS::fileno (output_fp),
182 SEEK_SET);
185 this->tm_.stop ();
188 if (map_input.unmap () == -1)
189 ACE_ERROR_RETURN ((LM_ERROR,
190 ACE_TEXT ("%C"),
191 this->name ()),
192 -1);
193 else
194 return 0;
197 Mmap2_Test::Mmap2_Test (const char *name,
198 ACE_Profile_Timer &tm)
199 : IO_Test (name, tm)
204 Mmap2_Test::run_test (int iterations,
205 FILE *input_fp,
206 FILE *output_fp)
208 ACE_Mem_Map map_input (ACE_OS::fileno (input_fp));
209 int size = map_input.size ();
210 ACE_Mem_Map map_output (ACE_OS::fileno (output_fp),
211 size,
212 PROT_WRITE,
213 MAP_SHARED);
214 void *src = map_input.addr ();
215 void *dst = map_output.addr ();
217 if (src == MAP_FAILED || dst == MAP_FAILED)
218 return -1;
219 else
221 this->tm_.start ();
223 while (--iterations >= 0)
224 ACE_OS::memcpy (dst, src, size);
226 this->tm_.stop ();
229 if (map_input.unmap () == -1
230 || map_output.unmap () == -1)
231 return -1;
232 else
233 return 0;
235 #endif