Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / tests / NDDS_Timer_Test.cpp
blobd25ce5c0b9b2167de0e82e810dae2d9b35637384
1 //=============================================================================
2 /**
3 * @file NDDS_Timer_Test.cpp
5 * Simple test of NDDS_Timer. Copied from NDDS_Timer_Test.
7 * @author Marcel Smit <msmit@remedy.nl>
8 */
9 //=============================================================================
12 #include "test_config.h"
13 #include "ace/Log_Msg.h"
14 #include "ace/Sched_Params.h"
15 #include "ace/Get_Opt.h"
16 #include "ace/OS_NS_unistd.h"
18 #include <ndds/ndds_cpp.h>
19 #include <ndds/clock/clock_highResolution.h>
21 static
22 u_int
23 check (const time_t interval, const time_t measured)
25 time_t const threshold = 25 /* percent */;
27 time_t const difference =
28 interval > measured ? interval - measured : measured - interval;
30 time_t const percentage_difference = difference * 100 / interval;
32 if (percentage_difference < threshold)
33 return 0;
34 else
35 ACE_ERROR_RETURN ((LM_ERROR,
36 ACE_TEXT ("The measured time of %Q differs from ")
37 ACE_TEXT ("the interval of %Q by %Q percent.\n"),
38 measured,
39 interval,
40 percentage_difference),
41 1);
44 // Does a sanity check of the microseconds vs nanoseconds. They're supposed
45 // to represent the same interval.
46 static u_int
47 check_micro_nano (unsigned long long microinterval, unsigned long long nanointerval)
49 unsigned long long const threshold = 8 /* promille */;
51 microinterval *= 1000u;
52 unsigned long long const difference = (microinterval > nanointerval
53 ? (microinterval - nanointerval)
54 : (nanointerval - microinterval));
55 if (nanointerval == 0)
57 nanointerval = 1; // Prevent divide-by-zero
59 unsigned long long const promille_difference =
60 difference * 1000 / nanointerval;
62 if ((promille_difference < threshold) || (difference < 1500))
64 return 0;
66 else
67 ACE_ERROR_RETURN ((LM_ERROR,
68 ACE_TEXT ("The microseconds * 1000 of %Q ")
69 ACE_TEXT ("differs from the nanoseconds of %Q ")
70 ACE_TEXT (" by %Q promille\n"),
71 microinterval,
72 nanointerval,
73 promille_difference),
74 1);
77 static
78 void
79 time_interval (RTIClock *timer,
80 const ACE_Time_Value & interval,
81 RTINtpTime& duration)
83 struct RTINtpTime start_time;
84 struct RTINtpTime finish_time;
86 RTINtpTime_setZero(&start_time);
87 RTINtpTime_setZero(&finish_time);
89 // Initialize
90 timer->reset(timer);
92 // Run the test
93 timer->getTime(timer, &start_time);
94 ACE_OS::sleep (interval);
95 timer->getTime(timer, &finish_time);
97 // Measure.
98 RTINtpTime_subtract(duration, finish_time, start_time);
102 static u_int const intervals[] =
103 {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 4000000}; /*usec*/
106 run_main (int argc, ACE_TCHAR *argv[])
108 ACE_START_TEST (ACE_TEXT ("NDDS_Timer_Test"));
110 RTIClock *timer = RTIHighResolutionClock_new();
112 u_int errors = 0;
114 u_int iterations = 1;
116 ACE_Get_Opt getoptarg (argc, argv, ACE_TEXT ("i:"));
117 for (int c; (c = getoptarg ()) != -1; )
119 switch (c)
121 case 'i':
122 iterations = ACE_OS::atoi (getoptarg.opt_arg ());
123 break;
127 // We don't check for errors if the interval is shorter than this
128 // value because the OS has a finite resolution anyway.
129 static u_int const TIMER_RESOLUTION = 10000;
131 for (u_int i = 0; i < sizeof intervals / sizeof (u_int); ++i)
133 for (u_int j = 0; j < iterations; ++j)
135 ACE_Time_Value const interval (0, intervals[i]);
137 RTINtpTime duration;
138 time_interval (timer, interval, duration);
140 unsigned long long microseconds, nanoseconds;
141 int sec;
142 RTINtpTime_unpackToMicrosec (sec, microseconds, duration);
143 RTINtpTime_unpackToNanosec (sec, nanoseconds, duration);
145 time_t const interval_usec =
146 interval.sec () * ACE_ONE_SECOND_IN_USECS + interval.usec ();
147 time_t const measured_usec =
148 sec * ACE_ONE_SECOND_IN_USECS + microseconds;
151 ACE_DEBUG ((LM_DEBUG,
152 ACE_TEXT ("interval: %: usec, measured: %: usec %s\n"),
153 interval_usec,
154 measured_usec,
155 (intervals[i] <= TIMER_RESOLUTION
156 ? ACE_TEXT (" (interval and measured may differ)")
157 : ACE_TEXT (""))));
159 if (intervals[i] > TIMER_RESOLUTION)
161 errors += check (interval.sec () * ACE_ONE_SECOND_IN_USECS
162 + interval.usec (),
163 sec * ACE_ONE_SECOND_IN_USECS + microseconds);
164 // Don't check for error for intervals below 10 msec.
166 errors += check_micro_nano (microseconds, nanoseconds);
167 ACE_DEBUG ((LM_DEBUG,
168 ACE_TEXT ("NDDS_Timer usec: %Q, nsec: %Q\n"),
169 microseconds,
170 nanoseconds));
174 ACE_END_TEST;
175 return errors == 0 ? 0 : 1;