]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add unit tests for heap
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 8 Apr 2016 20:30:14 +0000 (21:30 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 8 Apr 2016 20:30:14 +0000 (21:30 +0100)
test/CMakeLists.txt
test/rspamd_heap_test.c [new file with mode: 0644]
test/rspamd_test_suite.c
test/tests.h

index fccdef5e8fa174f474eae9370a775b4503b399bd..c613d0cfd1583948309ede9a219c68dbd3002ccd 100644 (file)
@@ -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 (file)
index 0000000..db508fc
--- /dev/null
@@ -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));
+}
index 9ff50c6263a2c65ca8411a106389352e6a4b2236..8c6e2c31a96abc980d7162e2be30be40d602bb9a 100644 (file)
@@ -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 ();
 
index a2ba05b8415d15c9898a90efe57491957fb650d1..0ba03da8d290817f14cee799340e71ee26fd19e0 100644 (file)
@@ -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