2 #include "tao/CodecFactory/CodecFactory.h"
3 #include "tao/Codeset/Codeset.h"
5 #include "ace/OS_NS_string.h"
7 #include "ace/Log_Msg.h"
10 gen_wstring (CORBA::ULong max_length
)
12 CORBA::ULong len
= max_length
;
13 CORBA::WChar
*buf
= CORBA::wstring_alloc (len
);
16 ACE_OutputCDR::wchar_maxbytes() == 1 ? ACE_OCTET_MAX
: ACE_WCHAR_MAX
;
19 CORBA::WChar wc
= ACE_OS::rand () % limit
;
32 verify_data (Foo::Bar
*original
, const Foo::Bar
*extracted
)
34 if (!original
|| !extracted
)
59 if (original
->A
!= extracted
->A
60 || original
->B
!= extracted
->B
61 || original
->C
!= extracted
->C
62 || (ACE_OS::strcmp (original
->D
, extracted
->D
) != 0)
63 || (ACE_OS::strcmp (original
->E
, extracted
->E
) != 0))
70 test_codec (IOP::Codec_ptr codec
)
72 // ----------------------------------------------------------
74 // Test values to be placed in the test structure.
75 const CORBA::Long A
= 1010;
76 const CORBA::Long B
= -3427;
77 const CORBA::ULongLong C
= ACE_UINT64_LITERAL (2001);
78 const CORBA::Char D
[] = "I'm Batman.";
79 CORBA::WChar
* E
= gen_wstring (25);
81 // Create the structure to be encoded.
86 value
.D
= CORBA::string_dup (D
);
87 value
.E
= CORBA::wstring_dup (E
);
89 CORBA::wstring_free (E
);
94 // ----------------------------------------------------------
96 CORBA::OctetSeq_var encoded_data
;
97 CORBA::Any_var decoded_data
;
98 const Foo::Bar
*extracted_value
= 0;
100 // Encode the structure into an octet sequence using the CDR
101 // enscapsulation Codec.
103 ACE_DEBUG ((LM_DEBUG
,
104 "Testing CDR encapsulation Codec encode()/decode()\n"
105 "=================================================\n"));
107 // Start out with the encode() method, i.e. the one that
108 // includes the TypeCode in the CDR encapsulation.
109 encoded_data
= codec
->encode (data
);
111 if ((reinterpret_cast<ptrdiff_t> (encoded_data
->get_buffer ())
112 % ACE_CDR::MAX_ALIGNMENT
) == 0)
113 ACE_DEBUG ((LM_DEBUG
,
114 "\nData for decoding are already aligned "
115 "on MAX_ALIGNMENT.\n\n"));
116 // Extract the data from the octet sequence.
117 decoded_data
= codec
->decode (encoded_data
.in ());
119 if (!(decoded_data
.in() >>= extracted_value
))
120 ACE_ERROR_RETURN ((LM_ERROR
,
121 "ERROR: Unable to extract decoded data "
125 // Verify that the extracted data matches the data that was
126 // originally encoded into the octet sequence.
127 if (::verify_data (&value
, extracted_value
) != 0)
128 ACE_ERROR_RETURN ((LM_ERROR
,
129 "ERROR: Data extracted using "
130 "IOP::Codec::decode() does not match "
134 ACE_DEBUG ((LM_DEBUG
,
135 "Testing CDR encapsulation Codec "
136 "encode_value()/decode_value()\n"
137 "================================"
138 "=============================\n"));
140 // Now use the encode_value() method, i.e. the one that does
141 // *not* include the TypeCode in the CDR encapsulation.
142 encoded_data
= codec
->encode_value (data
);
144 if ((reinterpret_cast<ptrdiff_t> (encoded_data
->get_buffer ())
145 % ACE_CDR::MAX_ALIGNMENT
) == 0)
146 ACE_DEBUG ((LM_WARNING
,
148 "WARNING: Data to be decoded is already aligned "
149 "on MAX_ALIGNMENT.\n\n"));
151 // Extract the data from the octet sequence.
152 decoded_data
= codec
->decode_value (encoded_data
.in (),
155 if (!(decoded_data
.in() >>= extracted_value
))
156 ACE_ERROR_RETURN ((LM_ERROR
,
157 "ERROR: Unable to extract decoded data "
161 // Verify that the extracted data matches the data that was
162 // originally encoded into the octet sequence.
163 if (::verify_data (&value
, extracted_value
) != 0)
164 ACE_ERROR_RETURN ((LM_ERROR
,
165 "ERROR: Data extracted using "
166 "IOP::Codec::decode_value() does not match "
174 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
180 CORBA::ORB_init (argc
,
184 // Obtain a reference to the CodecFactory.
185 CORBA::Object_var obj
=
186 orb
->resolve_initial_references ("CodecFactory");
188 IOP::CodecFactory_var codec_factory
=
189 IOP::CodecFactory::_narrow (obj
.in ());
191 // Set up a structure that contains information necessary to
192 // create a GIOP 1.1 CDR encapsulation Codec.
193 IOP::Encoding_1_2 encoding_1_2
;
194 encoding_1_2
.format
= IOP::ENCODING_CDR_ENCAPS
;
195 encoding_1_2
.major_version
= 1;
196 encoding_1_2
.minor_version
= 2;
197 encoding_1_2
.char_codeset
= 0x00010001U
;
198 encoding_1_2
.wchar_codeset
= 0x00010109U
;
200 // Obtain the CDR encapsulation Codec.
201 IOP::Codec_var codec_1_2
=
202 codec_factory
->create_codec_with_codesets (encoding_1_2
);
204 retval
= test_codec (codec_1_2
.in ());
206 // ----------------------------------------------------------
208 // Set up a structure that contains information necessary to
209 // create a GIOP 1.1 CDR encapsulation Codec.
210 IOP::Encoding encoding
;
211 encoding
.format
= IOP::ENCODING_CDR_ENCAPS
;
212 encoding
.major_version
= 1;
213 encoding
.minor_version
= 1;
215 // Obtain the CDR encapsulation Codec.
216 IOP::Codec_var codec
=
217 codec_factory
->create_codec (encoding
);
219 retval
= test_codec (codec
.in ());
221 catch (const CORBA::Exception
& ex
)
223 ex
._tao_print_exception ("Codec test:");
227 ACE_DEBUG ((LM_DEBUG
, "Codec test passed.\n"));