extract ck_assert_mem macros to separate file check_mem.h

pull/25/head
Pavol Rusnak 8 years ago
parent ed755120af
commit e6574f8eea
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -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

@ -21,13 +21,15 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <check.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <check.h>
#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;

Loading…
Cancel
Save