rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

test_algorithm.c (3890B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "algorithm.h"
     17 
     18 struct data {
     19   char dummy[16];
     20   double dbl;
     21 };
     22 
     23 static int
     24 cmp_int(const void* a, const void* b)
     25 {
     26   CHK(a != NULL);
     27   CHK(b != NULL);
     28   return *(const int*)a - *(const int*)b;
     29 }
     30 
     31 static int
     32 cmp_dbl(const void* a, const void* b)
     33 {
     34   CHK(a != NULL);
     35   CHK(b != NULL);
     36   return *(const double*)a < *(const double*) b
     37     ? -1 : (*(const double*)a > *(const double*)b ? 1 : 0);
     38 }
     39 
     40 static int
     41 cmp_key_data(const void* key, const void* data)
     42 {
     43   const double h = *((const double*)key);
     44   const struct data* d = (const struct data*)data;
     45   return h < d->dbl ? -1 : (h > d->dbl ? 1 : 0);
     46 }
     47 
     48 int
     49 main(int argc, char** argv)
     50 {
     51   const int array_int[] = { 0, 2, 4, 4, 4, 6, 10, 11, 13, 15, 17 };
     52   const size_t nints = sizeof(array_int)/sizeof(int);
     53   const double array_dbl[] = {
     54     -1.123, 2.3, 2.3, 2.3, 4, 5.0, 5.2, 6, 9.1, 19.123
     55   };
     56   const size_t ndbls = sizeof(array_dbl)/sizeof(double);
     57   const struct data data[] = {
     58     {{0},-1.123},
     59     {{0},2.3},
     60     {{0},2.3},
     61     {{0},2.3},
     62     {{0},4.0},
     63     {{0},5.0},
     64     {{0},5.2},
     65     {{0},6.0},
     66     {{0},9.1},
     67     {{0},19.123}
     68   };
     69   const size_t ndata = sizeof(data)/sizeof(struct data);
     70 
     71   int* pi;
     72   int i;
     73   double* pd;
     74   struct data* pdata;
     75   double d;
     76   (void)argc, (void)argv;
     77 
     78   #define SEARCH search_lower_bound
     79 
     80   i = 3, pi = SEARCH(&i, array_int, nints, sizeof(int), cmp_int);
     81   CHK(*pi == 4);
     82   CHK(pi[3] == 6);
     83   i = 4, pi = SEARCH(&i, array_int, nints, sizeof(int), cmp_int);
     84   CHK(*pi == 4);
     85   CHK(pi[3] == 6);
     86   i = 5, pi = SEARCH(&i, array_int, nints, sizeof(int), cmp_int);
     87   CHK(*pi == 6);
     88   i = 13, pi = SEARCH(&i, array_int, nints, sizeof(int), cmp_int);
     89   CHK(*pi == 13);
     90   i = -1, pi = SEARCH(&i, array_int, nints, sizeof(int), cmp_int);
     91   CHK(*pi == 0);
     92   i = 19, pi = SEARCH(&i, array_int, nints, sizeof(int), cmp_int);
     93   CHK(pi == NULL);
     94 
     95   d = 2.1, pd = SEARCH(&d, array_dbl, ndbls, sizeof(double), cmp_dbl);
     96   CHK(*pd == 2.3);
     97   d = 2.3, pd = SEARCH(&d, array_dbl, ndbls, sizeof(double), cmp_dbl);
     98   CHK(*pd == 2.3);
     99   CHK(pd[3] == 4);
    100   d = -1.0, pd = SEARCH(&d, array_dbl, ndbls, sizeof(double), cmp_dbl);
    101   CHK(*pd == 2.3);
    102   d = 6.001, pd = SEARCH(&d, array_dbl, ndbls, sizeof(double), cmp_dbl);
    103   CHK(*pd == 9.1);
    104   d = 19.0, pd = SEARCH(&d, array_dbl, ndbls, sizeof(double), cmp_dbl);
    105   CHK(*pd == 19.123);
    106   d = 20.0, pd = SEARCH(&d, array_dbl, ndbls, sizeof(double), cmp_dbl);
    107   CHK(pd == NULL);
    108 
    109   d = 2.1, pdata = SEARCH(&d, data, ndata, sizeof(struct data), cmp_key_data);
    110   CHK(pdata->dbl == 2.3);
    111   d = 2.3, pdata = SEARCH(&d, data, ndata, sizeof(struct data), cmp_key_data);
    112   CHK(pdata->dbl == 2.3);
    113   CHK(pdata[3].dbl == 4);
    114   d = -1.0, pdata = SEARCH(&d, data, ndata, sizeof(struct data), cmp_key_data);
    115   CHK(pdata->dbl == 2.3);
    116   d = 6.001, pdata = SEARCH(&d, data, ndata, sizeof(struct data), cmp_key_data);
    117   CHK(pdata->dbl == 9.1);
    118   d = 19.0, pdata = SEARCH(&d, data, ndata, sizeof(struct data), cmp_key_data);
    119   CHK(pdata->dbl == 19.123);
    120   d = 20.0, pdata = SEARCH(&d, data, ndata, sizeof(struct data), cmp_key_data);
    121   CHK(pdata == NULL);
    122 
    123   i = -1, pi = SEARCH(&i, array_int, 1, sizeof(int), cmp_int);
    124   CHK(*pi ==  0);
    125 
    126   return 0;
    127 }