1 /* Test of ordered set data type implementation.
2 Copyright (C) 2020 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/>. */
19 action (const void *str
, void *data
)
21 ((char *) str
)[0] += *(int *)data
;
25 test_update (gl_oset_implementation_t implementation
)
33 gl_oset_nx_create_empty (implementation
, (gl_setelement_compar_fn
) strcmp
, NULL
);
34 ASSERT (set1
!= NULL
);
37 ASSERT (gl_oset_nx_add (set1
, C
) == 1);
38 ASSERT (gl_oset_nx_add (set1
, A
) == 1);
39 ASSERT (gl_oset_nx_add (set1
, B
) == 1);
40 ASSERT (gl_oset_nx_add (set1
, D
) == 1);
42 /* Verify that set1 = ["A", "B", "C", "D"]. */
44 gl_oset_iterator_t iter
= gl_oset_iterator (set1
);
47 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
49 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
51 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
53 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
55 ASSERT (!gl_oset_iterator_next (&iter
, &elt
));
58 /* Make a side effect on an element in the set, that moves the element. */
61 ASSERT (gl_oset_update (set1
, B
, action
, &data
) == 1);
63 /* Verify that set1 = ["A", "C", "D", "G"]. */
65 gl_oset_iterator_t iter
= gl_oset_iterator (set1
);
68 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
70 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
72 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
74 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
76 ASSERT (!gl_oset_iterator_next (&iter
, &elt
));
79 /* Make a side effect on an element in the set, that does not move the
83 ASSERT (gl_oset_update (set1
, D
, action
, &data
) == 0);
85 /* Verify that set1 = ["A", "C", "E", "G"]. */
87 gl_oset_iterator_t iter
= gl_oset_iterator (set1
);
90 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
92 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
94 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
96 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
98 ASSERT (!gl_oset_iterator_next (&iter
, &elt
));
101 /* Make a side effect on an element in the set, that provokes a
104 int data
= 'G' - 'A';
105 ASSERT (gl_oset_update (set1
, A
, action
, &data
) == -1);
107 /* Verify that set1 = ["C", "E", "G"]. */
109 gl_oset_iterator_t iter
= gl_oset_iterator (set1
);
112 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
114 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
116 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
118 ASSERT (!gl_oset_iterator_next (&iter
, &elt
));
121 /* Make a side effect on an element that is not in the set. */
123 int data
= 'R' - 'G';
124 ASSERT (gl_oset_update (set1
, A
, action
, &data
) == 0);
126 /* Verify that set1 = ["C", "E", "G"]. */
128 gl_oset_iterator_t iter
= gl_oset_iterator (set1
);
131 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
133 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
135 ASSERT (gl_oset_iterator_next (&iter
, &elt
));
137 ASSERT (!gl_oset_iterator_next (&iter
, &elt
));