gwenhywfar  5.14.1
quicksort-t.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Sat Jan 10 2026
3  copyright : (C) 2026 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 /* This file is included by "quicksort.c" */
26 
27 #include "quicksort-t.h"
28 #include <gwenhywfar/error.h>
29 #include <gwenhywfar/debug.h>
30 
31 
32 
33 #ifdef GWENHYWFAR_ENABLE_TESTCODE
34 
35 /* ------------------------------------------------------------------------------------------------
36  * definitions
37  * ------------------------------------------------------------------------------------------------
38  */
39 
40 #define TEST1_ARRAYSIZE 16
41 
42 
43 
44 /* ------------------------------------------------------------------------------------------------
45  * forward declarations
46  * ------------------------------------------------------------------------------------------------
47  */
48 
49 static int GWENHYWFAR_CB test1(GWEN_TEST_MODULE *mod);
50 static int _compareInts_cb(const void *pA, const void *pB, void *pArg);
51 
52 
53 
54 /* ------------------------------------------------------------------------------------------------
55  * implementations
56  * ------------------------------------------------------------------------------------------------
57  */
58 
60 {
61  GWEN_TEST_MODULE *newMod;
62 
63  newMod=GWEN_Test_Module_AddModule(mod, "GWEN_Quicksort", NULL);
64 
65  GWEN_Test_Module_AddTest(newMod, "sort integer array", test1, NULL);
66 
67  return 0;
68 }
69 
70 
71 
72 
73 /* ................................................................................................
74  * test 1: sort an array of numbers
75  * ................................................................................................
76  */
77 
79 {
80  int currentArrayOfInts[TEST1_ARRAYSIZE] ={64, 84, 3, 95, 45, 3, 65, 46, 45, 12, 7, 39, 23, 54, 73, 99};
81  const int expectedArrayOfInts[TEST1_ARRAYSIZE]={ 3, 3, 7, 12, 23, 39, 45, 45, 46, 54, 64, 65, 73, 84, 95, 99};
82  int n;
83  int argUseCount=0;
84  int i;
85  int errors=0;
86 
87  n=TEST1_ARRAYSIZE;
88 
89  GWEN_QuickSort(currentArrayOfInts, n, sizeof(int), _compareInts_cb, (void*) &argUseCount);
90 
91  if (argUseCount==0) {
92  fprintf(stderr, "Compare function never called.\n");
93  errors++;
94  }
95 
96 #if 0
97  /* print array */
98  fprintf(stderr, "Array: ");
99  for (i=0; i<n; i++)
100  fprintf(stderr, "%s%d", i?", ":"", currentArrayOfInts[i]);
101  fprintf(stderr, "\n");
102 #endif
103 
104  for (i=0; i<n; i++) {
105  if (currentArrayOfInts[i]!=expectedArrayOfInts[i]) {
106  fprintf(stderr, "Sorted array differes at pos %d: %d != %d\n", i, currentArrayOfInts[i], expectedArrayOfInts[i]);
107  errors++;
108  }
109  }
110 
111  if (errors) {
112  fprintf(stderr, "At least some parts of the test failed.\n");
113  return GWEN_ERROR_GENERIC;
114  }
115 
116  return 0;
117 }
118 
119 
120 
121 int _compareInts_cb(const void *pA, const void *pB, void *pArg)
122 {
123  const int *pIntA;
124  const int *pIntB;
125  int *pIntArg;
126 
127  pIntArg=(int*) pArg;
128  (*pIntArg)++;
129  pIntA=(const int*) pA;
130  pIntB=(const int*) pB;
131 
132  if (*pIntA < *pIntB)
133  return -1;
134  else if (*pIntA > *pIntB)
135  return 1;
136  else
137  return 0;
138 }
139 
140 
141 
142 #else
143 
145 {
146  DBG_ERROR(GWEN_LOGDOMAIN, "Gwenhywfar was compiled without test code enabled.");
147  return GWEN_ERROR_GENERIC;
148 }
149 
150 
151 
152 #endif
#define DBG_ERROR(dbg_logger, format,...)
Definition: debug.h:97
int test1(int argc, char **argv)
#define NULL
Definition: binreloc.c:300
#define GWEN_LOGDOMAIN
Definition: logger.h:32
GWEN_TEST_MODULE * GWEN_Test_Module_AddTest(GWEN_TEST_MODULE *st, const char *tName, GWEN_TEST_MODULE_TEST_FN fn, const char *tDescr)
Definition: testmodule.c:424
#define GWENHYWFAR_CB
Definition: gwenhywfarapi.h:89
struct GWEN_TEST_MODULE GWEN_TEST_MODULE
Definition: testmodule.h:65
#define GWEN_ERROR_GENERIC
Definition: error.h:62
void GWEN_QuickSort(void *array, int numElems, int sizeElems, GWEN_QUICKSORT_COMPARE_CB cb, void *arg)
Definition: quicksort.c:49
int GWEN_Quicksort_AddTests(GWEN_TEST_MODULE *mod)
Definition: quicksort-t.c:144
GWEN_TEST_MODULE * GWEN_Test_Module_AddModule(GWEN_TEST_MODULE *st, const char *tName, const char *tDescr)
Definition: testmodule.c:440
#define GWEN_UNUSED