Format code.

This commit is contained in:
Danila Fedorin 2016-12-21 14:50:32 -08:00
parent d41a085bcd
commit 52640ed8f9
7 changed files with 305 additions and 337 deletions

3
.clang-format Normal file
View File

@ -0,0 +1,3 @@
DerivePointerAlignment: false
PointerAlignment: Left
BasedOnStyle: google

View File

@ -3,10 +3,7 @@
#include <stdarg.h> #include <stdarg.h>
enum libds_result_e { enum libds_result_e { SUCCESS, MALLOC };
SUCCESS,
MALLOC
};
typedef enum libds_result_e libds_result; typedef enum libds_result_e libds_result;
typedef int (*compare_func)(void*, void*); typedef int (*compare_func)(void*, void*);

View File

@ -1,6 +1,6 @@
#include "ht.h" #include "ht.h"
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
unsigned int ht_default_hash_func(void* data) { unsigned int ht_default_hash_func(void* data) {
const unsigned int fnv_prime = 16777619; const unsigned int fnv_prime = 16777619;
@ -15,9 +15,7 @@ unsigned int ht_default_hash_func(void* data){
} }
return hash; return hash;
} }
int ht_default_cmp_func(void* a, void* b){ int ht_default_cmp_func(void* a, void* b) { return strcmp(a, b) == 0; }
return strcmp(a, b) == 0;
}
void* ht_default_copy_func(void* from) { void* ht_default_copy_func(void* from) {
void* copy = malloc(sizeof(char) * (strlen(from) + 1)); void* copy = malloc(sizeof(char) * (strlen(from) + 1));
if (copy) { if (copy) {
@ -25,9 +23,7 @@ void* ht_default_copy_func(void* from) {
} }
return copy; return copy;
} }
void ht_default_free_func(void* data){ void ht_default_free_func(void* data) { free(data); }
free(data);
}
void ht_init(ht* ht) { void ht_init(ht* ht) {
ht->cmp_func = ht_default_cmp_func; ht->cmp_func = ht_default_cmp_func;
@ -54,7 +50,6 @@ libds_result ht_put(ht* ht, void* key, void* data){
ht_node* new_node = NULL; ht_node* new_node = NULL;
unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
new_node = malloc(sizeof(*new_node)); new_node = malloc(sizeof(*new_node));
if (new_node) { if (new_node) {
new_node->data = data; new_node->data = data;
@ -110,7 +105,8 @@ void ht_remove(ht* ht, void* key){
} }
} }
int ht_foreach(ht* ht, void* data, compare_func compare, foreach_func foreach, ...){ int ht_foreach(ht* ht, void* data, compare_func compare, foreach_func foreach,
...) {
int index = 0; int index = 0;
int return_code = 0; int return_code = 0;
va_list args; va_list args;

View File

@ -1,5 +1,3 @@
#include "libds.h" #include "libds.h"
int compare_always(void* a, void* b){ int compare_always(void* a, void* b) { return 1; }
return 1;
}

View File

@ -1,9 +1,9 @@
#include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "vec.h"
#include "ht.h" #include "ht.h"
#include "vec.h"
int _test_vec_foreach_func(void* data, va_list args) { int _test_vec_foreach_func(void* data, va_list args) {
char* real_data = data; char* real_data = data;
@ -12,9 +12,7 @@ int _test_vec_foreach_func(void* data, va_list args){
*sum += *real_data; *sum += *real_data;
return 0; return 0;
} }
int _test_vec_find_string(void* a, void* b){ int _test_vec_find_string(void* a, void* b) { return strcmp(a, b) == 0; }
return strcmp(a, b) == 0;
}
int test_vec_basic() { int test_vec_basic() {
vec test_vec; vec test_vec;
@ -55,7 +53,8 @@ int test_vec_find(){
vec_init(&test_vec); vec_init(&test_vec);
vec_add(&test_vec, test_string); vec_add(&test_vec, test_string);
return_code = vec_find(&test_vec, test_string, _test_vec_find_string) == test_string; return_code =
vec_find(&test_vec, test_string, _test_vec_find_string) == test_string;
vec_free(&test_vec); vec_free(&test_vec);
return return_code; return return_code;
} }
@ -63,29 +62,22 @@ int test_vec_foreach(){
vec test_vec; vec test_vec;
int return_code; int return_code;
int sum = 0; int sum = 0;
char* test_strings[3] = { char* test_strings[3] = {"1", "2", "3"};
"1",
"2",
"3"
};
vec_init(&test_vec); vec_init(&test_vec);
vec_add(&test_vec, test_strings[0]); vec_add(&test_vec, test_strings[0]);
vec_add(&test_vec, test_strings[1]); vec_add(&test_vec, test_strings[1]);
vec_add(&test_vec, test_strings[2]); vec_add(&test_vec, test_strings[2]);
vec_foreach(&test_vec, NULL, compare_always, _test_vec_foreach_func, &sum); vec_foreach(&test_vec, NULL, compare_always, _test_vec_foreach_func, &sum);
return_code = test_strings[0][0] + test_strings[1][0] + test_strings[2][0] == sum; return_code =
test_strings[0][0] + test_strings[1][0] + test_strings[2][0] == sum;
vec_free(&test_vec); vec_free(&test_vec);
return return_code; return return_code;
} }
int test_vec_index() { int test_vec_index() {
vec test_vec; vec test_vec;
int return_code; int return_code;
char* test_strings[3] = { char* test_strings[3] = {"1", "2", "3"};
"1",
"2",
"3"
};
vec_init(&test_vec); vec_init(&test_vec);
vec_add(&test_vec, test_strings[0]); vec_add(&test_vec, test_strings[0]);
@ -130,7 +122,8 @@ int test_ht_get() {
ht_init(&test_ht); ht_init(&test_ht);
ht_put(&test_ht, test_one, test_two); ht_put(&test_ht, test_one, test_two);
ht_put(&test_ht, test_two, test_one); ht_put(&test_ht, test_two, test_one);
return_code = ht_get(&test_ht, test_one) == test_two && ht_get(&test_ht, test_two) == test_one; return_code = ht_get(&test_ht, test_one) == test_two &&
ht_get(&test_ht, test_two) == test_one;
ht_free(&test_ht); ht_free(&test_ht);
return return_code; return return_code;
} }
@ -171,34 +164,14 @@ int run_test(char* test_name, int (*test_func)()) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
char* test_names[11] = {"vec_basic", "vec_add", "vec_remove", "vec_find",
char* test_names[11] = { "vec_foreach", "vec_index", "ht_basic", "ht_put",
"vec_basic", "ht_get", "ht_remove", "ht_foreach"};
"vec_add",
"vec_remove",
"vec_find",
"vec_foreach",
"vec_index",
"ht_basic",
"ht_put",
"ht_get",
"ht_remove",
"ht_foreach"
};
int (*test_functions[11])() = { int (*test_functions[11])() = {
test_vec_basic, test_vec_basic, test_vec_add, test_vec_remove, test_vec_find,
test_vec_add, test_vec_foreach, test_vec_index, test_ht_basic, test_ht_put,
test_vec_remove, test_ht_get, test_ht_remove, test_ht_foreach};
test_vec_find,
test_vec_foreach,
test_vec_index,
test_ht_basic,
test_ht_put,
test_ht_get,
test_ht_remove,
test_ht_foreach
};
int test_index = 0; int test_index = 0;
int result = 1; int result = 1;

View File

@ -1,6 +1,6 @@
#include "vec.h" #include "vec.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
libds_result vec_init(vec* v) { libds_result vec_init(vec* v) {
@ -84,7 +84,8 @@ void* vec_find(vec* v, void* data, compare_func compare){
} }
return found; return found;
} }
int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach, ...){ int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach,
...) {
int elements = v->size; int elements = v->size;
int index = 0; int index = 0;
int return_code = 0; int return_code = 0;