2 //=============================================================================
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
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;
32 reverse_file (ACE_HANDLE file_handle
,
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
;
49 ACE_OS::write (file_handle
, array
+ i
+ 1, count
);
50 ACE_OS::write (file_handle
, ACE_TEXT ("\n"), 1);
56 ACE_OS::write (file_handle
, array
, count
+1);
60 create_test_file (ACE_TCHAR
*filename
, int line_length
, int num_lines
)
64 ACE_NEW_RETURN (mybuf
, char[line_length
+ 1], -1);
65 const char *c
= ACE_ALPHABET
;
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();
73 ACE_HANDLE file_handle
= ACE_OS::open (filename
,
74 O_RDWR
| O_CREAT
| O_TRUNC
,
75 ACE_DEFAULT_FILE_PERMS
);
77 if (file_handle
== ACE_INVALID_HANDLE
)
80 ACE_ERROR_RETURN ((LM_ERROR
,
81 ACE_TEXT ("Open failed for %s\n"),
86 for (int j
= 0; j
< num_lines
; j
++)
88 for (int i
= 0; i
< line_length
; i
++)
94 mybuf
[line_length
] = '\0';
98 if (ACE_OS::write (file_handle
, mybuf
, line_length
) != line_length
)
101 ACE_ERROR_RETURN ((LM_ERROR
,
102 ACE_TEXT ("%p (%d) <%s>\n"),
103 ACE_TEXT ("Write to file failed:"),
109 if (ACE_OS::write (file_handle
, ACE_TEXT ("\n"), 1) != 1)
112 ACE_ERROR_RETURN ((LM_ERROR
,
113 ACE_TEXT ("write to file %s failed\n"),
118 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
121 ACE_OS::close (file_handle
);
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))
139 ACE_TEXT ("mmap on QNX Neutrino/VxWorks can map only shared memory files\n")));
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")),
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")),
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"),
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();
190 ACE_HANDLE temp_file_handle
= ACE_OS::open (temp_file1
,
191 O_RDWR
| O_TRUNC
| O_CREAT
,
192 ACE_DEFAULT_FILE_PERMS
);
195 if (temp_file_handle
== ACE_INVALID_HANDLE
)
196 ACE_ERROR_RETURN ((LM_ERROR
,
197 ACE_TEXT ("Open failed\n")),
200 // Reverse the original file and write the output to the temporary
202 reverse_file (temp_file_handle
,
203 (char *) mmap
.addr (),
205 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
208 ACE_OS::close (temp_file_handle
);
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"),
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();
226 temp_file_handle
= ACE_OS::open (temp_file2
,
227 O_RDWR
| O_TRUNC
| O_CREAT
,
228 ACE_DEFAULT_FILE_PERMS
);
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
236 reverse_file (temp_file_handle
,
237 (char *) temp_mmap
.addr (),
239 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
242 ACE_OS::close (temp_file_handle
);
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"),
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 (),
261 // Delete the test file
264 // Delete ACE_TEMP_TEST_FILE
267 // Delete ACE_TEMP_TEST_FILE_2
268 temp_mmap2
.remove ();
270 #else /* !ACE_LACKS_MMAP */
273 ACE_TEXT ("mmap is not supported on this platform\n")));
275 #endif /* !ACE_LACKS_MMAP */