data.cpp : Replaced assertion when sorting failed with log && merged test_sort and test_argsort

This commit is contained in:
saundersp
2025-08-24 18:55:07 +02:00
parent cb53d0bd3d
commit 69a0bed64e

View File

@@ -1,4 +1,3 @@
#include <cassert>
#include <random> #include <random>
#include "toolbox.hpp" #include "toolbox.hpp"
#include "data.hpp" #include "data.hpp"
@@ -20,35 +19,19 @@ static constexpr bool is_sorted(const asp::Array<T>& a) noexcept {
} }
template<typename T, size_t N> template<typename T, size_t N>
static constexpr void test_sort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, void (* const fnc)(const asp::Array<T>&), const char* const title) noexcept { static constexpr void test_sort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, void (* const fnc)(asp::Array<T>&), const char* const title) noexcept {
#ifdef __DEBUG asp::Array<T> a(original);
printf("xxxxxxxxxxxxxxx IGNORE COPY ");
#endif
const asp::Array<T> a(original);
asp::measure_time_void(gaps, title, fnc, a); asp::measure_time_void(gaps, title, fnc, a);
#ifdef __DEBUG const bool result = is_sorted<T>(a);
const bool result = asp::measure_time<bool>(gaps, "=> Unit test", is_sorted<T>, a); asp::formatted_row(gaps, { result ? "=> Unit test success" : "=> Unit test failure", "-", "-" });
// asp::print(original);
// asp::print(a);
asp::formatted_row(gaps, { result ? "Success" : "Failure", "-", "-" });
#else
assert(is_sorted(a));
#endif
} }
template<typename T, size_t N> template<typename T, size_t N>
static void test_argsort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, asp::Array<size_t>(* const fnc)(const asp::Array<T>&), const char* const title) noexcept { static inline void test_sort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, asp::Array<size_t>(* const fnc)(asp::Array<T>&), const char* const title) noexcept {
#ifdef __DEBUG asp::Array<T> a(original);
printf("xxxxxxxxxxxxxxx IGNORE COPY ");
#endif
const asp::Array<T> a(original);
const asp::Array<size_t> indices = asp::measure_time<asp::Array<size_t>>(gaps, title, fnc, a); const asp::Array<size_t> indices = asp::measure_time<asp::Array<size_t>>(gaps, title, fnc, a);
#ifdef __DEBUG const bool result = is_arg_sorted<T>(original, indices);
const bool result = asp::measure_time<bool>(gaps, "=> Unit test", is_arg_sorted<T>, original, indices); asp::formatted_row(gaps, { result ? "=> Unit test success" : "=> Unit test failure", "-", "-" });
asp::formatted_row(gaps, { result ? "Success" : "Failure", "-", "-" });
#else
assert(is_arg_sorted(original, indices));
#endif
} }
template<typename T> template<typename T>
@@ -64,7 +47,7 @@ int32_t main(int32_t argc, char** argv) {
asp::toolbox_unit_test(); asp::toolbox_unit_test();
using array_type = uint16_t; using array_type = uint16_t;
int64_t N = (static_cast<int64_t>(1)<<15) - 1; int64_t N = (static_cast<int64_t>(1)<<16) - 1;
// int64_t N = std::numeric_limits<array_type>::max(); // int64_t N = std::numeric_limits<array_type>::max();
if (argc > 2) { if (argc > 2) {
fprintf(stderr, "Too many arguments\nUsage: ./data <ARRAY_SIZE>\n"); fprintf(stderr, "Too many arguments\nUsage: ./data <ARRAY_SIZE>\n");
@@ -80,21 +63,17 @@ int32_t main(int32_t argc, char** argv) {
const asp::Array<array_type> original = asp::measure_time<asp::Array<array_type>>(gaps, "Creating random array", create_random_array<array_type>, N); const asp::Array<array_type> original = asp::measure_time<asp::Array<array_type>>(gaps, "Creating random array", create_random_array<array_type>, N);
test_sort(gaps, original, asp::bubble_sort<array_type>, "Bubble sort"); test_sort(gaps, original, asp::bubble_sort<array_type>, "Bubble sort");
test_argsort(gaps, original, asp::bubble_sort_arg<array_type>, "Bubble sort (arg)"); test_sort(gaps, original, asp::bubble_sort_arg<array_type>, "Bubble sort (arg)");
test_sort(gaps, original, asp::quicksort<array_type>, "Quicksort"); test_sort(gaps, original, asp::quicksort<array_type>, "Quicksort");
test_argsort(gaps, original, asp::quicksort_arg<array_type>, "Quicksort (arg)"); test_sort(gaps, original, asp::quicksort_arg<array_type>, "Quicksort (arg)");
test_sort(gaps, original, asp::quicksort_iter<array_type>, "Quicksort (iterative)"); test_sort(gaps, original, asp::quicksort_iter<array_type>, "Quicksort (iterative)");
test_argsort(gaps, original, asp::quicksort_arg_iter<array_type>, "Quicksort (iterative) (arg)"); test_sort(gaps, original, asp::quicksort_arg_iter<array_type>, "Quicksort (iterative) (arg)");
test_sort(gaps, original, asp::mergesort<array_type>, "Mergesort"); test_sort(gaps, original, asp::mergesort<array_type>, "Mergesort");
test_argsort(gaps, original, asp::mergesort_arg<array_type>, "Mergesort (arg)"); test_sort(gaps, original, asp::mergesort_arg<array_type>, "Mergesort (arg)");
test_sort(gaps, original, asp::insertion_sort<array_type>, "Insertion sort"); test_sort(gaps, original, asp::insertion_sort<array_type>, "Insertion sort");
// test_argsort(gaps, original, asp::insertion_argsort<array_type>, "Insertion argsort"); test_sort(gaps, original, asp::insertion_argsort<array_type>, "Insertion (arg)");
test_sort(gaps, original, asp::counting_sort<array_type>, "Counting sort");
// W.I.P test_sort(gaps, original, asp::radix_sort<array_type>, "Radix sort");
// test_sort(gaps, original, asp::counting_sort<array_type>, "Counting sort");
// test_argsort(gaps, original, asp::counting_sort_arg<array_type>, "Counting sort (arg)");
// test_sort(gaps, original, asp::radix_sort<array_type>, "Radix sort");
// test_argsort(gaps, original, asp::radix_sort_arg<array_type>, "Radix sort (arg)");
asp::footer(gaps); asp::footer(gaps);