From e6574f8eea98efb13fec98cbfab30733cc107b10 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 19 Oct 2016 20:42:50 +0200 Subject: [PATCH] extract ck_assert_mem macros to separate file check_mem.h --- check_mem.h | 26 ++++++++++++++++++++++++++ tests.c | 26 +++----------------------- 2 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 check_mem.h diff --git a/check_mem.h b/check_mem.h new file mode 100644 index 0000000000..a66751e211 --- /dev/null +++ b/check_mem.h @@ -0,0 +1,26 @@ +#ifndef CHECK_MEM_H +#define CHECK_MEM_H + +#define _ck_assert_mem(X, Y, L, OP) do { \ + const char* _ck_x = (const char*)(void*)(X); \ + const char* _ck_y = (const char*)(void*)(Y); \ + size_t _ck_l = (L); \ + char _ck_x_str[129]; \ + char _ck_y_str[129]; \ + static char _ck_hexdigits[] = "0123456789abcdef"; \ + size_t _ck_i; \ + for (_ck_i = 0; _ck_i < ((_ck_l > 64) ? 64 : _ck_l); _ck_i++) { \ + _ck_x_str[_ck_i * 2 ] = _ck_hexdigits[(_ck_x[_ck_i] >> 4) & 0xF]; \ + _ck_y_str[_ck_i * 2 ] = _ck_hexdigits[(_ck_y[_ck_i] >> 4) & 0xF]; \ + _ck_x_str[_ck_i * 2 + 1] = _ck_hexdigits[_ck_x[_ck_i] & 0xF]; \ + _ck_y_str[_ck_i * 2 + 1] = _ck_hexdigits[_ck_y[_ck_i] & 0xF]; \ + } \ + _ck_x_str[_ck_i * 2] = 0; \ + _ck_y_str[_ck_i * 2] = 0; \ + ck_assert_msg(0 OP memcmp(_ck_y, _ck_x, _ck_l), \ + "Assertion '"#X#OP#Y"' failed: "#X"==\"%s\", "#Y"==\"%s\"", _ck_x_str, _ck_y_str); \ +} while (0) +#define ck_assert_mem_eq(X, Y, L) _ck_assert_mem(X, Y, L, ==) +#define ck_assert_mem_ne(X, Y, L) _ck_assert_mem(X, Y, L, !=) + +#endif diff --git a/tests.c b/tests.c index 3a492db10c..632b361473 100644 --- a/tests.c +++ b/tests.c @@ -21,13 +21,15 @@ * OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include #include #include +#include +#include "check_mem.h" + #include "aes.h" #include "bignum.h" #include "base58.h" @@ -61,28 +63,6 @@ uint8_t *fromhex(const char *str) return buf; } -char *tohex(const uint8_t *bin, size_t l) -{ - char *buf = (char *)malloc(l * 2 + 1); - static char digits[] = "0123456789abcdef"; - for (size_t i = 0; i < l; i++) { - buf[i*2 ] = digits[(bin[i] >> 4) & 0xF]; - buf[i*2+1] = digits[bin[i] & 0xF]; - } - buf[l * 2] = 0; - return buf; -} - -#define _ck_assert_mem(X, Y, L, OP) do { \ - const void* _ck_x = (X); \ - const void* _ck_y = (Y); \ - size_t _ck_l = (L); \ - ck_assert_msg(0 OP memcmp(_ck_y, _ck_x, _ck_l), \ - "Assertion '"#X#OP#Y"' failed: "#X"==\"%s\", "#Y"==\"%s\"", tohex(_ck_x, _ck_l), tohex(_ck_y, _ck_l)); \ -} while (0) -#define ck_assert_mem_eq(X, Y, L) _ck_assert_mem(X, Y, L, ==) -#define ck_assert_mem_ne(X, Y, L) _ck_assert_mem(X, Y, L, !=) - START_TEST(test_bignum_read_be) { bignum256 a;