Advertisement
2607

test.c

Dec 5th, 2021
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. #include <check.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. START_TEST(cat_test) {
  6.     FILE* fp1;
  7.     FILE* fp2;
  8.     char* line1 = NULL;
  9.     char* line2 = NULL;
  10.     size_t len1 = 0, len2 = 0;
  11.     int read1;
  12.     int flag = 0;
  13.     if ((fp1 = fopen("s21_cat_log.txt", "r")) == NULL)
  14.         flag++;
  15.     if ((fp2 = fopen("cat_log.txt", "r")) == NULL)
  16.         flag++;
  17.     ck_assert_msg(flag == 0, "files not found");
  18.     while ((read1 = getline(&line1, &len1, fp1)) != EOF) {
  19.         getline(&line2, &len2, fp2);
  20.         ck_assert_msg(strcmp(line1, line2) == 0, "cat didn't work");
  21.     }
  22.     fclose(fp1);
  23.     fclose(fp2);
  24.     if (line1) {
  25.         free(line1);
  26.         free(line2);
  27.     }
  28. }
  29. END_TEST
  30.  
  31.  
  32. START_TEST(grep_test) {
  33.     FILE* fp1;
  34.     FILE* fp2;
  35.     char* line1 = NULL;
  36.     char* line2 = NULL;
  37.     size_t len1 = 0, len2 = 0;
  38.     int read1;
  39.     int flag = 0;
  40.     if ((fp1 = fopen("s21_grep_log.txt", "r")) == NULL)
  41.         flag++;
  42.     if ((fp2 = fopen("grep_log.txt", "r")) == NULL)
  43.         flag++;
  44.     ck_assert_msg(flag == 0, "files not found");
  45.     while ((read1 = getline(&line1, &len1, fp1)) != EOF) {
  46.             getline(&line2, &len2, fp2);
  47.         ck_assert_msg(strcmp(line1, line2) == 0, "grep didn't work");
  48.     }
  49.     fclose(fp1);
  50.     fclose(fp2);
  51.     if (line1) {
  52.         free(line1);
  53.         free(line2);
  54.     }
  55. }
  56. END_TEST
  57.  
  58. int main(void) {
  59.     Suite* s1 = suite_create("Core");
  60.     TCase* tc1_1 = tcase_create("Core");
  61.     SRunner* sr = srunner_create(s1);
  62.     int nf;
  63.     suite_add_tcase(s1, tc1_1);
  64.     tcase_add_test(tc1_1, cat_test);
  65.     tcase_add_test(tc1_1, grep_test);
  66.  
  67.     srunner_run_all(sr, CK_ENV);
  68.     nf = srunner_ntests_failed(sr);
  69.     srunner_free(sr);
  70.     return nf == 0 ? 0 : 1;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement