substring_test.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @file substring_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <time.h>
  33. #include <misc/substring.h>
  34. #include <misc/debug.h>
  35. #include <misc/balloc.h>
  36. #include <misc/print_macros.h>
  37. static int find_substring_slow (const char *str, size_t str_len, const char *sub, size_t sub_len, size_t *out_pos)
  38. {
  39. ASSERT(sub_len > 0)
  40. if (str_len < sub_len) {
  41. return 0;
  42. }
  43. for (size_t i = 0; i <= str_len - sub_len; i++) {
  44. if (!memcmp(str + i, sub, sub_len)) {
  45. *out_pos = i;
  46. return 1;
  47. }
  48. }
  49. return 0;
  50. }
  51. static void print_data (const char *str, size_t str_len)
  52. {
  53. while (str_len > 0) {
  54. printf("%02"PRIx8" ", (uint8_t)(*str));
  55. str++;
  56. str_len--;
  57. }
  58. printf("\n");
  59. }
  60. static void print_table (const size_t *table, size_t len)
  61. {
  62. for (size_t i = 1; i < len; i++) {
  63. printf("%zu ", table[i]);
  64. }
  65. printf("\n");
  66. }
  67. static void test_tables (int len, int count)
  68. {
  69. ASSERT(len > 0)
  70. ASSERT(count >= 0)
  71. char *word = (char *)BAllocSize(bsize_fromint(len));
  72. ASSERT_FORCE(word)
  73. size_t *table = (size_t *)BAllocSize(bsize_mul(bsize_fromint(len), bsize_fromsize(sizeof(table[0]))));
  74. ASSERT_FORCE(table)
  75. for (int i = 0; i < count; i++) {
  76. for (int j = 0; j < len; j++) {
  77. word[j] = rand() % 2;
  78. }
  79. build_substring_backtrack_table(MemRef_Make(word, len), table);
  80. for (int j = 1; j < len; j++) {
  81. for (int k = j - 1; k >= 0; k--) {
  82. if (!memcmp(word + j - k, word, k)) {
  83. ASSERT_FORCE(table[j] == k)
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. BFree(table);
  90. BFree(word);
  91. }
  92. static void test_substring (int word_len, int text_len, int word_count, int text_count)
  93. {
  94. assert(word_len > 0);
  95. assert(text_len >= 0);
  96. assert(word_count >= 0);
  97. assert(text_count >= 0);
  98. char *word = (char *)BAllocSize(bsize_fromint(word_len));
  99. ASSERT_FORCE(word)
  100. size_t *table = (size_t *)BAllocSize(bsize_mul(bsize_fromint(word_len), bsize_fromsize(sizeof(table[0]))));
  101. ASSERT_FORCE(table)
  102. char *text = (char *)BAllocSize(bsize_fromint(text_len));
  103. ASSERT_FORCE(text)
  104. for (int i = 0; i < word_count; i++) {
  105. for (int j = 0; j < word_len; j++) {
  106. word[j] = rand() % 2;
  107. }
  108. build_substring_backtrack_table(MemRef_Make(word, word_len), table);
  109. for (int j = 0; j < text_count; j++) {
  110. for (int k = 0; k < text_len; k++) {
  111. text[k] = rand() % 2;
  112. }
  113. size_t pos = 36; // to remove warning
  114. int res = find_substring(MemRef_Make(text, text_len), MemRef_Make(word, word_len), table, &pos);
  115. size_t spos = 59; // to remove warning
  116. int sres = find_substring_slow(text, text_len, word, word_len, &spos);
  117. ASSERT_FORCE(res == sres)
  118. if (res) {
  119. ASSERT_FORCE(pos == spos)
  120. }
  121. }
  122. }
  123. BFree(text);
  124. BFree(table);
  125. BFree(word);
  126. }
  127. int main (int argc, char *argv[])
  128. {
  129. if (argc != 7) {
  130. printf("Usage: %s <tables length> <tables count> <word len> <text len> <word count> <text count>\n", (argc == 0 ? "" : argv[0]));
  131. return 1;
  132. }
  133. int tables_len = atoi(argv[1]);
  134. int tables_count = atoi(argv[2]);
  135. int word_len = atoi(argv[3]);
  136. int text_len = atoi(argv[4]);
  137. int word_count = atoi(argv[5]);
  138. int text_count = atoi(argv[6]);
  139. if (tables_len <= 0 || tables_count < 0 || word_len <= 0 || text_len < 0 || word_count < 0 || text_count < 0) {
  140. printf("Bad arguments.\n");
  141. return 1;
  142. }
  143. srand(time(NULL));
  144. test_tables(tables_len, tables_count);
  145. test_substring(word_len, text_len, word_count, text_count);
  146. {
  147. char text[] = "aggagaa";
  148. char word[] = "aga";
  149. size_t table[sizeof(word) - 1];
  150. build_substring_backtrack_table(MemRef_MakeCstr(word), table);
  151. size_t pos;
  152. int res = find_substring(MemRef_MakeCstr(text), MemRef_MakeCstr(word), table, &pos);
  153. ASSERT_FORCE(res)
  154. ASSERT_FORCE(pos == 3)
  155. }
  156. {
  157. char text[] = "aagagga";
  158. char word[] = "aga";
  159. size_t table[sizeof(word) - 1];
  160. build_substring_backtrack_table_reverse(MemRef_MakeCstr(word), table);
  161. size_t pos;
  162. int res = find_substring_reverse(MemRef_MakeCstr(text), MemRef_MakeCstr(word), table, &pos);
  163. ASSERT_FORCE(res)
  164. ASSERT_FORCE(pos == 1)
  165. }
  166. return 0;
  167. }