yadro-task/tests/filetape_sort_tests.cpp

62 lines
2 KiB
C++
Raw Normal View History

#include "tapelib/filetape.h"
#include "tapelib/tape.h"
#include "tapelib/tape_util.h"
#include <algorithm>
#include <catch2/catch_test_macros.hpp>
#include <cstdint>
#include <vector>
using std::vector;
using namespace tape;
const static vector<uint32_t> TEST_DATA = {123, 26, 87, 266, 111, 234,
6, 63, 28, 1, 90, 33};
const static vector<uint32_t> TEST_DATA_SORTED = {1, 6, 26, 28, 33, 63,
87, 90, 111, 123, 234, 266};
vector<uint32_t> read_from_tape_backwards(Tape &tape) {
vector<uint32_t> data;
do {
data.push_back(tape.read());
} while (tape.seek_backwards());
std::ranges::reverse(data);
return data;
}
// NOLINTBEGIN
TEST_CASE("Sorting FileTape with external sort", "[sort]") {
FileTape input(TEST_DATA);
FileTape output(input);
SECTION("Sorting with no limitations") {
external_sort(input, output, FILETAPE_FACTORY);
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
}
SECTION("Sorting with 4 elements in memory limit") {
external_sort(input, output, FILETAPE_FACTORY, 4);
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
}
SECTION("Sorting with 9 elements in memory limit") {
external_sort(input, output, FILETAPE_FACTORY, 9);
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
}
SECTION("Sorting with vector size elements in memory limit") {
external_sort(input, output, FILETAPE_FACTORY, TEST_DATA.size());
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
}
SECTION("Sorting with 1000 elements in memory limit") {
external_sort(input, output, FILETAPE_FACTORY, 1000);
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
}
SECTION("Sorting with 1 elements in memory limit") {
external_sort(input, output, FILETAPE_FACTORY, 1);
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
}
}
// NOLINTEND