diff options
author | Thomas Jarosch <tomj@simonv.com> | 2015-01-03 16:34:32 +0100 |
---|---|---|
committer | Thomas Jarosch <tomj@simonv.com> | 2015-01-03 18:15:07 +0100 |
commit | e13801625634809a3b16bc41911d180b9497336d (patch) | |
tree | 1d8445a56ed210947f669943a935a56dfb6685a9 /firmware | |
parent | eb9c845db55eb048f6027e19f7016b20d2827f47 (diff) |
Initial import of simple buflib unit test
Taken from kugel's out-of-tree version:
https://github.com/kugel-/buflib
Needs some API adaptions. More test will follow.
Note: The expected output needs an update since
the in-tree version of buflib does buffer alignment
and also progressed a bit. Still the tests are a very good start.
Added Rockbox copyright header during import were needed.
Change-Id: Ib39ec4301285f1dd53059b7bed0c0d6646297dc5
Diffstat (limited to 'firmware')
-rw-r--r-- | firmware/test/buflib/Makefile | 50 | ||||
-rw-r--r-- | firmware/test/buflib/autoconf.h | 2 | ||||
-rw-r--r-- | firmware/test/buflib/system-hosted.h | 43 | ||||
-rw-r--r-- | firmware/test/buflib/test_main.c | 87 |
4 files changed, 182 insertions, 0 deletions
diff --git a/firmware/test/buflib/Makefile b/firmware/test/buflib/Makefile new file mode 100644 index 0000000000..7c0d36a8ca --- /dev/null +++ b/firmware/test/buflib/Makefile @@ -0,0 +1,50 @@ +FIRMWARE=../.. + +CC ?= gcc +CFLAGS += -g -O2 -DDEBUG -D__PCTOOL__ -std=gnu99 -I$(FIRMWARE)/include -I$(FIRMWARE)/export -I. +LDFLAGS += -L. -lpthread + +.PHONY: clean all + +TARGETS_OBJ = test_main.o +TARGETS = $(TARGETS_OBJ:.o=) + +LIB_OBJ = buflib.o \ + crc32.o \ + strlcpy.o +LIB_FILE = libbuflib.a +LIB = buflib + + +ifndef V +SILENT:=@ +else +VERBOSEOPT:=-v +endif + +PRINTS=$(SILENT)$(call info,$(1)) + +all: $(TARGETS) + +test_%: test_%.o $(LIB_FILE) + $(call PRINTS,LD $@)$(CC) $(LDFLAGS) -o $@ $< -l$(LIB) + +$(TARGETS): $(TARGETS_OBJ) $(LIB_FILE) + +buflib.o: $(FIRMWARE)/buflib.c + $(CC) $(CFLAGS) -c $< -o $@ + +crc32.o: $(FIRMWARE)/common/crc32.c + $(CC) $(CFLAGS) -c $< -o $@ + +strlcpy.o: $(FIRMWARE)/common/strlcpy.c + $(CC) $(CFLAGS) -c $< -o $@ + +%.o: %.c + $(call PRINTS,CC $<)$(CC) $(CFLAGS) -c $< + +$(LIB_FILE): $(LIB_OBJ) + $(call PRINTS,AR $@)ar rcs $@ $^ + +clean: + rm *.o $(TARGETS) $(LIB_FILE) diff --git a/firmware/test/buflib/autoconf.h b/firmware/test/buflib/autoconf.h new file mode 100644 index 0000000000..abfaa482e1 --- /dev/null +++ b/firmware/test/buflib/autoconf.h @@ -0,0 +1,2 @@ +/* Define endianess for the target or simulator platform */ +#define ROCKBOX_LITTLE_ENDIAN 1 diff --git a/firmware/test/buflib/system-hosted.h b/firmware/test/buflib/system-hosted.h new file mode 100644 index 0000000000..40b5ea8f9f --- /dev/null +++ b/firmware/test/buflib/system-hosted.h @@ -0,0 +1,43 @@ +/*************************************************************************** +* __________ __ ___. +* Open \______ \ ____ ____ | | _\_ |__ _______ ___ +* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +* \/ \/ \/ \/ \/ +* $Id$ +* +* Copyright (C) 2015 Thomas Jarosch +* +* Loosely based upon rbcodecplatform-unix.h from rbcodec +* +* 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. +* +****************************************************************************/ + +#ifndef _COMMON_UNITTEST_H +#define _COMMON_UNITTEST_H + +/* debugf, logf */ +#define debugf(...) fprintf(stderr, __VA_ARGS__) + +#ifndef logf +#define logf(...) do { fprintf(stderr, __VA_ARGS__); \ + putc('\n', stderr); \ + } while (0) +#endif + +#ifndef panicf +#define panicf(...) do { fprintf(stderr, __VA_ARGS__); \ + putc('\n', stderr); \ + exit(-1); \ + } while (0) +#endif + +#endif /* _COMMON_UNITTEST_H */ diff --git a/firmware/test/buflib/test_main.c b/firmware/test/buflib/test_main.c new file mode 100644 index 0000000000..050deca006 --- /dev/null +++ b/firmware/test/buflib/test_main.c @@ -0,0 +1,87 @@ +/*************************************************************************** +* __________ __ ___. +* 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 <stdio.h> +#include <stdlib.h> +#include "buflib.h" + +#define BUFLIB_BUFFER_SIZE (12<<10) +static char buflib_buffer[BUFLIB_BUFFER_SIZE]; +static struct buflib_context ctx; +#define assert(x) do { if (!(x)) exit(1); } while(0) + +int move_callback(int handle, void* current, void* new) +{ + (void)handle;(void)current;(void)new; + printf("Move!\n"); +} + +int shrink_callback(int handle, unsigned hints, void* start, size_t old_size) +{ + (void)handle;(void)start;(void)old_size;(void)hints; + printf("Shrink"); +} + +struct buflib_callbacks ops = { + .move_callback = move_callback, + .shrink_callback = shrink_callback, +}; + +int main(int argc, char **argv) +{ + buflib_init(&ctx, buflib_buffer, BUFLIB_BUFFER_SIZE); + + int id = buflib_alloc_ex(&ctx, 512, "foo", &ops); + int id2 = buflib_alloc_ex(&ctx, 1024, "bar", &ops); + int id3 = buflib_alloc_ex(&ctx, 8<<10, "8K", &ops); + + assert(id > 0 && id2 > 0 && id3 > 0); + + #define STR "<TEST>" + strncpy(buflib_get_data(&ctx, id3), STR, sizeof STR); + if (id > 0) + { +// buflib_print_allocs(&ctx); + buflib_free(&ctx, id); +// buflib_print_allocs(&ctx); + buflib_free(&ctx, id2); +// buflib_print_allocs(&ctx); + + id = buflib_alloc_ex(&ctx, 3<<10, "should compact", &ops); + if (id <= 0) printf("compacting alloc failed\n"); + +// buflib_print_allocs(&ctx); + + printf("id I: %p\n", buflib_get_data(&ctx, id3)); + id2 = buflib_alloc_ex(&ctx, 3<<10, "should fail", &ops); + printf("id II: %p\n", buflib_get_data(&ctx, id3)); + if (id2 <= 0) printf("failing alloc failed\n"); + else buflib_free(&ctx, id2); + + if (id > 0) + buflib_free(&ctx, id); + + printf("Check string: \"%s\"\n", buflib_get_data(&ctx, id3)); +// buflib_print_allocs(&ctx); + } + + return 0; +} |