diff --git a/tests/memarray/Makefile b/tests/memarray/Makefile new file mode 100644 index 0000000000..7076703cf1 --- /dev/null +++ b/tests/memarray/Makefile @@ -0,0 +1,8 @@ +include ../Makefile.tests_common +USEMODULE += memarray + +# Used for invoking _ps_handler +USEMODULE += shell_commands +USEMODULE += ps + +include $(RIOTBASE)/Makefile.include diff --git a/tests/memarray/README.md b/tests/memarray/README.md new file mode 100644 index 0000000000..7ff2aa234c --- /dev/null +++ b/tests/memarray/README.md @@ -0,0 +1,18 @@ +Expected result +=============== + +This application should run a number of tests equal to NUMBER_OF_TESTS (Default 12). + +At the beginning of the tests memory usage of each thread is printed. +At the end of the tests, the threads memory usage is printed once more time. + +This test is passed if the memory used by the main thread remains static. + +Background +========== + +The module `memarray` is the fixed-size block allocator for RIOT-OS. + +This test application is therefore specialized for only testing the use of the module. + +This is also inspired by `test/malloc`. diff --git a/tests/memarray/main.c b/tests/memarray/main.c new file mode 100644 index 0000000000..9b68d44af4 --- /dev/null +++ b/tests/memarray/main.c @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2018 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Simple memarray module tests + * + * @author Tobias Heider + * @author Raul Fuentes + * + * @} + */ + +#include +#include +#include + +#include "memarray.h" + +#define MAX_NUMBER_BLOCKS (10) +#define MESSAGE_SIZE (8U) +#define NUMBER_OF_TESTS (12) + +extern int _ps_handler(int argc, char **argv); + +struct block_t { + struct node *next; + int number; + /* static size for the components */ + unsigned char message[MESSAGE_SIZE]; +}; + +struct block_t block_storage_data[MAX_NUMBER_BLOCKS]; +memarray_t block_storage; + +int total = 0; + +static void memory_block_init(void) +{ + memarray_init(&block_storage, block_storage_data, sizeof(struct block_t), MAX_NUMBER_BLOCKS); +} + +void fill_memory(struct block_t *head) +{ + int aux = 0; + + while ((aux < MAX_NUMBER_BLOCKS) && (head)) { + memset(head->message, '@', MESSAGE_SIZE - 1); + head->message[MESSAGE_SIZE - 1] = 0; + head->number = aux; + + printf("\t(%i, %s) Allocated %u Bytes at %p, total %d\n", + head->number, head->message, (unsigned)sizeof(struct block_t), + (void *)head, total); + + /* NOTE: If there is not space, memarray_alloc returns zero */ + head->next = memarray_alloc(&block_storage); + head = (struct block_t *)head->next; + + total += sizeof(struct block_t); + aux++; + } +} + +void free_memory(struct block_t *head) +{ + struct block_t *old; + + while (head) { + total -= sizeof(struct block_t); + printf("\tFree (%i) %u Bytes at %p, total %d\n", \ + head->number, (unsigned)sizeof(struct block_t), + (void *)head, total); + + if (head->next) { + old = head; + head = (struct block_t *) head->next; + memarray_free(&block_storage, old); + } + else { + memarray_free(&block_storage, head); + head = 0; + } + } +} + +int main(void) +{ + memory_block_init(); + int count = 0; + + printf("Starting (%d, %u)\n", MAX_NUMBER_BLOCKS, MESSAGE_SIZE); + _ps_handler(0, NULL); + + while (count < NUMBER_OF_TESTS) { + struct block_t *head = (struct block_t *) memarray_alloc(&block_storage); + + printf("TEST #%i:\n", count + 1 ); + fill_memory(head); + free_memory(head); + + count++; + } + + printf("Finishing\n"); + _ps_handler(0, NULL); + + return 0; +}