testing.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stdio.h>
  2. #include "test_multiaddr.h"
  3. const char* names[] = {
  4. "test_new_from_string",
  5. "test_full",
  6. "test_hex_to_var",
  7. "test_int_to_hex",
  8. "test_multiaddr_utils",
  9. "test_multiaddr_peer_id",
  10. "test_multiaddr_get_peer_id",
  11. "test_multiaddr_bytes",
  12. "test_new_like_libp2p"
  13. };
  14. int (*funcs[])(void) = {
  15. test_new_from_string,
  16. test_full,
  17. test_hex_to_var,
  18. test_int_to_hex,
  19. test_multiaddr_utils,
  20. test_multiaddr_peer_id,
  21. test_multiaddr_get_peer_id,
  22. test_multiaddr_bytes,
  23. test_new_like_libp2p
  24. };
  25. int testit(const char* name, int (*func)(void)) {
  26. printf("Testing %s...\n", name);
  27. int retVal = func();
  28. if (retVal)
  29. printf("%s success!\n", name);
  30. else
  31. printf("** Uh oh! %s failed.**\n", name);
  32. return retVal;
  33. }
  34. int main(int argc, char** argv) {
  35. int counter = 0;
  36. int tests_ran = 0;
  37. char* test_wanted;
  38. int only_one = 0;
  39. if(argc > 1) {
  40. only_one = 1;
  41. if (argv[1][0] == '\'') { // some shells put quotes around arguments
  42. argv[1][strlen(argv[1])-1] = 0;
  43. test_wanted = &(argv[1][1]);
  44. }
  45. else
  46. test_wanted = argv[1];
  47. }
  48. int array_length = sizeof(funcs) / sizeof(funcs[0]);
  49. int array2_length = sizeof(names) / sizeof(names[0]);
  50. if (array_length != array2_length) {
  51. printf("Test arrays are not of the same length. Funcs: %d, Names: %d\n", array_length, array2_length);
  52. }
  53. for (int i = 0; i < array_length; i++) {
  54. if (only_one) {
  55. const char* currName = names[i];
  56. if (strcmp(currName, test_wanted) == 0) {
  57. tests_ran++;
  58. counter += testit(names[i], funcs[i]);
  59. }
  60. }
  61. else
  62. if (!only_one) {
  63. tests_ran++;
  64. counter += testit(names[i], funcs[i]);
  65. }
  66. }
  67. if (tests_ran == 0)
  68. printf("***** No tests found *****\n");
  69. else {
  70. if (tests_ran - counter > 0) {
  71. printf("***** There were %d failed test(s) (%d successful) *****\n", tests_ran - counter, counter);
  72. } else {
  73. printf("All %d tests passed\n", tests_ran);
  74. }
  75. }
  76. return 1;
  77. }