-
Notifications
You must be signed in to change notification settings - Fork 0
к/р 1.2 задача 3 #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
к/р 1.2 задача 3 #31
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| cmake_minimum_required(VERSION 3.0.0) | ||
| project(task3 VERSION 0.1.0 LANGUAGES C) | ||
|
|
||
| include(CTest) | ||
| enable_testing() | ||
|
|
||
| add_executable(task3 main.c | ||
| read/read.c | ||
| tests/test.c) | ||
|
|
||
| set(CPACK_PROJECT_NAME ${PROJECT_NAME}) | ||
| set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) | ||
| include(CPack) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| aafgbbbaaaaiiiiiiefkjg;dfaghpdufdahhghhghhhhhghdghrhiag'ffdgsz | ||
| fsdgsyjukhgfretfdsf | ||
|
|
||
|
|
||
|
|
||
| hytjhkytresdsxzdfggggghhhhhhrrrrr |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| #include "read/read.h" | ||
| #include "tests/test.h" | ||
|
|
||
| #define PROGRAM_FINISHED_CORRECTLY 0 | ||
| #define PROGRAM_FAILED_TESTS 1 | ||
| #define MEMORY_ERROR 2 | ||
| #define FILE_READING_ERROR 3 | ||
|
|
||
| #define FILENAME "../file.txt" | ||
|
|
||
| int main(void) | ||
| { | ||
| // if (!test()) | ||
| // { | ||
| // printf("Программа сейчас не работает\n"); | ||
| // return PROGRAM_FAILED_TESTS; | ||
| // } | ||
|
|
||
| FILE *file = fopen(FILENAME, "r"); | ||
| if (file == NULL) | ||
| { | ||
| printf("Не удается получить доступ к файлу\n"); | ||
| return FILE_READING_ERROR; | ||
| } | ||
|
|
||
|
|
||
| size_t len = 0; | ||
| char *text = getString(&len, file, EOF); | ||
| fclose(file); | ||
| if (text == NULL) | ||
| { | ||
| printf("Закончилась память\n"); | ||
| return MEMORY_ERROR; | ||
| } | ||
| printf("%s\n", text); | ||
| free(text); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #include "read.h" | ||
|
|
||
| char *getString(size_t *const len, FILE *file, char const endOfLine); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут точка с запятой лишняя, не компилируется |
||
| { | ||
| *len = 0; | ||
| size_t capacity = 1; | ||
| char *s = (char *)malloc(sizeof(char)); | ||
| if (s == NULL) | ||
| { | ||
| return NULL; | ||
| } | ||
| char previousChar = '\0'; | ||
| for (char c = fgetc(file); c != endOfLine && c != EOF; c = fgetc(file)) | ||
| { | ||
| if (c != previousChar) | ||
| { | ||
| s[(*len)++] = c; | ||
|
|
||
| if (*len >= capacity) | ||
| { | ||
| capacity *= 2; | ||
| char *tmp = (char *)realloc(s, capacity * sizeof(char)); | ||
| if (tmp == NULL) | ||
| { | ||
| free(s); | ||
| return NULL; | ||
| } | ||
| s = tmp; | ||
| } | ||
| previousChar = c; | ||
| } | ||
| } | ||
|
|
||
| s[*len] = '\0'; | ||
|
|
||
| return s; | ||
| } | ||
|
|
||
| char *copyString(char const *const string) | ||
| { | ||
| const size_t len = strlen(string); | ||
| char *copy = (char *)malloc(len * sizeof(char)); | ||
| if (copy != NULL) | ||
| { | ||
| strcpy(copy, string); | ||
| } | ||
| return copy; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strdup? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
| #include <string.h> | ||
| #include <stdio.h> | ||
|
|
||
| char *getString(size_t * const len, FILE * file, char const endOfLine); | ||
| char *copyString(char const * const string); | ||
|
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо комментарии |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #include "test.h" | ||
| #include "../read/read.h" | ||
|
|
||
| const bool testCase(char const *const testFileName, char const *const answerFileName) | ||
| { | ||
| FILE *file = fopen(testFileName, "r"); | ||
| if (file == NULL) | ||
| { | ||
| return false; | ||
| } | ||
| size_t len = 0; | ||
| char *str = getString(&len, file, EOF); | ||
| fclose(file); | ||
| if (str == NULL) | ||
| { | ||
| return false; | ||
| } | ||
| FILE *answerFile = fopen(answerFileName, "r"); | ||
| if (answerFile == NULL) | ||
| { | ||
| free(str); | ||
| return false; | ||
| } | ||
| char *str2 = getString(&len, answerFile, EOF); | ||
| fclose(answerFile); | ||
| if (str2 == NULL) | ||
| { | ||
| free(str); | ||
| return false; | ||
| } | ||
| bool answer = strcmp(str, str2) == 0; | ||
| free(str); | ||
| free(str2); | ||
| return answer; | ||
| } | ||
|
|
||
| const bool test(void) | ||
| { | ||
| const size_t testNumber = 3; | ||
| char const *const testFileNames[testNumber] = {"../tests/testFile1.txt", "../tests/testFile2.txt", "../tests/testFile3.txt"}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Стандарт не позволяет так делать, testNumber должен быть константным литералом, объявленным через |
||
| char const *const answerFileNames[testNumber] = {"../tests/answerFile1.txt", | ||
| "../tests/answerFile1.txt", | ||
| "../tests/answerFile1.txt"}; | ||
|
|
||
| for (size_t i = 0; i < testNumber; ++i) | ||
| { | ||
| if (!testCase(testFileNames[i], answerFileNames[i])) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| const bool test(void); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:(