Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 5 additions & 0 deletions test_1_2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project(test_1_2)

set(testName "${PROJECT_NAME}")

add_subdirectory(task_3)
6 changes: 6 additions & 0 deletions test_1_2/task_3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
33 changes: 33 additions & 0 deletions test_1_2/task_3/commentsExtractor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "commentsExtractor.h"

#include <stdbool.h>
#include <stdio.h>

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;
}
10 changes: 10 additions & 0 deletions test_1_2/task_3/commentsExtractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <stdbool.h>
#include <stdio.h>

/// @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);
65 changes: 65 additions & 0 deletions test_1_2/task_3/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#define CTEST_MAIN
#define CTEST_SEGFAULT
#include "../../ctest/ctest.h"

#include <stdio.h>
#include <stdlib.h>

#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);
}
5 changes: 5 additions & 0 deletions test_1_2/task_3/testFiles/severalComments_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

123456789 ; qwertyuiop ; asdfghjkl ; zxcvbnm
; aaaaaaaa
qwezxc
012345;6789
3 changes: 3 additions & 0 deletions test_1_2/task_3/testFiles/severalComments_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; qwertyuiop ; asdfghjkl ; zxcvbnm
; aaaaaaaa
;6789
1 change: 1 addition & 0 deletions test_1_2/task_3/testFiles/singleComment_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gwfhygefgehfg;sdfersfyergfyryerfer
1 change: 1 addition & 0 deletions test_1_2/task_3/testFiles/singleComment_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;sdfersfyergfyryerfer