diff options
author | Adrian Moreno <amorenoz@redhat.com> | 2022-03-23 12:56:17 +0100 |
---|---|---|
committer | Ilya Maximets <i.maximets@ovn.org> | 2022-03-30 16:59:02 +0200 |
commit | 9e56549c2bba79e644de2d3876b363553175210c (patch) | |
tree | bf0bf39f86c3e63eda7171f727a6ca3dcc69922b /tests | |
parent | 860e69a8c3d6994dc000b37d682bd16cd2925bef (diff) | |
download | openvswitch-9e56549c2bba79e644de2d3876b363553175210c.tar.gz |
hmap: use short version of safe loops if possible.
Using SHORT version of the *_SAFE loops makes the code cleaner and less
error prone. So, use the SHORT version and remove the extra variable
when possible for hmap and all its derived types.
In order to be able to use both long and short versions without changing
the name of the macro for all the clients, overload the existing name
and select the appropriate version depending on the number of arguments.
Acked-by: Dumitru Ceara <dceara@redhat.com>
Acked-by: Eelco Chaudron <echaudro@redhat.com>
Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-cmap.c | 4 | ||||
-rw-r--r-- | tests/test-hmap.c | 32 |
2 files changed, 34 insertions, 2 deletions
diff --git a/tests/test-cmap.c b/tests/test-cmap.c index 070547560..bc1f45642 100644 --- a/tests/test-cmap.c +++ b/tests/test-cmap.c @@ -584,7 +584,7 @@ benchmark_hmap(void) { struct helement *elements; struct hmap hmap; - struct helement *e, *next; + struct helement *e; struct timeval start; pthread_t *threads; struct hmap_aux aux; @@ -622,7 +622,7 @@ benchmark_hmap(void) /* Destruction. */ xgettimeofday(&start); - HMAP_FOR_EACH_SAFE (e, next, node, &hmap) { + HMAP_FOR_EACH_SAFE (e, node, &hmap) { hmap_remove(&hmap, &e->node); } hmap_destroy(&hmap); diff --git a/tests/test-hmap.c b/tests/test-hmap.c index 47b475538..e50c7c380 100644 --- a/tests/test-hmap.c +++ b/tests/test-hmap.c @@ -275,6 +275,38 @@ test_hmap_for_each_safe(hash_func *hash) } } assert(n == n_remaining); + hmap_destroy(&hmap); + + /* Test short version (without next variable). */ + make_hmap(&hmap, elements, values, n, hash); + + i = 0; + n_remaining = n; + HMAP_FOR_EACH_SAFE (e, node, &hmap) { + assert(i < n); + if (pattern & (1ul << e->value)) { + size_t j; + hmap_remove(&hmap, &e->node); + for (j = 0; ; j++) { + assert(j < n_remaining); + if (values[j] == e->value) { + values[j] = values[--n_remaining]; + break; + } + } + } + check_hmap(&hmap, values, n_remaining, hash); + i++; + } + assert(i == n); + assert(e == NULL); + + for (i = 0; i < n; i++) { + if (pattern & (1ul << i)) { + n_remaining++; + } + } + assert(n == n_remaining); hmap_destroy(&hmap); } |