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/>. */
21 #include "gl_array_oset.h"
28 reverse_strcmp (const char *str1
, const char *str2
)
30 int cmp
= strcmp (str1
, str2
);
31 return (cmp
> 0 ? -1 : cmp
< 0 ? 1 : 0);
35 action (const char *str
, int *data
)
37 const_cast<char *> (str
) [0] += *data
;
41 is_at_most (const char *str
, const char *threshold
)
43 return strcmp (str
, threshold
) <= 0;
47 main (int argc
, char *argv
[])
50 gl_OSet
<const char *> set1
;
52 set1
= gl_OSet
<const char *> (GL_ARRAY_OSET
, reverse_strcmp
, NULL
);
57 ASSERT (set1
.size () == 3);
60 gl_OSet
<const char *>::iterator iter1
= set1
.begin ();
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
));
72 ASSERT (set1
.update (A
, action
, &data
) == 1);
75 gl_OSet
<const char *>::iterator iter2
= set1
.begin ();
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");
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
));
98 return test_exit_status
;