1 // ============================================================================
3 * @file Integer_Truncate_Test.cpp
5 * Test @c ACE_Utils::truncate_cast<> function template.
7 * @author Ossama Othman <ossama_othman at symantec dot com>
9 // ============================================================================
11 #include "test_config.h"
13 #include <ace/Truncate.h>
14 #include <ace/Numeric_Limits.h>
19 using namespace ACE_Utils
;
21 // ----------------------------------------------------
24 sizeof_from_lt_sizeof_to ()
27 ACE_TEXT ("Running sizeof(FROM) < sizeof(TO) test\n")));
31 // signed from_type, unsigned to_type
33 using from_type
= signed char;
34 using to_type
= unsigned int;
36 ACE_TEST_ASSERT (sizeof (from_type
) < sizeof (to_type
));
39 ACE_Numeric_Limits
<from_type
>::max (); // Should not be truncated.
41 if (truncate_cast
<to_type
> (f
) != static_cast<to_type
> (f
))
46 ACE_TEXT ("\tsigned from_type / unsigned to_type ")
47 ACE_TEXT ("truncation test failed")));
51 // unsigned from_type, signed to_type
53 using from_type
= unsigned char;
56 ACE_TEST_ASSERT (sizeof (from_type
) < sizeof (to_type
));
59 ACE_Numeric_Limits
<from_type
>::max (); // Should not be truncated.
61 if (truncate_cast
<to_type
> (f
) != static_cast<to_type
> (f
))
66 ACE_TEXT ("\tunsigned from_type / signed to_type ")
67 ACE_TEXT ("truncation test failed")));
71 // signed from_type, signed to_type
73 using from_type
= signed char;
76 ACE_TEST_ASSERT (sizeof (from_type
) < sizeof (to_type
));
78 from_type f1
= -1; // Should not be truncated.
80 ACE_Numeric_Limits
<from_type
>::max (); // Should not be truncated.
82 if (truncate_cast
<to_type
> (f1
) != f1
83 || truncate_cast
<to_type
> (f2
) != f2
)
88 ACE_TEXT ("\tsigned from_type / signed to_type ")
89 ACE_TEXT ("truncation test failed")));
93 // unsigned from_type, unsigned to_type
95 using from_type
= unsigned char;
96 using to_type
= unsigned int;
98 ACE_TEST_ASSERT (sizeof (from_type
) < sizeof (to_type
));
101 ACE_Numeric_Limits
<from_type
>::max (); // Should not be truncated.
103 if (truncate_cast
<to_type
> (f
) != static_cast<to_type
> (f
))
107 ACE_ERROR ((LM_ERROR
,
108 ACE_TEXT ("\tunsigned from_type / unsigned to_type ")
109 ACE_TEXT ("truncation test failed")));
116 ? ACE_TEXT ("PASSED")
117 : ACE_TEXT ("FAILED")));
123 sizeof_from_eq_sizeof_to ()
126 ACE_TEXT ("Running sizeof(FROM) == sizeof(TO) test\n")));
130 // signed from_type, unsigned to_type
132 using from_type
= int;
133 using to_type
= unsigned int;
135 ACE_TEST_ASSERT (sizeof (from_type
) == sizeof (to_type
));
137 from_type f1
= -1; // Should not be truncated.
139 ACE_Numeric_Limits
<from_type
>::max (); // Should not be truncated.
141 if (static_cast<from_type
> (truncate_cast
<to_type
> (f1
)) != f1
142 || static_cast<from_type
> (truncate_cast
<to_type
> (f2
)) != f2
)
146 ACE_ERROR ((LM_ERROR
,
147 ACE_TEXT ("\tsigned from_type / unsigned to_type ")
148 ACE_TEXT ("truncation test failed")));
152 // unsigned from_type, signed to_type
154 using from_type
= unsigned int;
157 ACE_TEST_ASSERT (sizeof (from_type
) == sizeof (to_type
));
160 ACE_Numeric_Limits
<from_type
>::max (); // Should be truncated.
162 if (truncate_cast
<to_type
> (f
) != ACE_Numeric_Limits
<to_type
>::max ())
166 ACE_ERROR ((LM_ERROR
,
167 ACE_TEXT ("\tunsigned from_type / signed to_type ")
168 ACE_TEXT ("truncation test failed")));
172 // signed from_type, signed to_type
174 using from_type
= int;
177 ACE_TEST_ASSERT (sizeof (from_type
) == sizeof (to_type
));
179 from_type f1
= -1; // Should not be truncated.
181 ACE_Numeric_Limits
<from_type
>::max (); // Should not be truncated.
183 if (truncate_cast
<to_type
> (f1
) != f1
184 || truncate_cast
<to_type
> (f2
) != f2
)
188 ACE_ERROR ((LM_ERROR
,
189 ACE_TEXT ("\tsigned from_type / signed to_type ")
190 ACE_TEXT ("truncation test failed")));
194 // unsigned from_type, unsigned to_type
196 using from_type
= unsigned int;
197 using to_type
= unsigned int;
199 ACE_TEST_ASSERT (sizeof (from_type
) == sizeof (to_type
));
202 ACE_Numeric_Limits
<from_type
>::max (); // Should not be truncated.
204 if (truncate_cast
<to_type
> (f
) != f
)
208 ACE_ERROR ((LM_ERROR
,
209 ACE_TEXT ("\tunsigned from_type / unsigned to_type ")
210 ACE_TEXT ("truncation test failed")));
217 ? ACE_TEXT ("PASSED")
218 : ACE_TEXT ("FAILED")));
224 sizeof_from_gt_sizeof_to ()
227 ACE_TEXT ("Running sizeof(FROM) > sizeof(TO) test\n")));
231 // signed from_type, unsigned to_type
233 using from_type
= int;
234 using to_type
= unsigned char;
236 ACE_TEST_ASSERT (sizeof (from_type
) > sizeof (to_type
));
239 ACE_Numeric_Limits
<from_type
>::max (); // Should be truncated.
241 if (truncate_cast
<to_type
> (f
) != ACE_Numeric_Limits
<to_type
>::max ())
245 ACE_ERROR ((LM_ERROR
,
246 ACE_TEXT ("\tsigned from_type / unsigned to_type ")
247 ACE_TEXT ("truncation test failed")));
251 // unsigned from_type, signed to_type
253 using from_type
= unsigned int;
254 using to_type
= signed char;
256 ACE_TEST_ASSERT (sizeof (from_type
) > sizeof (to_type
));
259 ACE_Numeric_Limits
<from_type
>::max (); // Should be truncated.
261 if (truncate_cast
<to_type
> (f
) != ACE_Numeric_Limits
<to_type
>::max ())
265 ACE_ERROR ((LM_ERROR
,
266 ACE_TEXT ("\tunsigned from_type / signed to_type ")
267 ACE_TEXT ("truncation test failed")));
271 // signed from_type, signed to_type
273 using from_type
= int;
274 using to_type
= signed char;
276 ACE_TEST_ASSERT (sizeof (from_type
) > sizeof (to_type
));
278 from_type f1
= -1; // Should not be truncated.
280 ACE_Numeric_Limits
<from_type
>::max (); // Should be truncated.
282 if (truncate_cast
<to_type
> (f1
) != f1
283 || truncate_cast
<to_type
> (f2
) != ACE_Numeric_Limits
<to_type
>::max ())
287 ACE_ERROR ((LM_ERROR
,
288 ACE_TEXT ("\tsigned from_type / signed to_type ")
289 ACE_TEXT ("truncation test failed")));
293 // unsigned from_type, unsigned to_type
295 using from_type
= unsigned int;
296 using to_type
= unsigned char;
298 ACE_TEST_ASSERT (sizeof (from_type
) > sizeof (to_type
));
301 ACE_Numeric_Limits
<from_type
>::max (); // Should be truncated.
303 if (truncate_cast
<to_type
> (f
) != ACE_Numeric_Limits
<to_type
>::max ())
307 ACE_ERROR ((LM_ERROR
,
308 ACE_TEXT ("\tunsigned from_type / unsigned to_type ")
309 ACE_TEXT ("truncation test failed")));
316 ? ACE_TEXT ("PASSED")
317 : ACE_TEXT ("FAILED")));
322 // ----------------------------------------------------
327 * @brief Test method invocation functor.
329 * Test method invocation functor.
331 template <typename T
>
334 typedef T argument_type
;
335 typedef void result_type
;
338 Caller () : success (true) {}
340 /// Function call operator overload.
341 void operator() (T f
)
347 /// Flag that indicates success of all tests.
351 // ----------------------------------------------------
354 run_main (int, ACE_TCHAR
*[])
356 ACE_START_TEST (ACE_TEXT ("Integer_Truncate_Test"));
358 using test_func
= bool (*)();
360 static test_func
const tests
[] =
362 sizeof_from_lt_sizeof_to
363 , sizeof_from_eq_sizeof_to
364 , sizeof_from_gt_sizeof_to
367 static size_t const test_count
= sizeof (tests
) / sizeof (tests
[0]);
369 // Have some fun with the STL. :-)
370 Caller
<test_func
> c
=
371 std::for_each (tests
,
373 Caller
<test_func
> ());
377 return c
.success
? 0 : -1;