Last active
August 29, 2015 14:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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