Format code.
This commit is contained in:
parent
d41a085bcd
commit
52640ed8f9
3
.clang-format
Normal file
3
.clang-format
Normal file
|
@ -0,0 +1,3 @@
|
|||
DerivePointerAlignment: false
|
||||
PointerAlignment: Left
|
||||
BasedOnStyle: google
|
|
@ -3,10 +3,7 @@
|
|||
|
||||
#include <stdarg.h>
|
||||
|
||||
enum libds_result_e {
|
||||
SUCCESS,
|
||||
MALLOC
|
||||
};
|
||||
enum libds_result_e { SUCCESS, MALLOC };
|
||||
|
||||
typedef enum libds_result_e libds_result;
|
||||
typedef int (*compare_func)(void*, void*);
|
||||
|
|
14
src/ht.c
14
src/ht.c
|
@ -1,6 +1,6 @@
|
|||
#include "ht.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
unsigned int ht_default_hash_func(void* data) {
|
||||
const unsigned int fnv_prime = 16777619;
|
||||
|
@ -15,9 +15,7 @@ unsigned int ht_default_hash_func(void* data){
|
|||
}
|
||||
return hash;
|
||||
}
|
||||
int ht_default_cmp_func(void* a, void* b){
|
||||
return strcmp(a, b) == 0;
|
||||
}
|
||||
int ht_default_cmp_func(void* a, void* b) { return strcmp(a, b) == 0; }
|
||||
void* ht_default_copy_func(void* from) {
|
||||
void* copy = malloc(sizeof(char) * (strlen(from) + 1));
|
||||
if (copy) {
|
||||
|
@ -25,9 +23,7 @@ void* ht_default_copy_func(void* from) {
|
|||
}
|
||||
return copy;
|
||||
}
|
||||
void ht_default_free_func(void* data){
|
||||
free(data);
|
||||
}
|
||||
void ht_default_free_func(void* data) { free(data); }
|
||||
|
||||
void ht_init(ht* ht) {
|
||||
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;
|
||||
unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
|
||||
|
||||
|
||||
new_node = malloc(sizeof(*new_node));
|
||||
if (new_node) {
|
||||
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 return_code = 0;
|
||||
va_list args;
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
#include "libds.h"
|
||||
|
||||
int compare_always(void* a, void* b){
|
||||
return 1;
|
||||
}
|
||||
int compare_always(void* a, void* b) { return 1; }
|
||||
|
|
61
src/main.c
61
src/main.c
|
@ -1,9 +1,9 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "vec.h"
|
||||
#include "ht.h"
|
||||
#include "vec.h"
|
||||
|
||||
int _test_vec_foreach_func(void* data, va_list args) {
|
||||
char* real_data = data;
|
||||
|
@ -12,9 +12,7 @@ int _test_vec_foreach_func(void* data, va_list args){
|
|||
*sum += *real_data;
|
||||
return 0;
|
||||
}
|
||||
int _test_vec_find_string(void* a, void* b){
|
||||
return strcmp(a, b) == 0;
|
||||
}
|
||||
int _test_vec_find_string(void* a, void* b) { return strcmp(a, b) == 0; }
|
||||
|
||||
int test_vec_basic() {
|
||||
vec test_vec;
|
||||
|
@ -55,7 +53,8 @@ int test_vec_find(){
|
|||
|
||||
vec_init(&test_vec);
|
||||
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);
|
||||
return return_code;
|
||||
}
|
||||
|
@ -63,29 +62,22 @@ int test_vec_foreach(){
|
|||
vec test_vec;
|
||||
int return_code;
|
||||
int sum = 0;
|
||||
char* test_strings[3] = {
|
||||
"1",
|
||||
"2",
|
||||
"3"
|
||||
};
|
||||
char* test_strings[3] = {"1", "2", "3"};
|
||||
|
||||
vec_init(&test_vec);
|
||||
vec_add(&test_vec, test_strings[0]);
|
||||
vec_add(&test_vec, test_strings[1]);
|
||||
vec_add(&test_vec, test_strings[2]);
|
||||
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);
|
||||
return return_code;
|
||||
}
|
||||
int test_vec_index() {
|
||||
vec test_vec;
|
||||
int return_code;
|
||||
char* test_strings[3] = {
|
||||
"1",
|
||||
"2",
|
||||
"3"
|
||||
};
|
||||
char* test_strings[3] = {"1", "2", "3"};
|
||||
|
||||
vec_init(&test_vec);
|
||||
vec_add(&test_vec, test_strings[0]);
|
||||
|
@ -130,7 +122,8 @@ int test_ht_get() {
|
|||
ht_init(&test_ht);
|
||||
ht_put(&test_ht, test_one, test_two);
|
||||
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);
|
||||
return return_code;
|
||||
}
|
||||
|
@ -171,34 +164,14 @@ int run_test(char* test_name, int (*test_func)()) {
|
|||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
char* test_names[11] = {
|
||||
"vec_basic",
|
||||
"vec_add",
|
||||
"vec_remove",
|
||||
"vec_find",
|
||||
"vec_foreach",
|
||||
"vec_index",
|
||||
"ht_basic",
|
||||
"ht_put",
|
||||
"ht_get",
|
||||
"ht_remove",
|
||||
"ht_foreach"
|
||||
};
|
||||
char* test_names[11] = {"vec_basic", "vec_add", "vec_remove", "vec_find",
|
||||
"vec_foreach", "vec_index", "ht_basic", "ht_put",
|
||||
"ht_get", "ht_remove", "ht_foreach"};
|
||||
|
||||
int (*test_functions[11])() = {
|
||||
test_vec_basic,
|
||||
test_vec_add,
|
||||
test_vec_remove,
|
||||
test_vec_find,
|
||||
test_vec_foreach,
|
||||
test_vec_index,
|
||||
test_ht_basic,
|
||||
test_ht_put,
|
||||
test_ht_get,
|
||||
test_ht_remove,
|
||||
test_ht_foreach
|
||||
};
|
||||
test_vec_basic, test_vec_add, test_vec_remove, 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 result = 1;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "vec.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
libds_result vec_init(vec* v) {
|
||||
|
@ -84,7 +84,8 @@ void* vec_find(vec* v, void* data, compare_func compare){
|
|||
}
|
||||
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 index = 0;
|
||||
int return_code = 0;
|
||||
|
|
Loading…
Reference in New Issue
Block a user