yadro-task/tests/filetape_tests.cpp

75 lines
2.5 KiB
C++
Raw Normal View History

#include "filetape.h"
#include <catch2/catch_test_macros.hpp>
#include <cstdio>
// NOLINTBEGIN(readability-function-cognitive-complexity)
TEST_CASE("Reading data from a FileTape", "[filetape]") {
tape::FileTape tape("assets/filetape_test_data.txt");
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("assets/filetape_test_data.txt");
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]") {
// init a temp file to modify
// std::string tmp_file_name = "/tmp/"
// std::unique_ptr<std::FILE> tmp_file(std::tmpfile());
// std::fstream tmp(tmp_file);
// std::fstream tmp(tmp_file);
// tape::FileTape tape("assets/filetape_test_data.txt");
}
// NOLINTEND(readability-function-cognitive-complexity)