view tests/test-qsort_r.c @ 17744:9c8d212db038

qsort_r: new module, for GNU-style qsort_r This works even on FreeBSD, which has an incompatible qsort_r API. * MODULES.html.sh: Add it. * doc/glibc-functions/qsort_r.texi: It's now supported. * lib/qsort.c: New file, taken from glibc with minor changes inside "#ifndef _LIBC" and with an unnecessary "#include <alloca.h>" removed. * lib/qsort_r.c: New file, compiled only on FreeBSD. * lib/stdlib.in.h (qsort_r): Declare in the usual way. * m4/stdlib_h.m4 (gl_STDLIB_H_DEFAULTS): * modules/qsort_r, modules/qsort_r-tests: New files. * modules/stdlib (Makefile): Set up its defaults. * tests/test-qsort_r.c: New file.
author Paul Eggert <eggert@cs.ucla.edu>
date Fri, 29 Aug 2014 13:00:16 -0700
parents
children ab58d4870664
line wrap: on
line source

/* Test qsort_r.

   Copyright 2014 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* Written by Paul Eggert.  */

#include <config.h>

#include <stdlib.h>
#include <string.h>

int
cmp (void const *va, void const *vb, void *varg)
{
  char const *a = va;
  char const *b = vb;
  int *arg = varg;
  return (*a < *b ? -1 : *a > *b) * *arg;
}

int
main (void)
{
  char buf[] = "thequickbrownfoxjumpedoverthelazydogs";
  int forward = 1;
  int reverse = -1;
  qsort_r (buf, sizeof buf - 1, 1, cmp, &forward);
  if (strcmp (buf, "abcddeeeefghhijklmnoooopqrrsttuuvwxyz") != 0)
    return 1;
  qsort_r (buf, sizeof buf - 1, 1, cmp, &reverse);
  if (strcmp (buf, "zyxwvuuttsrrqpoooonmlkjihhgfeeeeddcba") != 0)
    return 1;
  return 0;
}