dovecot-2.2: lib: test-bits - fix nearest_power for 32-bit size_t

dovecot at dovecot.org dovecot at dovecot.org
Tue Jun 10 15:51:19 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/655c4b63035c
changeset: 17453:655c4b63035c
user:      Phil Carmody <phil at dovecot.fi>
date:      Tue Jun 10 17:49:45 2014 +0200
description:
lib: test-bits - fix nearest_power for 32-bit size_t
The test blindly went up to b=63, and the function correctly asserted.

Signed-off-by: Phil Carmody <phil at dovecot.fi>

diffstat:

 src/lib/test-bits.c |  12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)

diffs (29 lines):

diff -r c8efc101826b -r 655c4b63035c src/lib/test-bits.c
--- a/src/lib/test-bits.c	Tue Jun 10 16:40:46 2014 +0200
+++ b/src/lib/test-bits.c	Tue Jun 10 17:49:45 2014 +0200
@@ -20,16 +20,22 @@
 static void test_nearest_power(void) 
 {
 	unsigned int b;
+	size_t num;
 	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;
+	for (b = 2; b < CHAR_BIT*sizeof(size_t) - 1; ++b) {
+		/* b=2 tests 3,4,5; b=3 tests 7,8,9; ... b=30 tests ~1G */
+		num = (size_t)1 << 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);
 	}
+	/* With 32-bit size_t, now: b=31 tests 2G-1, 2G, not 2G+1. */
+	num = (size_t)1 << b;
+	test_assert_idx(nearest_power(num-1) == num,    b);
+	test_assert_idx(nearest_power(num  ) == num,    b);
+	/* i_assert()s: test_assert_idx(nearest_power(num+1) == num<<1, b); */
 	test_end();
 }
 


More information about the dovecot-cvs mailing list