root/maint/gnulib/tests/test-oset-c++.cc

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

DEFINITIONS

This source file includes following definitions.
  1. reverse_strcmp
  2. action
  3. is_at_most
  4. main

   1 /* Test of ordered set data type implementation as a C++ class.
   2    Copyright (C) 2020-2021 Free Software Foundation, Inc.
   3    Written by Bruno Haible <bruno@clisp.org>, 2020.
   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_oset.hh"
  21 #include "gl_array_oset.h"
  22 
  23 #include <string.h>
  24 
  25 #include "macros.h"
  26 
  27 static int
  28 reverse_strcmp (const char *str1, const char *str2)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30   int cmp = strcmp (str1, str2);
  31   return (cmp > 0 ? -1 : cmp < 0 ? 1 : 0);
  32 }
  33 
  34 static void
  35 action (const char *str, int *data)
     /* [previous][next][first][last][top][bottom][index][help] */
  36 {
  37   const_cast<char *> (str) [0] += *data;
  38 }
  39 
  40 static bool
  41 is_at_most (const char *str, const char *threshold)
     /* [previous][next][first][last][top][bottom][index][help] */
  42 {
  43   return strcmp (str, threshold) <= 0;
  44 }
  45 
  46 int
  47 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49   char A[2] = "A";
  50   gl_OSet<const char *> set1;
  51 
  52   set1 = gl_OSet<const char *> (GL_ARRAY_OSET, reverse_strcmp, NULL);
  53   set1.add (A);
  54   set1.add ("C");
  55   set1.add ("D");
  56   set1.add ("C");
  57   ASSERT (set1.size () == 3);
  58 
  59   {
  60     gl_OSet<const char *>::iterator iter1 = set1.begin ();
  61     const char *elt;
  62     ASSERT (iter1.next (elt));
  63     ASSERT (strcmp (elt, "D") == 0);
  64     ASSERT (iter1.next (elt));
  65     ASSERT (strcmp (elt, "C") == 0);
  66     ASSERT (iter1.next (elt));
  67     ASSERT (strcmp (elt, "A") == 0);
  68     ASSERT (!iter1.next (elt));
  69   }
  70 
  71   int data = 'Z' - 'A';
  72   ASSERT (set1.update (A, action, &data) == 1);
  73 
  74   {
  75     gl_OSet<const char *>::iterator iter2 = set1.begin ();
  76     const char *elt;
  77     ASSERT (iter2.next (elt));
  78     ASSERT (strcmp (elt, "Z") == 0);
  79     ASSERT (iter2.next (elt));
  80     ASSERT (strcmp (elt, "D") == 0);
  81     ASSERT (iter2.next (elt));
  82     ASSERT (strcmp (elt, "C") == 0);
  83     ASSERT (!iter2.next (elt));
  84   }
  85 
  86   {
  87     gl_OSet<const char *>::iterator iter3 = set1.begin_atleast (is_at_most, "R");
  88     const char *elt;
  89     ASSERT (iter3.next (elt));
  90     ASSERT (strcmp (elt, "D") == 0);
  91     ASSERT (iter3.next (elt));
  92     ASSERT (strcmp (elt, "C") == 0);
  93     ASSERT (!iter3.next (elt));
  94   }
  95 
  96   set1.free ();
  97 
  98   return 0;
  99 }

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