Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / Codec / client.cpp
blobb9c96d3511281a21e204bfb39100f5fedc019fa6
1 // -*- C++ -*-
2 #include "tao/CodecFactory/CodecFactory.h"
3 #include "tao/Codeset/Codeset.h"
4 #include "testC.h"
5 #include "ace/OS_NS_string.h"
7 #include "ace/Log_Msg.h"
9 CORBA::WChar *
10 gen_wstring (CORBA::ULong max_length)
12 CORBA::ULong len = max_length;
13 CORBA::WChar *buf = CORBA::wstring_alloc (len);
14 CORBA::ULong i = 0;
15 CORBA::WChar limit =
16 ACE_OutputCDR::wchar_maxbytes() == 1 ? ACE_OCTET_MAX : ACE_WCHAR_MAX;
17 while (i < len)
19 CORBA::WChar wc = ACE_OS::rand () % limit;
20 if (wc)
22 buf[i] = wc;
23 i++;
27 buf[i] = 0;
28 return buf;
31 int
32 verify_data (Foo::Bar *original, const Foo::Bar *extracted)
34 if (!original || !extracted)
35 return -1;
37 ACE_DEBUG ((LM_DEBUG,
38 "Original\n"
39 "--------\n"
40 "%d\n"
41 "%d\n"
42 // "%Lu\n"
43 "%C\n\n"
44 "Extracted\n"
45 "---------\n"
46 "%d\n"
47 "%d\n"
48 // "%Lu\n"
49 "%C\n\n",
50 original->A,
51 original->B,
52 // original->C,
53 original->D.in (),
54 extracted->A,
55 extracted->B,
56 // extracted->C,
57 extracted->D.in ()));
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))
64 return -1;
66 return 0;
69 int
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.
82 Foo::Bar value;
83 value.A = A;
84 value.B = B;
85 value.C = C;
86 value.D = CORBA::string_dup (D);
87 value.E = CORBA::wstring_dup (E);
89 CORBA::wstring_free (E);
91 CORBA::Any data;
92 data <<= value;
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 "
122 "from Any\n"),
123 -1);
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 "
131 "original data.\n"),
132 -1);
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,
147 "\n"
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 (),
153 Foo::_tc_Bar);
155 if (!(decoded_data.in() >>= extracted_value))
156 ACE_ERROR_RETURN ((LM_ERROR,
157 "ERROR: Unable to extract decoded data "
158 "from Any\n"),
159 -1);
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 "
167 "original data.\n"),
168 -1);
170 return 0;
174 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
176 int retval = 0;
179 CORBA::ORB_var orb =
180 CORBA::ORB_init (argc,
181 argv,
182 "my_orb");
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:");
224 return -1;
227 ACE_DEBUG ((LM_DEBUG, "Codec test passed.\n"));
229 return retval;