Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / interop-tests / wchar / client.cpp
blob663e04382132c0ed0fd610b5fab07371f6f87ac5
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file client.cpp
7 * C++ client side for testing interoperability with wchar data.
9 * @author Phil Mesnier <mesnier_p@ociweb.com>
11 //=============================================================================
14 #include "interop_wcharC.h"
15 #include "wchar_reference.h"
17 #include "ace/Get_Opt.h"
18 #include "ace/Argv_Type_Converter.h"
20 const int WCHAR_TO_SERVER = 0x00000001;
21 const int WSTRING_TO_SERVER = 0x00000002;
22 const int WARRAY_TO_SERVER = 0x00000004;
23 const int ANY_WCHAR_TO_SERVER = 0x00000008;
24 const int WSTRING_EXCEPTION = 0x00000010;
25 const int WCHAR_FROM_SERVER = 0x00000020;
26 const int WSTRING_FROM_SERVER = 0x00000040;
27 const int WARRAY_FROM_SERVER = 0x00000080;
28 const int ANY_WCHAR_FROM_SERVER = 0x00000100;
29 const int ANY_WSTRING_TO_SERVER = 0x00000200;
30 const int ANY_WSTRING_FROM_SERVER = 0x00000400;
31 const int ANY_WSTRING_ECHO = 0x00000800;
32 const int WSTRUCTSEQ_FROM_SERVER = 0x00001000;
33 const int WSTRUCTSEQ_TO_SERVER = 0x00002000;
34 const int NUL_WSTRING_TO_SERVER = 0x00004000;
35 const int TBD_16 = 0x00008000; // update ALL_TESTS if this is defined
36 const int TBD_15 = 0x00010000; // update ALL_TESTS if this is defined
37 const int TBD_14 = 0x00020000; // update ALL_TESTS if this is defined
38 const int TBD_13 = 0x00040000; // update ALL_TESTS if this is defined
39 const int TBD_12 = 0x00080000; // update ALL_TESTS if this is defined
40 const int TBD_11 = 0x00100000; // update ALL_TESTS if this is defined
41 const int TBD_10 = 0x00200000; // update ALL_TESTS if this is defined
42 const int TBD_9 = 0x00400000; // update ALL_TESTS if this is defined
43 const int TBD_8 = 0x00800000; // update ALL_TESTS if this is defined
44 const int TBD_7 = 0x01000000; // update ALL_TESTS if this is defined
45 const int TBD_6 = 0x02000000; // update ALL_TESTS if this is defined
46 const int TBD_5 = 0x04000000; // update ALL_TESTS if this is defined
47 const int TBD_4 = 0x08000000; // update ALL_TESTS if this is defined
48 const int TBD_3 = 0x10000000; // update ALL_TESTS if this is defined
49 const int TBD_2 = 0x20000000; // update ALL_TESTS if this is defined
50 const int TBD_1 = 0x40000000; // update ALL_TESTS if this is defined
51 const int TBD_0 = 0x80000000; // update ALL_TESTS if this is defined
53 const int ALL_TESTS = 0x00007FFF;
55 // The length of this array determines which tests are run for "all tests"
56 const char * test_name[] =
58 "wchar_to_server",
59 "wstring_to_server",
60 "warray_to_server",
61 "any(wchar)_to_server",
62 "wstring_exception",
63 "wchar_from_server",
64 "wstring_from_server",
65 "warray_from_server",
66 "any(wchar)_from_server",
67 "any(wstring)_to_server",
68 "any(wstring)_from_server",
69 "any(wstring)_echo",
70 "wstructseq_from_server",
71 "wstructseq_to_server",
72 "nul_wstring_to_server"
75 const int LAST_TEST = sizeof (test_name) / sizeof (test_name[0]);
76 const ACE_TCHAR *ior = ACE_TEXT("file://IOR");
77 int tests_to_run = 0;
78 int verbose = 0;
79 int kill_server = 0;
80 int data_set = 0;
82 #if defined (ACE_HAS_WCHAR) || defined (ACE_HAS_XPG4_MULTIBYTE_CHAR)
84 wchar_reference ref;
86 void
87 assign_wstruct (short key, interop::wstruct & ws)
89 ws.st_char = ref.get_wchar(key);
90 ws.st_string = CORBA::wstring_dup(ref.get_wstring(key));
91 ref.assign_warray(key, ws.st_array);
92 ws.st_any <<= CORBA::wstring_dup(ref.get_wstring(key));
96 CORBA::Boolean
97 run_one_test (interop::WChar_Passer_ptr server,
98 int test_num)
100 switch (test_num)
102 case WCHAR_TO_SERVER:
103 return server->wchar_to_server (ref.get_wchar(data_set), data_set);
104 case WSTRING_TO_SERVER:
105 return server->wstring_to_server (ref.get_wstring(data_set), data_set);
106 case WARRAY_TO_SERVER:
107 return server->warray_to_server (ref.get_warray(data_set), data_set);
108 case ANY_WCHAR_TO_SERVER:
110 CORBA::Any a;
111 a <<= CORBA::Any::from_wchar(ref.get_wchar(data_set));
112 return server->any_to_server (a,data_set);
114 case WSTRING_EXCEPTION:
118 server->exception_test(data_set);
120 catch (const interop::WChar_Passer::WStringException& e)
122 return ref.match_except (data_set,e.why.in());
124 break;
126 case WCHAR_FROM_SERVER:
128 CORBA::WChar wc = server->wchar_from_server (data_set);
129 return ref.match_wchar (data_set,wc);
131 case WSTRING_FROM_SERVER:
133 CORBA::WString_var ws =
134 server->wstring_from_server (data_set);
135 return ref.match_wstring (data_set,ws.in());
137 case WARRAY_FROM_SERVER:
139 interop::warray_var wa =
140 server->warray_from_server (data_set);
141 return ref.match_warray (data_set,wa.in());
143 case ANY_WCHAR_FROM_SERVER:
145 CORBA::WChar wc;
146 CORBA::Any_var test = server->any_from_server (data_set,
147 interop::is_wchar);
148 if (test >>= CORBA::Any::to_wchar(wc))
150 return ref.match_wchar (data_set,wc);
152 return 0;
154 case ANY_WSTRING_TO_SERVER:
156 CORBA::Any a;
157 a <<= ref.get_wstring(data_set);
158 return server->any_to_server (a,data_set);
160 case ANY_WSTRING_FROM_SERVER:
162 const CORBA::WChar *ws;
163 CORBA::Any_var test = server->any_from_server (data_set,
164 interop::is_wstring);
165 if (test >>= ws)
167 return ref.match_wstring (data_set,ws);
169 return 0;
171 case ANY_WSTRING_ECHO:
173 CORBA::Any a;
174 a <<= ref.get_wstring(data_set);
175 const CORBA::WChar *ws;
176 CORBA::Any_var test = server->any_echo (a);
177 if (test >>= ws)
179 return ref.match_wstring (data_set,ws);
181 return 0;
183 case WSTRUCTSEQ_FROM_SERVER:
185 interop::wstructseq_var wsList =
186 server->wstructseq_from_server(data_set);
187 int result = 1;
189 for (CORBA::ULong i = 0; i < wsList->length(); i++)
190 result &= ref.match_wstring (data_set,
191 wsList[i].st_string.in());
193 return result;
195 case WSTRUCTSEQ_TO_SERVER:
197 interop::wstructseq_var wsList = new interop::wstructseq;
198 wsList->length(5);
199 for (CORBA::ULong i = 0; i < wsList->length(); ++i)
201 assign_wstruct(data_set, (*wsList)[i]);
203 return server->wstructseq_to_server(wsList.in(), data_set);
205 case NUL_WSTRING_TO_SERVER:
206 return server->wstring_to_server (L"", -1);
207 default:
208 break;
210 return 0;
213 short
214 run_tests (interop::WChar_Passer_ptr server)
216 short successes = 0;
217 short numtests = 0;
218 ref.set_verbose (verbose);
219 int t = 1;
220 for (int i = 0; i < LAST_TEST; i++, t <<= 1)
221 if ((tests_to_run & t) == t)
223 CORBA::Boolean result = run_one_test (server,t);
224 ++numtests;
225 if (result) ++successes;
226 if (verbose)
227 ACE_DEBUG ((LM_DEBUG, "%s[%d] %s\n",
228 test_name[i],data_set,
229 (result ? "passed" : "failed")));
231 if (verbose)
232 ACE_DEBUG ((LM_DEBUG, "Total of %d successes out of %d tests\n",
233 successes, numtests));
234 return successes == numtests;
236 #endif // ACE_HAS_WCHAR || ACE_HAS_XPG4_MULTIBYTE_CHAR
239 parse_args (int argc, ACE_TCHAR *argv[])
241 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:t:vx"));
242 int c;
243 while ((c = get_opts ()) != -1)
244 switch (c)
246 case 'k':
247 ior = get_opts.opt_arg ();
248 break;
249 case 't':
251 int tnum = ACE_OS::atoi(get_opts.opt_arg());
252 if (tnum >= 0 && tnum < LAST_TEST)
253 tests_to_run |= (1 << tnum);
254 else
255 ACE_ERROR_RETURN ((LM_ERROR,"test %d is out of range\n",tnum),-1);
256 break;
258 case 'v':
259 verbose = 1;
260 break;
261 case 'x':
262 kill_server = 1;
263 break;
264 case '?':
265 default:
267 ACE_ERROR ((LM_ERROR,
268 "usage: %s "
269 "-k <ior> "
270 "[-t <0 <= test_num < %d>][ -t ...] "
271 "[-v] "
272 "[-x] "
273 "\n",
274 argv [0], LAST_TEST));
275 ACE_ERROR ((LM_ERROR, "Available tests:\n"));
276 for (int i = 0; i < LAST_TEST; i++)
277 ACE_ERROR ((LM_ERROR, " %d - %s\n",i, test_name[i]));
278 return -1;
282 if (tests_to_run == 0)
283 tests_to_run = ALL_TESTS;
284 // Indicates successful parsing of the command line
285 return 0;
289 ACE_TMAIN( int argc, ACE_TCHAR *argv[] )
291 #if (!defined ACE_HAS_WCHAR) && (!defined ACE_HAS_XPG4_MULTIBYTE_CHAR)
292 ACE_UNUSED_ARG (argc);
293 ACE_UNUSED_ARG (argv);
294 ACE_ERROR_RETURN ((LM_ERROR,ACE_TEXT ("This test requires wchar support\n")),0);
295 #else
298 // Initialize orb
299 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
300 if (parse_args(argc, argv) == -1)
301 return 0;
303 // Destringify ior
304 CORBA::Object_var obj = orb->string_to_object(ior);
305 if( CORBA::is_nil( obj.in() ) )
306 ACE_ERROR_RETURN ((LM_ERROR,
307 "arg is not a valid ior sting"),
308 -1);
310 // Narrow
311 interop::WChar_Passer_var server =
312 interop::WChar_Passer::_narrow( obj.in());
314 if( CORBA::is_nil( server.in() ))
315 ACE_ERROR_RETURN ((LM_ERROR,
316 "arg is not a interop::WChar_Passer reference\n"),
317 -1);
319 short result = run_tests (server.in());
320 CORBA::String_var server_orb =
321 server->orb_name();
322 ACE_ERROR ((LM_ERROR,
323 "wchar_interop test (TAO client, %C server) %C\n",
324 server_orb.in(),
325 (result ? "passed" : "failed")));
326 if (kill_server)
328 server->shutdown();
331 catch (const CORBA::Exception& ex)
333 ex._tao_print_exception ("Uncaught CORBA exception: ");
334 return 1;
336 return 0;
337 #endif