Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / DynAny_Test / test_dynsequence.cpp
blob04a4ca1b9639c516acf2d2397bf5aa5cc77144b3
1 //=============================================================================
2 /**
3 * @file test_dynsequence.cpp
5 * Implementation of the simple DynSequence test
7 * @author Jeff Parsons <jp4@cs.wustl.edu>
8 */
9 //=============================================================================
12 #include "test_dynsequence.h"
13 #include "da_testsC.h"
14 #include "data.h"
15 #include "analyzer.h"
16 #include "tao/DynamicAny/DynamicAny.h"
17 #include "ace/OS_NS_string.h"
19 namespace
21 bool
22 compare_string_sequence_elements(
23 CORBA::ULong i,
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]);
34 if (!match)
36 CORBA::String_var rhs_string =
37 rhs[i]->get_string();
39 ACE_DEBUG ((LM_DEBUG,
40 " mismatch with elements[%d] = %C\n", i,
41 rhs_string.in() ));
44 return match;
48 Test_DynSequence::Test_DynSequence (CORBA::ORB_var orb, int debug)
49 : orb_ (orb),
50 test_name_ (CORBA::string_dup ("test_dynsequence")),
51 error_count_ (0),
52 debug_ (debug)
56 Test_DynSequence::~Test_DynSequence ()
58 CORBA::string_free (this->test_name_);
59 this->test_name_ = 0;
62 const char*
63 Test_DynSequence::test_name () const
65 return this->test_name_;
68 int
69 Test_DynSequence::run_test ()
71 Data data (this->orb_);
72 DynAnyTests::test_seq ts (2);
73 ts.length (2);
75 try
77 ACE_DEBUG ((LM_DEBUG,
78 "\t*=*=*=*= %C =*=*=*=*\n",
79 data.labels[11]));
81 ACE_DEBUG ((LM_DEBUG,
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"),
95 -1);
98 DynAnyAnalyzer analyzer (this->orb_.in (),
99 dynany_factory.in (),
100 debug_);
102 DynAnyTests::SeqShort shortseq1;
104 shortseq1.length (2);
105 shortseq1[0] = 2;
106 shortseq1[1] = -2;
108 CORBA::Any any;
109 any <<= shortseq1;
111 DynamicAny::DynAny_var shortdany =
112 dynany_factory->create_dyn_any (any);
114 analyzer.analyze(shortdany.in ());
116 CORBA::Any_var any3;
117 any3 = shortdany->to_any ();
119 ts[0] = data.m_string2;
120 ts[1] = data.m_string2;
121 CORBA::Any in_any1;
122 in_any1 <<= ts;
123 DynamicAny::DynAny_var dp1 =
124 dynany_factory->create_dyn_any (in_any1);
126 DynamicAny::DynSequence_var fa1 =
127 DynamicAny::DynSequence::_narrow (dp1.in ());
129 fa1->seek (1);
131 fa1->insert_string (data.m_string1);
133 fa1->rewind ();
135 fa1->seek (1);
137 CORBA::String_var out_str1 =
138 fa1->get_string ();
140 if (!ACE_OS::strcmp (out_str1.in (), data.m_string1))
142 ACE_DEBUG ((LM_DEBUG,
143 "++ OK ++\n"));
145 else
147 ++this->error_count_;
150 ACE_DEBUG ((
151 LM_DEBUG,
152 "testing: set_elements_as_dyn_any/get_elements_as_dyn_any/equal\n"
155 static const char *values[] =
157 "zero",
158 "one",
159 "two"
162 CORBA::ULong length = 3;
163 DynamicAny::DynAnySeq *elem_ptr = 0;
164 ACE_NEW_RETURN (elem_ptr,
165 DynamicAny::DynAnySeq (length),
166 -1);
167 elem_ptr->length (length);
168 DynamicAny::DynAnySeq_var elements (elem_ptr);
169 CORBA::ULong i;
171 for (i = 0; i < length; ++i)
173 CORBA::Any elem_any;
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_;
186 return -1;
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()))
196 error_count_++;
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";
211 CORBA::Any tmp;
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))
223 ACE_DEBUG((LM_DEBUG,
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,
235 "++ OK ++\n"));
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"),
252 -1);
255 ACE_DEBUG ((
256 LM_DEBUG,
257 ACE_TEXT ("testing: constructor(%C)/create_dyn_any_from_type_code\n"),
258 "BooleanSeq"
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 ()))
270 ACE_DEBUG ((
271 LM_DEBUG,
272 ACE_TEXT ("DynSequence::_narrow() returned nil\n")
274 this->error_count_++;
277 ACE_DEBUG ((
278 LM_DEBUG,
279 ACE_TEXT ("testing: constructor(%C)/create_dyn_any_from_type_code\n"),
280 "ShortSeq"
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 ()))
291 ACE_DEBUG ((
292 LM_DEBUG,
293 ACE_TEXT ("DynSequence::_narrow() returned nil\n")
295 this->error_count_++;
298 ts[0] = CORBA::string_dup (data.m_string1);
299 CORBA::Any in_any2;
300 in_any2 <<= ts;
301 ftc1->from_any (in_any2);
303 analyzer.analyze (ftc1.in());
305 CORBA::Any_var out_any1 =
306 ftc1->to_any ();
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,
314 "++ OK ++\n"));
316 else
318 ++this->error_count_;
321 ACE_DEBUG ((LM_DEBUG,
322 "testing: length/set_elements/get_elements\n"));
324 length = ftc1->get_length ();
326 if (length != 2)
328 ++this->error_count_;
331 ftc1->set_length (3);
332 DynamicAny::AnySeq as_in (3);
333 as_in.length (3);
334 CORBA::Any in_any3;
335 in_any3 <<= CORBA::Any::from_string (data.m_string2, 8);
336 as_in[0] = in_any3;
337 as_in[1] = in_any3;
338 in_any3 <<= CORBA::Any::from_string (data.m_string1, 8);
339 as_in[2] = in_any3;
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];
347 char* out_str2;
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,
358 "++ OK ++\n"));
361 fa1->destroy ();
363 ftc1->destroy ();
365 catch (const CORBA::Exception& ex)
367 ex._tao_print_exception ("test_dynsequence::run_test");
368 ++this->error_count_;
371 ACE_DEBUG ((LM_DEBUG,
372 "\n%d errors\n",
373 this->error_count_));
376 return !!error_count_;