substring_test.c 3.7 KB

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