Continued changes from peer review
[ACE_TAO.git] / TAO / tests / Param_Test / recursive_struct.cpp
blobe1c09e1bce0b96da477eb801d6fb45b7b5b0437c
2 //=============================================================================
3 /**
4 * @file recursive_struct.cpp
6 * test structure that contains a sequence of itself
8 * @author Aniruddha Gokhale
9 * @author Jeff Parsons
11 //=============================================================================
14 #include "recursive_struct.h"
16 const CORBA::ULong MAX_DEPTH = 5;
17 const CORBA::ULong MAX_SEQ_LENGTH = 3;
19 // ************************************************************************
20 // Test_Recursive_Struct
21 // ************************************************************************
23 Test_Recursive_Struct::Test_Recursive_Struct (void)
24 : opname_ (CORBA::string_dup ("test_recursive_struct"))
28 Test_Recursive_Struct::~Test_Recursive_Struct (void)
30 CORBA::string_free (this->opname_);
31 this->opname_ = 0;
32 // the other data members will be freed as they are "_var"s and objects
33 // (rather than pointers to objects)
36 const char *
37 Test_Recursive_Struct::opname (void) const
39 return this->opname_;
42 void
43 Test_Recursive_Struct::dii_req_invoke (CORBA::Request *req)
45 req->add_in_arg ("s1") <<= this->in_;
46 req->add_inout_arg ("s2") <<= this->inout_.in ();
47 req->add_out_arg ("s3") <<= this->out_.in ();
49 req->set_return_type (Param_Test::_tc_Recursive_Struct);
51 req->invoke ();
53 const Param_Test::Recursive_Struct *tmp = 0;
54 req->return_value () >>= tmp;
55 this->ret_ = new Param_Test::Recursive_Struct (*tmp);
57 CORBA::NamedValue_ptr o2 =
58 req->arguments ()->item (1);
59 *o2->value () >>= tmp;
60 this->inout_ = new Param_Test::Recursive_Struct (*tmp);
62 CORBA::NamedValue_ptr o3 =
63 req->arguments ()->item (2);
64 *o3->value () >>= tmp;
65 this->out_ = new Param_Test::Recursive_Struct (*tmp);
68 int
69 Test_Recursive_Struct::init_parameters (Param_Test_ptr)
71 // The client calls init_parameters() before the first
72 // call and reset_parameters() after each call. For this
73 // test, we want the same thing to happen each time.
74 return this->reset_parameters ();
77 int
78 Test_Recursive_Struct::reset_parameters (void)
80 // Since these are _vars, we do this the first call and
81 // every call thereafter (if any).
82 this->inout_ = new Param_Test::Recursive_Struct;
83 this->out_ = new Param_Test::Recursive_Struct;
84 this->ret_ = new Param_Test::Recursive_Struct;
86 // value generator
87 Generator *gen = GENERATOR::instance ();
89 // Set the depth of recursion.
90 CORBA::ULong depth = (CORBA::ULong) (gen->gen_long () % MAX_DEPTH) + 1;
92 // No recursion for inout_ until after the call.
93 this->inout_->children.length (0);
95 // Keeps Purify happy.
96 this->inout_->x = 0;
98 // Call the recursive helper function.
99 this->deep_init (this->in_,
100 gen,
101 depth);
103 return 0;
107 Test_Recursive_Struct::run_sii_test (Param_Test_ptr objref)
111 Param_Test::Recursive_Struct_out out (this->out_.out ());
113 this->ret_ = objref->test_recursive_struct (this->in_,
114 this->inout_.inout (),
115 out);
117 return 0;
119 catch (const CORBA::Exception& ex)
121 ex._tao_print_exception ("Test_Recursive_Struct::run_sii_test\n");
124 return -1;
127 CORBA::Boolean
128 Test_Recursive_Struct::check_validity (void)
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_Struct::check_validity (CORBA::Request_ptr)
164 return this->check_validity ();
167 void
168 Test_Recursive_Struct::print_values (void)
172 // Private helper function to recursively initialize the struct.
173 void
174 Test_Recursive_Struct::deep_init (Param_Test::Recursive_Struct &rs,
175 Generator *gen,
176 CORBA::ULong level)
178 rs.x = gen->gen_long ();
180 if (level == 1)
181 // No more recursion.
183 rs.children.length (0);
185 return;
187 else
189 // Generate a sequence length.
190 CORBA::ULong len = (CORBA::ULong) (gen->gen_long () % MAX_SEQ_LENGTH) + 1;
192 rs.children.length (len);
194 // We recurse for each element of the member sequence.
195 for (CORBA::ULong i = 0; i < len; i++)
197 this->deep_init (rs.children[i],
198 gen,
199 level - 1);
204 // Private helper function for check_validity (so we can recurse).
205 CORBA::Boolean
206 Test_Recursive_Struct::deep_check (const Param_Test::Recursive_Struct &in_struct,
207 const Param_Test::Recursive_Struct &test_struct)
209 // Do the CORBA::Long members match?
210 if (in_struct.x != test_struct.x)
212 ACE_DEBUG ((LM_DEBUG,
213 "mismatch of CORBA::Long struct members\n"));
215 return 0;
218 // Do the sequence lengths match?
219 if (in_struct.children.length () != test_struct.children.length ())
221 ACE_DEBUG ((LM_DEBUG,
222 "mismatch of member sequence lengths\n"));
224 return 0;
227 // At the bottom level, the length is 0 and we skip this part.
228 // Otherwise recurse.
229 for (CORBA::ULong i = 0; i < in_struct.children.length (); i++)
231 if (!this->deep_check (in_struct.children[i],
232 test_struct.children[i]))
234 ACE_DEBUG ((LM_DEBUG,
235 "mismatch of contained structs\n"));
237 return 0;
241 return 1;