summaryrefslogtreecommitdiff
path: root/support/regression/tests/qsort.c
diff options
context:
space:
mode:
authorXavier ASUS <xavi92psx@gmail.com>2019-10-18 00:31:54 +0200
committerXavier ASUS <xavi92psx@gmail.com>2019-10-18 00:31:54 +0200
commit268a53de823a6750d6256ee1fb1e7707b4b45740 (patch)
tree42c1799a9a82b2f7d9790ee9fe181d72a7274751 /support/regression/tests/qsort.c
downloadsdcc-gas-268a53de823a6750d6256ee1fb1e7707b4b45740.tar.gz
sdcc-3.9.0 fork implementing GNU assembler syntax
This fork aims to provide better support for stm8-binutils
Diffstat (limited to 'support/regression/tests/qsort.c')
-rw-r--r--support/regression/tests/qsort.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/support/regression/tests/qsort.c b/support/regression/tests/qsort.c
new file mode 100644
index 0000000..89fde81
--- /dev/null
+++ b/support/regression/tests/qsort.c
@@ -0,0 +1,57 @@
+/** qsort.c - test sorting
+
+ type: signed int, signed long
+*/
+
+#include <testfwk.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+#define NUM_ELEM 20
+
+#if !defined( __SDCC_pdk14) && !defined( __SDCC_pdk15) // Lack of memory
+{type} unsorted[NUM_ELEM] = {120, 110, 90, 100, 190, 190, 190, 130, 125, 80, 132, -8, 20, 40, 60, -10, 20, 30, 40, 50};
+
+const {type} sorted[NUM_ELEM] = {-10, -8, 20, 20, 30, 40, 40, 50, 60, 80, 90, 100, 110, 120, 125, 130, 132, 190, 190, 190};
+#endif
+
+int cmp(const void *lp, const void *rp) __reentrant
+{
+ {type} l = *((const {type} *)lp);
+ {type} r = *((const {type} *)rp);
+
+ if(l < r)
+ return(-1);
+ else if (l == r)
+ return(0);
+ else
+ return(1);
+}
+
+void testSort(void)
+{
+#if !defined( __SDCC_pdk14) && !defined( __SDCC_pdk15) // Lack of memory
+ qsort(unsorted, NUM_ELEM, sizeof({type}), &cmp);
+
+ ASSERT(!memcmp(unsorted, sorted, sizeof({type}) * NUM_ELEM));
+#if !(defined (__SDCC_mcs51) && defined (__SDCC_MODEL_SMALL)) // Not enough RAM
+ {
+ const {type} e95 = 95;
+ const {type} e35 = 35;
+ const {type} e10 = -10;
+ const {type} e20 = 20;
+ const {type} e60 = 60;
+ const {type} e190 = 190;
+
+ ASSERT(bsearch(&e95, sorted, NUM_ELEM, sizeof({type}), &cmp) == 0);
+ ASSERT(bsearch(&e35, sorted, NUM_ELEM, sizeof({type}), &cmp) == 0);
+ ASSERT(*(const {type} *)(bsearch(&e10, sorted, NUM_ELEM, sizeof({type}), &cmp)) == -10);
+ ASSERT(*(const {type} *)(bsearch(&e20, sorted, NUM_ELEM, sizeof({type}), &cmp)) == 20);
+ ASSERT(*(const {type} *)(bsearch(&e60, sorted, NUM_ELEM, sizeof({type}), &cmp)) == 60);
+ ASSERT(*(const {type} *)(bsearch(&e190, sorted, NUM_ELEM, sizeof({type}), &cmp)) == 190);
+ }
+#endif
+#endif
+}
+