Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

102 lines
2.5KB

  1. #include "jeger.h"
  2. static int test_counter = 0;
  3. static int passed_tests = 0;
  4. static int positives = 0;
  5. static int positive_successes = 0;
  6. static int negatives = 0;
  7. static int negative_successes = 0;
  8. static int test_counter2 = 0;
  9. static int passed_tests2 = 0;
  10. static
  11. void asprint_match_t( char * * destination,
  12. const match_t * const match) {
  13. if (match) {
  14. asprintf(destination, "%p {%d, %d}", (void *)match, match->position, match->width);
  15. } else {
  16. asprintf(destination, "0x000000000000 {N/A, N/A}");
  17. }
  18. }
  19. static
  20. void print_leader(const bool passed, const int n) {
  21. if (passed) {
  22. printf("\033[32;1mSuccess\033[0m. %02d\033[1m - \033[0m", n);
  23. } else {
  24. printf("\033[31;1mFailure\033[0m. %02d\033[1m - \033[0m", n);
  25. }
  26. }
  27. static
  28. void do_flush(void) {
  29. if(!(test_counter % 5)) {
  30. fflush(stdout);
  31. }
  32. }
  33. static
  34. void TEST(const char * const what,
  35. const char * const on,
  36. const bool expect) {
  37. do_flush();
  38. ++test_counter;
  39. regex_t * r = regex_compile(what);
  40. bool result = regex_search(r, on);
  41. regex_free(r);
  42. bool passed = (result == expect);
  43. expect ? ++positives : ++negatives;
  44. print_leader(passed, test_counter);
  45. char * quoted_what, * quoted_on;
  46. asprintf(&quoted_what, "'%s'", what);
  47. asprintf(&quoted_on, "'%s'", on);
  48. printf("%14s\033[1m vs \033[0m%14s\033[1m:\033[0m Result = %d, Expected = %d\n", quoted_what, quoted_on, result, expect);
  49. free(quoted_what);
  50. free(quoted_on);
  51. if (passed) {
  52. ++passed_tests;
  53. expect ? ++positive_successes : ++negative_successes;
  54. }
  55. }
  56. static
  57. void TEST2(const char * const what,
  58. const char * const on,
  59. const match_t expect){
  60. do_flush();
  61. ++test_counter2;
  62. regex_t * r = regex_compile(what);
  63. match_t * result = regex_match(r, on, true);
  64. bool passed = (result->position == expect.position
  65. && result->width == expect.width
  66. );
  67. print_leader(passed, test_counter2);
  68. char * quoted_what, * quoted_on;
  69. asprintf(&quoted_what, "'%s'", what);
  70. asprintf(&quoted_on, "'%s'", on);
  71. char * result_string, * expect_string;
  72. asprint_match_t(&result_string, result);
  73. asprint_match_t(&expect_string, &expect);
  74. printf("%s\033[1m vs \033[0m%s\033[1m:\033[0m\n\tResult = %s\n\tExpected = %s\n", quoted_what, quoted_on, result_string, expect_string);
  75. free(quoted_what);
  76. free(quoted_on);
  77. free(result_string);
  78. free(expect_string);
  79. free(result);
  80. if (passed) {
  81. ++passed_tests2;
  82. }
  83. }