erius
33fee958a5
Added 2 new constructors for FileTape which copy the size of existing tape into a new one Implemented external sort of a Tape using heap sort Rewritten FileTape constructors implementation to better take advantage of existing constructor calls Added const to some parameters to indicate their immutability Added unit tests for external_sort
55 lines
2 KiB
C++
55 lines
2 KiB
C++
#include "tape_util.h"
|
|
#include <algorithm>
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
const static std::vector<uint32_t> TEST_DATA = {123, 26, 87, 266, 111, 234,
|
|
6, 63, 28, 1, 90, 33};
|
|
const static std::vector<uint32_t> TEST_DATA_SORTED = {
|
|
1, 6, 26, 28, 33, 63, 87, 90, 111, 123, 234, 266};
|
|
|
|
std::vector<uint32_t> read_from_tape_backwards(tape::Tape &tape) {
|
|
std::vector<uint32_t> data;
|
|
do {
|
|
data.push_back(tape.read());
|
|
} while (tape.seek_backwards());
|
|
std::ranges::reverse(data);
|
|
return data;
|
|
}
|
|
|
|
// NOLINTBEGIN(*-magic-numbers)
|
|
TEST_CASE("Sorting FileTape with external sort", "[sort]") {
|
|
tape::FileTape input(TEST_DATA);
|
|
tape::FileTape output(input);
|
|
|
|
SECTION("Sorting with no limitations") {
|
|
tape::external_sort(input, output, tape::FILETAPE_FACTORY);
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
}
|
|
|
|
SECTION("Sorting with 4 elements in memory limit") {
|
|
tape::external_sort(input, output, tape::FILETAPE_FACTORY, 4);
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
}
|
|
|
|
SECTION("Sorting with 9 elements in memory limit") {
|
|
tape::external_sort(input, output, tape::FILETAPE_FACTORY, 9);
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
}
|
|
|
|
SECTION("Sorting with vector size elements in memory limit") {
|
|
tape::external_sort(input, output, tape::FILETAPE_FACTORY,
|
|
TEST_DATA.size());
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
}
|
|
|
|
SECTION("Sorting with 1000 elements in memory limit") {
|
|
tape::external_sort(input, output, tape::FILETAPE_FACTORY, 1000);
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
}
|
|
|
|
SECTION("Sorting with 1 elements in memory limit") {
|
|
tape::external_sort(input, output, tape::FILETAPE_FACTORY, 1);
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
}
|
|
}
|
|
// NOLINTEND(*-magic-numbers)
|