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