From 43458cc74f83a47c05b8b43e038211e6ab164b16 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 7 Dec 2023 10:16:15 +0200 Subject: [PATCH] BAN: implement exchange sort and test for it --- BAN/include/BAN/Sort.h | 20 ++++++++++++++++ userspace/CMakeLists.txt | 1 + userspace/test-sort/CMakeLists.txt | 16 +++++++++++++ userspace/test-sort/main.cpp | 37 ++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 BAN/include/BAN/Sort.h create mode 100644 userspace/test-sort/CMakeLists.txt create mode 100644 userspace/test-sort/main.cpp diff --git a/BAN/include/BAN/Sort.h b/BAN/include/BAN/Sort.h new file mode 100644 index 00000000..4f1f6ab2 --- /dev/null +++ b/BAN/include/BAN/Sort.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace BAN +{ + + template> + void sort_exchange(It begin, It end, Comp comp = {}) + { + for (It lhs = begin; lhs != end; ++lhs) + for (It rhs = lhs; ++rhs != end;) + if (!comp(*lhs, *rhs)) + swap(*lhs, *rhs); + } + + + +} diff --git a/userspace/CMakeLists.txt b/userspace/CMakeLists.txt index 1c36198b..69031c7d 100644 --- a/userspace/CMakeLists.txt +++ b/userspace/CMakeLists.txt @@ -28,6 +28,7 @@ set(USERSPACE_PROJECTS test test-globals test-framebuffer + test-sort touch u8sum whoami diff --git a/userspace/test-sort/CMakeLists.txt b/userspace/test-sort/CMakeLists.txt new file mode 100644 index 00000000..1a88a60c --- /dev/null +++ b/userspace/test-sort/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.26) + +project(test-sort CXX) + +set(SOURCES + main.cpp +) + +add_executable(test-sort ${SOURCES}) +target_compile_options(test-sort PUBLIC -O2 -g) +target_link_libraries(test-sort PUBLIC libc) + +add_custom_target(test-sort-install + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/test-sort ${BANAN_BIN}/ + DEPENDS test-sort +) diff --git a/userspace/test-sort/main.cpp b/userspace/test-sort/main.cpp new file mode 100644 index 00000000..0955f994 --- /dev/null +++ b/userspace/test-sort/main.cpp @@ -0,0 +1,37 @@ +#include +#include + +#include +#include +#include + +template +bool is_sorted(BAN::Vector& vec) +{ + for (size_t i = 0; i < vec.size() - 1; i++) + if (vec[i] > vec[i + 1]) + return false; + return true; +} + +#define CURRENT_NS() ({ timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); ts.tv_sec * 1'000'000'000 + ts.tv_nsec; }) + +#define TEST(name, function, count) do { \ + BAN::Vector ivec(count, 0); \ + for (int& i : ivec) \ + i = rand() % 100; \ + uint64_t start_ns = CURRENT_NS(); \ + function(ivec.begin(), ivec.end()); \ + uint64_t end_ns = CURRENT_NS(); \ + uint64_t dur_us = (end_ns - start_ns) / 1000; \ + printf(name " (" #count "): %s\n", is_sorted(ivec) ? "success" : "fail"); \ + printf(" took %" PRIu64 ".%03" PRIu64 " ms\n", dur_us / 1000, dur_us % 1000); \ + } while (0) + +int main() +{ + srand(time(0)); + TEST("exchange sort", BAN::sort_exchange, 100); + TEST("exchange sort", BAN::sort_exchange, 1000); + TEST("exchange sort", BAN::sort_exchange, 10000); +}