Tests passed.

This commit is contained in:
rhinemann 2024-12-27 18:50:35 +02:00
commit 797d731eb0
5 changed files with 146 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
cmake-build-debug/

11
CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.30)
project(code)
set(CMAKE_CXX_STANDARD 23)
add_executable(code main.cpp
sort.cpp
sort.h
)
target_link_libraries(code gtest gmock pthread)

77
main.cpp Normal file
View File

@ -0,0 +1,77 @@
#include "sort.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
TEST(QuickSortTest, SortsArrayInAscendingOrder)
{
// Test input array
char* arr[] = { "banana", "apple", "cherry", "date", "elderberry" };
const size_t length = std::size(arr);
// Sort the array
quick_sort(arr, length);
// Check if the array is sorted in ascending order
for (size_t i = 1; i < length; i++)
{
ASSERT_TRUE(strcmp(arr[i - 1], arr[i]) <= 0);
}
}
TEST(QuickSortTest, SingleElementArray) {
// Test sorting an array with a single element.
char* arr[] = { "hello" };
quick_sort(arr, 1);
ASSERT_STREQ(arr[0], "hello");
}
TEST(QuickSortTest, SortedArray) {
// Test sorting an array that is already sorted.
char* arr[] = { "alpha", "beta", "gamma" };
quick_sort(arr, 3);
ASSERT_STREQ(arr[0], "alpha");
ASSERT_STREQ(arr[1], "beta");
ASSERT_STREQ(arr[2], "gamma");
}
TEST(QuickSortTest, ReverseSortedArray) {
// Test sorting an array that is reverse sorted.
char* arr[] = { "gamma", "beta", "alpha" };
quick_sort(arr, 3);
ASSERT_STREQ(arr[0], "alpha");
ASSERT_STREQ(arr[1], "beta");
ASSERT_STREQ(arr[2], "gamma");
}
TEST(QuickSortTest, RandomArray) {
// Test sorting an array with random elements.
char* arr[] = { "hello", "world", "foo", "bar", "baz", "qux", "quux", "corge" };
quick_sort(arr, 8);
ASSERT_STREQ(arr[0], "bar");
ASSERT_STREQ(arr[1], "baz");
ASSERT_STREQ(arr[2], "corge");
ASSERT_STREQ(arr[3], "foo");
ASSERT_STREQ(arr[4], "hello");
ASSERT_STREQ(arr[5], "quux");
ASSERT_STREQ(arr[6], "qux");
ASSERT_STREQ(arr[7], "world");
}
TEST(SwapTest, SwapsValuesCorrectly)
{
char* a = "apple";
char* b = "banana";
swap(&a, &b);
EXPECT_STREQ(a, "banana");
EXPECT_STREQ(b, "apple");
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

50
sort.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "sort.h"
#include <cstring>
void swap(char **a, char **b) {
char *temp = *a;
*a = *b;
*b = temp;
}
int partition(char **arr, const int first, const int last)
{
const char *pivot = arr[last];
int i = first;
for (int j = first; j < last; j++) {
if (strcmp(arr[j], pivot) <= 0) {
swap(&arr[i], &arr[j]);
i++;
}
}
swap(&arr[i], &arr[last]);
return (i);
}
void quick_sort(char **arr, const size_t length) {
if (length <= 1) {
return;
}
int low = 0, high = length - 1;
int stack[high - low + 1];
int top = -1;
stack[++top] = low;
stack[++top] = high;
while (top >= 0) {
high = stack[top--];
low = stack[top--];
const int pivot_pos = partition(arr, low, high);
if (pivot_pos - 1 > low) {
stack[++top] = low;
stack[++top] = pivot_pos - 1;
}
if (pivot_pos + 1 < high) {
stack[++top] = pivot_pos + 1;
stack[++top] = high;
}
}
}

6
sort.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef SORT_H
#define SORT_H
#include <cstddef>
void swap(char** a, char** b);
void quick_sort(char** arr, size_t length);
#endif //SORT_H