Advertisement
2607

grep_test_grep.c

Dec 5th, 2021
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <check.h>
  5.  
  6.  
  7. int read_file(char** buffer, size_t* size, const char* file);
  8.  
  9. #define DESTROY(VALUE) if (VALUE) free(VALUE)
  10.  
  11. #define TEST_GREP(STRING)                                   \
  12.     {                                                       \
  13.         system("./s21_"STRING" >1");                        \
  14.         system(STRING" >2");                                \
  15.         char* buffer1 = NULL;                               \
  16.         char* buffer2 = NULL;                               \
  17.         size_t size1;                                       \
  18.         size_t size2;                                       \
  19.         if (read_file(&buffer1, &size1, "1")) {             \
  20.             printf("%s\n", strerror(errno));                \
  21.         } else {                                            \
  22.             if (read_file(&buffer2, &size2, "2")) {         \
  23.                 printf("%s\n", strerror(errno));            \
  24.             } else {                                        \
  25.                 ck_assert_int_eq(size1, size2);             \
  26.                 ck_assert_mem_eq(buffer1, buffer2, size1);  \
  27.                 DESTROY(buffer2);                           \
  28.             }                                               \
  29.             DESTROY(buffer1);                               \
  30.         }                                                   \
  31.     }
  32.  
  33.  
  34. START_TEST(s21_grep_test_l) {
  35.     TEST_GREP("grep -l -e if ttt.txt");
  36.     TEST_GREP("grep -l -e for -e if ttt.txt");
  37.     TEST_GREP("grep -li -e for -e if ttt.txt");
  38.     TEST_GREP("grep -l -e return ttt.txt");
  39.     TEST_GREP("grep -lv -e return ttt.txt");
  40.     TEST_GREP("grep -li -e IF ttt.txt");
  41.     TEST_GREP("grep -lin -e IF ttt.txt");
  42.     TEST_GREP("grep -liv -e IF ttt.txt");
  43.     TEST_GREP("grep -l -e argv ttt.txt file.txt");
  44.     TEST_GREP("grep -lv -e argv ttt.txt file.txt");
  45.     TEST_GREP("grep -l -e argv ttt.txt file.txt1");
  46.     TEST_GREP("grep -ls -e argv ttt.txt file.txt1");
  47. }
  48. END_TEST
  49.  
  50. START_TEST(s21_grep_test_c) {
  51.     TEST_GREP("grep -c -e if ttt.txt");
  52.     TEST_GREP("grep -c -e for -e if ttt.txt");
  53.     TEST_GREP("grep -ci -e for -e if ttt.txt");
  54.     TEST_GREP("grep -c -e return ttt.txt");
  55.     TEST_GREP("grep -cv -e return ttt.txt");
  56.     TEST_GREP("grep -ci -e IF ttt.txt");
  57.     TEST_GREP("grep -cin -e IF ttt.txt");
  58.     TEST_GREP("grep -civ -e IF ttt.txt");
  59.     TEST_GREP("grep -c -e argv ttt.txt file.txt");
  60.     TEST_GREP("grep -cv -e argv ttt.txt file.txt");
  61.     TEST_GREP("grep -c -e argv ttt.txt file.txt1");
  62.     TEST_GREP("grep -ch -e argv ttt.txt file.txt");
  63.     TEST_GREP("grep -cvh -e argv ttt.txt file.txt");
  64.     TEST_GREP("grep -ch -e argv ttt.txt file.txt1");
  65.     TEST_GREP("grep -chs -e argv ttt.txt file.txt1");
  66. }
  67. END_TEST
  68.  
  69. START_TEST(s21_grep_test_o) {
  70.     TEST_GREP("grep -o -e if ttt.txt");
  71.     TEST_GREP("grep -o -e for -e if ttt.txt");
  72.     TEST_GREP("grep -oi -e for -e if ttt.txt");
  73.     TEST_GREP("grep -oin -e for -e if ttt.txt");
  74.     TEST_GREP("grep -oin -e for -e if ttt.txt file.txt");
  75.     // TEST_GREP("grep -oin -e ty -e if ttt.txt file.txt");       // !!!
  76.     TEST_GREP("grep -oinv -e for -e if ttt.txt");
  77.     TEST_GREP("grep -o -e return ttt.txt");
  78.     TEST_GREP("grep -ov -e return ttt.txt");
  79.     TEST_GREP("grep -oi -e IF ttt.txt");
  80.     TEST_GREP("grep -oin -e IF ttt.txt");
  81.     TEST_GREP("grep -oiv -e IF ttt.txt");                           //  ??? linux
  82.     TEST_GREP("grep -o -e for ttt.txt file.txt");
  83.     TEST_GREP("grep -o -e argv ttt.txt file.txt");
  84.     TEST_GREP("grep -ov -e argv ttt.txt file.txt");
  85.     TEST_GREP("grep -o -e argv ttt.txt file.txt1");
  86.     TEST_GREP("grep -oh -e argv ttt.txt file.txt");
  87.     TEST_GREP("grep -ovh -e argv ttt.txt file.txt");
  88.     TEST_GREP("grep -onh -e argv ttt.txt file.txt1");
  89.     TEST_GREP("grep -os -e argv ttt.txt file.txt1");
  90.     TEST_GREP("grep -ohs -e argv ttt.txt file.txt1");
  91. }
  92. END_TEST
  93.  
  94.  
  95. START_TEST(s21_grep_test_f) {
  96.     TEST_GREP("grep -f file.txt ttt.txt");
  97.     TEST_GREP("grep -e while -f file.txt ttt.txt");
  98.     TEST_GREP("grep -e while -eerror -f file.txt ttt.txt");
  99.     TEST_GREP("grep -e while -e end -f file.txt ttt.txt");
  100.     // TEST_GREP("grep while -f file.txt ttt.txt");                   // !!!
  101.     TEST_GREP("grep -i -e while -f file.txt ttt.txt");
  102.     TEST_GREP("grep -in -e while -f file.txt ttt.txt");
  103.     TEST_GREP("grep -in -e while -f file.txt ttt.txt ttt.txt");
  104.     TEST_GREP("grep -inh -e while -f file.txt ttt.txt ttt.txt");
  105.     TEST_GREP("grep -inhv -e while -f file.txt ttt.txt ttt.txt");
  106.     TEST_GREP("grep -v -f file.txt ttt.txt");
  107.     TEST_GREP("grep -vi -f file.txt ttt.txt");
  108.     TEST_GREP("grep -e while -f file.txt ttt.txt ttt.sys");
  109.     TEST_GREP("grep -e while -f file.txt ttt.sys");
  110.     TEST_GREP("grep -s -e while -f file.txt ttt.txt ttt.sys");
  111.     TEST_GREP("grep -shn -e while -f file.txt ttt.txt ttt.sys");
  112.     TEST_GREP("grep -e while -f file.php ttt.txt");
  113.     TEST_GREP("grep -f file.txt *.*");
  114. }
  115. END_TEST
  116.  
  117. START_TEST(s21_grep_test_) {
  118.     TEST_GREP("grep if ttt.txt");
  119.     TEST_GREP("grep -i if ttt.txt");
  120.     TEST_GREP("grep -in if ttt.txt");
  121.     TEST_GREP("grep -in if ttt.txt ttt.txt");
  122.     TEST_GREP("grep -inh if ttt.txt ttt.txt");
  123.     TEST_GREP("grep -inv if ttt.txt ttt.txt");
  124.     TEST_GREP("grep -n if file.txt *.*");
  125.     TEST_GREP("grep -z if ttt.txt ttt.txt");
  126.     TEST_GREP("grep if -in ttt.txt");                                 //  !!!
  127. }
  128. END_TEST
  129.  
  130. int main(void) {
  131.     Suite *s1 = suite_create("grep_tests");
  132.     SRunner *sr = srunner_create(s1);
  133.     int nf;
  134.  
  135.     TCase *tc1_1 = tcase_create("s21_grep_test_l");
  136.     tcase_add_test(tc1_1, s21_grep_test_l);
  137.     suite_add_tcase(s1, tc1_1);
  138.  
  139.     TCase *tc2_1 = tcase_create("s21_grep_test_c");
  140.     tcase_add_test(tc2_1, s21_grep_test_c);
  141.     suite_add_tcase(s1, tc2_1);
  142.  
  143.     TCase *tc3_1 = tcase_create("s21_grep_test_o");
  144.     tcase_add_test(tc3_1, s21_grep_test_o);
  145.     suite_add_tcase(s1, tc3_1);
  146.  
  147.     TCase *tc4_1 = tcase_create("s21_grep_test_f");
  148.     tcase_add_test(tc4_1, s21_grep_test_f);
  149.     suite_add_tcase(s1, tc4_1);
  150.  
  151.     TCase *tc5_1 = tcase_create("s21_grep_test_");
  152.     tcase_add_test(tc5_1, s21_grep_test_);
  153.     suite_add_tcase(s1, tc5_1);
  154.  
  155.     srunner_run_all(sr, CK_VERBOSE);
  156.     nf = srunner_ntests_failed(sr);
  157.     srunner_free(sr);
  158.  
  159.     return nf == 0 ? 0 : 1;
  160. }
  161.  
  162. int read_file(char** buffer, size_t* size, const char* file) {
  163.     int error = 0;
  164.     char *source = NULL;
  165.  
  166.     FILE *fp = fopen(file, "rb");
  167.     if (fp) {
  168.         if (!fseek(fp, 0L, SEEK_END)) {
  169.             long int bufsize = ftell(fp);
  170.             if (bufsize != -1) {
  171.                 source = (char*)malloc(bufsize * sizeof(char));
  172.                 if (!fseek(fp, 0L, SEEK_SET)) {
  173.                     *size = fread(source, sizeof(char), bufsize, fp);
  174.                     *buffer = source;
  175.                     if (ferror(fp)) {
  176.                         fputs("Error reading file", stderr);
  177.                         error = 1;
  178.                     }
  179.                 } else {
  180.                     error = 1;
  181.                 }
  182.             } else {
  183.                 error = 1;
  184.             }
  185.         } else {
  186.             error = 1;
  187.         }
  188.         fclose(fp);
  189.     } else {
  190.         error = 1;
  191.     }
  192.     return error;
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement