708deb9
From 647f68249f90855814de6eb6b0959c6096b41cae Mon Sep 17 00:00:00 2001
708deb9
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
708deb9
Date: Wed, 1 Oct 2014 09:32:16 -0400
708deb9
Subject: [PATCH] tests: add tests for {hashmap,set}_steal_first
708deb9
708deb9
Just to make sure that coverity is wrong.
708deb9
---
708deb9
 Makefile.am             |  7 +++++++
708deb9
 src/test/test-hashmap.c | 21 +++++++++++++++++++++
708deb9
 src/test/test-set.c     | 47 +++++++++++++++++++++++++++++++++++++++++++++++
708deb9
 3 files changed, 75 insertions(+)
708deb9
 create mode 100644 src/test/test-set.c
708deb9
708deb9
diff --git a/Makefile.am b/Makefile.am
708deb9
index 7bb7f75915..9e087bd9fb 100644
708deb9
--- a/Makefile.am
708deb9
+++ b/Makefile.am
708deb9
@@ -1341,6 +1341,7 @@ tests += \
708deb9
 	test-fileio \
708deb9
 	test-time \
708deb9
 	test-hashmap \
708deb9
+	test-set \
708deb9
 	test-list \
708deb9
 	test-tables \
708deb9
 	test-device-nodes \
708deb9
@@ -1572,6 +1573,12 @@ test_hashmap_SOURCES = \
708deb9
 test_hashmap_LDADD = \
708deb9
 	libsystemd-core.la
708deb9
 
708deb9
+test_set_SOURCES = \
708deb9
+	src/test/test-set.c
708deb9
+
708deb9
+test_set_LDADD = \
708deb9
+	libsystemd-core.la
708deb9
+
708deb9
 test_xml_SOURCES = \
708deb9
 	src/test/test-xml.c
708deb9
 
708deb9
diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c
708deb9
index d9863f8dab..f4afbb8e9d 100644
708deb9
--- a/src/test/test-hashmap.c
708deb9
+++ b/src/test/test-hashmap.c
708deb9
@@ -507,6 +507,26 @@ static void test_hashmap_steal_first_key(void) {
708deb9
         assert_se(hashmap_isempty(m));
708deb9
 }
708deb9
 
708deb9
+static void test_hashmap_steal_first(void) {
708deb9
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
708deb9
+        int seen[3] = {};
708deb9
+        char *val;
708deb9
+
708deb9
+        m = hashmap_new(&string_hash_ops);
708deb9
+        assert_se(m);
708deb9
+
708deb9
+        assert_se(hashmap_put(m, "key 1", (void*) "1") == 1);
708deb9
+        assert_se(hashmap_put(m, "key 2", (void*) "22") == 1);
708deb9
+        assert_se(hashmap_put(m, "key 3", (void*) "333") == 1);
708deb9
+
708deb9
+        while ((val = hashmap_steal_first(m)))
708deb9
+                seen[strlen(val) - 1]++;
708deb9
+
708deb9
+        assert(seen[0] == 1 && seen[1] == 1 && seen[2] == 1);
708deb9
+
708deb9
+        assert_se(hashmap_isempty(m));
708deb9
+}
708deb9
+
708deb9
 static void test_hashmap_clear_free_free(void) {
708deb9
         _cleanup_hashmap_free_ Hashmap *m = NULL;
708deb9
 
708deb9
@@ -560,6 +580,7 @@ int main(int argc, const char *argv[]) {
708deb9
         test_hashmap_many();
708deb9
         test_hashmap_first_key();
708deb9
         test_hashmap_steal_first_key();
708deb9
+        test_hashmap_steal_first();
708deb9
         test_hashmap_clear_free_free();
708deb9
         test_uint64_compare_func();
708deb9
         test_trivial_compare_func();
708deb9
diff --git a/src/test/test-set.c b/src/test/test-set.c
708deb9
new file mode 100644
708deb9
index 0000000000..060dba42df
708deb9
--- /dev/null
708deb9
+++ b/src/test/test-set.c
708deb9
@@ -0,0 +1,47 @@
708deb9
+/***
708deb9
+  This file is part of systemd
708deb9
+
708deb9
+  Copyright 2014 Zbigniew Jędrzejewski-Szmek
708deb9
+
708deb9
+  systemd is free software; you can redistribute it and/or modify it
708deb9
+  under the terms of the GNU Lesser General Public License as published by
708deb9
+  the Free Software Foundation; either version 2.1 of the License, or
708deb9
+  (at your option) any later version.
708deb9
+
708deb9
+  systemd is distributed in the hope that it will be useful, but
708deb9
+  WITHOUT ANY WARRANTY; without even the implied warranty of
708deb9
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
708deb9
+  Lesser General Public License for more details.
708deb9
+
708deb9
+  You should have received a copy of the GNU Lesser General Public License
708deb9
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
708deb9
+***/
708deb9
+
708deb9
+#include "util.h"
708deb9
+#include "set.h"
708deb9
+
708deb9
+static void test_set_steal_first(void) {
708deb9
+        _cleanup_set_free_ Set *m = NULL;
708deb9
+        int seen[3] = {};
708deb9
+        char *val;
708deb9
+
708deb9
+        m = set_new(&string_hash_ops);
708deb9
+        assert_se(m);
708deb9
+
708deb9
+        assert_se(set_put(m, (void*) "1") == 1);
708deb9
+        assert_se(set_put(m, (void*) "22") == 1);
708deb9
+        assert_se(set_put(m, (void*) "333") == 1);
708deb9
+
708deb9
+        while ((val = set_steal_first(m)))
708deb9
+                seen[strlen(val) - 1]++;
708deb9
+
708deb9
+        assert(seen[0] == 1 && seen[1] == 1 && seen[2] == 1);
708deb9
+
708deb9
+        assert_se(set_isempty(m));
708deb9
+}
708deb9
+
708deb9
+int main(int argc, const char *argv[]) {
708deb9
+        test_set_steal_first();
708deb9
+
708deb9
+        return 0;
708deb9
+}