Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Alt_Mapping / helper.cpp
blobb1c28b87cc12caf860031bf5676cd52a423e9002
2 //=============================================================================
3 /**
4 * @file helper.cpp
6 * Defines a helper class that can generate values for the parameters used
7 * for the Alt_Mapping example
9 * @author Jeff Parsons
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 Alt_Mapping::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 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, Generator, ACE_Recursive_Thread_Mutex);