test-framework-sh: Don't leave temporary directories on NetBSD.
[gnulib.git] / tests / test-oset-c++.cc
blob0226ccf717fe9ad15f70dc44e3b8618247fcd7d0
1 /* Test of ordered set data type implementation as a C++ class.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2020.
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.
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.
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/>. */
18 #include <config.h>
20 #include "gl_oset.hh"
21 #include "gl_array_oset.h"
23 #include <string.h>
25 #include "macros.h"
27 static int
28 reverse_strcmp (const char *str1, const char *str2)
30 int cmp = strcmp (str1, str2);
31 return (cmp > 0 ? -1 : cmp < 0 ? 1 : 0);
34 static void
35 action (const char *str, int *data)
37 const_cast<char *> (str) [0] += *data;
40 static bool
41 is_at_most (const char *str, const char *threshold)
43 return strcmp (str, threshold) <= 0;
46 int
47 main (int argc, char *argv[])
49 char A[2] = "A";
50 gl_OSet<const char *> set1;
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);
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));
71 int data = 'Z' - 'A';
72 ASSERT (set1.update (A, action, &data) == 1);
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));
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));
96 set1.free ();
98 return test_exit_status;