Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / DynAny_Test / test_dynstruct.cpp
blob1d7151f0eb44fe6ed80ba0ceb5a1eedbd43a4995
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 (void)
29 CORBA::string_free (this->test_name_);
30 this->test_name_ = 0;
33 const char*
34 Test_DynStruct::test_name (void) const
36 return this->test_name_;
39 int
40 Test_DynStruct::run_test (void)
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
212 ftc2 = DynamicAny::DynStruct::_narrow (ftc2_base.in ());
214 if (CORBA::is_nil (ftc2.in ()))
216 ACE_ERROR_RETURN ((LM_ERROR,
217 "DynStruct::_narrow() returned nil\n"),
218 -1);
221 ts.c = data.m_char1;
222 ts.ss.length (0UL);
223 ts.es.f = data.m_float1;
224 ts.es.s = data.m_short1;
225 CORBA::Any in_any3;
226 in_any3 <<= ts;
227 ftc2->from_any (in_any3);
229 // char c = ftc2->get_char ();
230 // CORBA::Boolean status = ftc2->next ();
231 // CORBA::Long lo = ftc2->get_long ();
232 // analyzer.analyze (ftc2.in ());
234 out_any2 = ftc2->to_any ();
236 if (!(out_any2.in () >>= ts_out2)) // problem
238 ts_out2 = 0;
241 catch (const CORBA::TypeCode::BadKind& )
245 if (ts_out2 != 0 && ts_out2->es.s == data.m_short1)
247 ACE_DEBUG ((LM_DEBUG,
248 "++ OK ++\n"));
250 else
252 ++this->error_count_;
256 ACE_DEBUG ((LM_DEBUG,
257 "testing: current_member_name/current_member_kind\n"));
259 ftc1->seek (2);
261 DynamicAny::FieldName_var fn =
262 ftc1->current_member_name ();
264 if (ACE_OS::strcmp (fn.in (), "es"))
266 ++this->error_count_;
269 CORBA::TCKind tk =
270 ftc1->current_member_kind ();
272 if (tk != CORBA::tk_struct)
274 ++this->error_count_;
277 if (this->error_count_ == 0)
279 ACE_DEBUG ((LM_DEBUG,
280 "++ OK ++\n"));
283 ACE_DEBUG ((LM_DEBUG,
284 "testing: current_member_name/current_member_kind "
285 "with alias\n"));
286 ftc2->seek (2);
288 DynamicAny::FieldName_var fn2 =
289 ftc2->current_member_name ();
291 if (ACE_OS::strcmp (fn2.in (), "es"))
293 ++this->error_count_;
296 CORBA::TCKind tk2 =
297 ftc2->current_member_kind ();
299 if (tk2 != CORBA::tk_struct)
301 ++this->error_count_;
304 if (this->error_count_ == 0)
306 ACE_DEBUG ((LM_DEBUG,
307 "++ OK ++\n"));
310 ACE_DEBUG ((LM_DEBUG,
311 "testing: get_members/set_members\n"));
313 DynamicAny::NameValuePairSeq_var nvps =
314 fa1->get_members ();
316 DynamicAny::DynAny_var sm_base =
317 dynany_factory->create_dyn_any_from_type_code (
318 DynAnyTests::_tc_test_struct
321 DynamicAny::DynStruct_var sm =
322 DynamicAny::DynStruct::_narrow (sm_base.in ());
324 if (CORBA::is_nil (sm.in ()))
326 ACE_ERROR_RETURN ((LM_ERROR,
327 "DynStruct::_narrow()[2] returned nil\n"),
328 -1);
331 sm->set_members (nvps.in ());
333 DynamicAny::NameValuePairSeq_var gm =
334 sm->get_members ();
336 CORBA::ULong index = 2;
338 if (ACE_OS::strcmp (gm[index].id, "es"))
340 ++this->error_count_;
343 fa1->destroy ();
345 ftc1->destroy ();
347 sm->destroy ();
349 ACE_DEBUG ((LM_DEBUG,
350 "++ OK ++\n"));
352 ACE_DEBUG ((LM_DEBUG,
353 "testing: struct_with_long_double destroy\n"));
355 // Test fix for bug on destroy when struct contains long_double value.
356 DynAnyTests::test_struct_with_long_double l_long_double_in_struct;
358 CORBA::Any corba_long_double;
359 corba_long_double <<= l_long_double_in_struct;
361 DynamicAny::DynAny_var l_dynany = dynany_factory->create_dyn_any (corba_long_double);
364 l_dynany->destroy ();
365 ACE_DEBUG ((LM_DEBUG,
366 "++ OK ++\n"));
368 catch (...)
370 ++this->error_count_;
373 catch (const CORBA::Exception& ex)
375 ex._tao_print_exception ("test_dynstruct::run_test");
376 ++this->error_count_;
379 ACE_DEBUG ((LM_DEBUG,
380 "\n%d errors\n",
381 this->error_count_));
383 return !!error_count_;