Ver Fonte

Added simple-linked list lirary & changed CMake architecture

isundil há 10 anos atrás
pai
commit
1682db6e9a

+ 6 - 4
.gitignore

@@ -36,12 +36,14 @@
 *.swp
 
 # cmake files
-Makefile
-CMakeFiles
-cmake_install.cmake
-CMakeCache.txt
+/Makefile
+/CMakeFiles
+/cmake_install.cmake
+/CMakeCache.txt
 /bin
 /test/bin
+/CTestTestfile.cmake
+/Testing
 
 # test directory
 

+ 7 - 1
CMakeLists.txt

@@ -11,8 +11,14 @@ set_property(TARGET sllist  PROPERTY ARCHIVE_OUTPUT_DIRECTORY lib)
 add_definitions ("-Wall")
 add_definitions ("-g3")
 
-include_directories(src lib/sllist/include)
+include_directories(src lib/sllist/include test)
 target_link_libraries(sandbox sllist)
 
+add_executable(test_sllist test/sllist/main.c)
+set_property(TARGET test_sllist PROPERTY RUNTIME_OUTPUT_DIRECTORY test/bin)
+target_link_libraries(test_sllist sllist)
+
+enable_testing()
+add_test(sllist test/bin/test_sllist)
 #add_subdirectory(test/open)
 #add_subdirectory(test/test)

+ 63 - 0
lib/sllist/include/sllist.h

@@ -1,9 +1,72 @@
 #ifndef   SL_LIST_H__
 # define  SL_LIST_H__
 
+struct sl_list_item {
+	void *data;
+	struct sl_list_item *next;
+};
+
 typedef struct {
+	unsigned int count;
+	struct sl_list_item *first;
 } sl_list;
 
+/**
+ * Allocate a new list and return it
+ * result should be unallocated with sllist_destroy()
+**/
 sl_list *sllist_create();
 
+/**
+ * Free all ressources allocated by sllist_create
+**/
+void sllist_destroy(sl_list *list);
+
+/**
+ * Get the numbers of items contained in sl_list
+**/
+unsigned int sllist_count(const sl_list * const list);
+
+/**
+ * Add an item at the end of the list
+ * TODO
+**/
+sl_list *sllist_pushback(sl_list *list, void *item);
+
+/**
+ * Add an item at the begining of the list
+ * TODO
+**/
+sl_list *sllist_pushfront(sl_list *list, void *item);
+
+
+/**
+ * Remove the last item of the list and return it
+ * TODO
+**/
+void *sllist_popback(sl_list *list, void *item);
+
+/**
+ * Remove the first item of the list and return it
+ * TODO
+**/
+void *sllist_popfront(sl_list *list, void *item);
+
+/**
+ * Get the item at position %pos
+ * TODO
+**/
+void *sllist_at(sl_list *list, unsigned int pos);
+
+/**
+ * Remove the item at position %pos, and return it
+ * TODO
+**/
+void *sllist_removeat(sl_list *list, unsigned int pos);
+
+/**
+ * Remove all elements
+**/
+sl_list *sllist_clear(sl_list *list);
+
 #endif /* SL_LIST_H__ */

+ 24 - 0
lib/sllist/src/create.c

@@ -6,6 +6,30 @@ sl_list *sllist_create()
 	sl_list *result;
 
 	result = malloc(sizeof(*result));
+	result->count = 0;
+	result->first = NULL;
 	return result;
 }
 
+void sllist_destroy(sl_list *list)
+{
+	free(sllist_clear(list));
+}
+
+sl_list *sllist_clear(sl_list *list)
+{
+	while (list->first)
+	{
+		struct sl_list_item *i = list->first->next;
+		free(list->first);
+		list->first = i;
+	}
+	list->count = 0;
+	return list;
+}
+
+unsigned int sllist_count(const sl_list * const list)
+{
+	return list->count;
+}
+

+ 2 - 0
src/sandbox.h

@@ -7,6 +7,8 @@
 # include <sys/user.h>
 # include <unistd.h>
 
+# include "sllist.h"
+
 # ifndef NR_syscalls
 #  define NR_syscalls 386
 # endif

+ 4 - 0
test/common.h

@@ -6,6 +6,10 @@
 
 #define _assertTrue(a) { int _a = (int) a; if(!_a) { fprintf(stderr, "File %s, line %d: fail asserting %d as true\n", __FILE__, __LINE__, _a); return -1; }}
 
+#define _assertNotNull(a) { unsigned long long int _a = (unsigned long long int) a; if(!_a) { fprintf(stderr, "File %s, line %d: fail asserting 0x%llx not null\n", __FILE__, __LINE__, _a); return -1; }}
+
+#define _assertNull(a) { unsigned long long int _a = (unsigned long long int) a; if(_a) { fprintf(stderr, "File %s, line %d: fail asserting 0x%llx null\n", __FILE__, __LINE__, _a); return -1; }}
+
 #define _assertEqual(a, b) { long long int _a = (long long int) a; long long int _b = (long long int) b; if(_a != _b) { fprintf(stderr, "File %s, line %d: fail asserting %lld equals %lld\n", __FILE__, __LINE__, _a, _b); return -1; }}
 
 #define _assertDiff(a, b) { long long int _a = (long long int) a; long long int _b = (long long int) b; if(_a == _b) { fprintf(stderr, "File %s, line %d: fail asserting %lld is different than %lld\n", __FILE__, __LINE__, _a, _b); return -1; }}

+ 0 - 2
test/open/CMakeLists.txt

@@ -1,2 +0,0 @@
-include_directories(../)
-add_executable(open main.c)

+ 0 - 38
test/open/main.c

@@ -1,38 +0,0 @@
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include "common.h"
-
-int runTests()
-{
-	int fd = open("filenotfound", O_RDONLY);
-	_assertEqual(fd, -1);
-	_assertEqual(open("newFile", O_RDONLY), -1);
-	fd = open("newFile", O_CREAT, 0x644);
-	_assertDiff(fd, -1);
-	close(fd);
-	_assertDiff((fd = open("newFile", O_RDONLY)), -1);
-	close(fd);
-	return 0;
-}
-
-void cleanup()
-{
-	unlink("newFile");
-	unlink("filenotfound");
-}
-
-int main(int ac, char **av)
-{
-	cleanup();
-
-	if (runTests() == -1)
-	{
-		cleanup();
-		exit(EXIT_FAILURE);
-	}
-	cleanup();
-	printf("Success\n");
-	exit(EXIT_SUCCESS);
-}
-

+ 18 - 0
test/sllist/main.c

@@ -0,0 +1,18 @@
+#include "sllist.h"
+#include "common.h"
+
+static inline int test_create()
+{
+	sl_list *a = sllist_create();
+	_assertNotNull(a);
+	_assertEqual(sllist_count(a), 0);
+	return 0;
+}
+
+int main()
+{
+	int success = 1;
+	success &= !test_create();
+	exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
+}
+

+ 0 - 2
test/test/CMakeLists.txt

@@ -1,2 +0,0 @@
-include_directories(../)
-add_executable(test test.c)

+ 0 - 10
test/test/test.c

@@ -1,10 +0,0 @@
-#include <unistd.h>
-#include "common.h"
-
-int main()
-{
-	const char* t = "test\n";
-	write(1, t, 5);
-	printf("%p\n", t);
-}
-