root/maint/gnulib/tests/test-rbtree_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_rbtree_omap.h"
  21 
  22 #include <stdlib.h>
  23 #include <string.h>
  24 
  25 #include "gl_array_omap.h"
  26 #include "macros.h"
  27 
  28 extern void gl_rbtree_omap_check_invariants (gl_omap_t set);
  29 
  30 static const char *objects[30] =
  31   {
  32     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
  33     "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
  34   };
  35 
  36 #define RANDOM(n) (rand () % (n))
  37 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
  38 
  39 static void
  40 check_equals (gl_omap_t map1, gl_omap_t map2)
     /* [previous][next][first][last][top][bottom][index][help] */
  41 {
  42   size_t n = gl_omap_size (map1);
  43   gl_omap_iterator_t iter1, iter2;
  44   const void *key1;
  45   const void *value1;
  46   const void *key2;
  47   const void *value2;
  48   size_t i;
  49 
  50   iter1 = gl_omap_iterator (map1);
  51   iter2 = gl_omap_iterator (map2);
  52   for (i = 0; i < n; i++)
  53     {
  54       ASSERT (gl_omap_iterator_next (&iter1, &key1, &value1));
  55       ASSERT (gl_omap_iterator_next (&iter2, &key2, &value2));
  56       ASSERT (key1 == key2);
  57       ASSERT (value1 == value2);
  58     }
  59   ASSERT (!gl_omap_iterator_next (&iter1, &key1, &value1));
  60   ASSERT (!gl_omap_iterator_next (&iter2, &key2, &value2));
  61   gl_omap_iterator_free (&iter1);
  62   gl_omap_iterator_free (&iter2);
  63 }
  64 
  65 static void
  66 check_all (gl_omap_t map1, gl_omap_t map2)
     /* [previous][next][first][last][top][bottom][index][help] */
  67 {
  68   gl_rbtree_omap_check_invariants (map2);
  69   check_equals (map1, map2);
  70 }
  71 
  72 int
  73 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  74 {
  75   gl_omap_t map1, map2;
  76 
  77   /* Allow the user to provide a non-default random seed on the command line.  */
  78   if (argc > 1)
  79     srand (atoi (argv[1]));
  80 
  81   {
  82     size_t initial_size = RANDOM (20);
  83     size_t i;
  84     unsigned int repeat;
  85 
  86     /* Create map1.  */
  87     map1 = gl_omap_nx_create_empty (GL_ARRAY_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
  88     ASSERT (map1 != NULL);
  89 
  90     /* Create map2.  */
  91     map2 = gl_omap_nx_create_empty (GL_RBTREE_OMAP, (gl_mapkey_compar_fn) strcmp, NULL, NULL);
  92     ASSERT (map2 != NULL);
  93 
  94     check_all (map1, map2);
  95 
  96     /* Initialize them.  */
  97     for (i = 0; i < initial_size; i++)
  98       {
  99         const char *key = RANDOM_OBJECT ();
 100         const char *value = RANDOM_OBJECT ();
 101         ASSERT (gl_omap_nx_put (map1, key, value) == gl_omap_nx_put (map2, key, value));
 102         check_all (map1, map2);
 103       }
 104 
 105     for (repeat = 0; repeat < 100000; repeat++)
 106       {
 107         unsigned int operation = RANDOM (3);
 108         switch (operation)
 109           {
 110           case 0:
 111             {
 112               const char *key = RANDOM_OBJECT ();
 113               ASSERT (gl_omap_get (map1, key) == gl_omap_get (map2, key));
 114             }
 115             break;
 116           case 1:
 117             {
 118               const char *key = RANDOM_OBJECT ();
 119               const char *value = RANDOM_OBJECT ();
 120               ASSERT (gl_omap_nx_put (map1, key, value) == gl_omap_nx_put (map2, key, value));
 121             }
 122             break;
 123           case 2:
 124             {
 125               const char *key = RANDOM_OBJECT ();
 126               ASSERT (gl_omap_remove (map1, key) == gl_omap_remove (map2, key));
 127             }
 128             break;
 129           }
 130         check_all (map1, map2);
 131       }
 132 
 133     gl_omap_free (map1);
 134     gl_omap_free (map2);
 135   }
 136 
 137   return 0;
 138 }

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