From c5933f4a81d5f1e89f15ccfdbbdfb11365a90c72 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Sat, 3 Jan 2015 18:04:21 +0100 Subject: [PATCH] Add and adapt buflib shrink tests Change-Id: I8aad86226c9c9b2c04727a3703941615638b3a49 --- firmware/test/buflib/Makefile | 6 +- firmware/test/buflib/test_shrink.c | 56 +++++++++ firmware/test/buflib/test_shrink_cb.c | 108 ++++++++++++++++++ .../test/buflib/test_shrink_startchanged.c | 59 ++++++++++ firmware/test/buflib/test_shrink_unaligned.c | 56 +++++++++ 5 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 firmware/test/buflib/test_shrink.c create mode 100644 firmware/test/buflib/test_shrink_cb.c create mode 100644 firmware/test/buflib/test_shrink_startchanged.c create mode 100644 firmware/test/buflib/test_shrink_unaligned.c diff --git a/firmware/test/buflib/Makefile b/firmware/test/buflib/Makefile index 247937b552..33191c6cbe 100644 --- a/firmware/test/buflib/Makefile +++ b/firmware/test/buflib/Makefile @@ -12,7 +12,11 @@ TARGETS_OBJ = test_main.o \ test_main2.o \ test_move.o \ test_move2.o \ - test_max.o + test_max.o \ + test_shrink.o \ + test_shrink_unaligned.o \ + test_shrink_startchanged.o \ + test_shrink_cb.o TARGETS = $(TARGETS_OBJ:.o=) diff --git a/firmware/test/buflib/test_shrink.c b/firmware/test/buflib/test_shrink.c new file mode 100644 index 0000000000..7424f56d51 --- /dev/null +++ b/firmware/test/buflib/test_shrink.c @@ -0,0 +1,56 @@ +/*************************************************************************** +* __________ __ ___. +* Open \______ \ ____ ____ | | _\_ |__ _______ ___ +* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +* \/ \/ \/ \/ \/ +* $Id$ +* +* Copyright (C) 2011 Thomas Martitz +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY +* KIND, either express or implied. +* +****************************************************************************/ +#include +#include "core_alloc.h" +#include "util.h" + +/* + * Expected output: +------------------- +0x602520: val: 1285 (first) +0x604d48: val: -32 () +0x604e48: val: 1221 (second) +0x607470: val: -32 () +0x607570: val: 1285 (third) +------------------- +*/ +struct buflib_callbacks ops; + +int main(void) +{ + UT_core_allocator_init(); + + int first = core_alloc("first", 10<<10); + int second = core_alloc("second", 10<<10); + int third = core_alloc("third", 10<<10); + + strcpy((char*)core_get_data(second)+0x100, "foobar"); + core_shrink(second, (char*)core_get_data(second)+0x100, (10<<10)-0x200); + core_print_blocks(&print_simple); + + int ret = strcmp(core_get_data(second), "foobar"); + + core_free(first); + core_free(second); + core_free(third); + + return ret; +} diff --git a/firmware/test/buflib/test_shrink_cb.c b/firmware/test/buflib/test_shrink_cb.c new file mode 100644 index 0000000000..8c2c02ed1c --- /dev/null +++ b/firmware/test/buflib/test_shrink_cb.c @@ -0,0 +1,108 @@ +/*************************************************************************** +* __________ __ ___. +* Open \______ \ ____ ____ | | _\_ |__ _______ ___ +* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +* \/ \/ \/ \/ \/ +* $Id$ +* +* Copyright (C) 2011 Thomas Martitz +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY +* KIND, either express or implied. +* +****************************************************************************/ +#include +#include +#include "core_alloc.h" +#include "util.h" + +/* + * Expected output (64-bit): +------------------- +MOVED! +0x6032e0: val: 1285 (first) +0x605b08: val: 1285 (third) +0x608330: val: 1413 (fourth) +0x60af58: val: 2181 (fifth) +SHRINK! 517 +MOVED! +0x6032e0: val: 1285 (first) +0x605b08: val: 645 (third) +0x606f30: val: 1413 (fourth) +0x609b58: val: 2181 (fifth) +0x60df80: val: 517 (sixth) +------------------- +*/ + +#define error(...) do { printf(__VA_ARGS__); exit(1); } while(0) +static int move_callback(int handle, void* old, void* new) +{ + printf("MOVED!\n"); + return BUFLIB_CB_OK; +} + +static int shrink_callback(int handle, unsigned hints, void* start, size_t size) +{ + char* buf = start; + size /= 2; + + printf("SHRINK! %u\n", hints); + + memmove(buf + size/2, buf, size); + if (core_shrink(handle, buf + size/2, size)) + { + return BUFLIB_CB_OK; + } + return BUFLIB_CB_CANNOT_SHRINK; +} + +struct buflib_callbacks ops = { + .move_callback = move_callback, + .shrink_callback = shrink_callback, +}; + +int main(void) +{ + UT_core_allocator_init(); + + int first = core_alloc("first", 10<<10); + int second = core_alloc("second", 10<<10); + int third = core_alloc_ex("third", 10<<10, &ops); + + strcpy(core_get_data(third), "third"); + + core_free(second); + int fourth = core_alloc("fourth", 11<<10); + strcpy(core_get_data(fourth), "fourth"); + + /* this should cause MOVED! twice */ + int fifth = core_alloc("fifth", 17<<10); + if (fifth <= 0) error("fifth failed\n"); + strcpy(core_get_data(fifth), "fifth"); + + core_print_blocks(&print_simple); + int sixth = core_alloc("sixth", 4<<10); + if (sixth <= 0) error("sixth failed\n"); + strcpy(core_get_data(sixth), "sixth"); + + core_print_blocks(&print_simple); + + int ret = strcmp(core_get_data(third), "third") + || strcmp(core_get_data(fourth), "fourth") + || strcmp(core_get_data(fifth), "fifth") + || strcmp(core_get_data(sixth), "sixth"); + core_free(first); + core_free(third); + core_free(fourth); + core_free(fifth); + core_free(sixth); + + return ret; +} diff --git a/firmware/test/buflib/test_shrink_startchanged.c b/firmware/test/buflib/test_shrink_startchanged.c new file mode 100644 index 0000000000..63b864db88 --- /dev/null +++ b/firmware/test/buflib/test_shrink_startchanged.c @@ -0,0 +1,59 @@ +/*************************************************************************** +* __________ __ ___. +* Open \______ \ ____ ____ | | _\_ |__ _______ ___ +* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +* \/ \/ \/ \/ \/ +* $Id$ +* +* Copyright (C) 2011 Thomas Martitz +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY +* KIND, either express or implied. +* +****************************************************************************/ +#include +#include "core_alloc.h" +#include "util.h" + +/* + * Expected output: +------------------- +0x602620: val: 1285 (first) +0x604e48: val: -32 () +0x604f48: val: 1253 (second) +0x607670: val: 1285 (third) +------------------- +*/ +struct buflib_callbacks ops; + +int main(void) +{ + UT_core_allocator_init(); + + int first = core_alloc("first", 10<<10); + int second = core_alloc("second", 10<<10); + int third = core_alloc("third", 10<<10); + + strcpy(core_get_data(third), "baz"); + + strcpy((char*)core_get_data(second)+0x102, "foobar"); + core_shrink(second, (char*)core_get_data(second)+0x102, (10<<10)-0x102); + memset(core_get_data(second) + sizeof("foobar"), 0, (10<<10)-0x102-sizeof("foobar")); + core_print_blocks(&print_simple); + + int ret = strcmp(core_get_data(second), "foobar") + || strcmp(core_get_data(third), "baz"); + + core_free(first); + core_free(second); + core_free(third); + + return ret; +} diff --git a/firmware/test/buflib/test_shrink_unaligned.c b/firmware/test/buflib/test_shrink_unaligned.c new file mode 100644 index 0000000000..0f07685f65 --- /dev/null +++ b/firmware/test/buflib/test_shrink_unaligned.c @@ -0,0 +1,56 @@ +/*************************************************************************** +* __________ __ ___. +* Open \______ \ ____ ____ | | _\_ |__ _______ ___ +* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +* \/ \/ \/ \/ \/ +* $Id$ +* +* Copyright (C) 2011 Thomas Martitz +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY +* KIND, either express or implied. +* +****************************************************************************/ +#include +#include "core_alloc.h" +#include "util.h" + +/* + * Expected output: +------------------- +0x602520: val: 1285 (first) +0x604d48: val: -32 () +0x604e48: val: 1222 (second) +0x607470: val: -31 () +0x607570: val: 1285 (third) +------------------- +*/ +struct buflib_callbacks ops; + +int main(void) +{ + UT_core_allocator_init(); + + int first = core_alloc("first", 10<<10); + int second = core_alloc("second", 10<<10); + int third = core_alloc("third", 10<<10); + + strcpy((char*)core_get_data(second)+0x102, "foobar"); + core_shrink(second, (char*)core_get_data(second)+0x102, (10<<10)-0x200); + core_print_blocks(&print_simple); + + int ret = strcmp(core_get_data(second), "foobar"); + + core_free(first); + core_free(second); + core_free(third); + + return ret; +}