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

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

DEFINITIONS

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

   1 /* Test of ordered set data type implementation.
   2    Copyright (C) 2006-2021 Free Software Foundation, Inc.
   3    Written by Bruno Haible <bruno@clisp.org>, 2006.
   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_oset.h"
  21 
  22 #include <stdlib.h>
  23 #include <string.h>
  24 
  25 #include "gl_array_oset.h"
  26 #include "macros.h"
  27 
  28 #include "test-oset-update.h"
  29 
  30 extern void gl_rbtree_oset_check_invariants (gl_oset_t set);
  31 
  32 static const char *objects[30] =
  33   {
  34     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
  35     "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "<", ">", "[", "]"
  36   };
  37 
  38 #define RANDOM(n) (rand () % (n))
  39 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))]
  40 
  41 static void
  42 check_equals (gl_oset_t set1, gl_oset_t set2)
     /* [previous][next][first][last][top][bottom][index][help] */
  43 {
  44   size_t n = gl_oset_size (set1);
  45   gl_oset_iterator_t iter1, iter2;
  46   const void *elt1;
  47   const void *elt2;
  48   size_t i;
  49 
  50   iter1 = gl_oset_iterator (set1);
  51   iter2 = gl_oset_iterator (set2);
  52   for (i = 0; i < n; i++)
  53     {
  54       ASSERT (gl_oset_iterator_next (&iter1, &elt1));
  55       ASSERT (gl_oset_iterator_next (&iter2, &elt2));
  56       ASSERT (elt1 == elt2);
  57     }
  58   ASSERT (!gl_oset_iterator_next (&iter1, &elt1));
  59   ASSERT (!gl_oset_iterator_next (&iter2, &elt2));
  60   gl_oset_iterator_free (&iter1);
  61   gl_oset_iterator_free (&iter2);
  62 }
  63 
  64 static void
  65 check_all (gl_oset_t set1, gl_oset_t set2)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67   gl_rbtree_oset_check_invariants (set2);
  68   check_equals (set1, set2);
  69 }
  70 
  71 static bool
  72 is_at_least (const void *elt, const void *threshold)
     /* [previous][next][first][last][top][bottom][index][help] */
  73 {
  74   return strcmp ((const char *) elt, (const char *) threshold) >= 0;
  75 }
  76 
  77 int
  78 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  79 {
  80   gl_oset_t set1, set2;
  81 
  82   /* Allow the user to provide a non-default random seed on the command line.  */
  83   if (argc > 1)
  84     srand (atoi (argv[1]));
  85 
  86   {
  87     size_t initial_size = RANDOM (20);
  88     size_t i;
  89     unsigned int repeat;
  90 
  91     /* Create set1.  */
  92     set1 = gl_oset_nx_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL);
  93     ASSERT (set1 != NULL);
  94 
  95     /* Create set2.  */
  96     set2 = gl_oset_nx_create_empty (GL_RBTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL);
  97     ASSERT (set2 != NULL);
  98 
  99     check_all (set1, set2);
 100 
 101     /* Initialize them.  */
 102     for (i = 0; i < initial_size; i++)
 103       {
 104         const char *obj = RANDOM_OBJECT ();
 105         ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj));
 106         check_all (set1, set2);
 107       }
 108 
 109     for (repeat = 0; repeat < 100000; repeat++)
 110       {
 111         unsigned int operation = RANDOM (4);
 112         switch (operation)
 113           {
 114           case 0:
 115             {
 116               const char *obj = RANDOM_OBJECT ();
 117               ASSERT (gl_oset_search (set1, obj) == gl_oset_search (set2, obj));
 118             }
 119             break;
 120           case 1:
 121             {
 122               const char *obj = RANDOM_OBJECT ();
 123               ASSERT (gl_oset_nx_add (set1, obj) == gl_oset_nx_add (set2, obj));
 124             }
 125             break;
 126           case 2:
 127             {
 128               const char *obj = RANDOM_OBJECT ();
 129               ASSERT (gl_oset_remove (set1, obj) == gl_oset_remove (set2, obj));
 130             }
 131             break;
 132           case 3:
 133             {
 134               const char *obj = RANDOM_OBJECT ();
 135               gl_oset_iterator_t iter1 = gl_oset_iterator_atleast (set1, is_at_least, obj);
 136               gl_oset_iterator_t iter2 = gl_oset_iterator_atleast (set2, is_at_least, obj);
 137               const void *elt1;
 138               const void *elt2;
 139               /* Check the first two values that the iterator produces.
 140                  Checking them all would make this part of the test dominate the
 141                  run time of the test.  */
 142               bool havenext1 = gl_oset_iterator_next (&iter1, &elt1);
 143               bool havenext2 = gl_oset_iterator_next (&iter2, &elt2);
 144               ASSERT (havenext1 == havenext2);
 145               if (havenext1)
 146                 {
 147                   ASSERT (elt1 == elt2);
 148                   havenext1 = gl_oset_iterator_next (&iter1, &elt1);
 149                   havenext2 = gl_oset_iterator_next (&iter2, &elt2);
 150                   ASSERT (havenext1 == havenext2);
 151                   if (havenext1)
 152                     ASSERT (elt1 == elt2);
 153                 }
 154               gl_oset_iterator_free (&iter1);
 155               gl_oset_iterator_free (&iter2);
 156             }
 157             break;
 158           }
 159         check_all (set1, set2);
 160       }
 161 
 162     gl_oset_free (set1);
 163     gl_oset_free (set2);
 164   }
 165 
 166   test_update (GL_RBTREE_OSET);
 167 
 168   return 0;
 169 }

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