yadro-task/tests/filetape_tests.cpp
erius f3aaa26df8 Chaged FileSettings delays type to std::chrono:milliseconds
Changed file format for a FileTape - added and example in the class comment
Removed prev_line_pos and at_first_line fields from FieldTape
Changed Tape data type from int32_t to uint32_t
Cleand up includes
Implemented FileTape methods
Unit tests are in working state
2024-10-25 06:56:52 +03:00

82 lines
2.6 KiB
C++

#include "filetape.h"
#include <catch2/catch_test_macros.hpp>
// NOLINTBEGIN(readability-function-cognitive-complexity)
TEST_CASE("Reading data from a FileTape", "[filetape]") {
tape::FileTape tape(FILETAPE_TEST_FILE);
SECTION("Read all data sequentially") {
REQUIRE(tape.read() == 1);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.read() == 2);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.read() == 3);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.read() == 4);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.read() == 5);
}
SECTION("Read data non-sequentially") {
REQUIRE(tape.read() == 1);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.read() == 2);
REQUIRE(tape.seek_backwards() == true);
REQUIRE(tape.read() == 1);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.read() == 4);
REQUIRE(tape.seek_backwards() == true);
REQUIRE(tape.seek_backwards() == true);
REQUIRE(tape.read() == 2);
}
}
TEST_CASE("Seeking forward and backwards", "[filetape]") {
tape::FileTape tape(FILETAPE_TEST_FILE);
SECTION("Rewinding at the beginning of a file tape") {
REQUIRE(tape.seek_backwards() == false);
REQUIRE(tape.read() == 1);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.read() == 2);
REQUIRE(tape.seek_backwards() == true);
REQUIRE(tape.seek_backwards() == false);
REQUIRE(tape.read() == 1);
}
SECTION("Seeking at the end of a file tape") {
const int size = 5;
// seek to end of a file tape
for (int i = 0; i < size - 1; i++) {
REQUIRE(tape.seek_forward() == true);
}
REQUIRE(tape.seek_forward() == false);
REQUIRE(tape.read() == 5);
REQUIRE(tape.seek_backwards() == true);
REQUIRE(tape.read() == 4);
REQUIRE(tape.seek_forward() == true);
REQUIRE(tape.seek_forward() == false);
REQUIRE(tape.read() == 5);
}
}
TEST_CASE("Writing to a file tape", "[filetape]") {
tape::FileTape tape(3);
tape.write(0);
REQUIRE(tape.read() == 0);
tape.seek_forward();
tape.write(1);
REQUIRE(tape.read() == 1);
tape.seek_forward();
tape.write(2);
REQUIRE(tape.read() == 2);
tape.write(3);
REQUIRE(tape.read() == 3);
tape.seek_backwards();
tape.write(4);
REQUIRE(tape.read() == 4);
}
// NOLINTEND(readability-function-cognitive-complexity)