2024-10-28 02:23:42 +00:00
|
|
|
#include "tapelib/filetape.h"
|
2024-10-24 14:48:55 +00:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2024-10-28 10:32:14 +00:00
|
|
|
#include <cstdint>
|
2024-10-26 13:10:14 +00:00
|
|
|
#include <vector>
|
2024-10-24 14:48:55 +00:00
|
|
|
|
2024-10-28 10:32:14 +00:00
|
|
|
using std::vector;
|
|
|
|
using namespace tape;
|
2024-10-26 01:23:11 +00:00
|
|
|
|
2024-10-28 10:32:14 +00:00
|
|
|
const static vector<uint32_t> TEST_DATA = {1, 12345, 0, 2222222222, 4294967295};
|
|
|
|
|
|
|
|
// NOLINTBEGIN
|
2024-10-24 14:48:55 +00:00
|
|
|
TEST_CASE("Reading data from a FileTape", "[filetape]") {
|
2024-10-28 10:32:14 +00:00
|
|
|
FileTape tape(TEST_DATA);
|
2024-10-24 14:48:55 +00:00
|
|
|
|
|
|
|
SECTION("Read all data sequentially") {
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[0]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_forward() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[1]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_forward() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[2]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_forward() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[3]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_forward() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[4]);
|
2024-10-24 14:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Read data non-sequentially") {
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[0]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_forward() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[1]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_backwards() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[0]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_forward() == true);
|
|
|
|
REQUIRE(tape.seek_forward() == true);
|
|
|
|
REQUIRE(tape.seek_forward() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[3]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_backwards() == true);
|
|
|
|
REQUIRE(tape.seek_backwards() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[1]);
|
2024-10-24 14:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Seeking forward and backwards", "[filetape]") {
|
2024-10-28 10:32:14 +00:00
|
|
|
FileTape tape(TEST_DATA);
|
2024-10-24 14:48:55 +00:00
|
|
|
|
|
|
|
SECTION("Rewinding at the beginning of a file tape") {
|
|
|
|
REQUIRE(tape.seek_backwards() == false);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[0]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_forward() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[1]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_backwards() == true);
|
|
|
|
REQUIRE(tape.seek_backwards() == false);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[0]);
|
2024-10-24 14:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[4]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_backwards() == true);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[3]);
|
2024-10-24 14:48:55 +00:00
|
|
|
REQUIRE(tape.seek_forward() == true);
|
|
|
|
REQUIRE(tape.seek_forward() == false);
|
2024-10-26 13:10:14 +00:00
|
|
|
REQUIRE(tape.read() == TEST_DATA[4]);
|
2024-10-24 14:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Writing to a file tape", "[filetape]") {
|
2024-10-28 10:32:14 +00:00
|
|
|
FileTape tape(3);
|
2024-10-25 03:56:52 +00:00
|
|
|
|
|
|
|
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);
|
2024-10-24 14:48:55 +00:00
|
|
|
}
|
2024-10-28 10:32:14 +00:00
|
|
|
// NOLINTEND
|