Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Integer_Truncate_Test.cpp
blobff15888cc73109a228e148cb544ae6b8931eda68
1 // ============================================================================
2 /**
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>
8 */
9 // ============================================================================
11 #include "test_config.h"
13 #include <ace/Truncate.h>
14 #include <ace/Numeric_Limits.h>
16 #include <algorithm>
17 #include <functional>
19 using namespace ACE_Utils;
21 // ----------------------------------------------------
23 bool
24 sizeof_from_lt_sizeof_to ()
26 ACE_DEBUG ((LM_INFO,
27 ACE_TEXT ("Running sizeof(FROM) < sizeof(TO) test\n")));
29 bool success = true;
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));
38 from_type f =
39 ACE_Numeric_Limits<from_type>::max (); // Should not be truncated.
41 if (truncate_cast<to_type> (f) != static_cast<to_type> (f))
43 success = false;
45 ACE_ERROR ((LM_ERROR,
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;
54 using to_type = int;
56 ACE_TEST_ASSERT (sizeof (from_type) < sizeof (to_type));
58 from_type f =
59 ACE_Numeric_Limits<from_type>::max (); // Should not be truncated.
61 if (truncate_cast<to_type> (f) != static_cast<to_type> (f))
63 success = false;
65 ACE_ERROR ((LM_ERROR,
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;
74 using to_type = int;
76 ACE_TEST_ASSERT (sizeof (from_type) < sizeof (to_type));
78 from_type f1 = -1; // Should not be truncated.
79 from_type f2 =
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)
85 success = false;
87 ACE_ERROR ((LM_ERROR,
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));
100 from_type f =
101 ACE_Numeric_Limits<from_type>::max (); // Should not be truncated.
103 if (truncate_cast<to_type> (f) != static_cast<to_type> (f))
105 success = false;
107 ACE_ERROR ((LM_ERROR,
108 ACE_TEXT ("\tunsigned from_type / unsigned to_type ")
109 ACE_TEXT ("truncation test failed")));
113 ACE_DEBUG ((LM_INFO,
114 ACE_TEXT ("\t%s\n"),
115 success
116 ? ACE_TEXT ("PASSED")
117 : ACE_TEXT ("FAILED")));
119 return success;
122 bool
123 sizeof_from_eq_sizeof_to ()
125 ACE_DEBUG ((LM_INFO,
126 ACE_TEXT ("Running sizeof(FROM) == sizeof(TO) test\n")));
128 bool success = true;
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.
138 from_type f2 =
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)
144 success = false;
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;
155 using to_type = int;
157 ACE_TEST_ASSERT (sizeof (from_type) == sizeof (to_type));
159 from_type f =
160 ACE_Numeric_Limits<from_type>::max (); // Should be truncated.
162 if (truncate_cast<to_type> (f) != ACE_Numeric_Limits<to_type>::max ())
164 success = false;
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;
175 using to_type = int;
177 ACE_TEST_ASSERT (sizeof (from_type) == sizeof (to_type));
179 from_type f1 = -1; // Should not be truncated.
180 from_type f2 =
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)
186 success = false;
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));
201 from_type f =
202 ACE_Numeric_Limits<from_type>::max (); // Should not be truncated.
204 if (truncate_cast<to_type> (f) != f)
206 success = false;
208 ACE_ERROR ((LM_ERROR,
209 ACE_TEXT ("\tunsigned from_type / unsigned to_type ")
210 ACE_TEXT ("truncation test failed")));
214 ACE_DEBUG ((LM_INFO,
215 ACE_TEXT ("\t%s\n"),
216 success
217 ? ACE_TEXT ("PASSED")
218 : ACE_TEXT ("FAILED")));
220 return success;
223 bool
224 sizeof_from_gt_sizeof_to ()
226 ACE_DEBUG ((LM_INFO,
227 ACE_TEXT ("Running sizeof(FROM) > sizeof(TO) test\n")));
229 bool success = true;
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));
238 from_type f =
239 ACE_Numeric_Limits<from_type>::max (); // Should be truncated.
241 if (truncate_cast<to_type> (f) != ACE_Numeric_Limits<to_type>::max ())
243 success = false;
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));
258 from_type f =
259 ACE_Numeric_Limits<from_type>::max (); // Should be truncated.
261 if (truncate_cast<to_type> (f) != ACE_Numeric_Limits<to_type>::max ())
263 success = false;
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.
279 from_type f2 =
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 ())
285 success = false;
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));
300 from_type f =
301 ACE_Numeric_Limits<from_type>::max (); // Should be truncated.
303 if (truncate_cast<to_type> (f) != ACE_Numeric_Limits<to_type>::max ())
305 success = false;
307 ACE_ERROR ((LM_ERROR,
308 ACE_TEXT ("\tunsigned from_type / unsigned to_type ")
309 ACE_TEXT ("truncation test failed")));
313 ACE_DEBUG ((LM_INFO,
314 ACE_TEXT ("\t%s\n"),
315 success
316 ? ACE_TEXT ("PASSED")
317 : ACE_TEXT ("FAILED")));
319 return success;
322 // ----------------------------------------------------
325 * @struct Caller
327 * @brief Test method invocation functor.
329 * Test method invocation functor.
331 template <typename T>
332 struct Caller
334 typedef T argument_type;
335 typedef void result_type;
337 /// Constructor
338 Caller () : success (true) {}
340 /// Function call operator overload.
341 void operator() (T f)
343 if (!f ())
344 success = false;
347 /// Flag that indicates success of all tests.
348 bool success;
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,
372 tests + test_count,
373 Caller<test_func> ());
375 ACE_END_TEST;
377 return c.success ? 0 : -1;