aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-04-08 21:30:14 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-04-08 21:30:14 +0100
commit3789f106c11e9314ef29708d8156397b43387958 (patch)
tree97fdaadcd64ce0132045c78c3f770b29f658e981
parent430a991cd53273c2869e9b37e42438f4e827d1d0 (diff)
downloadrspamd-3789f106c11e9314ef29708d8156397b43387958.tar.gz
rspamd-3789f106c11e9314ef29708d8156397b43387958.zip
[Feature] Add unit tests for heap
-rw-r--r--test/CMakeLists.txt1
-rw-r--r--test/rspamd_heap_test.c91
-rw-r--r--test/rspamd_test_suite.c1
-rw-r--r--test/tests.h6
4 files changed, 97 insertions, 2 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index fccdef5e8..c613d0cfd 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -11,6 +11,7 @@ SET(TESTSRC rspamd_mem_pool_test.c
rspamd_http_test.c
rspamd_lua_test.c
rspamd_cryptobox_test.c
+ rspamd_heap_test.c
rspamd_test_suite.c)
ADD_EXECUTABLE(rspamd-test EXCLUDE_FROM_ALL ${TESTSRC})
diff --git a/test/rspamd_heap_test.c b/test/rspamd_heap_test.c
new file mode 100644
index 000000000..db508fcb7
--- /dev/null
+++ b/test/rspamd_heap_test.c
@@ -0,0 +1,91 @@
+/*-
+ * Copyright 2016 Vsevolod Stakhov
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+#include "rspamd.h"
+#include "heap.h"
+#include "ottery.h"
+
+static inline
+struct rspamd_min_heap_elt *
+new_elt (guint pri)
+{
+ struct rspamd_min_heap_elt *elt;
+
+ elt = g_slice_alloc0 (sizeof (*elt));
+ elt->pri = pri;
+
+ return elt;
+}
+
+void
+rspamd_heap_test_func (void)
+{
+ struct rspamd_min_heap *heap;
+ struct rspamd_min_heap_elt *elt;
+ gint i;
+ guint prev;
+
+ heap = rspamd_min_heap_create (32);
+
+ for (i = 100; i >= 0; i --) {
+ elt = new_elt (i);
+ rspamd_min_heap_push (heap, elt);
+ }
+
+ for (i = 0; i <= 100; i ++) {
+ elt = rspamd_min_heap_pop (heap);
+ g_assert (elt->pri == i);
+ }
+
+ rspamd_min_heap_destroy (heap);
+ heap = rspamd_min_heap_create (128);
+
+ for (i = 0; i < 100500; i ++) {
+ elt = new_elt (ottery_rand_uint32 () % G_MAXINT32 + 1);
+ rspamd_min_heap_push (heap, elt);
+ }
+
+ prev = 0;
+
+ for (i = 0; i < 100500; i ++) {
+ elt = rspamd_min_heap_pop (heap);
+
+ if (prev != 0) {
+ g_assert (elt->pri >= prev);
+ }
+
+ prev = elt->pri;
+ }
+
+ rspamd_min_heap_destroy (heap);
+
+ heap = rspamd_min_heap_create (32);
+
+ elt = new_elt (2);
+ elt->data = GINT_TO_POINTER (1);
+ rspamd_min_heap_push (heap, elt);
+ elt = new_elt (3);
+ elt->data = GINT_TO_POINTER (2);
+ rspamd_min_heap_push (heap, elt);
+ elt = new_elt (4);
+ elt->data = GINT_TO_POINTER (3);
+ rspamd_min_heap_push (heap, elt);
+
+ rspamd_min_heap_update_elt (heap, elt, 0);
+ elt = rspamd_min_heap_pop (heap);
+ g_assert (elt->idx == GINT_TO_POINTER (3));
+}
diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c
index 9ff50c626..8c6e2c31a 100644
--- a/test/rspamd_test_suite.c
+++ b/test/rspamd_test_suite.c
@@ -54,6 +54,7 @@ main (int argc, char **argv)
g_test_add_func ("/rspamd/lua", rspamd_lua_test_func);
g_test_add_func ("/rspamd/crypto", rspamd_cryptobox_test_func);
g_test_add_func ("/rspamd/cryptobox", rspamd_cryptobox_test_func);
+ g_test_add_func ("/rspamd/heap", rspamd_heap_test_func);
g_test_run ();
diff --git a/test/tests.h b/test/tests.h
index a2ba05b84..0ba03da8d 100644
--- a/test/tests.h
+++ b/test/tests.h
@@ -1,8 +1,8 @@
#ifndef RSPAMD_TESTS_H
#define RSPAMD_TESTS_H
-/*
- * Here are described test functions for rspamd test suite
+/*
+ * Here are described test functions for rspamd test suite
*/
/* URL parser test */
@@ -39,4 +39,6 @@ void rspamd_lua_test_func (void);
void rspamd_cryptobox_test_func (void);
+void rspamd_heap_test_func (void);
+
#endif