Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Param_Test / any.cpp
blob8f44fe95b87d43dbc2532a61730cb8a7f50b64e7
2 //=============================================================================
3 /**
4 * @file any.cpp
6 * tests Anys
8 * @author Aniruddha Gokhale
9 */
10 //=============================================================================
13 #include "helper.h"
14 #include "any.h"
15 #include "tao/debug.h"
17 // ************************************************************************
18 // Test_Any
19 // ************************************************************************
21 size_t Test_Any::counter = 0;
23 Test_Any::Test_Any ()
24 : opname_ (CORBA::string_dup ("test_any")),
25 out_ (new CORBA::Any),
26 ret_ (new CORBA::Any)
30 Test_Any::~Test_Any ()
32 CORBA::string_free (this->opname_);
33 this->opname_ = 0;
36 const char *
37 Test_Any::opname () const
39 return this->opname_;
42 void
43 Test_Any::dii_req_invoke (CORBA::Request *req)
45 req->add_in_arg ("o1") <<= this->in_;
46 req->add_inout_arg ("o2") <<= this->inout_;
47 req->add_out_arg ("o3") <<= this->out_.in ();
49 req->set_return_type (CORBA::_tc_any);
51 req->invoke ();
53 const CORBA::Any *tmp;
54 req->return_value () >>= tmp;
55 this->ret_ = new CORBA::Any (*tmp);
57 CORBA::NamedValue_ptr o2 =
58 req->arguments ()->item (1);
59 *o2->value () >>= tmp;
60 this->inout_ = CORBA::Any (*tmp);
62 CORBA::NamedValue_ptr o3 =
63 req->arguments ()->item (2);
64 *o3->value () >>= tmp;
65 this->out_ = new CORBA::Any (*tmp);
68 #if 0 /* any_table isn't currently used */
69 static const CORBA::TypeCode_ptr any_table [] =
71 // primitive parameterless typecodes
72 CORBA::_tc_short,
73 // typecode with a simple parameter
74 CORBA::_tc_string,
75 // complex typecodes
76 _tc_Coffee,
77 Param_Test::_tc_StrSeq,
78 Param_Test::_tc_StructSeq,
79 Param_Test::_tc_Nested_Struct
81 #endif /* any_table isn't currently used */
83 int
84 Test_Any::init_parameters (Param_Test_ptr objref)
86 try
88 // get access to a Coffee Object
89 this->cobj_ = objref->make_coffee ();
91 this->reset_parameters ();
92 return 0;
94 catch (const CORBA::SystemException& sysex)
96 sysex._tao_print_exception ("System Exception doing make_coffee");
98 return -1;
102 Test_Any::reset_parameters ()
104 Generator *gen = GENERATOR::instance (); // value generator
105 CORBA::ULong index = (counter++ % Test_Any::ANY_LAST_TEST_ITEM);
107 switch (index)
109 default:
110 case Test_Any::ANY_SHORT:
112 if (TAO_debug_level > 0)
113 ACE_DEBUG ((LM_DEBUG,
114 "Param_Test: ANY_SHORT subtest\n"));
115 CORBA::Short s;
116 s = gen->gen_short ();
118 if (TAO_debug_level > 0)
119 ACE_DEBUG ((LM_DEBUG, "setting short = %d\n", s));
120 this->in_ <<= s;
121 this->inout_ <<= s;
123 break;
125 case Test_Any::ANY_STRING:
127 if (TAO_debug_level > 0)
128 ACE_DEBUG ((LM_DEBUG,
129 "Param_Test: ANY_STRING subtest\n"));
130 char *str = gen->gen_string ();
132 if (TAO_debug_level > 0)
133 ACE_DEBUG ((LM_DEBUG, "setting string = %C\n", str));
134 this->in_ <<= str;
135 this->inout_ <<= str;
136 CORBA::string_free (str);
138 break;
140 case Test_Any::ANY_OBJREF:
142 if (TAO_debug_level > 0)
143 ACE_DEBUG ((LM_DEBUG,
144 "Param_Test: ANY_OBJREF subtest\n"));
146 // insert the coffee object into the Any
147 this->in_ <<= this->cobj_.in ();
148 this->inout_ <<= this->cobj_.in ();
150 break;
152 case Test_Any::ANY_ARRAY:
154 if (TAO_debug_level > 0)
155 ACE_DEBUG ((LM_DEBUG,
156 "Param_Test: ANY_ARRAY subtest\n"));
158 Param_Test::Fixed_Array array;
159 for (size_t i = 0; i < Param_Test::DIM1; i++)
160 array[i] = i;
161 if (TAO_debug_level > 0)
162 ACE_DEBUG ((LM_DEBUG, "Setting Fixed_Array\n"));
163 this->in_ <<= Param_Test::Fixed_Array_forany (array);
164 this->inout_ <<= Param_Test::Fixed_Array_forany (array);
166 break;
168 case Test_Any::ANY_SHORT_SEQ:
170 if (TAO_debug_level > 0)
171 ACE_DEBUG ((LM_DEBUG,
172 "Param_Test: ANY_SHORT_SEQ subtest\n"));
173 CORBA::ShortSeq seq;
174 seq.length (gen->gen_short () % 16);
175 for (size_t i = 0; i < seq.length (); i++)
176 seq[i] = gen->gen_short ();
177 this->in_ <<= seq;
178 this->inout_ <<= seq;
180 break;
182 case Test_Any::ANY_BD_SHORT_SEQ:
184 if (TAO_debug_level > 0)
185 ACE_DEBUG ((LM_DEBUG,
186 "Param_Test: ANY_BD_SHORT_SEQ subtest\n"));
187 Param_Test::Bounded_Short_Seq seq;
188 seq.length (gen->gen_short () % seq.maximum ());
189 for (size_t i = 0; i < seq.length (); i++)
190 seq[i] = gen->gen_short ();
191 this->in_ <<= seq;
192 this->inout_ <<= seq;
194 break;
196 case Test_Any::ANY_STRUCT:
198 if (TAO_debug_level > 0)
199 ACE_DEBUG ((LM_DEBUG,
200 "Param_Test: ANY_STRUCT subtest\n"));
201 Param_Test::Fixed_Struct structure;
202 structure = gen->gen_fixed_struct ();
203 this->in_ <<= structure;
204 this->inout_ <<= structure;
206 break;
208 case Test_Any::ANY_BIG_UNION:
210 CORBA::Long x = gen->gen_long ();
211 Param_Test::Big_Union the_union;
212 the_union.the_long (x);
213 this->in_ <<= the_union;
214 this->inout_ <<= the_union;
216 if (TAO_debug_level > 0)
218 const Param_Test::Big_Union *bu_in = 0;
219 const Param_Test::Big_Union *bu_inout = 0;
220 this->in_ >>= bu_in;
221 this->inout_ >>= bu_inout;
222 ACE_DEBUG ((LM_DEBUG,
223 "Param_Test: ANY_BIG_UNION subtest\n"
224 " in %d\n"
225 " inout %d\n",
226 bu_in->the_long (),
227 bu_inout->the_long ()));
230 break;
232 case Test_Any::ANY_SMALL_UNION:
234 CORBA::Long x = gen->gen_long ();
235 Param_Test::Small_Union the_union;
236 the_union.the_long (x);
237 this->in_ <<= the_union;
238 this->inout_ <<= the_union;
240 if (TAO_debug_level > 0)
242 const Param_Test::Small_Union *bu_in = 0;
243 const Param_Test::Small_Union *bu_inout = 0;
244 this->in_ >>= bu_in;
245 this->inout_ >>= bu_inout;
246 ACE_DEBUG ((LM_DEBUG,
247 "Param_Test: ANY_SMALL_UNION subtest\n"
248 " in %d\n"
249 " inout %d\n",
250 bu_in->the_long (),
251 bu_inout->the_long ()));
254 break;
256 return 0;
260 Test_Any::run_sii_test (Param_Test_ptr objref)
264 this->ret_ = objref->test_any (this->in_,
265 this->inout_,
266 this->out_.out ());
268 catch (const CORBA::Exception&)
270 return -1;
272 return 0;
275 CORBA::Boolean
276 Test_Any::check_validity ()
278 CORBA::Short short_in, short_inout, short_out, short_ret;
279 const char *str_in;
280 const char *str_inout;
281 const char *str_out;
282 const char *str_ret;
283 Coffee_ptr obj_in, obj_inout, obj_out, obj_ret;
284 Param_Test::Fixed_Array_forany array_in, array_inout, array_out, array_ret;
285 const Param_Test::Bounded_Short_Seq *bdss_in, *bdss_inout, *bdss_out, *bdss_ret;
286 const CORBA::ShortSeq *ubss_in, *ubss_inout, *ubss_out, *ubss_ret;
287 const Param_Test::Fixed_Struct *fs_in, *fs_inout, *fs_out, *fs_ret;
288 const Param_Test::Big_Union *bu_in, *bu_inout, *bu_out, *bu_ret;
289 const Param_Test::Small_Union *su_in, *su_inout, *su_out, *su_ret;
291 if ((this->in_ >>= short_in) &&
292 (this->inout_ >>= short_inout) &&
293 (this->out_.in () >>= short_out) &&
294 (this->ret_.in () >>= short_ret))
296 // ACE_DEBUG ((LM_DEBUG, "Received shorts: in = %d, "
297 // "inout = %d, out = %d, ret = %d\n",
298 // short_in, short_inout, short_out, short_ret));
300 if ((short_in == short_inout) &&
301 (short_in == short_out) &&
302 (short_in == short_ret))
303 return 1;
304 else
306 ACE_DEBUG ((LM_DEBUG, "mismatch of short values\n"));
307 return 0;
310 else if ((this->in_ >>= str_in) &&
311 (this->inout_ >>= str_inout) &&
312 (this->out_.in () >>= str_out) &&
313 (this->ret_.in () >>= str_ret))
315 if (!ACE_OS::strcmp (str_in, str_inout) &&
316 !ACE_OS::strcmp (str_in, str_out) &&
317 !ACE_OS::strcmp (str_in, str_ret))
318 return 1;
319 else
321 ACE_DEBUG ((LM_DEBUG, "mismatch of string values\n"));
322 return 0;
325 else if ((this->in_ >>= obj_in) &&
326 (this->inout_ >>= obj_inout) &&
327 (this->out_.in () >>= obj_out) &&
328 (this->ret_.in () >>= obj_ret))
330 // all the >>= operators returned true so we are OK.
331 return 1;
333 else if ((this->in_ >>= array_in) &&
334 (this->inout_ >>= array_inout) &&
335 (this->out_.in () >>= array_out) &&
336 (this->ret_.in () >>= array_ret))
338 for (CORBA::ULong i = 0; i < Param_Test::DIM1; i ++)
340 CORBA::Long ii = i;
341 CORBA::Long square = i * i;
342 if (array_in[i] != ii ||
343 array_out[i] != ii ||
344 array_inout[i] != square ||
345 array_ret[i] != square)
346 return 0;
348 return 1;
350 else if ((this->in_ >>= bdss_in) &&
351 (this->inout_ >>= bdss_inout) &&
352 (this->out_.in () >>= bdss_out) &&
353 (this->ret_.in () >>= bdss_ret))
355 for (size_t i = 0; i < bdss_in->length (); i ++)
357 ssize_t square = i * i;
358 if ((*bdss_in)[i] != (*bdss_out)[i] ||
359 (*bdss_inout)[i] != square ||
360 (*bdss_inout)[i] != (*bdss_ret)[i])
361 return 0;
363 return 1;
365 else if ((this->in_ >>= ubss_in) &&
366 (this->inout_ >>= ubss_inout) &&
367 (this->out_.in () >>= ubss_out) &&
368 (this->ret_.in () >>= ubss_ret))
370 for (size_t i = 0; i < ubss_in->length (); i ++)
372 ssize_t square = i * i;
373 if ((*ubss_in)[i] != (*ubss_out)[i] ||
374 (*ubss_inout)[i] != square ||
375 (*ubss_inout)[i] != (*ubss_ret)[i])
376 return 0;
378 return 1;
380 else if ((this->in_ >>= fs_in) &&
381 (this->inout_ >>= fs_inout) &&
382 (this->out_.in () >>= fs_out) &&
383 (this->ret_.in () >>= fs_ret))
385 // @@ Added test for data validity here.
386 return 1;
388 else if ((this->in_ >>= bu_in) &&
389 (this->inout_ >>= bu_inout) &&
390 (this->out_.in () >>= bu_out) &&
391 (this->ret_.in () >>= bu_ret))
393 if (bu_in->_d () != 2
394 || bu_inout->_d () != 2
395 || bu_out->_d () != 2
396 || bu_ret->_d () != 2)
398 ACE_DEBUG ((LM_DEBUG,
399 "Test_Any - not all unions contain a long\n"));
400 return 0;
403 if (bu_in->the_long () != bu_inout->the_long ()
404 || bu_in->the_long () != bu_out->the_long ()
405 || bu_in->the_long () != bu_ret->the_long ())
407 ACE_DEBUG ((LM_DEBUG,
408 "Test_Any - values mismatch\n"
409 " in %d\n"
410 " inout %d\n"
411 " out %d\n"
412 " ret %d\n",
413 bu_in->the_long (),
414 bu_inout->the_long (),
415 bu_out->the_long (),
416 bu_ret->the_long () ));
417 return 0;
419 return 1;
421 else if ((this->in_ >>= su_in) &&
422 (this->inout_ >>= su_inout) &&
423 (this->out_.in () >>= su_out) &&
424 (this->ret_.in () >>= su_ret))
426 if (su_in->_d () != Param_Test::A_LONG
427 || su_inout->_d () != Param_Test::A_LONG
428 || su_out->_d () != Param_Test::A_LONG
429 || su_ret->_d () != Param_Test::A_LONG)
431 ACE_DEBUG ((LM_DEBUG,
432 "Test_Any - not all unions contain a long\n"));
433 return 0;
436 if (su_in->the_long () != su_inout->the_long ()
437 || su_in->the_long () != su_out->the_long ()
438 || su_in->the_long () != su_ret->the_long ())
440 ACE_DEBUG ((LM_DEBUG,
441 "Test_Any - values mismatch\n"
442 " in %d\n"
443 " inout %d\n"
444 " out %d\n"
445 " ret %d\n",
446 su_in->the_long (),
447 su_inout->the_long (),
448 su_out->the_long (),
449 su_ret->the_long () ));
450 return 0;
452 return 1;
454 else
455 return 0;
458 CORBA::Boolean
459 Test_Any::check_validity (CORBA::Request_ptr /*req*/)
461 return this->check_validity ();
464 void
465 Test_Any::print_values ()