Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

104 rindas
2.4KB

  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){
  21. if (passed) {
  22. printf("\033[32;1mSuccess\033[0;1m. - \033[0m");
  23. } else {
  24. printf("\033[31;1mFailiour\033[0;1m. - \033[0m");
  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. regex_t * r = regex_compile(what);
  39. bool result = regex_search(r, on);
  40. regex_free(r);
  41. bool passed = (result == expect);
  42. expect ? ++positives : ++negatives;
  43. print_leader(passed);
  44. char * quoted_what, * quoted_on;
  45. asprintf(&quoted_what, "'%s'", what);
  46. asprintf(&quoted_on, "'%s'", on);
  47. printf("%14s\033[1m vs \033[0m%14s\033[1m:\033[0m Result = %d, Expected = %d\n", quoted_what, quoted_on, result, expect);
  48. free(quoted_what);
  49. free(quoted_on);
  50. if (passed) {
  51. ++passed_tests;
  52. expect ? ++positive_successes : ++negative_successes;
  53. }
  54. ++test_counter;
  55. }
  56. static
  57. void TEST2(const char * const what,
  58. const char * const on,
  59. const match_t expect){
  60. do_flush();
  61. regex_t * r = regex_compile(what);
  62. match_t * result = regex_match(r, on, true);
  63. bool passed = (result->position == expect.position
  64. && result->width == expect.width
  65. );
  66. print_leader(passed);
  67. char * quoted_what, * quoted_on;
  68. asprintf(&quoted_what, "'%s'", what);
  69. asprintf(&quoted_on, "'%s'", on);
  70. char * result_string, * expect_string;
  71. asprint_match_t(&result_string, result);
  72. asprint_match_t(&expect_string, &expect);
  73. 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);
  74. free(quoted_what);
  75. free(quoted_on);
  76. free(result_string);
  77. free(expect_string);
  78. free(result);
  79. if (passed) {
  80. ++passed_tests2;
  81. }
  82. ++test_counter2;
  83. }