Skip to content

Instantly share code, notes, and snippets.

@wolf99
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolf99/cdf3ef408b662366a018 to your computer and use it in GitHub Desktop.
Save wolf99/cdf3ef408b662366a018 to your computer and use it in GitHub Desktop.
Macro to test if whole number data types (int, char) are of the same value, type size and signedness
/* Note: 'x' and 'y' arguments must be modifiable lvalues.
* Example usage:
* int16_t a = 5, b = 5;
* TEST_EQUAL_INT_T(int16_t, a, b);
*/
#include <stdint.h> /* Needed for uintmax_t */
#define TEST_EQUAL_INT_T(T, x, y) { uintmax_t _x = (x), _y = (y); /* Save values to temp vars */ \
if (((x) == (y)) /* Check if values are equal*/ \
&& (sizeof(x) == sizeof(T)) /* Check if type sizes match */ \
&& (sizeof(y) == sizeof(T)) \
&& ((x) = -1, (y) = -1, \
((((T)(-1)) < 0) && ((x) < 0) && ((y) < 0)) /* Check if type signedness match */ \
|| ((((T)(-1)) > 0) && ((x) > 0) && ((y) > 0)))) \
{ TEST_PASS; } else { TEST_FAIL; } \
(x) = _x, (y) = _y; /* Reset values from temp vars */ \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment