summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorQC <qball@gmpclient.org>2014-08-29 12:14:43 +0200
committerQC <qball@gmpclient.org>2014-08-29 12:14:43 +0200
commit169c05cc1529d3165aaa5d14a41e45db6a75b48a (patch)
treeefc538ebef6a309e83caa63ab224616136d477c1 /test
parent5b017b017b1dfb6d15c70579246b2ffa9aaea763 (diff)
Fix crash in history when removing last entry.
Diffstat (limited to 'test')
-rw-r--r--test/Makefile13
-rw-r--r--test/history-test.c100
2 files changed, 113 insertions, 0 deletions
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 00000000..26bb0aea
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,13 @@
+CC=gcc
+CFLAGS=-I../include/ -I../ -std=c99 -fprofile-arcs -ftest-coverage -g3
+SOURCES=\
+ history-test.c\
+ ../config/config.c\
+ ../source/history.c
+
+history-test: $(SOURCES)
+ $(CC) -o $@ $^ `pkg-config --libs --cflags glib-2.0` $(CFLAGS)
+
+
+test: history-test
+ ./$^
diff --git a/test/history-test.c b/test/history-test.c
new file mode 100644
index 00000000..7d6f34f0
--- /dev/null
+++ b/test/history-test.c
@@ -0,0 +1,100 @@
+#include <unistd.h>
+
+#include <stdio.h>
+#include <assert.h>
+#include <glib.h>
+#include <history.h>
+#include <string.h>
+
+
+static int test = 0;
+
+#define TASSERT(a) {\
+ assert ( a );\
+ printf("Test %i passed (%s)\n", ++test, #a);\
+}
+
+const char *file = "text";
+
+int main ( int argc, char **argv )
+{
+ unlink(file);
+
+ // Empty list.
+ unsigned int length= 0;
+ char **retv = history_get_list ( file, &length);
+
+ TASSERT ( retv == NULL );
+ TASSERT ( length == 0 );
+
+ // 1 item
+ history_set( file, "aap");
+
+ retv = history_get_list ( file, &length);
+
+ TASSERT ( retv != NULL );
+ TASSERT ( length == 1 );
+
+ TASSERT ( strcmp(retv[0], "aap") == 0 );
+
+ g_strfreev(retv);
+
+ // Remove entry
+ history_remove ( file, "aap" );
+
+ length= 0;
+ retv = history_get_list ( file, &length);
+
+ TASSERT ( retv == NULL );
+ TASSERT ( length == 0 );
+
+ // 2 items
+ history_set( file, "aap");
+ history_set( file, "aap");
+
+ retv = history_get_list ( file, &length);
+
+ TASSERT ( retv != NULL );
+ TASSERT ( length == 1 );
+
+ TASSERT ( strcmp(retv[0], "aap") == 0 );
+
+ g_strfreev(retv);
+
+ for(int in=length+1; in < 26; in++) {
+ char *p = g_strdup_printf("aap%i", in);
+ printf("%s- %d\n",p, in);
+ history_set( file, p);
+ retv = history_get_list ( file, &length);
+
+ TASSERT ( retv != NULL );
+ TASSERT ( length == (in) );
+
+ g_strfreev(retv);
+
+ g_free(p);
+ }
+ // Max 25 entries.
+ history_set( file, "blaat" );
+ retv = history_get_list ( file, &length);
+
+ TASSERT ( retv != NULL );
+ TASSERT ( length == 25 );
+
+ g_strfreev(retv);
+
+ // Test fail.
+ history_set ( NULL, "aap");
+
+ retv = history_get_list ( NULL , &length);
+ printf("Test %i passed\n", ++test);
+
+ TASSERT ( retv == NULL );
+ TASSERT ( length == 0 );
+
+ history_remove ( NULL, "aap" );
+ printf("Test %i passed\n", ++test);
+
+
+ return 0;
+}