root/maint/gnulib/tests/test-array_omap.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. check_equals
  2. check_all
  3. main

   1 /* Test of ordered map data type implementation.
   2    Copyright (C) 2006-2021 Free Software Foundation, Inc.
   3    Written by Bruno Haible <bruno@clisp.org>, 2018.
   4 
   5    This program is free software: you can redistribute it and/or modify
   6    it under the terms of the GNU General Public License as published by
   7    the Free Software Foundation; either version 3 of the License, or
   8    (at your option) any later version.
   9 
  10    This program is distributed in the hope that it will be useful,
  11    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13    GNU General Public License for more details.
  14 
  15    You should have received a copy of the GNU General Public License
  16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  17 
  18 #include <config.h>
  19 
  20 #include "gl_array_omap.h"
  21 
  22 #include <stdlib.h>
  23 #include <string.h>
  24 
  25 #include "gl_xlist.h"
  26 #include "gl_array_list.h"
  27 #include "macros.h"
  28 
  29 static const char *objects[30] =
  30   {
  31     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
  32     "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
  33   };
  34 
  35 #define RANDOM(n) (rand () % (n))
  36 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
  37 
  38 static void
  39 check_equals (gl_omap_t map1, gl_list_t keys, gl_list_t values)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41   size_t n = gl_omap_size (map1);
  42   gl_omap_iterator_t iter1;
  43   gl_list_iterator_t iterk, iterv;
  44   const void *key1;
  45   const void *value1;
  46   const void *key2;
  47   const void *value2;
  48   gl_list_node_t node;
  49   size_t i;
  50 
  51   iter1 = gl_omap_iterator (map1);
  52   iterk = gl_list_iterator (keys);
  53   iterv = gl_list_iterator (values);
  54   for (i = 0; i < n; i++)
  55     {
  56       ASSERT (gl_omap_iterator_next (&iter1, &key1, &value1));
  57       ASSERT (gl_list_iterator_next (&iterk, &key2, &node));
  58       ASSERT (gl_list_iterator_next (&iterv, &value2, &node));
  59       ASSERT (key1 == key2);
  60       ASSERT (value1 == value2);
  61     }
  62   ASSERT (!gl_omap_iterator_next (&iter1, &key1, &value1));
  63   ASSERT (!gl_list_iterator_next (&iterk, &key2, &node));
  64   ASSERT (!gl_list_iterator_next (&iterv, &value2, &node));
  65   gl_omap_iterator_free (&iter1);
  66   gl_list_iterator_free (&iterk);
  67   gl_list_iterator_free (&iterv);
  68 }
  69 
  70 static void
  71 check_all (gl_omap_t map1, gl_list_t keys, gl_list_t values)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73   check_equals (map1, keys, values);
  74 }
  75 
  76 int
  77 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79   gl_omap_t map1;
  80   gl_list_t keys;
  81   gl_list_t values;
  82 
  83   /* Allow the user to provide a non-default random seed on the command line.  */
  84   if (argc > 1)
  85     srand (atoi (argv[1]));
  86 
  87   {
  88     size_t initial_size = RANDOM (20);
  89     size_t i;
  90     unsigned int repeat;
  91 
  92     /* Create map1.  */
  93     map1 = gl_omap_nx_create_empty (GL_ARRAY_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
  94     ASSERT (map1 != NULL);
  95 
  96     /* Create keys and values.  */
  97     keys = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
  98     values = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, false);
  99 
 100     check_all (map1, keys, values);
 101 
 102     /* Initialize them.  */
 103     for (i = 0; i < initial_size; i++)
 104       {
 105         const char *key = RANDOM_OBJECT ();
 106         const char *value = RANDOM_OBJECT ();
 107         bool added = gl_omap_nx_put (map1, key, value);
 108         size_t index = gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
 109         ASSERT (added == (index == (size_t)(-1)));
 110         if (added)
 111           {
 112             gl_sortedlist_add (keys, (gl_listelement_compar_fn)strcmp, key);
 113             index = gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
 114             gl_list_add_at (values, index, value);
 115           }
 116         else
 117           gl_list_set_at (values, index, value);
 118         check_all (map1, keys, values);
 119       }
 120 
 121     for (repeat = 0; repeat < 100000; repeat++)
 122       {
 123         unsigned int operation = RANDOM (3);
 124         switch (operation)
 125           {
 126           case 0:
 127             {
 128               const char *key = RANDOM_OBJECT ();
 129               const void *ret = gl_omap_get (map1, key);
 130               size_t index =
 131                 gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
 132               ASSERT (ret
 133                       == (index != (size_t)(-1) ? gl_list_get_at (values, index) : NULL));
 134             }
 135             break;
 136           case 1:
 137             {
 138               const char *key = RANDOM_OBJECT ();
 139               const char *value = RANDOM_OBJECT ();
 140               bool added = gl_omap_nx_put (map1, key, value);
 141               size_t index =
 142                 gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
 143               ASSERT (added == (index == (size_t)(-1)));
 144               if (added)
 145                 {
 146                   gl_sortedlist_add (keys, (gl_listelement_compar_fn)strcmp, key);
 147                   index = gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
 148                   gl_list_add_at (values, index, value);
 149                 }
 150               else
 151                 gl_list_set_at (values, index, value);
 152             }
 153             break;
 154           case 2:
 155             {
 156               const char *key = RANDOM_OBJECT ();
 157               bool removed = gl_omap_remove (map1, key);
 158               size_t index =
 159                 gl_sortedlist_indexof (keys, (gl_listelement_compar_fn)strcmp, key);
 160               ASSERT (removed == (index != (size_t)(-1)));
 161               if (removed)
 162                 {
 163                   gl_list_remove_at (keys, index);
 164                   gl_list_remove_at (values, index);
 165                 }
 166             }
 167             break;
 168           }
 169         check_all (map1, keys, values);
 170       }
 171 
 172     gl_omap_free (map1);
 173     gl_list_free (keys);
 174     gl_list_free (values);
 175   }
 176 
 177   return 0;
 178 }

/* [previous][next][first][last][top][bottom][index][help] */