substring_test.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <misc/substring.h>
  6. #include <misc/debug.h>
  7. #include <misc/balloc.h>
  8. #include <misc/print_macros.h>
  9. static int find_substring_slow (const char *str, size_t str_len, const char *sub, size_t sub_len, size_t *out_pos)
  10. {
  11. ASSERT(sub_len > 0)
  12. if (str_len < sub_len) {
  13. return 0;
  14. }
  15. for (size_t i = 0; i <= str_len - sub_len; i++) {
  16. if (!memcmp(str + i, sub, sub_len)) {
  17. *out_pos = i;
  18. return 1;
  19. }
  20. }
  21. return 0;
  22. }
  23. static void print_data (const char *str, size_t str_len)
  24. {
  25. while (str_len > 0) {
  26. printf("%02"PRIx8" ", (uint8_t)(*str));
  27. str++;
  28. str_len--;
  29. }
  30. printf("\n");
  31. }
  32. static void print_table (const size_t *table, size_t len)
  33. {
  34. for (size_t i = 1; i < len; i++) {
  35. printf("%zu ", table[i]);
  36. }
  37. printf("\n");
  38. }
  39. static void test_tables (int len, int count)
  40. {
  41. ASSERT(len > 0)
  42. ASSERT(count >= 0)
  43. char *word = (char *)BAllocSize(bsize_fromint(len));
  44. ASSERT_FORCE(word)
  45. size_t *table = (size_t *)BAllocSize(bsize_mul(bsize_fromint(len), bsize_fromsize(sizeof(table[0]))));
  46. ASSERT_FORCE(table)
  47. for (int i = 0; i < count; i++) {
  48. for (int j = 0; j < len; j++) {
  49. word[j] = rand() % 2;
  50. }
  51. build_substring_backtrack_table(word, len, table);
  52. for (int j = 1; j < len; j++) {
  53. for (int k = j - 1; k >= 0; k--) {
  54. if (!memcmp(word + j - k, word, k)) {
  55. ASSERT_FORCE(table[j] == k)
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. BFree(table);
  62. BFree(word);
  63. }
  64. static void test_substring (int word_len, int text_len, int word_count, int text_count)
  65. {
  66. assert(word_len > 0);
  67. assert(text_len >= 0);
  68. assert(word_count >= 0);
  69. assert(text_count >= 0);
  70. char *word = (char *)BAllocSize(bsize_fromint(word_len));
  71. ASSERT_FORCE(word)
  72. size_t *table = (size_t *)BAllocSize(bsize_mul(bsize_fromint(word_len), bsize_fromsize(sizeof(table[0]))));
  73. ASSERT_FORCE(table)
  74. char *text = (char *)BAllocSize(bsize_fromint(text_len));
  75. ASSERT_FORCE(text)
  76. for (int i = 0; i < word_count; i++) {
  77. for (int j = 0; j < word_len; j++) {
  78. word[j] = rand() % 2;
  79. }
  80. build_substring_backtrack_table(word, word_len, table);
  81. for (int j = 0; j < text_count; j++) {
  82. for (int k = 0; k < text_len; k++) {
  83. text[k] = rand() % 2;
  84. }
  85. size_t pos;
  86. int res = find_substring(text, text_len, word, word_len, table, &pos);
  87. size_t spos;
  88. int sres = find_substring_slow(text, text_len, word, word_len, &spos);
  89. ASSERT_FORCE(res == sres)
  90. if (res) {
  91. ASSERT_FORCE(pos == spos)
  92. }
  93. }
  94. }
  95. BFree(text);
  96. BFree(table);
  97. BFree(word);
  98. }
  99. int main (int argc, char *argv[])
  100. {
  101. if (argc != 7) {
  102. printf("Usage: %s <tables length> <tables count> <word len> <text len> <word count> <text count>\n", (argc == 0 ? "" : argv[0]));
  103. return 1;
  104. }
  105. int tables_len = atoi(argv[1]);
  106. int tables_count = atoi(argv[2]);
  107. int word_len = atoi(argv[3]);
  108. int text_len = atoi(argv[4]);
  109. int word_count = atoi(argv[5]);
  110. int text_count = atoi(argv[6]);
  111. if (tables_len <= 0 || tables_count < 0 || word_len <= 0 || text_len < 0 || word_count < 0 || text_count < 0) {
  112. printf("Bad arguments.\n");
  113. return 1;
  114. }
  115. srand(time(NULL));
  116. test_tables(tables_len, tables_count);
  117. test_substring(word_len, text_len, word_count, text_count);
  118. return 0;
  119. }