rsys

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

test_free_list.c (4931B)


      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 "rsys.h"
     17 #include "free_list.h"
     18 
     19 struct object {
     20   FITEM;
     21   unsigned int i;
     22 };
     23 
     24 #define FITEM_TYPE object
     25 #include "free_list.h"
     26 
     27 int
     28 main(int argc, char** argv)
     29 {
     30   #define NB_OBJ 1024
     31   struct flist_object list;
     32   struct object* obj = NULL;
     33   struct fid id[NB_OBJ];
     34   char find[NB_OBJ];
     35   size_t nitems;
     36   int i = 0;
     37   (void)argc, (void)argv;
     38 
     39   FOR_EACH(i, 0, NB_OBJ) {
     40     id[i] = FID_NULL;
     41   }
     42 
     43   flist_object_init(NULL, &list);
     44   CHK(flist_object_is_empty(&list));
     45   CHK(flist_object_hold(&list, id[0]) == 0);
     46   CHK(flist_object_get(&list, id[0]) == NULL);
     47 
     48   id[0] = flist_object_add(&list);
     49   CHK(id[0].index == 0);
     50   CHK(flist_object_is_empty(&list) == 0);
     51   flist_object_clear(&list);
     52 
     53   id[0] = flist_object_add(&list);
     54   id[1] = flist_object_add(&list);
     55   CHK(id[0].index == 0);
     56   CHK(id[1].index == 1);
     57   flist_object_del(&list, id[0]);
     58   CHK(id[1].index == 1);
     59   id[0] = flist_object_add(&list);
     60   CHK(id[0].index == 0);
     61   CHK(id[1].index == 1);
     62   flist_object_del(&list, id[0]);
     63   flist_object_del(&list, id[1]);
     64   id[0] = flist_object_add(&list);
     65   id[1] = flist_object_add(&list);
     66   flist_object_clear(&list);
     67   CHK(id[0].index != id[1].index);
     68   CHK(id[0].index <= 1);
     69   CHK(id[1].index <= 1);
     70   CHK(flist_object_is_empty(&list) == 1);
     71 
     72   FOR_EACH(i, 0, NB_OBJ / 2) {
     73     struct fid tmp_id;
     74     id[i] = flist_object_add(&list);
     75     CHK(flist_object_hold(&list, id[i]) == 1);
     76     obj = flist_object_get(&list, id[i]);
     77     tmp_id = object_id_get(obj);
     78     CHK(id[i].index == (unsigned)i);
     79     CHK(FID_EQ(tmp_id, id[i]) == 1);
     80     CHK(obj != NULL);
     81     obj->i = 0xDECAF000 + (unsigned)i;
     82   }
     83   CHK(flist_object_is_empty(&list) == 0);
     84 
     85   FOR_EACH(i, 0, NB_OBJ * 2 / 3) {
     86     const float rand_f /* in [0, 1] */ = (float)rand() / (float)RAND_MAX;
     87     const int ii = (int)(rand_f * (NB_OBJ - 1));
     88     flist_object_del(&list, id[ii]);
     89     id[ii] = FID_NULL;
     90   }
     91 
     92   FOR_EACH(i, NB_OBJ / 2, NB_OBJ) {
     93     id[i] = flist_object_add(&list);
     94     CHK(flist_object_hold(&list, id[i]) == 1);
     95     obj = flist_object_get(&list, id[i]);
     96     CHK(obj != NULL);
     97     obj->i = 0xDECAF000 + (unsigned)i;
     98   }
     99 
    100   FOR_EACH(i, 0, NB_OBJ) {
    101     if(IS_FID_NULL(id[i])) {
    102       CHK(flist_object_hold(&list, id[i]) == 0);
    103       CHK(flist_object_get(&list, id[i]) == NULL);
    104     } else {
    105       CHK(flist_object_hold(&list, id[i]) == 1);
    106       obj = flist_object_get(&list, id[i]);
    107       CHK(obj->i == 0xDECAF000 + (unsigned)i);
    108     }
    109   }
    110 
    111   flist_object_release(&list);
    112   flist_object_init(NULL, &list);
    113 
    114   FOR_EACH(i, 0, NB_OBJ) {
    115     id[i] = flist_object_add(&list);
    116     obj = flist_object_get(&list, id[i]);
    117     obj->i = (unsigned)i;
    118   }
    119 
    120   nitems = 0;
    121   memset(find, 0, NB_OBJ * sizeof(char));
    122   FLIST_FOR_EACH(obj, &list) {
    123     CHK(find[obj->i] == 0);
    124     find[obj->i] = 1;
    125     ++nitems;
    126   }
    127   CHK(nitems == NB_OBJ);
    128 
    129   FOR_EACH(i, 0, NB_OBJ / 2)
    130     flist_object_del(&list, id[i*2]);
    131 
    132   nitems = 0;
    133   memset(find, 0, NB_OBJ * sizeof(char));
    134   FLIST_FOR_EACH(obj, &list) {
    135     CHK(find[obj->i] == 0);
    136     find[obj->i] = 1;
    137     ++nitems;
    138   }
    139   CHK(nitems == NB_OBJ/2);
    140   FOR_EACH(i, 0, NB_OBJ) {
    141     if(i%2) {
    142       CHK(flist_object_hold(&list, id[i]) == 1);
    143       CHK(find[i] == 1);
    144     } else {
    145       CHK(flist_object_hold(&list, id[i]) == 0);
    146       CHK(find[i] == 0);
    147       CHK(IS_FID_NULL(object_id_get(list.items + id[i].index)) == 1);
    148     }
    149   }
    150 
    151   CHK(flist_object_is_empty(&list) == 0);
    152   flist_object_clear(&list);
    153   CHK(flist_object_is_empty(&list) == 1);
    154   nitems = 0;
    155   FLIST_FOR_EACH(obj, &list)
    156     ++nitems;
    157   CHK(nitems == 0);
    158 
    159   FOR_EACH(i, 0, NB_OBJ)
    160     CHK(flist_object_hold(&list, id[i]) == 0);
    161 
    162   FOR_EACH(i, 0, NB_OBJ/4) {
    163     id[i] = flist_object_add(&list);
    164     obj = flist_object_get(&list, id[i]);
    165     obj->i = (unsigned)i*2;
    166   }
    167 
    168   nitems = 0;
    169   memset(find, 0, NB_OBJ * sizeof(char));
    170   FLIST_FOR_EACH(obj, &list) {
    171     CHK(obj->i % 2 == 0);
    172     find[obj->i/2] = 1;
    173     ++nitems;
    174   }
    175   CHK(nitems == NB_OBJ / 4);
    176   FOR_EACH(i, 0, NB_OBJ/4) {
    177     CHK(find[i] == 1);
    178     CHK(flist_object_hold(&list, id[i]) == 1);
    179     CHK(flist_object_get(&list, id[i])->i == (unsigned)i * 2);
    180   }
    181 
    182   flist_object_release(&list);
    183   CHK(MEM_ALLOCATED_SIZE(&mem_default_allocator) == 0);
    184   return 0;
    185 }