Doxygen changes
[ACE_TAO.git] / ACE / tests / Mem_Map_Test.cpp
blob73bb98590cf68d4d37324f664555e5658693a2b3
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"
26 #if !defined (ACE_LACKS_MMAP)
28 static const char ACE_ALPHABET[] = "abcdefghijklmnopqrstuvwxyz";
29 static const int LINE_LENGTH = 10;
30 static const int NUM_LINES = 15;
32 static void
33 reverse_file (ACE_HANDLE file_handle,
34 char *array,
35 size_t size)
37 int count = 0;
38 // LynxOS 3.0.0/PowerPC needs the volatile qualifier, with -O2
39 // optimization enabled and without ACE_HAS_INLINE.
40 volatile size_t i = size;
41 --i;
43 if (array[i] == '\0')
44 array[i] = '\n';
46 while (i-- > 0)
48 if (array[i] == '\n')
50 ACE_OS::write (file_handle, array + i + 1, count);
51 ACE_OS::write (file_handle, ACE_TEXT ("\n"), 1);
52 count = 0;
54 else
55 count++;
57 ACE_OS::write (file_handle, array, count+1);
60 static int
61 create_test_file (ACE_TCHAR *filename, int line_length, int num_lines)
63 char *mybuf = 0;
65 ACE_NEW_RETURN (mybuf, char[line_length + 1], -1);
66 const char *c = ACE_ALPHABET;
67 const char *d = c;
68 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
69 // For NTO has to applied to open the file, as Mem_Map can map only shared memory
70 ACE_Mem_Map mmap_4_open;
71 mmap_4_open.open (filename, O_RDWR | O_CREAT | O_TRUNC, ACE_DEFAULT_FILE_PERMS);
72 ACE_HANDLE file_handle = mmap_4_open.handle();
73 #else
74 ACE_HANDLE file_handle = ACE_OS::open (filename,
75 O_RDWR | O_CREAT | O_TRUNC,
76 ACE_DEFAULT_FILE_PERMS);
77 #endif
78 if (file_handle == ACE_INVALID_HANDLE)
80 delete [] mybuf;
81 ACE_ERROR_RETURN ((LM_ERROR,
82 ACE_TEXT ("Open failed for %s\n"),
83 filename),
84 -1);
87 for (int j = 0; j < num_lines; j++)
89 for (int i = 0; i < line_length; i++)
91 mybuf[i] = *c;
92 c++;
95 mybuf[line_length] = '\0';
97 c = ++d;
99 if (ACE_OS::write (file_handle, mybuf, line_length) != line_length)
101 delete [] mybuf;
102 ACE_ERROR_RETURN ((LM_ERROR,
103 ACE_TEXT ("%p (%d) <%s>\n"),
104 ACE_TEXT ("Write to file failed:"),
105 ACE_ERRNO_GET,
106 filename),
107 -1);
110 if (ACE_OS::write (file_handle, ACE_TEXT ("\n"), 1) != 1)
112 delete [] mybuf;
113 ACE_ERROR_RETURN ((LM_ERROR,
114 ACE_TEXT ("write to file %s failed\n"),
115 filename),
116 -1);
119 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
120 mmap_4_open.close();
121 #else
122 ACE_OS::close (file_handle);
123 #endif
125 delete [] mybuf;
126 return 0;
129 #endif /* !ACE_LACKS_MMAP */
132 run_main (int, ACE_TCHAR *[])
134 ACE_START_TEST (ACE_TEXT ("Mem_Map_Test"));
136 #if !defined (ACE_LACKS_MMAP)
138 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
139 ACE_ERROR ((LM_INFO,
140 ACE_TEXT ("mmap on QNX Neutrino/VxWorks can map only shared memory files\n")));
141 #endif
143 // = Initialize the temporary variable names
145 ACE_TCHAR test_file[MAXPATHLEN + 1];
146 ACE_TCHAR temp_file1[MAXPATHLEN + 1];
147 ACE_TCHAR temp_file2[MAXPATHLEN + 1];
149 // Get the temporary directory
150 // - 18 is for the filenames, ace_mem_map_temp_1 is the longest
151 if (ACE::get_temp_dir (test_file, MAXPATHLEN - 18) == -1)
152 ACE_ERROR_RETURN ((LM_ERROR,
153 ACE_TEXT ("Temporary path too long\n")),
154 -1);
156 // Copy the temp directory to the other variables
157 ACE_OS::strcpy (temp_file1, test_file);
158 ACE_OS::strcpy (temp_file2, test_file);
160 // Add the filenames to the end
161 ACE_OS::strcat (test_file,
162 ACE_TEXT ("ace_mem_map_test"));
163 ACE_OS::strcat (temp_file1,
164 ACE_TEXT ("ace_mem_map_temp_1"));
165 ACE_OS::strcat (temp_file2,
166 ACE_TEXT ("ace_mem_map_temp_2"));
168 // First create a test file to work on
169 if (create_test_file (test_file, LINE_LENGTH, NUM_LINES) != 0)
170 ACE_ERROR_RETURN ((LM_ERROR,
171 ACE_TEXT ("Create test file failed\n")),
172 -1);
173 ACE_Mem_Map mmap;
175 // First memory map the test file
176 if (mmap.map (test_file) == -1)
177 ACE_ERROR_RETURN ((LM_ERROR,
178 ACE_TEXT ("%n: %p %s\n%a"),
179 ACE_TEXT ("mmap"),
180 test_file),
181 -1);
183 // Now create a temporary file for intermediate processing
184 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
185 ACE_Mem_Map mmap_4_open;
186 mmap_4_open.open(temp_file1,
187 O_RDWR | O_TRUNC | O_CREAT,
188 ACE_DEFAULT_FILE_PERMS);
189 ACE_HANDLE temp_file_handle = mmap_4_open.handle();
190 #else
191 ACE_HANDLE temp_file_handle = ACE_OS::open (temp_file1,
192 O_RDWR | O_TRUNC | O_CREAT,
193 ACE_DEFAULT_FILE_PERMS);
194 #endif
196 if (temp_file_handle == ACE_INVALID_HANDLE)
197 ACE_ERROR_RETURN ((LM_ERROR,
198 ACE_TEXT ("Open failed\n")),
199 -1);
201 // Reverse the original file and write the output to the temporary
202 // file.
203 reverse_file (temp_file_handle,
204 (char *) mmap.addr (),
205 mmap.size ());
206 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
207 mmap_4_open.close();
208 #else
209 ACE_OS::close (temp_file_handle);
210 #endif
212 ACE_Mem_Map temp_mmap;
214 // Now memory map the temporary file
215 if (temp_mmap.map (temp_file1) == -1)
216 ACE_ERROR_RETURN ((LM_ERROR,
217 ACE_TEXT ("%n: %p %s\n%a"),
218 ACE_TEXT ("mmap"),
219 temp_file1),
220 -1);
221 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
222 mmap_4_open.open(temp_file2,
223 O_RDWR | O_TRUNC | O_CREAT,
224 ACE_DEFAULT_FILE_PERMS);
225 temp_file_handle = mmap_4_open.handle();
226 #else
227 temp_file_handle = ACE_OS::open (temp_file2,
228 O_RDWR | O_TRUNC | O_CREAT,
229 ACE_DEFAULT_FILE_PERMS);
230 #endif
231 if ( temp_file_handle == ACE_INVALID_HANDLE)
233 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Open failed\n")), -1);
235 // Now reverse the temporary file and write everything to the second
236 // temporary file.
237 reverse_file (temp_file_handle,
238 (char *) temp_mmap.addr (),
239 temp_mmap.size ());
240 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
241 mmap_4_open.close();
242 #else
243 ACE_OS::close (temp_file_handle);
244 #endif
246 // Memory map the second temporary file
247 ACE_Mem_Map temp_mmap2;
249 if (temp_mmap2.map (temp_file2) == -1)
250 ACE_ERROR_RETURN ((LM_ERROR,
251 ACE_TEXT ("%n: %p %s\n%a"),
252 ACE_TEXT ("mmap"),
253 temp_file2),
254 -1);
256 // Now do a memcmp -- the orig file and the second temporary file
257 // should be identical.
258 ACE_TEST_ASSERT (ACE_OS::memcmp (temp_mmap2.addr (),
259 mmap.addr (),
260 mmap.size ()) == 0);
262 // Delete the test file
263 mmap.remove ();
265 // Delete ACE_TEMP_TEST_FILE
266 temp_mmap.remove ();
268 // Delete ACE_TEMP_TEST_FILE_2
269 temp_mmap2.remove ();
271 #else /* !ACE_LACKS_MMAP */
273 ACE_ERROR ((LM_INFO,
274 ACE_TEXT ("mmap is not supported on this platform\n")));
276 #endif /* !ACE_LACKS_MMAP */
278 ACE_END_TEST;
279 return 0;