1 //=============================================================================
3 * @file test_dynsequence.cpp
5 * Implementation of the simple DynSequence test
7 * @author Jeff Parsons <jp4@cs.wustl.edu>
9 //=============================================================================
12 #include "test_dynsequence.h"
13 #include "da_testsC.h"
16 #include "tao/DynamicAny/DynamicAny.h"
17 #include "ace/OS_NS_string.h"
22 compare_string_sequence_elements(
24 DynamicAny::DynAnySeq
const & lhs
,
25 DynamicAny::DynAnySeq
const & rhs
)
27 CORBA::String_var lhs_string
= lhs
[i
]->get_string ();
29 ACE_DEBUG ((LM_DEBUG
, "elem[%d] = %C\n", i
, lhs_string
.in()));
31 CORBA::Boolean match
=
32 lhs
[i
]->equal (rhs
[i
]);
36 CORBA::String_var rhs_string
=
40 " mismatch with elements[%d] = %C\n", i
,
48 Test_DynSequence::Test_DynSequence (CORBA::ORB_var orb
, int debug
)
50 test_name_ (CORBA::string_dup ("test_dynsequence")),
56 Test_DynSequence::~Test_DynSequence ()
58 CORBA::string_free (this->test_name_
);
63 Test_DynSequence::test_name () const
65 return this->test_name_
;
69 Test_DynSequence::run_test ()
71 Data
data (this->orb_
);
72 DynAnyTests::test_seq
ts (2);
78 "\t*=*=*=*= %C =*=*=*=*\n",
82 "testing: constructor(Any)/insert/get/seek/"
83 "rewind/current_component\n"));
85 CORBA::Object_var factory_obj
=
86 this->orb_
->resolve_initial_references ("DynAnyFactory");
88 DynamicAny::DynAnyFactory_var dynany_factory
=
89 DynamicAny::DynAnyFactory::_narrow (factory_obj
.in ());
91 if (CORBA::is_nil (dynany_factory
.in ()))
93 ACE_ERROR_RETURN ((LM_ERROR
,
94 "Nil dynamic any factory after narrow\n"),
98 DynAnyAnalyzer
analyzer (this->orb_
.in (),
102 DynAnyTests::SeqShort shortseq1
;
104 shortseq1
.length (2);
111 DynamicAny::DynAny_var shortdany
=
112 dynany_factory
->create_dyn_any (any
);
114 analyzer
.analyze(shortdany
.in ());
117 any3
= shortdany
->to_any ();
119 ts
[0] = data
.m_string2
;
120 ts
[1] = data
.m_string2
;
123 DynamicAny::DynAny_var dp1
=
124 dynany_factory
->create_dyn_any (in_any1
);
126 DynamicAny::DynSequence_var fa1
=
127 DynamicAny::DynSequence::_narrow (dp1
.in ());
131 fa1
->insert_string (data
.m_string1
);
137 CORBA::String_var out_str1
=
140 if (!ACE_OS::strcmp (out_str1
.in (), data
.m_string1
))
142 ACE_DEBUG ((LM_DEBUG
,
147 ++this->error_count_
;
152 "testing: set_elements_as_dyn_any/get_elements_as_dyn_any/equal\n"
155 static const char *values
[] =
162 CORBA::ULong length
= 3;
163 DynamicAny::DynAnySeq
*elem_ptr
= 0;
164 ACE_NEW_RETURN (elem_ptr
,
165 DynamicAny::DynAnySeq (length
),
167 elem_ptr
->length (length
);
168 DynamicAny::DynAnySeq_var
elements (elem_ptr
);
171 for (i
= 0; i
< length
; ++i
)
174 elem_any
<<= CORBA::Any::from_string (values
[i
], 8);
175 elements
[i
] = dynany_factory
->create_dyn_any (elem_any
);
178 fa1
->set_elements_as_dyn_any (elements
.in ());
180 DynamicAny::DynAnySeq_var out_elems
=
181 fa1
->get_elements_as_dyn_any ();
183 if (out_elems
->length () != 3)
185 ++this->error_count_
;
189 analyzer
.analyze (fa1
.in());
191 for (i
= 0; i
< length
; ++i
)
193 if (!compare_string_sequence_elements(i
,
194 elements
.in(), out_elems
.in()))
199 // To show that calling destroy() on a component does
200 // nothing, as required by the spec.
201 out_elems
[i
]->destroy ();
204 // Now verify that we can replace an element using the in/out return
205 // type. This illustrates the problems described in bug # 2877:
206 // http://bugzilla.dre.vanderbilt.edu/show_bug.cgi?id=2877
207 // notice that this code has side-effects on the out_elems sequence, so
208 // do not move this code up or you will make the comparisons above fail.
210 char replacement
[] = "replacement";
212 tmp
<<= CORBA::Any::from_string (replacement
,20);
213 CORBA::ULong io_index
= 0;
214 // Since we directly change an element in a sequence we give
215 // ownership for the previous element to a local temporary object.
216 DynamicAny::DynAny_var io_owner
= out_elems
[io_index
].inout();
217 out_elems
[io_index
].inout() = dynany_factory
->create_dyn_any (tmp
);
219 // Compare the value of the replaced string.
220 CORBA::String_var new_str
= out_elems
[io_index
]->get_string();
221 if (0 != ACE_OS::strcmp(new_str
.in(), replacement
))
224 " Mismatch after replacing through in/out.\n"
225 " expected = %C, got = %C\n",
226 replacement
, new_str
.in()));
227 this->error_count_
++;
232 if (this->error_count_
== 0)
234 ACE_DEBUG ((LM_DEBUG
,
238 ACE_DEBUG ((LM_DEBUG
,
239 "testing: constructor(TypeCode)/from_any/to_any\n"));
241 DynamicAny::DynAny_var ftc1_base
=
242 dynany_factory
->create_dyn_any_from_type_code (
243 DynAnyTests::_tc_test_seq
);
245 DynamicAny::DynSequence_var ftc1
=
246 DynamicAny::DynSequence::_narrow (ftc1_base
.in ());
248 if (CORBA::is_nil (ftc1
.in ()))
250 ACE_ERROR_RETURN ((LM_ERROR
,
251 "DynSequence::_narrow() returned nil\n"),
257 ACE_TEXT ("testing: constructor(%C)/create_dyn_any_from_type_code\n"),
261 DynamicAny::DynAny_var da_bool
=
262 dynany_factory
->create_dyn_any_from_type_code (
263 DynAnyTests::_tc_SeqBoolean
);
265 DynamicAny::DynSequence_var ds_bool
=
266 DynamicAny::DynSequence::_narrow (da_bool
.in ());
268 if (CORBA::is_nil (ds_bool
.in ()))
272 ACE_TEXT ("DynSequence::_narrow() returned nil\n")
274 this->error_count_
++;
279 ACE_TEXT ("testing: constructor(%C)/create_dyn_any_from_type_code\n"),
283 DynamicAny::DynAny_var da_short
=
284 dynany_factory
->create_dyn_any_from_type_code (DynAnyTests::_tc_SeqShort
);
286 DynamicAny::DynSequence_var ds_short
=
287 DynamicAny::DynSequence::_narrow (da_short
.in ());
289 if (CORBA::is_nil (ds_short
.in ()))
293 ACE_TEXT ("DynSequence::_narrow() returned nil\n")
295 this->error_count_
++;
298 ts
[0] = CORBA::string_dup (data
.m_string1
);
301 ftc1
->from_any (in_any2
);
303 analyzer
.analyze (ftc1
.in());
305 CORBA::Any_var out_any1
=
308 const DynAnyTests::test_seq
*ts_out
= 0;
309 out_any1
.in () >>= ts_out
;
311 if (!ACE_OS::strcmp ((*ts_out
)[0U], data
.m_string1
))
313 ACE_DEBUG ((LM_DEBUG
,
318 ++this->error_count_
;
321 ACE_DEBUG ((LM_DEBUG
,
322 "testing: length/set_elements/get_elements\n"));
324 length
= ftc1
->get_length ();
328 ++this->error_count_
;
331 ftc1
->set_length (3);
332 DynamicAny::AnySeq
as_in (3);
335 in_any3
<<= CORBA::Any::from_string (data
.m_string2
, 8);
338 in_any3
<<= CORBA::Any::from_string (data
.m_string1
, 8);
340 ftc1
->set_elements (as_in
);
342 DynamicAny::AnySeq_var as_out
=
343 ftc1
->get_elements ();
345 CORBA::ULong index
= 2;
346 CORBA::Any out_any2
= as_out
[index
];
348 out_any2
>>= CORBA::Any::to_string (out_str2
, 8);
350 if (ACE_OS::strcmp (out_str2
, data
.m_string1
))
352 ++this->error_count_
;
355 if (this->error_count_
== 0)
357 ACE_DEBUG ((LM_DEBUG
,
365 catch (const CORBA::Exception
& ex
)
367 ex
._tao_print_exception ("test_dynsequence::run_test");
368 ++this->error_count_
;
371 ACE_DEBUG ((LM_DEBUG
,
373 this->error_count_
));
376 return !!error_count_
;