#include #include #include "toolbox.hpp" #include "data.hpp" template static constexpr bool is_arg_sorted(const asp::Array& a, const asp::Array& indices) noexcept { for (size_t i = 1; i < a.length; ++i) if (indices[i - 1] == indices[i] || a[indices[i - 1]] > a[indices[i]]) return false; return true; } template static constexpr bool is_sorted(const asp::Array& a) noexcept { for (size_t i = 1; i < a.length; ++i) if (a[i - 1] > a[i]) return false; return true; } template static constexpr void test_sort(const std::array& gaps, const asp::Array& original, void (* const fnc)(const asp::Array&), const char* const title) noexcept { #ifdef __DEBUG printf("xxxxxxxxxxxxxxx IGNORE COPY "); #endif const asp::Array a(original); asp::measure_time_void(gaps, title, fnc, a); #ifdef __DEBUG const bool result = asp::measure_time(gaps, "=> Unit test", is_sorted, a); // asp::print(original); // asp::print(a); asp::formatted_row(gaps, { result ? "Success" : "Failure", "-", "-" }); #else assert(is_sorted(a)); #endif } template static void test_argsort(const std::array& gaps, const asp::Array& original, asp::Array(* const fnc)(const asp::Array&), const char* const title) noexcept { #ifdef __DEBUG printf("xxxxxxxxxxxxxxx IGNORE COPY "); #endif const asp::Array a(original); const asp::Array indices = asp::measure_time>(gaps, title, fnc, a); #ifdef __DEBUG const bool result = asp::measure_time(gaps, "=> Unit test", is_arg_sorted, original, indices); asp::formatted_row(gaps, { result ? "Success" : "Failure", "-", "-" }); #else assert(is_arg_sorted(original, indices)); #endif } template static asp::Array create_random_array(const size_t& n) noexcept { asp::Array original(n); std::random_device rd; std::default_random_engine gen(rd()); std::uniform_int_distribution distribution(std::numeric_limits::min(), std::numeric_limits::max()); return std::move(asp::map(original, [&distribution, &gen](const size_t&, const T&) -> const T { return distribution(gen); })); } int32_t main(int32_t argc, char** argv) { asp::toolbox_unit_test(); using array_type = uint16_t; size_t N = (static_cast(1)<<15) - 1; // size_t N = std::numeric_limits::max(); if (argc > 2) { fprintf(stderr, "Too many arguments\nUsage: ./data \n"); return EXIT_FAILURE; } else if (argc == 2) N = std::strtoul(argv[1], argv + strlen(argv[1]) + 1, 10); char title[64]; sprintf(title, "Sorting %s elements of %s", asp::thousand_sep(N).c_str(), asp::format_byte_size(2 * N * sizeof(array_type)).c_str()); const std::array gaps = { 48, -17, 25 }; asp::header(gaps, { title, "Time spent (ns)", "Formatted time spent" }); const asp::Array original = asp::measure_time>(gaps, "Creating random array", create_random_array, N); // const asp::Array original = { 2, 5, 3, 0, 2, 3, 0, 3 }; // asp::print(original); test_sort(gaps, original, asp::bubble_sort, "Bubble sort"); test_argsort(gaps, original, asp::bubble_sort_arg, "Bubble sort (arg)"); test_sort(gaps, original, asp::quicksort, "Quicksort"); test_argsort(gaps, original, asp::quicksort_arg, "Quicksort (arg)"); test_sort(gaps, original, asp::quicksort_iter, "Quicksort (iterative)"); test_argsort(gaps, original, asp::quicksort_arg_iter, "Quicksort (iterative) (arg)"); test_sort(gaps, original, asp::mergesort, "Mergesort"); test_argsort(gaps, original, asp::mergesort_arg, "Mergesort (arg)"); test_sort(gaps, original, asp::insertion_sort, "Insertion sort"); // test_argsort(gaps, original, asp::insertion_argsort, "Insertion argsort"); // W.I.P // test_sort(gaps, original, asp::counting_sort, "Counting sort"); // test_argsort(gaps, original, asp::counting_sort_arg, "Counting sort (arg)"); // test_sort(gaps, original, asp::radix_sort, "Radix sort"); // test_argsort(gaps, original, asp::radix_sort_arg, "Radix sort (arg)"); asp::footer(gaps); return EXIT_SUCCESS; }