Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/list/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_library(list linkedlist.c)
add_library(dict dictionary.c)

install(TARGETS list
install(TARGETS list dict
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
113 changes: 113 additions & 0 deletions src/list/dictionary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"

typedef struct {
char* key;
void* value;
} KeyValuePair;

typedef struct {
KeyValuePair* pairs;
int size;
int capacity;
DictAction free_value;
} _Dict;

// Private defs
void add_pair_and_resize(_Dict* dict, char* key, void* value);

// Implementation
Dict* new_dict(DictAction free_value){
Dict* handle;
_Dict* dict;

handle = (Dict*)malloc(sizeof(Dict));
if(handle == NULL){
printf("ERROR: DICT: malloc error");
}

dict = malloc(sizeof(_Dict));
if(dict == NULL){
printf("ERROR: DICT: malloc error");
}
dict->size = 0;
dict->capacity = 10;
dict->pairs = malloc(sizeof(KeyValuePair) * 10);
if(dict->pairs == NULL){
printf("ERROR: DICT: malloc error");
}
dict->free_value = free_value;
}

Dict* free_dict(Dict** p_handle){
_Dict* dict;
int i;

dict = (_Dict*)(*p_handle)->obj;

// free pairs
for(i = 0; i < dict->size; i++){
dict->free_value(dict->pairs[i].value);
free(dict->pairs[i].key);
}
free(dict->pairs);
free(dict);
free(*p_handle);
*p_handle = NULL;
}

int dict_try_get(Dict* handle, char* key, void** value){
int i;
_Dict* dict;

dict = (_Dict*)handle->obj;

for(i = 0; i < dict->size; i++){
if(strcmp(key, dict->pairs[i].key) == 0){
*value = dict->pairs[i].value;
return 1;
}
}

return 0;
}

void dict_add(Dict* handle, char* key, void* value){
int i;
_Dict* dict;

dict = (_Dict*)handle->obj;

for(i = 0; i < dict->size; i++){
if(strcmp(key, dict->pairs[i].key) == 0){
dict->free_value(dict->pairs[i].value);
dict->pairs[i].value = value;
return;
}
}

// add the new pair
add_pair_and_resize(dict, key, value);
}

// Private functions
void add_pair_and_resize(_Dict* dict, char* key, void* value){
KeyValuePair* temp;

if(dict->capacity <= dict->size){
temp = malloc(sizeof(KeyValuePair) * dict->capacity * 2);
if(temp == NULL){
printf("ERROR: DICT: malloc error");
exit(1);
}

memcpy(temp, dict->pairs, sizeof(KeyValuePair) * dict->capacity);
free(dict->pairs);
dict->pairs = temp;
}

dict->pairs[dict->size].value = value;
strcpy(dict->pairs[dict->size].key, key);
}
15 changes: 15 additions & 0 deletions src/list/dictionary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef DICT
#define DICT

typedef void (*DictAction) (void*) ;

typedef struct {
void* obj;
} Dict;

Dict* new_dict(DictAction);
Dict* free_dict(Dict** dict);
int dict_try_get(Dict* handle, char* key, void** value);
void dict_add(Dict* handle, char* key, void* value);

#endif
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ add_executable(linkedlist_tests linkedlist_test.c)
add_executable(textpager_tests textpager_tests.c ${CMAKE_SOURCE_DIR}/src/textpager.c)

add_executable(editorState_tests editorStateTests.c)
add_executable(dict_tests dictionary_test.c)

target_link_libraries(linkedlist_tests PRIVATE cmocka-static list)
target_link_libraries(textpager_tests PRIVATE cmocka-static list)
target_link_libraries(editorState_tests PRIVATE cmocka-static list me_state)
target_link_libraries(dict_tests PRIVATE cmocka-static dict)

#tests
enable_testing()
add_test(linkedList linkedlist_tests)
add_test(textPager, textpager_tests)
add_test(editorState, editorState_tests)
add_test(dictionary, dict_tests)
51 changes: 51 additions & 0 deletions test/dictionary_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>
#include <stdlib.h>
#include "dictionary.h"

void simple_free_item(void* item){
free(item);
}
void dict_new_dict_returns_not_null(void ** state){
Dict* dict;
dict = new_dict(simple_free_item);

assert_true(dict != NULL);
free_dict(&dict);
assert_true(dict == NULL);
}

/* These functions will be used to initialize
and clean resources up after each test run */
int setup (void ** state)
{
return 0;
}

int teardown (void ** state)
{
return 0;
}


int main (void)
{
const struct CMUnitTest tests [] =
{
cmocka_unit_test (dict_new_dict_returns_not_null),
};

/* If setup and teardown functions are not
needed, then NULL may be passed instead */

int count_fail_tests =
cmocka_run_group_tests (tests, setup, teardown);

return count_fail_tests;
}