main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include "toolbox/hashtable.h"
  2. #include "toolbox/args.h"
  3. #include "toolbox/hash.h"
  4. #include "toolbox/array.h"
  5. #include "toolbox/log.h"
  6. #include "toolbox/ansi_escape.h"
  7. #include "toolbox/errno.h"
  8. #include "toolbox/vptr.h"
  9. #include <stdbool.h>
  10. #include <stddef.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. // TODO: Implement my own malloc that allocs a bit over that is write protected
  15. bool test_array(void);
  16. bool test_hashtable(void);
  17. bool test_args(void);
  18. uint8_t error = (uint8_t) -1;
  19. uint8_t nine_six = 96;
  20. uint8_t four_two = 42;
  21. uint8_t six_nine = 69;
  22. #define TEST(WHAT) if ( WHAT() ) { \
  23. printf(FG_GREEN"SUCCESS "#WHAT"\n"RST_ALL); \
  24. } else { \
  25. printf(FG_RED""SET_BLINK"FAILED "#WHAT"\n"RST_ALL); \
  26. }
  27. int main(int argc, char *argv[]) {
  28. (void) argc;
  29. (void) argv;
  30. #ifdef TOOLBOX_ARGS_H
  31. TEST(test_args)
  32. #endif
  33. #ifdef TOOLBOX_ARRAY_H
  34. TEST(test_array)
  35. #endif
  36. #ifdef TOOLBOX_HASHTABLE_H
  37. TEST(test_hashtable)
  38. #endif
  39. printf("Hello World!\n");
  40. exit(EXIT_SUCCESS);
  41. }
  42. #ifdef TOOLBOX_ARRAY_H
  43. bool test_array(void) {
  44. printf("Start\t-> %s\n", __func__);
  45. struct array *arr = array_create(0, 1);
  46. if ( arr == NULL ) {
  47. LOG_ERROR("[create] Failed to create array");
  48. return false;
  49. }
  50. array_append(arr, &six_nine);
  51. array_prepend(arr, &nine_six);
  52. {
  53. if ( array_set(arr, 0, &four_two) != RET_OK ) {
  54. LOG_ERROR("[set] Failed when wasn't suposed to");
  55. return false;
  56. }
  57. if ( array_set(arr, 5, &four_two) == RET_OK ) {
  58. LOG_ERROR("[set] Succeded when wasn't suposed to");
  59. return false;
  60. }
  61. } // array_set
  62. {
  63. if ( array_index_of(arr, &six_nine) != 1 ) {
  64. LOG_ERROR("[index_of] Returned the wrong index");
  65. return false;
  66. }
  67. if ( array_index_of(arr, &nine_six) != (size_t) -1 ) {
  68. LOG_ERROR("[index_of] Returned index when wasn't suposed to");
  69. return false;
  70. }
  71. } // array_index_of
  72. {
  73. if ( ! array_contain(arr, &four_two) ) {
  74. LOG_ERROR("[contain] Returned false when wasn't suposed to");
  75. return false;
  76. }
  77. if ( array_contain(arr, &nine_six) ) {
  78. LOG_ERROR("[contain] Returned true when wasn't suposed to");
  79. return false;
  80. }
  81. } // array_contain
  82. {
  83. if ( array_count(arr, &four_two) != 1 ) {
  84. LOG_ERROR("[count] Returned the wrong value");
  85. return false;
  86. }
  87. if ( array_count(arr, &nine_six) != 0 ) {
  88. LOG_ERROR("[count] Returned the wrong value");
  89. return false;
  90. }
  91. } // array_count
  92. array_delete(arr, 0);
  93. array_remove(arr, &six_nine);
  94. array_destroy(&arr);
  95. if ( arr != NULL ) {
  96. LOG_ERROR("[destroy] Failed to destroy array");
  97. return false;
  98. }
  99. printf("End\t-> %s\n", __func__);
  100. return true;
  101. }
  102. #endif
  103. #ifdef TOOLBOX_HASHTABLE_H
  104. bool test_hashtable(void) {
  105. printf("Start\t-> %s\n", __func__);
  106. struct hashtable *dic = hashtable_create();
  107. if ( dic == NULL ) {
  108. LOG_ERROR("[create] failed to create hashtable");
  109. return false;
  110. }
  111. if ( ! hashtable_is_empty(dic) ) {
  112. LOG_ERROR("[is_empty] hashtable is said to not be empty when it is");
  113. return false;
  114. }
  115. {
  116. if ( hashtable_put(dic, "nice", &six_nine, sizeof(six_nine))
  117. != RET_OK ) {
  118. LOG_ERROR("[put] Failed to put item when wasn't supposed to");
  119. return false;
  120. }
  121. if ( hashtable_put(dic, "nice", &nine_six, sizeof(nine_six))
  122. == RET_OK) {
  123. LOG_ERROR("[put] Didn't fail to put item when was supposed to");
  124. return false;
  125. }
  126. if ( hashtable_put(dic, "weed", &four_two, sizeof(four_two))
  127. != RET_OK ) {
  128. LOG_ERROR("[put] Failed to put item when wasn't supposed to");
  129. return false;
  130. }
  131. } // hashtable_put
  132. if ( hashtable_is_empty(dic) ) {
  133. LOG_ERROR("[is_empty] hashtable is reported empty when it isn't");
  134. return false;
  135. }
  136. if ( *(uint8_t*) hashtable_get_or_default(dic, "nice", &error) == error ) {
  137. LOG_ERROR("[get_or_default] returning default");
  138. return false;
  139. }
  140. if ( *(uint8_t*) hashtable_replace(dic, "nice"
  141. , &nine_six, sizeof(nine_six))
  142. != six_nine ) {
  143. LOG_ERROR("[replace] return the wrong old value");
  144. return false;
  145. }
  146. {
  147. if ( hashtable_contain_item(dic, &six_nine, sizeof(six_nine)) ) {
  148. LOG_ERROR("[contains_item] contain an unexpected item");
  149. return false;
  150. }
  151. if ( ! hashtable_contain_item(dic, &nine_six, sizeof(nine_six)) ) {
  152. LOG_ERROR("[contains_item] does not contain an expected item");
  153. return false;
  154. }
  155. } // hashtable_contain_item
  156. {
  157. if ( hashtable_contain_key(dic, "does not exist") ) {
  158. LOG_ERROR("[contain_key] contain an unexpected item");
  159. return false;
  160. }
  161. if ( ! hashtable_contain_key(dic, "nice") ) {
  162. LOG_ERROR("[contain_key] does not contain an expected item");
  163. return false;
  164. }
  165. } // hashtable_contains_key
  166. /* { */
  167. /* size_t unexpedted_key = hash_cstr("does not exist") % HASHTABLE_SIZE; */
  168. /* size_t expedted_key = hash_cstr("nice") % HASHTABLE_SIZE; */
  169. /* if ( hashtable_contain_key_hash(dic, unexpedted_key) ) { */
  170. /* LOG_ERROR("[contain_key_hash] contain an unexpected item"); */
  171. /* return false; */
  172. /* } */
  173. /* if ( ! hashtable_contain_key_hash(dic, expedted_key) ) { */
  174. /* LOG_ERROR("[contain_key_hash] does not contain an expected item"); */
  175. /* return false; */
  176. /* } */
  177. /* } // hashtable_contain_key_hash */
  178. if ( hashtable_replace_if_equal(dic, "nice"
  179. , &six_nine, sizeof(six_nine)
  180. , &four_two, sizeof(four_two)) ) {
  181. LOG_ERROR("[replace_if_equal] replaced when was not suposed to");
  182. return false;
  183. }
  184. if ( *(uint8_t*)hashtable_get(dic, "nice") != nine_six ) {
  185. LOG_ERROR("[get] return the wrong value");
  186. return false;
  187. }
  188. {
  189. if ( *(uint8_t*) hashtable_remove(dic, "nice") != nine_six ) {
  190. LOG_ERROR("[remove] Remove returned the wrong value");
  191. return false;
  192. }
  193. if ( hashtable_remove(dic, "nice") != NULL ) {
  194. LOG_ERROR("[remove] Remove returned the value when was suposed to be NULL");
  195. return false;
  196. }
  197. } // hashtable_remove
  198. {
  199. if ( ! hashtable_remove_if_equal(dic, "weed"
  200. , &four_two, sizeof(four_two)) ) {
  201. LOG_ERROR("[remove_if_equal] failed to remove item");
  202. return false;
  203. }
  204. } // hashtable_remove_if_equal
  205. {
  206. if ( hashtable_used_size(dic) != 0 ) {
  207. LOG_ERROR("[used_size] Size was suposed to be 0");
  208. return false;
  209. }
  210. } // hashtable_used_size
  211. hashtable_destroy(&dic);
  212. if ( dic != NULL ) {
  213. LOG_ERROR("[destroy] failed to destroy hashtable");
  214. return false;
  215. }
  216. printf("End\t-> %s\n", __func__);
  217. return true;
  218. }
  219. #endif
  220. #ifdef TOOLBOX_ARGS_H
  221. bool test_args(void) {
  222. printf("Start\t-> %s\n", __func__);
  223. int test_argc = 7;
  224. char *test_argv[7] = {
  225. "program_name",
  226. "--int32=32",
  227. "--toggle",
  228. "-f12.345",
  229. "-d",
  230. "54.321567",
  231. "--string=This is a String"
  232. };
  233. struct args arg = args_create(test_argc, test_argv);
  234. char str[255] = {0};
  235. args_set_op_str(&arg, 's', "string"
  236. , "This is an String option test", false, str);
  237. int int32 = 0;
  238. args_set_op_int(&arg, 'i', "int32"
  239. , "This is an Int32 option test", true
  240. , sizeof(int32_t), &int32);
  241. float flo = 0.f;
  242. args_set_op_float(&arg, 'f', "float"
  243. , "This is an Float option test", false
  244. , false, &flo);
  245. double dou = 0.;
  246. args_set_op_float(&arg, 'd', "double"
  247. , "This is an double option test", false
  248. , true, &dou);
  249. bool toggle = false;
  250. args_set_toggle(&arg, 't', "toggle"
  251. , "This is a toggle test", false, &toggle);
  252. args_check(&arg);
  253. if ( strcmp(str, "This is a String") != 0 ) {
  254. LOG_ERROR("[args_set_op_str] Returned Wrong String");
  255. return false;
  256. }
  257. if ( int32 != 32 ) {
  258. LOG_ERROR("[args_set_op_int] Returned Wrong value");
  259. return false;
  260. }
  261. if ( flo != 12.345f ) {
  262. LOG_ERROR("[args_set_op_float] Returned Wrong value ");
  263. return false;
  264. }
  265. if ( dou != 54.321567 ) {
  266. LOG_ERROR("[args_set_op_float] Returned Wrong value ");
  267. return false;
  268. }
  269. if ( ! toggle ) {
  270. LOG_ERROR("[args_set_toggle] Returned wrong value");
  271. return false;
  272. }
  273. printf("End\t-> %s\n", __func__);
  274. return true;
  275. }
  276. #endif