Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / ACE / tests / Mem_Map_Test.cpp
blob7549be0c73aa91c70e0f46aa2d5711238f434197
2 //=============================================================================
3 /**
4 * @file Mem_Map_Test.cpp
6 * This test illustrates the use of ACE_Mem_Map to reverse a
7 * file. The test first creates a dummy file for testing, then
8 * reverses the file and then reverses it again to get back the
9 * original file.
11 * @author Prashant Jain <pjain@cs.wustl.edu>
13 //=============================================================================
16 #include "test_config.h"
17 #include "ace/Mem_Map.h"
18 #include "ace/Lib_Find.h"
19 #include "ace/OS_NS_string.h"
20 #include "ace/OS_NS_unistd.h"
21 #include "ace/OS_NS_fcntl.h"
22 #include "ace/OS_Memory.h"
25 #if !defined (ACE_LACKS_MMAP)
27 static const char ACE_ALPHABET[] = "abcdefghijklmnopqrstuvwxyz";
28 static const int LINE_LENGTH = 10;
29 static const int NUM_LINES = 15;
31 static void
32 reverse_file (ACE_HANDLE file_handle,
33 char *array,
34 size_t size)
36 int count = 0;
37 // LynxOS 3.0.0/PowerPC needs the volatile qualifier, with -O2
38 // optimization enabled and without ACE_HAS_INLINE.
39 volatile size_t i = size;
40 --i;
42 if (array[i] == '\0')
43 array[i] = '\n';
45 while (i-- > 0)
47 if (array[i] == '\n')
49 ACE_OS::write (file_handle, array + i + 1, count);
50 ACE_OS::write (file_handle, ACE_TEXT ("\n"), 1);
51 count = 0;
53 else
54 count++;
56 ACE_OS::write (file_handle, array, count+1);
59 static int
60 create_test_file (ACE_TCHAR *filename, int line_length, int num_lines)
62 char *mybuf = 0;
64 ACE_NEW_RETURN (mybuf, char[line_length + 1], -1);
65 const char *c = ACE_ALPHABET;
66 const char *d = c;
67 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
68 // For NTO has to applied to open the file, as Mem_Map can map only shared memory
69 ACE_Mem_Map mmap_4_open;
70 mmap_4_open.open (filename, O_RDWR | O_CREAT | O_TRUNC, ACE_DEFAULT_FILE_PERMS);
71 ACE_HANDLE file_handle = mmap_4_open.handle();
72 #else
73 ACE_HANDLE file_handle = ACE_OS::open (filename,
74 O_RDWR | O_CREAT | O_TRUNC,
75 ACE_DEFAULT_FILE_PERMS);
76 #endif
77 if (file_handle == ACE_INVALID_HANDLE)
79 delete [] mybuf;
80 ACE_ERROR_RETURN ((LM_ERROR,
81 ACE_TEXT ("Open failed for %s\n"),
82 filename),
83 -1);
86 for (int j = 0; j < num_lines; j++)
88 for (int i = 0; i < line_length; i++)
90 mybuf[i] = *c;
91 c++;
94 mybuf[line_length] = '\0';
96 c = ++d;
98 if (ACE_OS::write (file_handle, mybuf, line_length) != line_length)
100 delete [] mybuf;
101 ACE_ERROR_RETURN ((LM_ERROR,
102 ACE_TEXT ("%p (%d) <%s>\n"),
103 ACE_TEXT ("Write to file failed:"),
104 ACE_ERRNO_GET,
105 filename),
106 -1);
109 if (ACE_OS::write (file_handle, ACE_TEXT ("\n"), 1) != 1)
111 delete [] mybuf;
112 ACE_ERROR_RETURN ((LM_ERROR,
113 ACE_TEXT ("write to file %s failed\n"),
114 filename),
115 -1);
118 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
119 mmap_4_open.close();
120 #else
121 ACE_OS::close (file_handle);
122 #endif
124 delete [] mybuf;
125 return 0;
128 #endif /* !ACE_LACKS_MMAP */
131 run_main (int, ACE_TCHAR *[])
133 ACE_START_TEST (ACE_TEXT ("Mem_Map_Test"));
135 #if !defined (ACE_LACKS_MMAP)
137 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
138 ACE_ERROR ((LM_INFO,
139 ACE_TEXT ("mmap on QNX Neutrino/VxWorks can map only shared memory files\n")));
140 #endif
142 // = Initialize the temporary variable names
144 ACE_TCHAR test_file[MAXPATHLEN + 1];
145 ACE_TCHAR temp_file1[MAXPATHLEN + 1];
146 ACE_TCHAR temp_file2[MAXPATHLEN + 1];
148 // Get the temporary directory
149 // - 18 is for the filenames, ace_mem_map_temp_1 is the longest
150 if (ACE::get_temp_dir (test_file, MAXPATHLEN - 18) == -1)
151 ACE_ERROR_RETURN ((LM_ERROR,
152 ACE_TEXT ("Temporary path too long\n")),
153 -1);
155 // Copy the temp directory to the other variables
156 ACE_OS::strcpy (temp_file1, test_file);
157 ACE_OS::strcpy (temp_file2, test_file);
159 // Add the filenames to the end
160 ACE_OS::strcat (test_file,
161 ACE_TEXT ("ace_mem_map_test"));
162 ACE_OS::strcat (temp_file1,
163 ACE_TEXT ("ace_mem_map_temp_1"));
164 ACE_OS::strcat (temp_file2,
165 ACE_TEXT ("ace_mem_map_temp_2"));
167 // First create a test file to work on
168 if (create_test_file (test_file, LINE_LENGTH, NUM_LINES) != 0)
169 ACE_ERROR_RETURN ((LM_ERROR,
170 ACE_TEXT ("Create test file failed\n")),
171 -1);
172 ACE_Mem_Map mmap;
174 // First memory map the test file
175 if (mmap.map (test_file) == -1)
176 ACE_ERROR_RETURN ((LM_ERROR,
177 ACE_TEXT ("%n: %p %s\n%a"),
178 ACE_TEXT ("mmap"),
179 test_file),
180 -1);
182 // Now create a temporary file for intermediate processing
183 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
184 ACE_Mem_Map mmap_4_open;
185 mmap_4_open.open(temp_file1,
186 O_RDWR | O_TRUNC | O_CREAT,
187 ACE_DEFAULT_FILE_PERMS);
188 ACE_HANDLE temp_file_handle = mmap_4_open.handle();
189 #else
190 ACE_HANDLE temp_file_handle = ACE_OS::open (temp_file1,
191 O_RDWR | O_TRUNC | O_CREAT,
192 ACE_DEFAULT_FILE_PERMS);
193 #endif
195 if (temp_file_handle == ACE_INVALID_HANDLE)
196 ACE_ERROR_RETURN ((LM_ERROR,
197 ACE_TEXT ("Open failed\n")),
198 -1);
200 // Reverse the original file and write the output to the temporary
201 // file.
202 reverse_file (temp_file_handle,
203 (char *) mmap.addr (),
204 mmap.size ());
205 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
206 mmap_4_open.close();
207 #else
208 ACE_OS::close (temp_file_handle);
209 #endif
211 ACE_Mem_Map temp_mmap;
213 // Now memory map the temporary file
214 if (temp_mmap.map (temp_file1) == -1)
215 ACE_ERROR_RETURN ((LM_ERROR,
216 ACE_TEXT ("%n: %p %s\n%a"),
217 ACE_TEXT ("mmap"),
218 temp_file1),
219 -1);
220 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
221 mmap_4_open.open(temp_file2,
222 O_RDWR | O_TRUNC | O_CREAT,
223 ACE_DEFAULT_FILE_PERMS);
224 temp_file_handle = mmap_4_open.handle();
225 #else
226 temp_file_handle = ACE_OS::open (temp_file2,
227 O_RDWR | O_TRUNC | O_CREAT,
228 ACE_DEFAULT_FILE_PERMS);
229 #endif
230 if ( temp_file_handle == ACE_INVALID_HANDLE)
232 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Open failed\n")), -1);
234 // Now reverse the temporary file and write everything to the second
235 // temporary file.
236 reverse_file (temp_file_handle,
237 (char *) temp_mmap.addr (),
238 temp_mmap.size ());
239 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
240 mmap_4_open.close();
241 #else
242 ACE_OS::close (temp_file_handle);
243 #endif
245 // Memory map the second temporary file
246 ACE_Mem_Map temp_mmap2;
248 if (temp_mmap2.map (temp_file2) == -1)
249 ACE_ERROR_RETURN ((LM_ERROR,
250 ACE_TEXT ("%n: %p %s\n%a"),
251 ACE_TEXT ("mmap"),
252 temp_file2),
253 -1);
255 // Now do a memcmp -- the orig file and the second temporary file
256 // should be identical.
257 ACE_TEST_ASSERT (ACE_OS::memcmp (temp_mmap2.addr (),
258 mmap.addr (),
259 mmap.size ()) == 0);
261 // Delete the test file
262 mmap.remove ();
264 // Delete ACE_TEMP_TEST_FILE
265 temp_mmap.remove ();
267 // Delete ACE_TEMP_TEST_FILE_2
268 temp_mmap2.remove ();
270 #else /* !ACE_LACKS_MMAP */
272 ACE_ERROR ((LM_INFO,
273 ACE_TEXT ("mmap is not supported on this platform\n")));
275 #endif /* !ACE_LACKS_MMAP */
277 ACE_END_TEST;
278 return 0;