1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

test/cn-cbor: Use memarray as block allocator

This commit is contained in:
Koen Zandberg 2018-04-16 15:27:06 +02:00
parent ad8e69da96
commit 355c559ae9
No known key found for this signature in database
GPG Key ID: BA1718B37D79F51C
2 changed files with 35 additions and 6 deletions

View File

@ -1,2 +1,3 @@
USEPKG += cn-cbor
USEMODULE += fmt
USEMODULE += memarray

View File

@ -18,14 +18,17 @@
*/
#define EBUF_SIZE 32
#define NUM_BLOCKS 7
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "assert.h"
#include "cn-cbor/cn-cbor.h"
#include "embUnit.h"
#include "fmt.h"
#include "memarray.h"
typedef struct {
char *hex;
@ -36,19 +39,44 @@ static size_t test, offs;
static unsigned char ebuf[EBUF_SIZE];
static cn_cbor_errback errb;
/* Dummy context struct to force fallback to POSIX calloc/free */
/* Block allocator */
static cn_cbor block_storage_data[NUM_BLOCKS];
static memarray_t storage;
/* calloc/free functions */
static void *cbor_calloc(size_t count, size_t size, void *memblock);
static void cbor_free(void *ptr, void *memblock);
/* CN_CBOR block allocator context struct*/
static cn_cbor_context ct =
{
NULL,
NULL,
NULL,
.calloc_func = cbor_calloc,
.free_func = cbor_free,
.context = &storage,
};
static void *cbor_calloc(size_t count, size_t size, void *memblock)
{
(void)count;
assert(count == 1); /* Count is always 1 with cn-cbor */
void *block = memarray_alloc(memblock);
if (block) {
memset(block, 0, size);
}
return block;
}
static void cbor_free(void *ptr, void *memblock)
{
memarray_free(memblock, ptr);
}
static void setup_cn_cbor(void)
{
test = 0;
offs = 0;
memset(ebuf, '\0', EBUF_SIZE);
memarray_init(&storage, block_storage_data, sizeof(cn_cbor), NUM_BLOCKS);
}
static void test_parse(void)
@ -123,7 +151,7 @@ static void test_parse(void)
for (offs = 0; offs < len; offs++) {
TEST_ASSERT_EQUAL_INT(buf[offs], ebuf[offs]);
}
cn_cbor_free(cbor);
cn_cbor_free(cbor, &ct);
}
}
@ -154,7 +182,7 @@ static void test_errors(void)
cn_cbor *cbor = cn_cbor_decode(buf, len, &ct, &errb);
TEST_ASSERT_NULL(cbor);
TEST_ASSERT_EQUAL_INT(errb.err, tests[offs].err);
cn_cbor_free(cbor);
cn_cbor_free(cbor, &ct);
}
}