Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / DynAny_Test / test_dynstruct.cpp
blob3322bb5d0c5c2f1e5f35498720257945ddef34c5
1 //=============================================================================
2 /**
3 * @file test_dynstruct.cpp
5 * Implementation of the DynStruct basic test
7 * @author Jeff Parsons <parsons@cs.wustl.edu>
8 */
9 //=============================================================================
12 #include "test_dynstruct.h"
13 #include "da_testsC.h"
14 #include "data.h"
15 #include "tao/DynamicAny/DynamicAny.h"
16 #include "analyzer.h"
17 #include "ace/OS_NS_string.h"
19 Test_DynStruct::Test_DynStruct (CORBA::ORB_var orb, int debug)
20 : orb_ (orb),
21 test_name_ (CORBA::string_dup ("test_dynstruct")),
22 error_count_ (0),
23 debug_ (debug)
27 Test_DynStruct::~Test_DynStruct ()
29 CORBA::string_free (this->test_name_);
30 this->test_name_ = 0;
33 const char*
34 Test_DynStruct::test_name () const
36 return this->test_name_;
39 int
40 Test_DynStruct::run_test ()
42 Data data (this->orb_);
44 DynAnyTests::test_struct ts;
45 ts.c = data.m_char2;
46 ts.ss.length (0UL);
47 ts.es.f = data.m_float2;
48 ts.es.s = data.m_short2;
50 try
52 ACE_DEBUG ((LM_DEBUG,
53 "testing: constructor(Any)/insert/get/next/seek/"
54 "rewind/current_component\n"));
56 CORBA::Object_var factory_obj =
57 this->orb_->resolve_initial_references ("DynAnyFactory");
59 DynamicAny::DynAnyFactory_var dynany_factory =
60 DynamicAny::DynAnyFactory::_narrow (factory_obj.in ());
62 if (CORBA::is_nil (dynany_factory.in ()))
64 ACE_ERROR_RETURN ((LM_ERROR,
65 "Nil dynamic any factory after narrow\n"),
66 -1);
69 DynAnyAnalyzer analyzer (this->orb_.in (),
70 dynany_factory.in (),
71 debug_);
73 CORBA::Any in_any1;
74 in_any1 <<= ts;
75 DynamicAny::DynAny_var dp1 =
76 dynany_factory->create_dyn_any (in_any1);
78 DynamicAny::DynStruct_var fa1 =
79 DynamicAny::DynStruct::_narrow (dp1.in ());
81 fa1->insert_char (data.m_char1);
83 fa1->next ();
85 fa1->insert_short_seq (data.m_shortseq1);
87 fa1->next ();
89 DynamicAny::DynAny_var cc =
90 fa1->current_component ();
92 cc->insert_float (data.m_float1);
94 cc->next ();
96 cc->insert_short (data.m_short1);
97 cc->rewind ();
99 // To show that calling destroy() on a component does
100 // nothing, as required by the spec.
101 cc->destroy ();
103 fa1->rewind ();
105 CORBA::Char c = fa1->get_char ();
107 if (c != data.m_char1)
109 ++this->error_count_;
112 fa1->next ();
114 data.m_shortseq2 =
115 fa1->get_short_seq ();
117 bool good =
118 data.m_shortseq2->length () == data.m_shortseq1.length ();
120 if (good)
122 for (CORBA::ULong i = 0; i < data.m_shortseq1.length (); ++i)
124 if (data.m_shortseq2[i] != data.m_shortseq1[i])
126 ++this->error_count_;
127 break;
131 else
133 ++this->error_count_;
136 fa1->next ();
138 cc = fa1->current_component ();
140 cc->seek (1);
142 CORBA::Short s = cc->get_short ();
144 if (s != data.m_short1)
146 ++this->error_count_;
149 if (this->error_count_ == 0)
151 ACE_DEBUG ((LM_DEBUG,
152 "++ OK ++\n"));
155 ACE_DEBUG ((LM_DEBUG,
156 "testing: constructor(TypeCode)/from_any/to_any\n"));
158 DynamicAny::DynAny_var ftc1_base =
159 dynany_factory->create_dyn_any_from_type_code (DynAnyTests::_tc_test_struct);
161 DynamicAny::DynStruct_var ftc1 =
162 DynamicAny::DynStruct::_narrow (ftc1_base.in ());
164 if (CORBA::is_nil (ftc1.in ()))
166 ACE_ERROR_RETURN ((LM_ERROR,
167 "DynStruct::_narrow() returned nil\n"),
168 -1);
171 ts.c = data.m_char1;
172 ts.ss.length (0UL);
173 ts.es.f = data.m_float1;
174 ts.es.s = data.m_short1;
175 CORBA::Any in_any2;
176 in_any2 <<= ts;
177 ftc1->from_any (in_any2);
179 analyzer.analyze (ftc1.in ());
181 CORBA::Any_var out_any1 =
182 ftc1->to_any ();
184 const DynAnyTests::test_struct* ts_out = 0;
185 out_any1.in () >>= ts_out;
187 if (ts_out->es.s == data.m_short1)
189 ACE_DEBUG ((LM_DEBUG,
190 "++ OK ++\n"));
192 else
194 ++this->error_count_;
197 ACE_DEBUG ((LM_DEBUG,
198 "testing: constructor(TypeCode alias)/from_any/to_any\n"));
201 const DynAnyTests::test_struct *ts_out2 = 0;
202 CORBA::Any_var out_any2 ;
203 DynamicAny::DynStruct_var ftc2;
207 DynamicAny::DynAny_var ftc2_base =
208 dynany_factory->create_dyn_any_from_type_code (
209 DynAnyTests::_tc_test_struct_alias);
211 ftc2 = DynamicAny::DynStruct::_narrow (ftc2_base.in ());
213 if (CORBA::is_nil (ftc2.in ()))
215 ACE_ERROR_RETURN ((LM_ERROR,
216 "DynStruct::_narrow() returned nil\n"),
217 -1);
220 ts.c = data.m_char1;
221 ts.ss.length (0UL);
222 ts.es.f = data.m_float1;
223 ts.es.s = data.m_short1;
224 CORBA::Any in_any3;
225 in_any3 <<= ts;
226 ftc2->from_any (in_any3);
228 // char c = ftc2->get_char ();
229 // CORBA::Boolean status = ftc2->next ();
230 // CORBA::Long lo = ftc2->get_long ();
231 // analyzer.analyze (ftc2.in ());
233 out_any2 = ftc2->to_any ();
235 if (!(out_any2.in () >>= ts_out2)) // problem
237 ts_out2 = 0;
240 catch (const CORBA::TypeCode::BadKind& )
244 if (ts_out2 != 0 && ts_out2->es.s == data.m_short1)
246 ACE_DEBUG ((LM_DEBUG,
247 "++ OK ++\n"));
249 else
251 ++this->error_count_;
255 ACE_DEBUG ((LM_DEBUG,
256 "testing: current_member_name/current_member_kind\n"));
258 ftc1->seek (2);
260 DynamicAny::FieldName_var fn =
261 ftc1->current_member_name ();
263 if (ACE_OS::strcmp (fn.in (), "es"))
265 ++this->error_count_;
268 CORBA::TCKind tk =
269 ftc1->current_member_kind ();
271 if (tk != CORBA::tk_struct)
273 ++this->error_count_;
276 if (this->error_count_ == 0)
278 ACE_DEBUG ((LM_DEBUG,
279 "++ OK ++\n"));
282 ACE_DEBUG ((LM_DEBUG,
283 "testing: current_member_name/current_member_kind "
284 "with alias\n"));
285 ftc2->seek (2);
287 DynamicAny::FieldName_var fn2 =
288 ftc2->current_member_name ();
290 if (ACE_OS::strcmp (fn2.in (), "es"))
292 ++this->error_count_;
295 CORBA::TCKind tk2 =
296 ftc2->current_member_kind ();
298 if (tk2 != CORBA::tk_struct)
300 ++this->error_count_;
303 if (this->error_count_ == 0)
305 ACE_DEBUG ((LM_DEBUG,
306 "++ OK ++\n"));
309 ACE_DEBUG ((LM_DEBUG,
310 "testing: get_members/set_members\n"));
312 DynamicAny::NameValuePairSeq_var nvps =
313 fa1->get_members ();
315 DynamicAny::DynAny_var sm_base =
316 dynany_factory->create_dyn_any_from_type_code (
317 DynAnyTests::_tc_test_struct);
319 DynamicAny::DynStruct_var sm =
320 DynamicAny::DynStruct::_narrow (sm_base.in ());
322 if (CORBA::is_nil (sm.in ()))
324 ACE_ERROR_RETURN ((LM_ERROR,
325 "DynStruct::_narrow()[2] returned nil\n"),
326 -1);
329 sm->set_members (nvps.in ());
331 DynamicAny::NameValuePairSeq_var gm =
332 sm->get_members ();
334 CORBA::ULong index = 2;
336 if (ACE_OS::strcmp (gm[index].id, "es"))
338 ++this->error_count_;
341 fa1->destroy ();
343 ftc1->destroy ();
345 sm->destroy ();
347 ACE_DEBUG ((LM_DEBUG,
348 "++ OK ++\n"));
350 ACE_DEBUG ((LM_DEBUG,
351 "testing: struct_with_long_double destroy\n"));
353 // Test fix for bug on destroy when struct contains long_double value.
354 DynAnyTests::test_struct_with_long_double l_long_double_in_struct;
356 CORBA::Any corba_long_double;
357 corba_long_double <<= l_long_double_in_struct;
359 DynamicAny::DynAny_var l_dynany = dynany_factory->create_dyn_any (corba_long_double);
362 l_dynany->destroy ();
363 ACE_DEBUG ((LM_DEBUG,
364 "++ OK ++\n"));
366 catch (...)
368 ++this->error_count_;
371 catch (const CORBA::Exception& ex)
373 ex._tao_print_exception ("test_dynstruct::run_test");
374 ++this->error_count_;
377 ACE_DEBUG ((LM_DEBUG,
378 "\n%d errors\n",
379 this->error_count_));
381 return !!error_count_;