scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit f9dd9d11f059550c22ff819080400574d515da8e
parent d96e3eb8b442d7a67846161f37e368d5a64b95b2
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Fri,  1 May 2026 19:40:38 +0200

tests/libc: Add 0094-bsearch

Diffstat:
Mtests/libc/execute/.gitignore | 1+
Atests/libc/execute/0094-bsearch.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Mtests/libc/execute/libc-tests.lst | 1+
3 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore @@ -92,3 +92,4 @@ test.log 0091-atexit 0092-getenv 0093-system +0094-bsearch diff --git a/tests/libc/execute/0094-bsearch.c b/tests/libc/execute/0094-bsearch.c @@ -0,0 +1,46 @@ +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +/* +output: +testing +done +end: +*/ + +static int +cmp(const void *pp1, const void *pp2) +{ + const int *p1 = pp1, *p2 = pp2; + + return *p1 - *p2; +} + +static int +test(void) +{ + int *p, key, i, arr[64]; + + for (i = 0; i < 64; i++) + arr[i] = rand(); + + qsort(arr, 64, sizeof(int), cmp); + + for (i = 0; i < 63; i++) + assert(arr[i] <= arr[i+1]); + + key = arr[33]; + p = bsearch(&key, arr, 64, sizeof(int), cmp); + assert(*p == arr[33]); +} + +int +main(void) +{ + puts("testing"); + test(); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst @@ -90,3 +90,4 @@ 0091-atexit 0092-getenv 0093-system +0094-bsearch