Continued changes from peer review
[ACE_TAO.git] / TAO / tests / Param_Test / helper.cpp
blob8e5b49caf59670b14d001676c702e24b37914891
2 //=============================================================================
3 /**
4 * @file helper.cpp
6 * Defines a helper class that can generate values for the parameters used
7 * for the Param_Test example
9 * @author Aniruddha Gokhale
11 //=============================================================================
14 #include "helper.h"
15 #include "ace/OS_NS_ctype.h"
17 const CORBA::ULong TEST_BUFSIZE = 128;
19 Generator::Generator (void)
23 Generator::~Generator (void)
27 CORBA::Short
28 Generator::gen_short (void)
30 return (CORBA::Short) (ACE_OS::rand () % TEST_BUFSIZE);
33 CORBA::Long
34 Generator::gen_long (void)
36 return ::ACE_OS::rand () % TEST_BUFSIZE;
39 char *
40 Generator::gen_string (void)
42 return gen_string (TEST_BUFSIZE);
45 char *
46 Generator::gen_string (int max_length)
48 CORBA::ULong len = (CORBA::ULong) (::ACE_OS::rand () % max_length);
49 char *buf = CORBA::string_alloc (len);
50 CORBA::ULong i = 0;
52 while (i < len)
54 int c = ACE_OS::rand () % 128;
55 if (ACE_OS::ace_isprint (c) && !ACE_OS::ace_isspace (c))
57 buf [i] = c;
58 i++;
62 buf[i] = 0;
63 return buf;
66 CORBA::WChar *
67 Generator::gen_wstring (void)
69 return gen_wstring (TEST_BUFSIZE);
72 CORBA::WChar *
73 Generator::gen_wstring (int max_length)
75 CORBA::ULong len = (CORBA::ULong) (::ACE_OS::rand () % max_length);
76 CORBA::WChar *buf = CORBA::wstring_alloc (len);
77 CORBA::ULong i = 0;
78 CORBA::WChar limit =
79 ACE_OutputCDR::wchar_maxbytes() == 1 ? ACE_OCTET_MAX : ACE_WCHAR_MAX;
80 while (i < len)
82 CORBA::WChar wc = ACE_OS::rand () % limit;
83 if (wc)
85 buf[i] = wc;
86 i++;
90 buf[i] = 0;
91 return buf;
94 const Param_Test::Fixed_Struct
95 Generator::gen_fixed_struct (void)
97 this->fixed_struct_.l = ACE_OS::rand ();
98 this->fixed_struct_.c = ACE_OS::rand () % 128;
99 this->fixed_struct_.s = (CORBA::Short) ACE_OS::rand ();
100 this->fixed_struct_.o = ACE_OS::rand () % 128;
101 this->fixed_struct_.f = (CORBA::Float) (ACE_OS::rand () * 1.0);
102 this->fixed_struct_.b = (CORBA::Boolean) (ACE_OS::rand () % 2);
103 this->fixed_struct_.d = (ACE_OS::rand () * 1.0);
104 return this->fixed_struct_;
107 const Param_Test::Step
108 Generator::gen_step (void)
110 this->step_.name.id = this->gen_string ();
111 this->step_.name.kind = this->gen_string ();
112 this->step_.process = (CORBA::Boolean) (ACE_OS::rand () % 2);
113 return this->step_;
116 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, Generator, ACE_Recursive_Thread_Mutex);