2 //=============================================================================
4 * @file CDR_File_Test.cpp
6 * Checks the functionality of the ACE CDR streams used for file
9 * @author Giga Giguashvili <gregoryg@ParadigmGeo.com> and Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
11 //=============================================================================
13 #include "test_config.h"
14 #include "ace/OS_Memory.h"
15 #include "ace/OS_NS_stdlib.h"
16 #include "ace/OS_NS_string.h"
17 #include "ace/CDR_Stream.h"
18 #include "ace/FILE_Connector.h"
19 #include "ace/Auto_Ptr.h"
20 #include "ace/Get_Opt.h"
22 #include "ace/Truncate.h"
24 // FUZZ: disable check_for_streams_include
25 #include "ace/streams.h"
27 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
32 * @brief Simple class that's used to read and write CDR streams.
36 /// Output the state of a <CDR_Test> object to the <ostream>.
37 friend ostream
& operator << (ostream
&os
, const CDR_Test
&t
);
39 /// Convert the state of this object into an <ACE_OutputCDR>.
40 friend void operator << (ACE_OutputCDR
&os
, const CDR_Test
&t
);
42 /// Convert the <ACE_InputCDR> into the state of this object.
43 friend void operator >> (ACE_InputCDR
&is
, CDR_Test
&);
46 /// Default constructor.
50 CDR_Test (ACE_CDR::Char o
,
53 ACE_CDR::ULongLong lw
,
57 /// Compare <rhs> for equality with <this>.
58 bool operator == (const CDR_Test
&rhs
) const;
62 ACE_CDR::Short word2_
;
64 ACE_CDR::ULongLong word8_
;
65 ACE_CDR::Float fpoint_
;
66 ACE_CDR::Double dprec_
;
70 operator << (ostream
&os
,
73 os
<< "Char: " << t
.char_
<< endl
74 << "Short: " << t
.word2_
<< endl
75 << "Long: " << t
.word4_
<< endl
;
77 ACE_CDR::ULongLong hi
= (t
.word8_
>> 32);
78 ACE_CDR::ULongLong lo
= (t
.word8_
& 0xffffffff);
80 os
<< "ULongLong 1st half: "
82 << ACE_Utils::truncate_cast
<ACE_UINT32
> (hi
)
84 << "ULongLong 2nd half: "
86 << ACE_Utils::truncate_cast
<ACE_UINT32
> (lo
)
88 << "Float: " << t
.fpoint_
<< endl
89 << "Double: " << t
.dprec_
<< endl
;
103 CDR_Test::CDR_Test (ACE_CDR::Char o
,
106 ACE_CDR::ULongLong lw
,
119 operator << (ACE_OutputCDR
&os
, const CDR_Test
&t
)
130 operator >> (ACE_InputCDR
&is
, CDR_Test
&t
)
141 CDR_Test::operator == (const CDR_Test
&rhs
) const
143 // @@ Workaround bug in egcs-1.1.1 using a single && expression
144 // results in UMR errors in purify.
145 if (this->char_
!= rhs
.char_
)
147 if (this->word2_
!= rhs
.word2_
)
149 if (this->word4_
!= rhs
.word4_
)
151 if (this->word8_
!= rhs
.word8_
)
153 if (!ACE::is_equal (this->fpoint_
, rhs
.fpoint_
))
155 if (!ACE::is_equal (this->dprec_
, rhs
.dprec_
))
161 run_test (int write_file
,
163 const ACE_TCHAR
*filename
,
168 char byte_order
= ACE_CDR_BYTE_ORDER
;
169 size_t n
= file
.send (&byte_order
, 1);
171 ACE_ERROR_RETURN ((LM_ERROR
,
172 ACE_TEXT ("send failed on %p\n"),
176 ACE_OutputCDR
output_cdr (0,
181 ACE_DEFAULT_CDR_MEMCPY_TRADEOFF
,
182 ACE_CDR_GIOP_MAJOR_VERSION
,
183 ACE_CDR_GIOP_MINOR_VERSION
);
184 // Marshal the <CDR_Test> object data to the output CDR stream.
185 output_cdr
<< cdr_test
;
187 // Output the data to cout.
188 *ace_file_stream::instance ()->output_file () << cdr_test
;
191 const ACE_Message_Block
*output_mb
=
194 ACE_DEBUG ((LM_DEBUG
,
195 ACE_TEXT ("Writing file %s in %s endian format...\n"),
197 ACE_CDR_BYTE_ORDER
? ACE_TEXT("little") : ACE_TEXT("big")));
199 n
= file
.send (output_mb
->rd_ptr (),
200 output_mb
->length ());
201 if (n
!= (size_t) output_mb
->length ())
202 ACE_ERROR_RETURN ((LM_ERROR
,
203 ACE_TEXT ("send failed on %p\n"),
207 else // We're reading from the file.
210 if (file
.get_info (info
) == -1)
211 ACE_ERROR_RETURN ((LM_ERROR
,
212 ACE_TEXT ("get_info failed on %p\n"),
216 ACE_OFF_T msgsize
= info
.size_
- 1;
218 // Allocate the input buffer
220 ACE_NEW_RETURN (buffer
,
223 #if defined (ACE_INITIALIZE_MEMORY_BEFORE_USE)
224 ACE_OS::memset(buffer
, 0, sizeof (buffer
));
225 #endif /* ACE_INITIALIZE_MEMORY_BEFORE_USE */
227 // Make sure <buffer> is released automagically.
228 std::unique_ptr
<char[]> b (buffer
);
230 // Move the file pointer back to the beginning of the file.
233 ACE_ERROR_RETURN ((LM_ERROR
,
239 ssize_t size
= file
.recv (&byte_order
, 1);
241 ACE_ERROR_RETURN ((LM_ERROR
,
242 ACE_TEXT ("Read %d bytes, rather than expected ")
243 ACE_TEXT ("1 bytes\n"),
247 // Read the cdr data from the file into the buffer.
248 size
= file
.recv (buffer
, msgsize
);
250 ACE_ERROR_RETURN ((LM_ERROR
,
251 ACE_TEXT ("Read %d bytes, rather than expected ")
252 ACE_TEXT ("%d bytes\n"),
257 // Create message block for the whole file. Ensure that it is
258 // aligned to properly handle the double.
259 ACE_Message_Block
mb (ACE_CDR::MAX_ALIGNMENT
+ msgsize
);
260 ACE_CDR::mb_align (&mb
);
262 mb
.copy (buffer
, msgsize
);
264 // Create CDR input stream from the message block.
266 ACE_InputCDR
input_cdr (&mb
);
267 input_cdr
.reset_byte_order ((int) byte_order
);
269 ACE_DEBUG ((LM_DEBUG
,
270 ACE_TEXT ("Reading file %s in %s endian format...\n"),
272 ACE_CDR_BYTE_ORDER
? ACE_TEXT("little") : ACE_TEXT("big")));
276 // Demarshal the data from the input CDR stream into the
277 // <CDR_Test> object.
280 *ace_file_stream::instance ()->output_file () << temp
;
282 if (!(temp
== cdr_test
))
283 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Data mismatch across file\n")));
290 usage (const ACE_TCHAR
*cmd
)
292 ACE_ERROR ((LM_ERROR
,
293 ACE_TEXT ("Usage: %s ")
294 ACE_TEXT ("[-f filename [-w|-r]]"),
302 run_main (int argc
, ACE_TCHAR
*argv
[])
304 ACE_START_TEST (ACE_TEXT ("CDR_File_Test"));
306 ACE_DEBUG ((LM_DEBUG
,
307 ACE_TEXT ("This is ACE Version %u.%u.%u\n\n"),
308 ACE::major_version (),
309 ACE::minor_version (),
310 ACE::micro_version ()));
312 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT ("f:rw"));
317 while ((opt
= get_opt ()) != EOF
)
322 fn
= get_opt
.opt_arg ();
332 usage (ACE_TEXT("CDR_File_Test"));
336 if ((!reading
|| !writing
) && fn
== 0)
337 usage (ACE_TEXT("CDR_File_Test"));
339 if (!reading
&& !writing
)
340 usage (ACE_TEXT("CDR_File_Test"));
342 // Create a temporary filename.
343 ACE_FILE_Addr
filename (ACE_sap_any_cast (ACE_FILE_Addr
&));
348 ACE_FILE_Connector connector
;
352 if (connector
.connect (file
,
357 ((writing
) ? (O_RDWR
| O_CREAT
) : O_RDONLY
),
358 ACE_DEFAULT_FILE_PERMS
) == -1)
359 ACE_ERROR_RETURN ((LM_ERROR
,
360 ACE_TEXT ("connect failed for %p\n"),
361 filename
.get_path_name ()),
364 CDR_Test
cdr_test ('a',
376 filename
.get_path_name (),
385 filename
.get_path_name (),
392 if (file
.unlink () == -1)
393 ACE_ERROR_RETURN ((LM_ERROR
,
394 ACE_TEXT ("unlink failed for %p\n"),
395 filename
.get_path_name ()),
403 #else /* ! ACE_LACKS_IOSTREAM_TOTALLY */
406 run_main (int, ACE_TCHAR
*[])
408 ACE_START_TEST (ACE_TEXT ("CDR_File_Test"));
411 ACE_TEXT ("iostreams not supported on this platform\n")));
417 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */