From 3023dbb3c0d4db0b329d2adeecd5d829135ba74d Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Thu, 1 Jun 2023 08:11:27 -0600
Subject: [PATCH] Add qsort test

---
 tests/Makefile                                |  1 +
 .../expected/bins_static/stdlib/qsort.stderr  |  0
 .../expected/bins_static/stdlib/qsort.stdout  |  2 ++
 tests/stdlib/qsort.c                          | 32 +++++++++++++++++++
 4 files changed, 35 insertions(+)
 create mode 100644 tests/expected/bins_static/stdlib/qsort.stderr
 create mode 100644 tests/expected/bins_static/stdlib/qsort.stdout
 create mode 100644 tests/stdlib/qsort.c

diff --git a/tests/Makefile b/tests/Makefile
index f6ccb321d..b9790c1ec 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -53,6 +53,7 @@ EXPECT_NAMES=\
 	stdlib/div \
 	stdlib/env \
 	stdlib/mkostemps \
+	stdlib/qsort \
 	stdlib/rand \
 	stdlib/rand48 \
 	stdlib/random \
diff --git a/tests/expected/bins_static/stdlib/qsort.stderr b/tests/expected/bins_static/stdlib/qsort.stderr
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/expected/bins_static/stdlib/qsort.stdout b/tests/expected/bins_static/stdlib/qsort.stdout
new file mode 100644
index 000000000..49755a611
--- /dev/null
+++ b/tests/expected/bins_static/stdlib/qsort.stdout
@@ -0,0 +1,2 @@
+Before: 23 16 8 4 42 15 
+After: 4 8 15 16 23 42 
diff --git a/tests/stdlib/qsort.c b/tests/stdlib/qsort.c
new file mode 100644
index 000000000..24c742d35
--- /dev/null
+++ b/tests/stdlib/qsort.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
+
+int values[] = { 23, 16, 8, 4, 42, 15 };
+
+int cmpfunc (const void * a_ptr, const void * b_ptr) {
+    int a = *(const int *)a_ptr;
+    int b = *(const int *)b_ptr;
+    return a - b;
+}
+
+int main () {
+    int i;
+
+    printf("Before: ");
+    for(i = 0; i < ARRAY_SIZE(values); i++) {
+        printf("%d ", values[i]);
+    }
+    printf("\n");
+
+    qsort(values, ARRAY_SIZE(values), sizeof(int), cmpfunc);
+
+    printf("After: ");
+    for(i = 0; i < ARRAY_SIZE(values); i++) {
+        printf("%d ", values[i]);
+    }
+    printf("\n");
+
+    return 0;
+}
-- 
GitLab