diff --git a/CMakeLists.txt b/CMakeLists.txt index 22d007a..2ef91a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ set(CMAKE_C_STANDARD 23) set(CMAKE_C_STANDARD_REQUIRED TRUE) add_subdirectory(test_1_1) +add_subdirectory(test_1_2) add_subdirectory(test_2_1) add_subdirectory(test_final) add_subdirectory(test_final_1) diff --git a/test_1_2/CMakeLists.txt b/test_1_2/CMakeLists.txt new file mode 100644 index 0000000..ae5aed0 --- /dev/null +++ b/test_1_2/CMakeLists.txt @@ -0,0 +1,5 @@ +project(test_1_2) + +set(testName "${PROJECT_NAME}") + +add_subdirectory(task_3) diff --git a/test_1_2/task_3/CMakeLists.txt b/test_1_2/task_3/CMakeLists.txt new file mode 100644 index 0000000..c62c971 --- /dev/null +++ b/test_1_2/task_3/CMakeLists.txt @@ -0,0 +1,6 @@ +project("${testName}_task_3") + +add_library(commentsExtractor commentsExtractor.c) + +add_executable(${PROJECT_NAME}_test test.c) +target_link_libraries(${PROJECT_NAME}_test commentsExtractor) diff --git a/test_1_2/task_3/commentsExtractor.c b/test_1_2/task_3/commentsExtractor.c new file mode 100644 index 0000000..4a2d0ab --- /dev/null +++ b/test_1_2/task_3/commentsExtractor.c @@ -0,0 +1,33 @@ +#include "commentsExtractor.h" + +#include +#include + +bool extractSemicolonComments(FILE *input, FILE *output) { + if (input == NULL || output == NULL) { + return false; + } + + bool isComment = false; + while (!feof(input)) { + int readCharacter = fgetc(input); + if (readCharacter == EOF) { + break; + } + + if (readCharacter == ';') { + isComment = true; + } else if (readCharacter == '\n') { + if (isComment) { + fputc('\n', output); + } + isComment = false; + } + + if (isComment) { + fputc(readCharacter, output); + } + } + + return true; +} diff --git a/test_1_2/task_3/commentsExtractor.h b/test_1_2/task_3/commentsExtractor.h new file mode 100644 index 0000000..4f3b47b --- /dev/null +++ b/test_1_2/task_3/commentsExtractor.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +/// @brief Reads from file all comments starting with `;` and ending with '\n' +/// @param input File to read comments from +/// @param output File to write comments to +/// @return `true` if extracted comments successfully, `false` otherwise (some of files were `NULL`) +bool extractSemicolonComments(FILE *input, FILE *output); diff --git a/test_1_2/task_3/test.c b/test_1_2/task_3/test.c new file mode 100644 index 0000000..74f5b29 --- /dev/null +++ b/test_1_2/task_3/test.c @@ -0,0 +1,65 @@ +#define CTEST_MAIN +#define CTEST_SEGFAULT +#include "../../ctest/ctest.h" + +#include +#include + +#include "commentsExtractor.h" + +#define TEST_FILES "testFiles/" +#define INPUT "_input.txt" +#define OUTPUT "_output.txt" + +int main(int argc, const char *argv[]) { + return ctest_main(argc, argv); +} + +static void testCase(const char *inputFilename, const char *expectedResultFilename) { + const char *outputFilename = "testOutputFile.txt"; + + FILE *inputRead = fopen(inputFilename, "r"); + ASSERT_NOT_NULL(inputRead); + + FILE *outputWrite = fopen(outputFilename, "w"); + ASSERT_NOT_NULL(outputWrite); + + ASSERT_TRUE(extractSemicolonComments(inputRead, outputWrite)); + + ASSERT_EQUAL(fclose(inputRead), 0); + ASSERT_EQUAL(fclose(outputWrite), 0); + + FILE *realOutputRead = fopen(outputFilename, "r"); + ASSERT_NOT_NULL(realOutputRead); + + FILE *expectedOutputRead = fopen(expectedResultFilename, "r"); + ASSERT_NOT_NULL(expectedOutputRead); + + while (!feof(realOutputRead)) { + int realReadResult = fgetc(realOutputRead); + int expectedReadResult = fgetc(expectedOutputRead); + + ASSERT_EQUAL(realReadResult, expectedReadResult); + + if (realReadResult == EOF) { + break; + } + } + + ASSERT_EQUAL(fclose(realOutputRead), 0); + ASSERT_EQUAL(fclose(expectedOutputRead), 0); + + ASSERT_EQUAL(remove(outputFilename), 0); +} + +CTEST(commentExtractorTests, singleCommentTest) { + testCase( + TEST_FILES "singleComment" INPUT, + TEST_FILES "singleComment" OUTPUT); +} + +CTEST(commentExtractorTests, severalCommentsTest) { + testCase( + TEST_FILES "severalComments" INPUT, + TEST_FILES "severalComments" OUTPUT); +} diff --git a/test_1_2/task_3/testFiles/severalComments_input.txt b/test_1_2/task_3/testFiles/severalComments_input.txt new file mode 100644 index 0000000..38e2fff --- /dev/null +++ b/test_1_2/task_3/testFiles/severalComments_input.txt @@ -0,0 +1,5 @@ + +123456789 ; qwertyuiop ; asdfghjkl ; zxcvbnm +; aaaaaaaa +qwezxc +012345;6789 diff --git a/test_1_2/task_3/testFiles/severalComments_output.txt b/test_1_2/task_3/testFiles/severalComments_output.txt new file mode 100644 index 0000000..6453a91 --- /dev/null +++ b/test_1_2/task_3/testFiles/severalComments_output.txt @@ -0,0 +1,3 @@ +; qwertyuiop ; asdfghjkl ; zxcvbnm +; aaaaaaaa +;6789 diff --git a/test_1_2/task_3/testFiles/singleComment_input.txt b/test_1_2/task_3/testFiles/singleComment_input.txt new file mode 100644 index 0000000..12a7e7b --- /dev/null +++ b/test_1_2/task_3/testFiles/singleComment_input.txt @@ -0,0 +1 @@ +gwfhygefgehfg;sdfersfyergfyryerfer diff --git a/test_1_2/task_3/testFiles/singleComment_output.txt b/test_1_2/task_3/testFiles/singleComment_output.txt new file mode 100644 index 0000000..eb0659f --- /dev/null +++ b/test_1_2/task_3/testFiles/singleComment_output.txt @@ -0,0 +1 @@ +;sdfersfyergfyryerfer