dovecot-2.2: lib: unit tests for lib/bits
dovecot at dovecot.org
dovecot at dovecot.org
Mon Jun 9 20:04:14 UTC 2014
details: http://hg.dovecot.org/dovecot-2.2/rev/bc4f09a5cb11
changeset: 17449:bc4f09a5cb11
user: Phil Carmody <phil at dovecot.fi>
date: Mon Jun 09 23:02:52 2014 +0300
description:
lib: unit tests for lib/bits
Signed-off-by: Phil Carmody <phil at dovecot.fi>
diffstat:
src/lib/Makefile.am | 1 +
src/lib/test-bits.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
src/lib/test-lib.c | 1 +
src/lib/test-lib.h | 1 +
4 files changed, 64 insertions(+), 0 deletions(-)
diffs (98 lines):
diff -r ed9e50227250 -r bc4f09a5cb11 src/lib/Makefile.am
--- a/src/lib/Makefile.am Mon Jun 09 23:02:52 2014 +0300
+++ b/src/lib/Makefile.am Mon Jun 09 23:02:52 2014 +0300
@@ -273,6 +273,7 @@
test-array.c \
test-aqueue.c \
test-base64.c \
+ test-bits.c \
test-bsearch-insert-pos.c \
test-buffer.c \
test-crc32.c \
diff -r ed9e50227250 -r bc4f09a5cb11 src/lib/test-bits.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-bits.c Mon Jun 09 23:02:52 2014 +0300
@@ -0,0 +1,61 @@
+/* Copyright (c) 2001-2014 Dovecot authors, see the included COPYING file */
+
+/* Unit tests for bit twiddles library */
+
+#include "test-lib.h"
+
+#include <stdio.h>
+
+/* nearest_power(0) = error bits_requiredXX(0) = 0
+ nearest_power(1) = 1 = 1<<0 bits_requiredXX(1) = 1
+ nearest_power(2) = 2 = 1<<1 bits_requiredXX(2) = 2
+ nearest_power(3) = 4 = 1<<2 bits_requiredXX(3) = 2
+ nearest_power(4) = 4 = 1<<2 bits_requiredXX(4) = 3
+ nearest_power(5) = 8 = 1<<3 bits_requiredXX(5) = 3
+ nearest_power(7) = 8 = 1<<3 bits_requiredXX(7) = 3
+ nearest_power(8) = 8 = 1<<3 bits_requiredXX(8) = 4
+*/
+
+/* nearest_power(num) == 1ULL << bits_required64(num-1) */
+static void test_nearest_power(void)
+{
+ unsigned int b;
+ test_begin("nearest_power()");
+ test_assert(nearest_power(1)==1);
+ test_assert(nearest_power(2)==2);
+ for (b = 2; b < 63; ++b) {
+ /* b=2 tests 3,4,5; b=3 tests 7,8,9; ... */
+ uint64_t num = 1ULL << b;
+ test_assert_idx(nearest_power(num-1) == num, b);
+ test_assert_idx(nearest_power(num ) == num, b);
+ test_assert_idx(nearest_power(num+1) == num<<1, b);
+ }
+ test_end();
+}
+
+static void test_bits_requiredXX(void)
+{
+ /* As ..64 depends on ..32 and tests it twice,
+ * and ..32 depends on ..16 and tests it twice,
+ * etc., we only test ..64
+ */
+ unsigned int b;
+ test_begin("bits_requiredXX()");
+ test_assert(bits_required64(0) == 0);
+ test_assert(bits_required64(1) == 1);
+ test_assert(bits_required64(2) == 2);
+ for (b = 2; b < 64; ++b) {
+ /* b=2 tests 3,4,5; b=3 tests 7,8,9; ... */
+ uint64_t num = 1ULL << b;
+ test_assert_idx(bits_required64(num-1) == b, b);
+ test_assert_idx(bits_required64(num ) == b+1, b);
+ test_assert_idx(bits_required64(num+1) == b+1, b);
+ }
+ test_end();
+}
+
+void test_bits()
+{
+ test_nearest_power();
+ test_bits_requiredXX();
+}
diff -r ed9e50227250 -r bc4f09a5cb11 src/lib/test-lib.c
--- a/src/lib/test-lib.c Mon Jun 09 23:02:52 2014 +0300
+++ b/src/lib/test-lib.c Mon Jun 09 23:02:52 2014 +0300
@@ -8,6 +8,7 @@
test_aqueue,
test_array,
test_base64,
+ test_bits,
test_bsearch_insert_pos,
test_buffer,
test_crc32,
diff -r ed9e50227250 -r bc4f09a5cb11 src/lib/test-lib.h
--- a/src/lib/test-lib.h Mon Jun 09 23:02:52 2014 +0300
+++ b/src/lib/test-lib.h Mon Jun 09 23:02:52 2014 +0300
@@ -7,6 +7,7 @@
void test_aqueue(void);
void test_array(void);
void test_base64(void);
+void test_bits(void);
void test_bsearch_insert_pos(void);
void test_buffer(void);
void test_crc32(void);
More information about the dovecot-cvs
mailing list