From 5dad93b67a6b311a69e7a775c5caf60f2968f82a Mon Sep 17 00:00:00 2001 From: Daniel Thornburgh Date: Mon, 29 Jan 2024 16:24:20 -0800 Subject: [PATCH] Add qsort test --- SingleSource/UnitTests/qsort.c | 36 +++++++++++++++++++ SingleSource/UnitTests/qsort.reference_output | 1 + 2 files changed, 37 insertions(+) create mode 100644 SingleSource/UnitTests/qsort.c create mode 100644 SingleSource/UnitTests/qsort.reference_output diff --git a/SingleSource/UnitTests/qsort.c b/SingleSource/UnitTests/qsort.c new file mode 100644 index 000000000..0a63f35c2 --- /dev/null +++ b/SingleSource/UnitTests/qsort.c @@ -0,0 +1,36 @@ +// Copyright 2024 LLVM-MOS +// +// Licensed under the Apache License, Version 2.0 with LLVM Exceptions, +// See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license +// information. + +#undef NDEBUG +#include +#include +#include + +// Originally from the Public Domain C Library (PDCLib) + + +static int compare(const void *left, const void *right) { + return *((unsigned char *)left) - *((unsigned char *)right); +} + +int main(void) { + char presort[] = {"shreicnyjqpvozxmbt"}; + char sorted1[] = {"bcehijmnopqrstvxyz"}; + char sorted2[] = {"bticjqnyozpvreshxm"}; + char s[19]; + strcpy(s, presort); + qsort(s, 18, 1, compare); + assert(strcmp(s, sorted1) == 0); + strcpy(s, presort); + qsort(s, 9, 2, compare); + assert(strcmp(s, sorted2) == 0); + strcpy(s, presort); + qsort(s, 1, 1, compare); + assert(strcmp(s, presort) == 0); + qsort(s, 100, 0, compare); + assert(strcmp(s, presort) == 0); + return 0; +} diff --git a/SingleSource/UnitTests/qsort.reference_output b/SingleSource/UnitTests/qsort.reference_output new file mode 100644 index 000000000..ca916d098 --- /dev/null +++ b/SingleSource/UnitTests/qsort.reference_output @@ -0,0 +1 @@ +exit 0