Improve macros used in tests

* Be more verbose in which argument is `should` and which is `is`.
* Fix the order in the `ENSURE_EQ()` macro.

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
This commit is contained in:
Steffen Jaeckel
2023-07-26 12:47:46 +02:00
parent 9735af515a
commit 53d8eb17b6
2 changed files with 49 additions and 47 deletions

View File

@@ -35,46 +35,48 @@
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif #endif
#define COMPARE(v1, v2) \ #define COMPARE(should, is) \
do { \ do { \
const char *__v1 = v1; \ const char *__should = should; \
const char *__v2 = v2; \ const char *__is = is; \
if ((__v1 == NULL) && (__v2 == NULL)) { \ if ((__should == NULL) && (__is == NULL)) { \
/* noop */ \ /* noop */ \
} else if (!__v1 || !__v2 || strcmp(__v1, __v2) != 0) { \ } else if (!__should || !__is || strcmp(__should, __is) != 0) { \
printf("Error: %s\n" \ printf("Error: %s\n" \
"Expected: %s\n" \ "Expected: %s\n" \
"Got: %s\n", \ "Got: %s\n", \
#v1 " != " #v2, __v1, __v2); \ #should " != " #is, __should, __is); \
exit(1); \ exit(1); \
} \ } \
} while (0) } while (0)
#define COMPARE_BUF(v1, len1, v2, len2) \ #define COMPARE_BUF(should, should_len, is, is_len) \
do { \ do { \
const uint8_t *__v1 = (uint8_t *)(v1); \ const uint8_t *__should = (uint8_t *)(should); \
const uint8_t *__v2 = (uint8_t *)(v2); \ const uint8_t *__is = (uint8_t *)(is); \
size_t __len1 = len1; \ size_t __should_len = should_len; \
size_t __len2 = len2; \ size_t __is_len = is_len; \
if (__len1 != __len2 || memcmp(__v1, __v2, __len1) != 0) { \ if (__should_len != __is_len || \
printf("Error: %s\n", #v1 " != " #v2); \ memcmp(__should, __is, __should_len) != 0) { \
printf("Expected: 0x%s\n", test_bin_to_hex(__v1, __len1)); \ printf("Error: %s\n", #should " != " #is); \
printf("Got: 0x%s\n", test_bin_to_hex(__v2, __len2)); \ printf("Expected: 0x%s\n", \
exit(1); \ test_bin_to_hex(__should, __should_len)); \
} \ printf("Got: 0x%s\n", test_bin_to_hex(__is, __is_len)); \
exit(1); \
} \
} while (0) } while (0)
#define ENSURE_EQ(v1, v2) \ #define ENSURE_EQ(should, is) \
do { \ do { \
int __v1 = v1; \ int __should = should; \
int __v2 = v2; \ int __is = is; \
if (__v1 != __v2) { \ if (__should != __is) { \
printf("Error: %s\n" \ printf("Error: %s\n" \
"Expected: %d\n" \ "Expected: %d\n" \
"Got: %d\n", \ "Got: %d\n", \
#v1 " != " #v2, __v2, __v1); \ #should " != " #is, __is, __should); \
exit(1); \ exit(1); \
} \ } \
} while (0) } while (0)
void test_hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len); void test_hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len);

View File

@@ -40,48 +40,48 @@ int main()
xmpp_conn_set_sm_state(conn, sm_state); xmpp_conn_set_sm_state(conn, sm_state);
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 0); ENSURE_EQ(0, xmpp_conn_send_queue_len(conn));
state = conn->state; state = conn->state;
conn->state = XMPP_STATE_CONNECTED; conn->state = XMPP_STATE_CONNECTED;
xmpp_send_raw(conn, "foo", 3); xmpp_send_raw(conn, "foo", 3);
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 1); ENSURE_EQ(1, xmpp_conn_send_queue_len(conn));
xmpp_send_raw(conn, "bar", 3); xmpp_send_raw(conn, "bar", 3);
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 2); ENSURE_EQ(2, xmpp_conn_send_queue_len(conn));
xmpp_send_raw(conn, "baz", 3); xmpp_send_raw(conn, "baz", 3);
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 3); ENSURE_EQ(3, xmpp_conn_send_queue_len(conn));
xmpp_send_raw(conn, "baan", 4); xmpp_send_raw(conn, "baan", 4);
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 4); ENSURE_EQ(4, xmpp_conn_send_queue_len(conn));
conn->send_queue_head->wip = 1; conn->send_queue_head->wip = 1;
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 3); ENSURE_EQ(3, xmpp_conn_send_queue_len(conn));
ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_OLDEST); ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_OLDEST);
COMPARE("bar", ret); COMPARE("bar", ret);
xmpp_free(ctx, ret); xmpp_free(ctx, ret);
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 2); ENSURE_EQ(2, xmpp_conn_send_queue_len(conn));
conn->send_queue_head->wip = 0; conn->send_queue_head->wip = 0;
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 3); ENSURE_EQ(3, xmpp_conn_send_queue_len(conn));
ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_OLDEST); ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_OLDEST);
COMPARE("foo", ret); COMPARE("foo", ret);
xmpp_free(ctx, ret); xmpp_free(ctx, ret);
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 2); ENSURE_EQ(2, xmpp_conn_send_queue_len(conn));
ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_YOUNGEST); ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_YOUNGEST);
COMPARE("baan", ret); COMPARE("baan", ret);
xmpp_free(ctx, ret); xmpp_free(ctx, ret);
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 1); ENSURE_EQ(1, xmpp_conn_send_queue_len(conn));
ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_YOUNGEST); ret = xmpp_conn_send_queue_drop_element(conn, XMPP_QUEUE_YOUNGEST);
COMPARE("baz", ret); COMPARE("baz", ret);
xmpp_free(ctx, ret); xmpp_free(ctx, ret);
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 0); ENSURE_EQ(0, xmpp_conn_send_queue_len(conn));
conn->state = state; conn->state = state;