Skip to content

Commit

Permalink
Add node test
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 21, 2017
1 parent 922570c commit 9059b4f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ include_directories(include)

file(GLOB SOURCES "src/*.c")

add_executable(main ${SOURCES})
add_executable(main ${SOURCES})
add_executable(node_test "test/node_test.c" "src/node.c")
8 changes: 4 additions & 4 deletions include/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ typedef struct node {
struct node *next;
} node_t;

node_t init();
int insert(unsigned int, char[VALUESIZE]);
int delete(unsigned int);
node_t get(unsigned int);
node_t *init();
int insert(node_t *, unsigned int, char[VALUESIZE]);
int delete(node_t *, unsigned int);
node_t *get(node_t *, unsigned int);

#endif
14 changes: 7 additions & 7 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "node.h"
#include "msg.h"

node_t init() {
node_t *init() {
node_t *head = NULL;
head = malloc(sizeof(node_t));
if (head == NULL) {
Expand All @@ -16,12 +16,12 @@ node_t init() {
return head;
}

int insert(node_t head, unsigned int key, char value[VALUESIZE]) {
int insert(node_t *head, unsigned int key, char value[VALUESIZE]) {
if (head == NULL) {
return -1;
}

node_t *node = get(key);
node_t *node = get(head, key);

if (node != NULL) {
return -1;
Expand All @@ -47,7 +47,7 @@ int insert(node_t head, unsigned int key, char value[VALUESIZE]) {
return 0;
}

int delete(node_t head, unsigned int key) {
int delete(node_t *head, unsigned int key) {
if (head == NULL) {
return -1;
}
Expand All @@ -62,7 +62,7 @@ int delete(node_t head, unsigned int key) {
iter = iter->next;
}

if (iter->next == NULL) {
if (iter == NULL) {
return -1;
}

Expand All @@ -75,9 +75,9 @@ int delete(node_t head, unsigned int key) {
return 0;
}

node_t get(node_t head, unsigned int key) {
node_t *get(node_t *head, unsigned int key) {
if (head == NULL) {
return;
return NULL;
}

node_t *iter = head;
Expand Down
43 changes: 43 additions & 0 deletions test/node_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>

#include "node.h"

void print(node_t *);

int main() {
node_t *list = init();

if (list == NULL) {
return 0;
}

insert(list, 1, "hello");
insert(list, 2, "world");
insert(list, 3, "Han");
insert(list, 4, "Sangwoo");
insert(list, 5, "woo");
insert(list, 5, "aa");
print(list);

printf("delete test\n");
delete(list, 6);
delete(list, 4);
print(list);

printf("get test\n");
node_t *temp = get(list, 1);
printf("key: %d, value: %s\n", temp->key, temp->value);
temp = get(list, 6);
if (temp == NULL) {
printf("get is null\n");
}
return 0;
}

void print(node_t *list) {
node_t *iter = list;

while ((iter = iter->next) != NULL) {
printf("key: %d, value: %s\n", iter->key, iter->value);
}
}

0 comments on commit 9059b4f

Please sign in to comment.