1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-24 02:03:54 +03:00

basic/ordered-set: add functions to operate on OrderedSets of strings

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-02-20 22:29:20 +01:00
parent 1b78172b5a
commit ef79eae09a
4 changed files with 69 additions and 0 deletions

View File

@ -45,3 +45,21 @@ int ordered_set_put_strdupv(OrderedSet *s, char **l) {
return n;
}
int ordered_set_put_string_set(OrderedSet *s, OrderedSet *l) {
int n = 0, r;
Iterator i;
char *p;
/* Like ordered_set_put_strv, but for an OrderedSet of strings */
ORDERED_SET_FOREACH(p, l, i) {
r = ordered_set_put_strdup(s, p);
if (r < 0)
return r;
n += r;
}
return n;
}

View File

@ -48,9 +48,14 @@ static inline void* ordered_set_steal_first(OrderedSet *s) {
return ordered_hashmap_steal_first((OrderedHashmap*) s);
}
static inline char **ordered_set_get_strv(OrderedSet *s) {
return internal_hashmap_get_strv(HASHMAP_BASE((OrderedHashmap*) s));
}
int ordered_set_consume(OrderedSet *s, void *p);
int ordered_set_put_strdup(OrderedSet *s, const char *p);
int ordered_set_put_strdupv(OrderedSet *s, char **l);
int ordered_set_put_string_set(OrderedSet *s, OrderedSet *l);
#define ORDERED_SET_FOREACH(e, s, i) \
for ((i) = ITERATOR_FIRST; ordered_set_iterate((s), &(i), (void**)&(e)); )

View File

@ -1,6 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "ordered-set.h"
#include "string-util.h"
#include "strv.h"
static void test_set_steal_first(void) {
_cleanup_ordered_set_free_ OrderedSet *m = NULL;
@ -49,6 +51,7 @@ static void test_set_free_with_hash_ops(void) {
static void test_set_put(void) {
_cleanup_ordered_set_free_ OrderedSet *m = NULL;
_cleanup_free_ char **t = NULL;
m = ordered_set_new(&string_hash_ops);
assert_se(m);
@ -61,12 +64,48 @@ static void test_set_put(void) {
assert_se(ordered_set_put(m, (void*) "333") == 1);
assert_se(ordered_set_put(m, (void*) "333") == 0);
assert_se(ordered_set_put(m, (void*) "22") == 0);
assert_se(t = ordered_set_get_strv(m));
assert_se(streq(t[0], "1"));
assert_se(streq(t[1], "22"));
assert_se(streq(t[2], "333"));
assert_se(!t[3]);
}
static void test_set_put_string_set(void) {
_cleanup_ordered_set_free_free_ OrderedSet *m = NULL;
_cleanup_ordered_set_free_ OrderedSet *q = NULL;
_cleanup_free_ char **final = NULL; /* "just free" because the strings are in the set */
void *t;
m = ordered_set_new(&string_hash_ops);
assert_se(m);
q = ordered_set_new(&string_hash_ops);
assert_se(q);
assert_se(t = strdup("1"));
assert_se(ordered_set_put(m, t) == 1);
assert_se(t = strdup("22"));
assert_se(ordered_set_put(m, t) == 1);
assert_se(t = strdup("333"));
assert_se(ordered_set_put(m, t) == 1);
assert_se(ordered_set_put(q, (void*) "11") == 1);
assert_se(ordered_set_put(q, (void*) "22") == 1);
assert_se(ordered_set_put(q, (void*) "33") == 1);
assert_se(ordered_set_put_string_set(m, q) == 2);
assert_se(final = ordered_set_get_strv(m));
assert_se(strv_equal(final, STRV_MAKE("1", "22", "333", "11", "33")));
}
int main(int argc, const char *argv[]) {
test_set_steal_first();
test_set_free_with_hash_ops();
test_set_put();
test_set_put_string_set();
return 0;
}

View File

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "set.h"
#include "strv.h"
static void test_set_steal_first(void) {
_cleanup_set_free_ Set *m = NULL;
@ -77,6 +78,12 @@ static void test_set_put(void) {
assert_se(set_put(m, (void*) "333") == 1);
assert_se(set_put(m, (void*) "333") == 0);
assert_se(set_put(m, (void*) "22") == 0);
_cleanup_free_ char **t = set_get_strv(m);
assert_se(strv_contains(t, "1"));
assert_se(strv_contains(t, "22"));
assert_se(strv_contains(t, "333"));
assert_se(strv_length(t) == 3);
}
int main(int argc, const char *argv[]) {