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"
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;
33 reverse_file (ACE_HANDLE file_handle
,
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
;
50 ACE_OS::write (file_handle
, array
+ i
+ 1, count
);
51 ACE_OS::write (file_handle
, ACE_TEXT ("\n"), 1);
57 ACE_OS::write (file_handle
, array
, count
+1);
61 create_test_file (ACE_TCHAR
*filename
, int line_length
, int num_lines
)
65 ACE_NEW_RETURN (mybuf
, char[line_length
+ 1], -1);
66 const char *c
= ACE_ALPHABET
;
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();
74 ACE_HANDLE file_handle
= ACE_OS::open (filename
,
75 O_RDWR
| O_CREAT
| O_TRUNC
,
76 ACE_DEFAULT_FILE_PERMS
);
78 if (file_handle
== ACE_INVALID_HANDLE
)
81 ACE_ERROR_RETURN ((LM_ERROR
,
82 ACE_TEXT ("Open failed for %s\n"),
87 for (int j
= 0; j
< num_lines
; j
++)
89 for (int i
= 0; i
< line_length
; i
++)
95 mybuf
[line_length
] = '\0';
99 if (ACE_OS::write (file_handle
, mybuf
, line_length
) != line_length
)
102 ACE_ERROR_RETURN ((LM_ERROR
,
103 ACE_TEXT ("%p (%d) <%s>\n"),
104 ACE_TEXT ("Write to file failed:"),
110 if (ACE_OS::write (file_handle
, ACE_TEXT ("\n"), 1) != 1)
113 ACE_ERROR_RETURN ((LM_ERROR
,
114 ACE_TEXT ("write to file %s failed\n"),
119 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
122 ACE_OS::close (file_handle
);
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))
140 ACE_TEXT ("mmap on QNX Neutrino/VxWorks can map only shared memory files\n")));
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")),
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")),
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"),
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();
191 ACE_HANDLE temp_file_handle
= ACE_OS::open (temp_file1
,
192 O_RDWR
| O_TRUNC
| O_CREAT
,
193 ACE_DEFAULT_FILE_PERMS
);
196 if (temp_file_handle
== ACE_INVALID_HANDLE
)
197 ACE_ERROR_RETURN ((LM_ERROR
,
198 ACE_TEXT ("Open failed\n")),
201 // Reverse the original file and write the output to the temporary
203 reverse_file (temp_file_handle
,
204 (char *) mmap
.addr (),
206 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
209 ACE_OS::close (temp_file_handle
);
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"),
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();
227 temp_file_handle
= ACE_OS::open (temp_file2
,
228 O_RDWR
| O_TRUNC
| O_CREAT
,
229 ACE_DEFAULT_FILE_PERMS
);
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
237 reverse_file (temp_file_handle
,
238 (char *) temp_mmap
.addr (),
240 #if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
243 ACE_OS::close (temp_file_handle
);
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"),
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 (),
262 // Delete the test file
265 // Delete ACE_TEMP_TEST_FILE
268 // Delete ACE_TEMP_TEST_FILE_2
269 temp_mmap2
.remove ();
271 #else /* !ACE_LACKS_MMAP */
274 ACE_TEXT ("mmap is not supported on this platform\n")));
276 #endif /* !ACE_LACKS_MMAP */