Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Wip/core unittests #144

Merged
merged 3 commits into from

1 participant

@jaytaph
Owner

No description provided.

@jaytaph jaytaph merged commit 8f4893c into saffire:develop
@jaytaph jaytaph deleted the jaytaph:wip/core_unittests branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 30, 2013
  1. @jaytaph

    Added gcov script

    jaytaph authored
Commits on Apr 4, 2013
  1. @jaytaph

    Added bzip2 tests

    jaytaph authored
  2. @jaytaph

    Added more dll tests

    jaytaph authored
This page is out of date. Refresh to see the latest.
View
3  .gitignore
@@ -45,4 +45,5 @@ src/include/vm/vm_opcodes.h
src/include/gitversion.h
src/utmain
src/include/objects/_exceptions.h
-src/include/objects/_exceptions.inc
+src/include/objects/_exceptions.inc$
+html/
View
5 gcov.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+./configure --enable-gcov
+make clean
+make
+
View
3  src/Makefile.am
@@ -245,4 +245,5 @@ utmain_LDADD = $(SAFFIRE_LIBS) $(SAFFIRE_LIBS) $(edit_LIBS) $(augeas_LIBS) -lpth
utmain_SOURCES = \
../unittests/core/main.c \
../unittests/core/hashtable/hashtable.c \
- ../unittests/core/dll/dll.c
+ ../unittests/core/dll/dll.c \
+ ../unittests/core/bz2/bz2.c
View
100 src/components/general/dll.c
@@ -57,29 +57,30 @@ void dll_free(t_dll *dll) {
/**
- * Append an element before the head of the DLL.
+ * Prepend an element before the head of the DLL.
*/
t_dll_element *dll_prepend(t_dll *dll, void *data) {
t_dll_element *e = (t_dll_element *)smm_malloc(sizeof(t_dll_element));
- // Correct pointer of the previous tail element
- if (dll->tail == NULL) {
- dll->tail = e;
- }
-
// Set head element data
+ e->data = data;
e->prev = NULL;
e->next = dll->head;
- e->data = data;
-
+ // First element, this becomes the tail as well
+ if (dll->tail == NULL) {
+ dll->tail = e;
+ }
+
+ // Already an head element present, make sure previous link is corrected
if (dll->head != NULL) {
dll->head->prev = e;
}
- dll->head = e;
- // Set dll data
+ // This element becomes the head
+ dll->head = e;
+ // Increase DLL size
dll->size++;
return e;
@@ -91,23 +92,25 @@ t_dll_element *dll_prepend(t_dll *dll, void *data) {
t_dll_element *dll_append(t_dll *dll, void *data) {
t_dll_element *e = (t_dll_element *)smm_malloc(sizeof(t_dll_element));
- // Correct pointer of previous tail element
+ // Set tail element data
+ e->data = data;
+ e->prev = dll->tail;
+ e->next = NULL;
+
+ // First element, this becomes the head as well
if (dll->head == NULL) {
dll->head = e;
}
- if (dll->tail == NULL) {
- dll->tail = e;
- } else {
+
+ // Already an tail element present, make sure next link is corrected
+ if (dll->tail != NULL) {
dll->tail->next = e;
}
- // Set tail element data
- e->prev = dll->tail;
- e->next = NULL;
- e->data = data;
-
- // Set dll data
+ // This element becomes the tail
dll->tail = e;
+
+ // Increase DLL size
dll->size++;
return e;
@@ -126,17 +129,22 @@ t_dll_element *dll_insert_before(t_dll *dll, t_dll_element *before_element, void
return dll_prepend(dll, data);
}
- e->next = before_element;
e->data = data;
+ e->prev = before_element->prev;
+ e->next = before_element;
- before_element->prev->next = e;
+ if (before_element->prev) {
+ before_element->prev->next = e;
+ }
before_element->prev = e;
- dll->size++;
+
if (before_element == dll->head) {
dll->head = e;
}
+ dll->size++;
+
return e;
}
@@ -147,21 +155,26 @@ t_dll_element *dll_insert_before(t_dll *dll, t_dll_element *before_element, void
t_dll_element *dll_insert_after(t_dll *dll, t_dll_element *after_element, void *data) {
t_dll_element *e = (t_dll_element *)smm_malloc(sizeof(t_dll_element));
- // No element given, just prepend
+ // No element given, just append
if (after_element == NULL) {
return dll_append(dll, data);
}
- e->prev = after_element;
e->data = data;
+ e->prev = after_element;
+ e->next = after_element->next;
+ if (after_element->next) {
+ after_element->next->prev = e;
+ }
after_element->next = e;
- dll->size++;
if (after_element == dll->tail) {
dll->tail = e;
}
+ dll->size++;
+
return e;
}
@@ -170,23 +183,22 @@ t_dll_element *dll_insert_after(t_dll *dll, t_dll_element *after_element, void *
* Remove an element from the linked list
*/
int dll_remove(t_dll *dll, t_dll_element *element) {
- t_dll_element *tmp;
-
- if (element == dll->head) {
- dll->head = dll->head->next;
- }
- if (element == dll->tail) {
- dll->tail = dll->tail->prev;
- }
-
dll->size--;
+
if (dll->size == 0) {
dll->head = NULL;
dll->tail = NULL;
+ } else if (element == dll->head) {
+ dll->head = dll->head->next;
+ dll->head->prev = NULL;
+ return;
+ } else if (element == dll->tail) {
+ dll->tail = dll->tail->prev;
+ dll->tail->next = NULL;
+ return;
} else {
- tmp = element->next;
- element->next = element->prev;
- element->prev = tmp;
+ element->prev->next = element->next;
+ element->next->prev = element->prev;
}
return 1;
@@ -204,11 +216,11 @@ void dll_push(t_dll *dll, void *data) {
*/
void *dll_pop(t_dll *dll) {
t_dll_element *e = DLL_TAIL(dll);
- if (!e) return NULL;
+ if (!e) return NULL;
- void *ret = e->data;
- dll_remove(dll, e);
- return ret;
+ void *ret = e->data;
+ dll_remove(dll, e);
+ return ret;
}
/*
@@ -216,7 +228,7 @@ void *dll_pop(t_dll *dll) {
*/
void *dll_top(t_dll *dll) {
t_dll_element *e = DLL_TAIL(dll);
- if (!e) return NULL;
+ if (!e) return NULL;
- return e->data;
+ return e->data;
}
View
35 unittests/core/bz2/bz2.c
@@ -0,0 +1,35 @@
+#include <CUnit/CUnit.h>
+#include "../../src/include/general/bzip2.h"
+
+#define BZ2_MAXBUFLEN 1024
+
+char str[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et "
+ "dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex "
+ "ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu "
+ "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
+ "mollit anim id est laborum.";
+
+void test_comp_decomp(void) {
+ char *buf;
+ unsigned int buf_len;
+
+ int ret = bzip2_compress(&buf, &buf_len, str, strlen(str));
+
+ CU_ASSERT_EQUAL(ret, 1);
+ CU_ASSERT_EQUAL(buf_len, 302);
+
+
+ char *buf2 = malloc(BZ2_MAXBUFLEN);
+ unsigned int buf2_len = BZ2_MAXBUFLEN;
+
+ ret = bzip2_decompress(buf2, &buf2_len, buf, buf_len);
+
+ CU_ASSERT_EQUAL(ret, 1);
+ CU_ASSERT_EQUAL(buf2_len, strlen(str));
+}
+
+void test_bz2_init() {
+ CU_pSuite suite = CU_add_suite("bz2", NULL, NULL);
+
+ CU_add_test(suite, "bz2 comp/decomp works", test_comp_decomp);
+}
View
0  unittests/core/bz2/bz2.h
No changes.
View
163 unittests/core/dll/dll.c
@@ -3,44 +3,57 @@
#include "../../src/include/general/dll.h"
-void test_dll_dll_init() {
+static void test_dll_dll_init() {
t_dll *dll = dll_init();
CU_ASSERT_PTR_NOT_NULL(dll);
CU_ASSERT_EQUAL(dll->size, 0);
+
+ dll_free(dll);
}
-void test_dll_dll_append_adds_item() {
+static void test_dll_dll_append_adds_item() {
t_dll *dll = dll_init();
dll_append(dll, "test");
CU_ASSERT_STRING_EQUAL(DLL_HEAD(dll)->data, "test");
+
+ dll_free(dll);
}
-void test_dll_dll_prepend_prepends_item() {
+static void test_dll_dll_prepend_prepends_item() {
t_dll *dll = dll_init();
dll_append(dll, "test");
dll_prepend(dll, "prepend");
CU_ASSERT_STRING_EQUAL(DLL_HEAD(dll)->data, "prepend");
+
+ dll_free(dll);
}
-void test_dll_dll_insert_before_inserts_item_before() {
+static void test_dll_dll_insert_before_inserts_item_before() {
t_dll *dll = dll_init();
- dll_append(dll, "test1");
- dll_append(dll, "test3");
+ dll_insert_before(dll, NULL, "test3");
+ dll_insert_before(dll, NULL, "test1");
t_dll_element *element = DLL_TAIL(dll);
-
dll_insert_before(dll, element, "test2");
+ CU_ASSERT_STRING_EQUAL(DLL_HEAD(dll)->data, "test1");
CU_ASSERT_STRING_EQUAL(DLL_NEXT(DLL_HEAD(dll))->data, "test2");
+ CU_ASSERT_STRING_EQUAL(DLL_NEXT(DLL_NEXT(DLL_HEAD(dll)))->data, "test3");
+
+ element = DLL_HEAD(dll);
+ dll_insert_before(dll, element, "test0");
+ CU_ASSERT_STRING_EQUAL(DLL_HEAD(dll)->data, "test0");
+
+ dll_free(dll);
}
-void test_dll_dll_insert_after_inserts_item_after() {
+static void test_dll_dll_insert_after_inserts_item_after() {
t_dll *dll = dll_init();
dll_append(dll, "test1");
@@ -50,7 +63,132 @@ void test_dll_dll_insert_after_inserts_item_after() {
dll_insert_after(dll, element, "test2");
+ CU_ASSERT_STRING_EQUAL(DLL_HEAD(dll)->data, "test1");
CU_ASSERT_STRING_EQUAL(DLL_NEXT(DLL_HEAD(dll))->data, "test2");
+ CU_ASSERT_STRING_EQUAL(DLL_NEXT(DLL_NEXT(DLL_HEAD(dll)))->data, "test3");
+
+ element = DLL_TAIL(dll);
+ dll_insert_after(dll, element, "test4");
+ CU_ASSERT_STRING_EQUAL(DLL_TAIL(dll)->data, "test4");
+
+ dll_free(dll);
+}
+
+static void test_dll_dll_push() {
+ t_dll *dll = dll_init();
+
+ dll_push(dll, "test1");
+ CU_ASSERT_STRING_EQUAL(DLL_TAIL(dll)->data, "test1");
+
+ dll_push(dll, "test2");
+ CU_ASSERT_STRING_EQUAL(DLL_TAIL(dll)->data, "test2");
+
+ dll_free(dll);
+}
+
+static void test_dll_dll_pop() {
+ char *data;
+ t_dll *dll = dll_init();
+
+ dll_push(dll, "test1");
+ dll_push(dll, "test2");
+
+ data = dll_pop(dll);
+ CU_ASSERT_PTR_NOT_NULL(data);
+ CU_ASSERT_STRING_EQUAL(data, "test2");
+
+ data = dll_pop(dll);
+ CU_ASSERT_PTR_NOT_NULL(data);
+ CU_ASSERT_STRING_EQUAL(data, "test1");
+
+ data = dll_pop(dll);
+ CU_ASSERT_PTR_NULL(data);
+
+ dll_free(dll);
+}
+
+static void test_dll_dll_top() {
+ char *data;
+ t_dll *dll = dll_init();
+
+ dll_push(dll, "test1");
+ dll_push(dll, "test2");
+
+ data = dll_top(dll);
+ CU_ASSERT_STRING_EQUAL(data, "test2");
+
+ data = dll_top(dll);
+ CU_ASSERT_STRING_EQUAL(data, "test2");
+
+ dll_pop(dll);
+ data = dll_top(dll);
+ CU_ASSERT_STRING_EQUAL(data, "test1");
+
+ dll_free(dll);
+}
+
+static void test_dll_dll_remove() {
+ t_dll *dll;
+ char *data;
+
+ // Removing first element
+
+ dll = dll_init();
+
+ dll_push(dll, "test1");
+ dll_push(dll, "test2");
+ dll_push(dll, "test3");
+
+ dll_remove(dll, DLL_HEAD(dll));
+
+ data = dll_pop(dll);
+ CU_ASSERT_PTR_NOT_NULL(data);
+ CU_ASSERT_STRING_EQUAL(data, "test3");
+ data = dll_pop(dll);
+ CU_ASSERT_PTR_NOT_NULL(data);
+ CU_ASSERT_STRING_EQUAL(data, "test2");
+
+ dll_free(dll);
+
+ // Removing middle element
+
+ dll = dll_init();
+
+ dll_push(dll, "test1");
+ dll_push(dll, "test2");
+ dll_push(dll, "test3");
+
+ dll_remove(dll, DLL_NEXT(DLL_HEAD(dll)));
+
+ data = dll_pop(dll);
+ CU_ASSERT_PTR_NOT_NULL(data);
+ CU_ASSERT_STRING_EQUAL(data, "test3");
+ data = dll_pop(dll);
+ CU_ASSERT_PTR_NOT_NULL(data);
+ CU_ASSERT_STRING_EQUAL(data, "test1");
+
+ dll_free(dll);
+
+
+ // Removing last element
+
+ dll = dll_init();
+
+ dll_push(dll, "test1");
+ dll_push(dll, "test2");
+ dll_push(dll, "test3");
+
+ dll_remove(dll, DLL_TAIL(dll));
+
+ data = dll_pop(dll);
+ CU_ASSERT_PTR_NOT_NULL(data);
+ CU_ASSERT_STRING_EQUAL(data, "test2");
+ data = dll_pop(dll);
+ CU_ASSERT_PTR_NOT_NULL(data);
+ CU_ASSERT_STRING_EQUAL(data, "test1");
+
+ dll_free(dll);
+
}
void test_dll_init() {
@@ -59,7 +197,12 @@ void test_dll_init() {
CU_add_test(suite, "dll_init creates object", test_dll_dll_init);
CU_add_test(suite, "dll_append adds item", test_dll_dll_append_adds_item);
CU_add_test(suite, "dll_prepend prepends item", test_dll_dll_prepend_prepends_item);
- CU_add_test(suite, "dll_insert_before inserts item before", test_dll_dll_insert_before_inserts_item_before);
CU_add_test(suite, "dll_insert_after inserts item after", test_dll_dll_insert_after_inserts_item_after);
+ CU_add_test(suite, "dll_insert_before inserts item before", test_dll_dll_insert_before_inserts_item_before);
+ CU_add_test(suite, "dll_remove", test_dll_dll_remove);
+
+ CU_add_test(suite, "dll_push", test_dll_dll_push);
+ CU_add_test(suite, "dll_pop", test_dll_dll_pop);
+ CU_add_test(suite, "dll_top", test_dll_dll_top);
-}
+}
View
2  unittests/core/main.c
@@ -3,6 +3,7 @@
#include <CUnit/Basic.h>
#include "hashtable/hashtable.h"
#include "dll/dll.h"
+#include "bz2/bz2.h"
int main(int argc, char *argv[]) {
@@ -10,6 +11,7 @@ int main(int argc, char *argv[]) {
test_hashtable_init();
test_dll_init();
+ test_bz2_init();
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
Something went wrong with that request. Please try again.