summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
Diffstat (limited to 'firmware')
-rw-r--r--firmware/test/buflib/Makefile50
-rw-r--r--firmware/test/buflib/autoconf.h2
-rw-r--r--firmware/test/buflib/system-hosted.h43
-rw-r--r--firmware/test/buflib/test_main.c87
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;
+}