2024-10-28 10:32:14 +00:00
|
|
|
#include "tapelib/filetape.h"
|
|
|
|
#include "tapelib/tape.h"
|
2024-10-28 02:23:42 +00:00
|
|
|
#include "tapelib/tape_util.h"
|
2024-10-27 03:18:23 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2024-10-28 10:32:14 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
2024-10-27 03:18:23 +00:00
|
|
|
|
2024-10-28 10:32:14 +00:00
|
|
|
using std::vector;
|
|
|
|
using namespace tape;
|
2024-10-27 03:18:23 +00:00
|
|
|
|
2024-10-28 10:32:14 +00:00
|
|
|
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;
|
2024-10-27 03:18:23 +00:00
|
|
|
do {
|
|
|
|
data.push_back(tape.read());
|
|
|
|
} while (tape.seek_backwards());
|
|
|
|
std::ranges::reverse(data);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-10-28 10:32:14 +00:00
|
|
|
// NOLINTBEGIN
|
2024-10-27 03:18:23 +00:00
|
|
|
TEST_CASE("Sorting FileTape with external sort", "[sort]") {
|
2024-10-28 10:32:14 +00:00
|
|
|
FileTape input(TEST_DATA);
|
|
|
|
FileTape output(input);
|
2024-10-27 03:18:23 +00:00
|
|
|
|
|
|
|
SECTION("Sorting with no limitations") {
|
2024-10-28 10:32:14 +00:00
|
|
|
external_sort(input, output, FILETAPE_FACTORY);
|
2024-10-27 03:18:23 +00:00
|
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Sorting with 4 elements in memory limit") {
|
2024-10-28 10:32:14 +00:00
|
|
|
external_sort(input, output, FILETAPE_FACTORY, 4);
|
2024-10-27 03:18:23 +00:00
|
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Sorting with 9 elements in memory limit") {
|
2024-10-28 10:32:14 +00:00
|
|
|
external_sort(input, output, FILETAPE_FACTORY, 9);
|
2024-10-27 03:18:23 +00:00
|
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Sorting with vector size elements in memory limit") {
|
2024-10-28 10:32:14 +00:00
|
|
|
external_sort(input, output, FILETAPE_FACTORY, TEST_DATA.size());
|
2024-10-27 03:18:23 +00:00
|
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Sorting with 1000 elements in memory limit") {
|
2024-10-28 10:32:14 +00:00
|
|
|
external_sort(input, output, FILETAPE_FACTORY, 1000);
|
2024-10-27 03:18:23 +00:00
|
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Sorting with 1 elements in memory limit") {
|
2024-10-28 10:32:14 +00:00
|
|
|
external_sort(input, output, FILETAPE_FACTORY, 1);
|
2024-10-27 03:18:23 +00:00
|
|
|
REQUIRE(read_from_tape_backwards(output) == TEST_DATA_SORTED);
|
|
|
|
}
|
|
|
|
}
|
2024-10-28 10:32:14 +00:00
|
|
|
// NOLINTEND
|