#include "toolbox/hashtable.h" #include "toolbox/args.h" #include "toolbox/hash.h" #include "toolbox/array.h" #include "toolbox/log.h" #include "toolbox/ansi_escape.h" #include "toolbox/errno.h" #include "toolbox/vptr.h" #include #include #include #include #include // TODO: Implement my own malloc that allocs a bit over that is write protected bool test_array(void); bool test_hashtable(void); bool test_args(void); uint8_t error = (uint8_t) -1; uint8_t nine_six = 96; uint8_t four_two = 42; uint8_t six_nine = 69; #define TEST(WHAT) if ( WHAT() ) { \ printf(FG_GREEN"SUCCESS "#WHAT"\n"RST_ALL); \ } else { \ printf(FG_RED""SET_BLINK"FAILED "#WHAT"\n"RST_ALL); \ } int main(int argc, char *argv[]) { (void) argc; (void) argv; #ifdef TOOLBOX_ARGS_H TEST(test_args) #endif #ifdef TOOLBOX_ARRAY_H TEST(test_array) #endif #ifdef TOOLBOX_HASHTABLE_H TEST(test_hashtable) #endif printf("Hello World!\n"); exit(EXIT_SUCCESS); } #ifdef TOOLBOX_ARRAY_H bool test_array(void) { printf("Start\t-> %s\n", __func__); struct array *arr = array_create(0, 1); if ( arr == NULL ) { LOG_ERROR("[create] Failed to create array"); return false; } array_append(arr, &six_nine); array_prepend(arr, &nine_six); { if ( array_set(arr, 0, &four_two) != RET_OK ) { LOG_ERROR("[set] Failed when wasn't suposed to"); return false; } if ( array_set(arr, 5, &four_two) == RET_OK ) { LOG_ERROR("[set] Succeded when wasn't suposed to"); return false; } } // array_set { if ( array_index_of(arr, &six_nine) != 1 ) { LOG_ERROR("[index_of] Returned the wrong index"); return false; } if ( array_index_of(arr, &nine_six) != (size_t) -1 ) { LOG_ERROR("[index_of] Returned index when wasn't suposed to"); return false; } } // array_index_of { if ( ! array_contain(arr, &four_two) ) { LOG_ERROR("[contain] Returned false when wasn't suposed to"); return false; } if ( array_contain(arr, &nine_six) ) { LOG_ERROR("[contain] Returned true when wasn't suposed to"); return false; } } // array_contain { if ( array_count(arr, &four_two) != 1 ) { LOG_ERROR("[count] Returned the wrong value"); return false; } if ( array_count(arr, &nine_six) != 0 ) { LOG_ERROR("[count] Returned the wrong value"); return false; } } // array_count array_delete(arr, 0); array_remove(arr, &six_nine); array_destroy(&arr); if ( arr != NULL ) { LOG_ERROR("[destroy] Failed to destroy array"); return false; } printf("End\t-> %s\n", __func__); return true; } #endif #ifdef TOOLBOX_HASHTABLE_H bool test_hashtable(void) { printf("Start\t-> %s\n", __func__); struct hashtable *dic = hashtable_create(); if ( dic == NULL ) { LOG_ERROR("[create] failed to create hashtable"); return false; } if ( ! hashtable_is_empty(dic) ) { LOG_ERROR("[is_empty] hashtable is said to not be empty when it is"); return false; } { if ( hashtable_put(dic, "nice", &six_nine, sizeof(six_nine)) != RET_OK ) { LOG_ERROR("[put] Failed to put item when wasn't supposed to"); return false; } if ( hashtable_put(dic, "nice", &nine_six, sizeof(nine_six)) == RET_OK) { LOG_ERROR("[put] Didn't fail to put item when was supposed to"); return false; } if ( hashtable_put(dic, "weed", &four_two, sizeof(four_two)) != RET_OK ) { LOG_ERROR("[put] Failed to put item when wasn't supposed to"); return false; } } // hashtable_put if ( hashtable_is_empty(dic) ) { LOG_ERROR("[is_empty] hashtable is reported empty when it isn't"); return false; } if ( *(uint8_t*) hashtable_get_or_default(dic, "nice", &error) == error ) { LOG_ERROR("[get_or_default] returning default"); return false; } if ( *(uint8_t*) hashtable_replace(dic, "nice" , &nine_six, sizeof(nine_six)) != six_nine ) { LOG_ERROR("[replace] return the wrong old value"); return false; } { if ( hashtable_contain_item(dic, &six_nine, sizeof(six_nine)) ) { LOG_ERROR("[contains_item] contain an unexpected item"); return false; } if ( ! hashtable_contain_item(dic, &nine_six, sizeof(nine_six)) ) { LOG_ERROR("[contains_item] does not contain an expected item"); return false; } } // hashtable_contain_item { if ( hashtable_contain_key(dic, "does not exist") ) { LOG_ERROR("[contain_key] contain an unexpected item"); return false; } if ( ! hashtable_contain_key(dic, "nice") ) { LOG_ERROR("[contain_key] does not contain an expected item"); return false; } } // hashtable_contains_key /* { */ /* size_t unexpedted_key = hash_cstr("does not exist") % HASHTABLE_SIZE; */ /* size_t expedted_key = hash_cstr("nice") % HASHTABLE_SIZE; */ /* if ( hashtable_contain_key_hash(dic, unexpedted_key) ) { */ /* LOG_ERROR("[contain_key_hash] contain an unexpected item"); */ /* return false; */ /* } */ /* if ( ! hashtable_contain_key_hash(dic, expedted_key) ) { */ /* LOG_ERROR("[contain_key_hash] does not contain an expected item"); */ /* return false; */ /* } */ /* } // hashtable_contain_key_hash */ if ( hashtable_replace_if_equal(dic, "nice" , &six_nine, sizeof(six_nine) , &four_two, sizeof(four_two)) ) { LOG_ERROR("[replace_if_equal] replaced when was not suposed to"); return false; } if ( *(uint8_t*)hashtable_get(dic, "nice") != nine_six ) { LOG_ERROR("[get] return the wrong value"); return false; } { if ( *(uint8_t*) hashtable_remove(dic, "nice") != nine_six ) { LOG_ERROR("[remove] Remove returned the wrong value"); return false; } if ( hashtable_remove(dic, "nice") != NULL ) { LOG_ERROR("[remove] Remove returned the value when was suposed to be NULL"); return false; } } // hashtable_remove { if ( ! hashtable_remove_if_equal(dic, "weed" , &four_two, sizeof(four_two)) ) { LOG_ERROR("[remove_if_equal] failed to remove item"); return false; } } // hashtable_remove_if_equal { if ( hashtable_used_size(dic) != 0 ) { LOG_ERROR("[used_size] Size was suposed to be 0"); return false; } } // hashtable_used_size hashtable_destroy(&dic); if ( dic != NULL ) { LOG_ERROR("[destroy] failed to destroy hashtable"); return false; } printf("End\t-> %s\n", __func__); return true; } #endif #ifdef TOOLBOX_ARGS_H bool test_args(void) { printf("Start\t-> %s\n", __func__); int test_argc = 7; char *test_argv[7] = { "program_name", "--int32=32", "--toggle", "-f12.345", "-d", "54.321567", "--string=This is a String" }; struct args arg = args_create(test_argc, test_argv); char str[255] = {0}; args_set_op_str(&arg, 's', "string" , "This is an String option test", false, str); int int32 = 0; args_set_op_int(&arg, 'i', "int32" , "This is an Int32 option test", true , sizeof(int32_t), &int32); float flo = 0.f; args_set_op_float(&arg, 'f', "float" , "This is an Float option test", false , false, &flo); double dou = 0.; args_set_op_float(&arg, 'd', "double" , "This is an double option test", false , true, &dou); bool toggle = false; args_set_toggle(&arg, 't', "toggle" , "This is a toggle test", false, &toggle); args_check(&arg); if ( strcmp(str, "This is a String") != 0 ) { LOG_ERROR("[args_set_op_str] Returned Wrong String"); return false; } if ( int32 != 32 ) { LOG_ERROR("[args_set_op_int] Returned Wrong value"); return false; } if ( flo != 12.345f ) { LOG_ERROR("[args_set_op_float] Returned Wrong value "); return false; } if ( dou != 54.321567 ) { LOG_ERROR("[args_set_op_float] Returned Wrong value "); return false; } if ( ! toggle ) { LOG_ERROR("[args_set_toggle] Returned wrong value"); return false; } printf("End\t-> %s\n", __func__); return true; } #endif