=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / tests / Param_Test / recursive_union.cpp
blobca65bc1ffaae4b5a66a7551cc1c0f8b77f288366
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 ()
23 : opname_ (CORBA::string_dup ("test_recursive_union"))
27 Test_Recursive_Union::~Test_Recursive_Union ()
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 () 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 ()
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");
124 return -1;
127 CORBA::Boolean
128 Test_Recursive_Union::check_validity ()
130 // Pair in_ with each of the returned values and call the
131 // helper function with that pair.
133 if (this->deep_check (this->in_, this->inout_.in ()) == 0)
135 ACE_DEBUG ((LM_DEBUG,
136 "mismatch of inout arg\n"));
138 return 0;
141 if (this->deep_check (this->in_, this->out_.in ()) == 0)
143 ACE_DEBUG ((LM_DEBUG,
144 "mismatch of out arg\n"));
146 return 0;
149 if (this->deep_check (this->in_, this->ret_.in ()) == 0)
151 ACE_DEBUG ((LM_DEBUG,
152 "mismatch of ret value\n"));
154 return 0;
157 // If we get this far, all is correct.
158 return 1;
161 CORBA::Boolean
162 Test_Recursive_Union::check_validity (CORBA::Request_ptr)
164 return this->check_validity ();
167 void
168 Test_Recursive_Union::print_values ()
172 // Private helper function to recursively initialize the union.
173 void
174 Test_Recursive_Union::deep_init (Param_Test::Recursive_Union &ru,
175 Generator *gen,
176 CORBA::ULong level)
178 if (level == 1)
179 // No more recursion, just insert a nested_rec_union.
181 CORBA::ULong nested_depth =
182 (CORBA::ULong) (gen->gen_long () % MAX_DEPTH) + 1;
184 Param_Test::nested_rec_union nru;
186 this->deep_init_nested (nru,
187 gen,
188 nested_depth);
190 ru.nested_member (nru);
192 Param_Test::RecUnionSeq tmp (MAX_SEQ_LENGTH);
194 ru.rec_member (tmp);
196 return;
198 else
200 // Generate a member sequence.
201 CORBA::ULong len =
202 (CORBA::ULong) (gen->gen_long () % MAX_SEQ_LENGTH) + 1;
204 // This line is TAO-specific, but some compilers we support
205 // are broken in their handling of the portable scoped typedef
206 // required by CORBA 2.3
207 Param_Test::RecUnionSeq tmp (MAX_SEQ_LENGTH);
209 tmp.length (len);
211 ru.rec_member (tmp);
213 // We recurse for each element of the member sequence.
214 for (CORBA::ULong i = 0; i < len; i++)
216 this->deep_init (ru.rec_member ()[i],
217 gen,
218 level - 1);
223 // Private helper function to recursively initialize the nested union.
224 void
225 Test_Recursive_Union::deep_init_nested (Param_Test::nested_rec_union &nu,
226 Generator *gen,
227 CORBA::ULong level)
229 if (level == 1)
230 // No more recursion
232 nu.value (gen->gen_long ());
234 return;
236 else
238 // Generate a sequence length.
239 CORBA::ULong len =
240 (CORBA::ULong) (gen->gen_long () % MAX_SEQ_LENGTH) + 1;
242 // This line is TAO-specific, but some compilers we support
243 // are broken in their handling of the portable scoped typedef
244 // required by CORBA 2.3
245 Param_Test::NestedSeq tmp (MAX_SEQ_LENGTH);
247 tmp.length (len);
249 nu.nested_rec_member (tmp);
251 // We recurse for each element of the member sequence.
252 for (CORBA::ULong i = 0; i < len; i++)
254 this->deep_init_nested (nu.nested_rec_member ()[i],
255 gen,
256 level - 1);
261 // Private helper function for check_validity (so we can recurse).
262 CORBA::Boolean
263 Test_Recursive_Union::deep_check (const Param_Test::Recursive_Union &in_union,
264 const Param_Test::Recursive_Union &test_union)
266 // Do the discriminators match?
267 if (in_union._d () != test_union._d ())
269 ACE_DEBUG ((LM_DEBUG,
270 "mismatch of Recursive_Union discriminators\n"));
272 return 0;
275 switch (in_union._d ())
277 // Active member is the Recursive_Union sequence.
278 case 0:
280 // Do the sequence lengths match?
281 if (in_union.rec_member ().length () !=
282 test_union.rec_member ().length ())
284 ACE_DEBUG ((LM_DEBUG,
285 "mismatch of Recursive_Union member sequence lengths\n"));
287 return 0;
290 for (CORBA::ULong i = 0; i < in_union.rec_member ().length (); i++)
292 if (!this->deep_check (in_union.rec_member ()[i],
293 test_union.rec_member ()[i]))
295 ACE_DEBUG ((LM_DEBUG,
296 "mismatch of contained Recursive_Unions\n"));
298 return 0;
302 break;
305 // Active member is the nested union.
306 case 1:
307 return this->deep_check_nested (in_union.nested_member (),
308 test_union.nested_member ());
310 default:
311 ACE_DEBUG ((LM_DEBUG,
312 "bad discriminator value\n"));
314 break;
317 return 1;
320 CORBA::Boolean
321 Test_Recursive_Union::deep_check_nested (const Param_Test::nested_rec_union &in,
322 const Param_Test::nested_rec_union &test)
324 if (in._d () != test._d ())
326 ACE_DEBUG ((LM_DEBUG,
327 "mismatch of nested union discriminators\n"));
329 return 0;
332 switch (in._d ())
334 // Active member is the long integer.
335 case 0:
336 // Do the nested_rec_union member values match?
337 if (in.value () != test.value ())
339 ACE_DEBUG ((LM_DEBUG,
340 "mismatch of nested_rec_union member values\n"));
342 return 0;
345 break;
347 // Active member is the recursive sequence.
348 case 1:
350 // Do the sequence lengths match?
351 if (in.nested_rec_member ().length () !=
352 test.nested_rec_member ().length ())
354 ACE_DEBUG ((LM_DEBUG,
355 "mismatch of nested_rec_union member sequence lengths\n"));
357 return 0;
360 for (CORBA::ULong i = 0; i < in.nested_rec_member ().length (); i++)
362 if (!this->deep_check_nested (in.nested_rec_member ()[i],
363 test.nested_rec_member ()[i]))
365 ACE_DEBUG ((LM_DEBUG,
366 "mismatch of contained nested_rec_unions\n"));
368 return 0;
374 return 1;