Branch
Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/* Test of ordered set data type implementation.
Copyright (C) 2020-2025 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2020.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
static void
action (const void *str, void *data)
{
((char *) str)[0] += *(int *)data;
}
static void
test_update (gl_oset_implementation_t implementation)
{
char A[2] = "A";
char B[2] = "B";
char C[2] = "C";
char D[2] = "D";
gl_oset_t set1 =
gl_oset_nx_create_empty (implementation, (gl_setelement_compar_fn) strcmp, NULL);
ASSERT (set1 != NULL);
/* Fill the set. */
ASSERT (gl_oset_nx_add (set1, C) == 1);
ASSERT (gl_oset_nx_add (set1, A) == 1);
ASSERT (gl_oset_nx_add (set1, B) == 1);
ASSERT (gl_oset_nx_add (set1, D) == 1);
/* Verify that set1 = ["A", "B", "C", "D"]. */
{
gl_oset_iterator_t iter = gl_oset_iterator (set1);
const void *elt;
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == A);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == B);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == C);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == D);
ASSERT (!gl_oset_iterator_next (&iter, &elt));
}
/* Make a side effect on an element in the set, that moves the element. */
{
int data = 'G' - 'B';
ASSERT (gl_oset_update (set1, B, action, &data) == 1);
}
/* Verify that set1 = ["A", "C", "D", "G"]. */
{
gl_oset_iterator_t iter = gl_oset_iterator (set1);
const void *elt;
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == A);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == C);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == D);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == B);
ASSERT (!gl_oset_iterator_next (&iter, &elt));
}
/* Make a side effect on an element in the set, that does not move the
element. */
{
int data = 'E' - 'D';
ASSERT (gl_oset_update (set1, D, action, &data) == 0);
}
/* Verify that set1 = ["A", "C", "E", "G"]. */
{
gl_oset_iterator_t iter = gl_oset_iterator (set1);
const void *elt;
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == A);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == C);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == D);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == B);
ASSERT (!gl_oset_iterator_next (&iter, &elt));
}
/* Make a side effect on an element in the set, that provokes a
collision. */
{
int data = 'G' - 'A';
ASSERT (gl_oset_update (set1, A, action, &data) == -1);
}
/* Verify that set1 = ["C", "E", "G"]. */
{
gl_oset_iterator_t iter = gl_oset_iterator (set1);
const void *elt;
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == C);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == D);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == B);
ASSERT (!gl_oset_iterator_next (&iter, &elt));
}
/* Make a side effect on an element that is not in the set. */
{
int data = 'R' - 'G';
ASSERT (gl_oset_update (set1, A, action, &data) == 0);
}
/* Verify that set1 = ["C", "E", "G"]. */
{
gl_oset_iterator_t iter = gl_oset_iterator (set1);
const void *elt;
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == C);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == D);
ASSERT (gl_oset_iterator_next (&iter, &elt));
ASSERT (elt == B);
ASSERT (!gl_oset_iterator_next (&iter, &elt));
}
gl_oset_free (set1);
}