Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / Mem_Map / IO-tests / test_io.cpp
blob4ddbe0931e470380739a674ef1aff45e3fdf7f05
1 // Test program for different methods of copying files.
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/OS_NS_unistd.h"
5 #include "ace/ACE.h"
6 #include "ace/Profile_Timer.h"
7 #include "ace/Get_Opt.h"
8 #include "ace/Signal.h"
9 #include "ace/Log_Msg.h"
10 #include "IO_Test.h"
12 #if !defined(ACE_WIN32)
14 // Name of program.
15 static const ACE_TCHAR *program_name;
17 // Name of default input file.
18 static const ACE_TCHAR *input_filename = ACE_TEXT ("/usr/dict/words");
20 // Name of default output file.
21 static const ACE_TCHAR *output_filename = ACE_TEXT ("/tmp/foo");
23 // Check if removing output file upon completion...
24 static int remove_output = 1;
26 // Count of the number of iterations to run the tests.
27 static int iteration_count = 100;
29 // Profiler used to keep track of file I/O time.
30 static ACE_Profile_Timer profile_timer;
32 // Explain usage and exit.
34 static void
35 print_usage_and_die ()
37 ACE_OS::fprintf (stderr, "usage: %s"
38 " [-i input_file] [-o output_file] [-n iteration_count] [-r]\n",
39 ACE_TEXT_ALWAYS_CHAR (program_name));
40 ACE_OS::exit (1);
43 // Clean up the output file on exit from a signal.
45 extern "C" void
46 cleanup (int = 0)
48 if (remove_output)
49 ACE_OS::unlink (output_filename);
50 ACE_OS::exit (0);
53 // Parse the command-line arguments and set options.
55 static void
56 parse_args (int argc, ACE_TCHAR *argv[])
58 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("i:n:o:r"));
60 for (int c; ((c = get_opt ()) != -1); )
61 switch (c)
63 case 'i':
64 input_filename = get_opt.opt_arg ();
65 break;
66 case 'n':
67 iteration_count = ACE_OS::atoi (get_opt.opt_arg ());
68 break;
69 case 'o':
70 output_filename = get_opt.opt_arg ();
71 break;
72 case 'r':
73 remove_output = 0;
74 break;
75 default:
76 print_usage_and_die ();
77 break;
81 // Vector of pointers to derived classes that inherit from IO_Test
82 // base class.
84 static IO_Test *test_vector[100];
86 static int
87 run_tests (int iterations, FILE *input_fp, FILE *output_fp)
89 int i = 0;
91 ACE_NEW_RETURN (test_vector[i],
92 Stdio_Test ("Stdio_Test",
93 profile_timer),
94 -1);
95 i++;
96 ACE_NEW_RETURN (test_vector[i],
97 Block_Fread_Fwrite_Test ("Block_Fread_Fwrite_Test",
98 profile_timer),
99 -1);
100 i++;
101 ACE_NEW_RETURN (test_vector[i],
102 Block_Read_Write_Test ("Block_Read_Write_Test",
103 profile_timer),
104 -1);
105 i++;
106 ACE_NEW_RETURN (test_vector[i],
107 Mmap1_Test ("Mmap1_Test",
108 profile_timer),
109 -1);
110 i++;
111 ACE_NEW_RETURN (test_vector[i],
112 Mmap2_Test ("Mmap2_Test",
113 profile_timer),
114 -1);
115 i++;
116 ACE_NEW_RETURN (test_vector[i],
117 Slow_Read_Write_Test ("Slow_Read_Write_Test",
118 profile_timer),
119 -1);
120 i++;
122 test_vector[i] = (IO_Test *) 0;
124 for (i = 0; test_vector[i] != 0; i++)
126 ACE_HANDLE hfile = fileno (output_fp);
127 if (ACE_OS::ftruncate (hfile, 0) == -1)
128 ACE_ERROR_RETURN ((LM_ERROR,
129 ACE_TEXT ("%s\n"),
130 ACE_TEXT ("ftruncate")),
131 -1);
133 ACE_DEBUG ((LM_DEBUG,
134 ACE_TEXT ("--------------------\n")
135 ACE_TEXT ("starting %C for %d iterations(s):\n"),
136 test_vector[i]->name (),
137 iterations));
139 test_vector[i]->run_test (iterations,
140 input_fp,
141 output_fp);
143 ACE_Profile_Timer::ACE_Elapsed_Time et;
144 profile_timer.elapsed_time (et);
146 ACE_DEBUG ((LM_DEBUG,
147 ACE_TEXT ("wallclock time = %f, user time = %f, system time = %f\n"),
148 et.real_time,
149 et.user_time,
150 et.system_time));
152 delete test_vector[i];
155 ACE_DEBUG ((LM_DEBUG,
156 ACE_TEXT ("--------------------\n")));
157 return 0;
161 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
163 program_name = ACE::basename (argv[0],
164 ACE_DIRECTORY_SEPARATOR_CHAR);
165 parse_args (argc, argv);
167 ACE_Sig_Action sa ((ACE_SignalHandler) cleanup, SIGINT);
168 ACE_UNUSED_ARG (sa);
170 FILE *input_fp =
171 ACE_OS::fopen (input_filename, ACE_TEXT ("r"));
172 FILE *output_fp =
173 ACE_OS::fopen (output_filename, ACE_TEXT ("w+"));
175 if (input_fp == 0)
176 ACE_ERROR_RETURN ((LM_ERROR,
177 ACE_TEXT ("%p\n"),
178 input_filename),
179 -1);
181 if (output_fp == 0)
182 ACE_ERROR_RETURN ((LM_ERROR,
183 ACE_TEXT ("%p\n"),
184 output_filename),
185 -1);
187 ACE_OS::unlink (output_filename);
189 if (run_tests (iteration_count,
190 input_fp,
191 output_fp) == -1)
192 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("run_tests")),
193 -1);
195 if (ACE_OS::fclose (input_fp) == -1
196 || ACE_OS::fclose (output_fp) == -1)
197 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("fclose")),
198 -1);
199 cleanup ();
200 return 0;
202 #else
203 int ACE_TMAIN (int, ACE_TCHAR*[]) {
204 // not supported on win32
205 return 0;
207 #endif