ACE+TAO-6_5_17
[ACE_TAO.git] / TAO / tests / Param_Test / recursive_union.cpp
blob131bd96766a7cec77ad259773e3394c337950aac
2 //=============================================================================
3 /**
4 * @file recursive_union.cpp
6 * test union that contains a sequence of itself
8 * @author Jeff Parsons <parsons@cs.wustl.edu>
9 */
10 //=============================================================================
13 #include "recursive_union.h"
15 const CORBA::ULong MAX_DEPTH = 3;
16 const CORBA::ULong MAX_SEQ_LENGTH = 2;
18 // ************************************************************************
19 // Test_Recursive_Union
20 // ************************************************************************
22 Test_Recursive_Union::Test_Recursive_Union (void)
23 : opname_ (CORBA::string_dup ("test_recursive_union"))
27 Test_Recursive_Union::~Test_Recursive_Union (void)
29 CORBA::string_free (this->opname_);
30 this->opname_ = 0;
31 // the other data members will be freed as they are "_var"s and objects
32 // (rather than pointers to objects)
35 const char *
36 Test_Recursive_Union::opname (void) const
38 return this->opname_;
41 void
42 Test_Recursive_Union::dii_req_invoke (CORBA::Request *req)
44 req->add_in_arg ("s1") <<= this->in_;
45 req->add_inout_arg ("s2") <<= this->inout_.in ();
46 req->add_out_arg ("s3") <<= this->out_.in ();
48 req->set_return_type (Param_Test::_tc_Recursive_Union);
50 req->invoke ();
52 const Param_Test::Recursive_Union *tmp = 0;
53 req->return_value () >>= tmp;
54 this->ret_ = new Param_Test::Recursive_Union (*tmp);
56 CORBA::NamedValue_ptr o2 =
57 req->arguments ()->item (1);
58 *o2->value () >>= tmp;
59 this->inout_ = new Param_Test::Recursive_Union (*tmp);
61 CORBA::NamedValue_ptr o3 =
62 req->arguments ()->item (2);
63 *o3->value () >>= tmp;
64 this->out_ = new Param_Test::Recursive_Union (*tmp);
67 int
68 Test_Recursive_Union::init_parameters (Param_Test_ptr)
70 // The client calls init_parameters() before the first
71 // call and reset_parameters() after each call. For this
72 // test, we want the same thing to happen each time.
73 return this->reset_parameters ();
76 int
77 Test_Recursive_Union::reset_parameters (void)
79 // Since these are _vars, we do this the first call and
80 // every call thereafter (if any).
81 this->inout_ = new Param_Test::Recursive_Union;
82 this->out_ = new Param_Test::Recursive_Union;
83 this->ret_ = new Param_Test::Recursive_Union;
85 // value generator
86 Generator *gen = GENERATOR::instance ();
88 // Set the depth of recursion.
89 CORBA::ULong depth =
90 (CORBA::ULong) (gen->gen_long () % MAX_DEPTH) + 1;
92 // Create a nested union to put in inout_.
93 Param_Test::nested_rec_union nru;
95 nru.value (0);
97 this->inout_->nested_member (nru);
99 // Call the recursive helper function. to initialize in_.
100 this->deep_init (this->in_,
101 gen,
102 depth);
104 return 0;
108 Test_Recursive_Union::run_sii_test (Param_Test_ptr objref)
112 Param_Test::Recursive_Union_out out (this->out_.out ());
114 this->ret_ = objref->test_recursive_union (this->in_,
115 this->inout_.inout (),
116 out);
118 return 0;
120 catch (const CORBA::Exception& ex)
122 ex._tao_print_exception ("Test_Recursive_Union::run_sii_test\n");
125 return -1;
128 CORBA::Boolean
129 Test_Recursive_Union::check_validity (void)
131 // Pair in_ with each of the returned values and call the
132 // helper function with that pair.
134 if (this->deep_check (this->in_, this->inout_.in ()) == 0)
136 ACE_DEBUG ((LM_DEBUG,
137 "mismatch of inout arg\n"));
139 return 0;
142 if (this->deep_check (this->in_, this->out_.in ()) == 0)
144 ACE_DEBUG ((LM_DEBUG,
145 "mismatch of out arg\n"));
147 return 0;
150 if (this->deep_check (this->in_, this->ret_.in ()) == 0)
152 ACE_DEBUG ((LM_DEBUG,
153 "mismatch of ret value\n"));
155 return 0;
158 // If we get this far, all is correct.
159 return 1;
162 CORBA::Boolean
163 Test_Recursive_Union::check_validity (CORBA::Request_ptr)
165 return this->check_validity ();
168 void
169 Test_Recursive_Union::print_values (void)
173 // Private helper function to recursively initialize the union.
174 void
175 Test_Recursive_Union::deep_init (Param_Test::Recursive_Union &ru,
176 Generator *gen,
177 CORBA::ULong level)
179 if (level == 1)
180 // No more recursion, just insert a nested_rec_union.
182 CORBA::ULong nested_depth =
183 (CORBA::ULong) (gen->gen_long () % MAX_DEPTH) + 1;
185 Param_Test::nested_rec_union nru;
187 this->deep_init_nested (nru,
188 gen,
189 nested_depth);
191 ru.nested_member (nru);
193 Param_Test::RecUnionSeq tmp (MAX_SEQ_LENGTH);
195 ru.rec_member (tmp);
197 return;
199 else
201 // Generate a member sequence.
202 CORBA::ULong len =
203 (CORBA::ULong) (gen->gen_long () % MAX_SEQ_LENGTH) + 1;
205 // This line is TAO-specific, but some compilers we support
206 // are broken in their handling of the portable scoped typedef
207 // required by CORBA 2.3
208 Param_Test::RecUnionSeq tmp (MAX_SEQ_LENGTH);
210 tmp.length (len);
212 ru.rec_member (tmp);
214 // We recurse for each element of the member sequence.
215 for (CORBA::ULong i = 0; i < len; i++)
217 this->deep_init (ru.rec_member ()[i],
218 gen,
219 level - 1);
224 // Private helper function to recursively initialize the nested union.
225 void
226 Test_Recursive_Union::deep_init_nested (Param_Test::nested_rec_union &nu,
227 Generator *gen,
228 CORBA::ULong level)
230 if (level == 1)
231 // No more recursion
233 nu.value (gen->gen_long ());
235 return;
237 else
239 // Generate a sequence length.
240 CORBA::ULong len =
241 (CORBA::ULong) (gen->gen_long () % MAX_SEQ_LENGTH) + 1;
243 // This line is TAO-specific, but some compilers we support
244 // are broken in their handling of the portable scoped typedef
245 // required by CORBA 2.3
246 Param_Test::NestedSeq tmp (MAX_SEQ_LENGTH);
248 tmp.length (len);
250 nu.nested_rec_member (tmp);
252 // We recurse for each element of the member sequence.
253 for (CORBA::ULong i = 0; i < len; i++)
255 this->deep_init_nested (nu.nested_rec_member ()[i],
256 gen,
257 level - 1);
262 // Private helper function for check_validity (so we can recurse).
263 CORBA::Boolean
264 Test_Recursive_Union::deep_check (const Param_Test::Recursive_Union &in_union,
265 const Param_Test::Recursive_Union &test_union)
267 // Do the discriminators match?
268 if (in_union._d () != test_union._d ())
270 ACE_DEBUG ((LM_DEBUG,
271 "mismatch of Recursive_Union discriminators\n"));
273 return 0;
276 switch (in_union._d ())
278 // Active member is the Recursive_Union sequence.
279 case 0:
281 // Do the sequence lengths match?
282 if (in_union.rec_member ().length () !=
283 test_union.rec_member ().length ())
285 ACE_DEBUG ((LM_DEBUG,
286 "mismatch of Recursive_Union member sequence lengths\n"));
288 return 0;
291 for (CORBA::ULong i = 0; i < in_union.rec_member ().length (); i++)
293 if (!this->deep_check (in_union.rec_member ()[i],
294 test_union.rec_member ()[i]))
296 ACE_DEBUG ((LM_DEBUG,
297 "mismatch of contained Recursive_Unions\n"));
299 return 0;
303 break;
306 // Active member is the nested union.
307 case 1:
308 return this->deep_check_nested (in_union.nested_member (),
309 test_union.nested_member ());
311 default:
312 ACE_DEBUG ((LM_DEBUG,
313 "bad discriminator value\n"));
315 break;
318 return 1;
321 CORBA::Boolean
322 Test_Recursive_Union::deep_check_nested (const Param_Test::nested_rec_union &in,
323 const Param_Test::nested_rec_union &test)
325 if (in._d () != test._d ())
327 ACE_DEBUG ((LM_DEBUG,
328 "mismatch of nested union discriminators\n"));
330 return 0;
333 switch (in._d ())
335 // Active member is the long integer.
336 case 0:
337 // Do the nested_rec_union member values match?
338 if (in.value () != test.value ())
340 ACE_DEBUG ((LM_DEBUG,
341 "mismatch of nested_rec_union member values\n"));
343 return 0;
346 break;
348 // Active member is the recursive sequence.
349 case 1:
351 // Do the sequence lengths match?
352 if (in.nested_rec_member ().length () !=
353 test.nested_rec_member ().length ())
355 ACE_DEBUG ((LM_DEBUG,
356 "mismatch of nested_rec_union member sequence lengths\n"));
358 return 0;
361 for (CORBA::ULong i = 0; i < in.nested_rec_member ().length (); i++)
363 if (!this->deep_check_nested (in.nested_rec_member ()[i],
364 test.nested_rec_member ()[i]))
366 ACE_DEBUG ((LM_DEBUG,
367 "mismatch of contained nested_rec_unions\n"));
369 return 0;
375 return 1;