Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / DynAny_Test / test_dynenum.cpp
blob494522dc89dab6d8c944698b3d9fdc3d85963674
1 //=============================================================================
2 /**
3 * @file test_dynenum.cpp
5 * Implementation of the simple DynEnum test
7 * @author Jeff Parsons <parsons@cs.wustl.edu>
8 */
9 //=============================================================================
12 #include "test_dynenum.h"
13 #include "da_testsC.h"
14 #include "data.h"
15 #include "tao/DynamicAny/DynamicAny.h"
16 #include "analyzer.h"
17 #include "ace/OS_NS_string.h"
19 Test_DynEnum::Test_DynEnum (CORBA::ORB_var orb, int debug)
20 : orb_ (orb),
21 test_name_ (CORBA::string_dup ("test_dynenum")),
22 error_count_ (0),
23 debug_ (debug)
27 Test_DynEnum::~Test_DynEnum (void)
29 CORBA::string_free (this->test_name_);
30 this->test_name_ = 0;
33 const char*
34 Test_DynEnum::test_name (void) const
36 return this->test_name_;
39 int
40 Test_DynEnum::run_test (void)
42 Data data (this->orb_);
44 try
46 CORBA::Object_var factory_obj =
47 this->orb_->resolve_initial_references ("DynAnyFactory");
49 DynamicAny::DynAnyFactory_var dynany_factory =
50 DynamicAny::DynAnyFactory::_narrow (factory_obj.in ());
52 if (CORBA::is_nil (dynany_factory.in ()))
54 ACE_ERROR_RETURN ((LM_ERROR,
55 "Nil dynamic any factory after narrow\n"),
56 -1);
59 DynAnyAnalyzer analyzer (this->orb_.in(),
60 dynany_factory.in (),
61 debug_);
63 ACE_DEBUG ((LM_DEBUG,
64 "\t*=*=*=*= %C =*=*=*=*\n",
65 data.labels[18]));
66 ACE_DEBUG ((LM_DEBUG,
67 "testing: constructor(Any)/set_as_string/get_as_ulong\n"));
70 DynAnyTests::test_enum te = DynAnyTests::TE_ZEROTH;
71 CORBA::Any in_any1;
72 in_any1 <<= te;
73 DynamicAny::DynAny_var dp1 =
74 dynany_factory->create_dyn_any (in_any1);
76 DynamicAny::DynEnum_var de1 =
77 DynamicAny::DynEnum::_narrow (dp1.in ());
79 analyzer.analyze(dp1.in());
81 de1->set_as_string ("TE_FIRST");
83 CORBA::ULong ul_out1 =
84 de1->get_as_ulong ();
86 analyzer.analyze(de1.in());
88 CORBA::Any_var out_any2 =
89 de1->to_any ();
91 if (ul_out1 == 1)
93 ACE_DEBUG ((LM_DEBUG,
94 "++ OK ++\n"));
96 else
98 ++this->error_count_;
102 ACE_DEBUG ((LM_DEBUG,
103 "testing: set_as_ulong/get_as_string\n"));
105 de1->set_as_ulong (3);
106 CORBA::String_var s =
107 de1->get_as_string ();
109 if (ACE_OS::strcmp (s.in (), "TE_THIRD") == 0)
111 ACE_DEBUG ((LM_DEBUG,
112 "++ OK ++\n"));
114 else
116 ++this->error_count_;
120 ACE_DEBUG ((LM_DEBUG,
121 "testing: constructor(TypeCode)/from_any/to_any\n"));
123 DynamicAny::DynAny_var de2_base =
124 dynany_factory->create_dyn_any_from_type_code (DynAnyTests::_tc_test_enum);
126 DynamicAny::DynEnum_var de2 =
127 DynamicAny::DynEnum::_narrow (de2_base.in ());
129 if (CORBA::is_nil (de2.in ()))
131 ACE_ERROR_RETURN ((LM_ERROR,
132 "DynEnum::_narrow() returned nil\n"),
133 -1);
136 CORBA::Any in_any2;
137 in_any2 <<= DynAnyTests::TE_THIRD;
138 de2->from_any (in_any2);
140 analyzer.analyze(de2.in());
142 CORBA::Any_var out_any1 =
143 de2->to_any ();
145 out_any1.in () >>= te;
147 if (te == DynAnyTests::TE_THIRD)
149 ACE_DEBUG ((LM_DEBUG,
150 "++ OK ++\n"));
152 else
154 ++this->error_count_;
157 ACE_DEBUG ((LM_DEBUG,
158 "testing: equal\n"));
160 CORBA::Boolean equal =
161 de1->equal (de2.in ());
163 if (equal)
165 ACE_DEBUG ((LM_DEBUG,
166 "++ OK ++\n"));
168 else
170 ++this->error_count_;
173 de1->destroy ();
175 de2->destroy ();
178 ACE_DEBUG ((LM_DEBUG,
179 "\t*=*=*=*= %C =*=*=*=*\n",
180 data.labels[19]));
181 ACE_DEBUG ((LM_DEBUG,
182 "testing: set_as_string/get_as_ulong\n"));
184 // initialise instances
185 DynAnyTests::StructTypedefEnum l_struct_typedef_enum;
186 l_struct_typedef_enum.field_1 = DynAnyTests::TE_ZEROTH;
187 l_struct_typedef_enum.field_2 = DynAnyTests::TE_FIRST;
188 l_struct_typedef_enum.field_3 = DynAnyTests::TE_SECOND;
189 CORBA::Any l_any_struct_typedef_enum;
190 l_any_struct_typedef_enum <<= l_struct_typedef_enum;
192 DynamicAny::DynAny_var da
193 = dynany_factory->create_dyn_any (l_any_struct_typedef_enum);
194 DynamicAny::DynStruct_var ds =
195 DynamicAny::DynStruct::_narrow (da.in ());
196 DynamicAny::DynAny_var cc
197 = ds->current_component();
199 DynamicAny::FieldName_var fn
200 = ds->current_member_name();
202 DynamicAny::DynEnum_var member1
203 = DynamicAny::DynEnum::_narrow(cc.in ());
205 member1->set_as_string ("TE_FOURTH");
207 CORBA::ULong ul_out1 = member1->get_as_ulong ();
209 if (ul_out1 == 4)
211 ACE_DEBUG ((LM_DEBUG,
212 "++ OK ++\n"));
214 else
216 ++this->error_count_;
219 ACE_DEBUG ((LM_DEBUG,
220 "testing: set_as_ulong/get_as_string\n"));
222 member1->set_as_ulong (3);
224 CORBA::String_var s = member1->get_as_string ();
226 if (ACE_OS::strcmp (s.in (), "TE_THIRD") == 0)
228 ACE_DEBUG ((LM_DEBUG,
229 "++ OK ++\n"));
231 else
233 ++this->error_count_;
237 catch (const CORBA::Exception& ex)
239 ex._tao_print_exception ("test_dynenum::run_test");
240 ++this->error_count_;
243 ACE_DEBUG ((LM_DEBUG,
244 "\n%d errors\n",
245 this->error_count_));
247 return !!error_count_;