From 935385c85df5cbc512bf10c268ba945d3905a64a Mon Sep 17 00:00:00 2001 From: Ted Ross Date: Wed, 9 Oct 2013 02:01:06 +0000 Subject: QPID-5216 - Fixed a couple of bugs in the bitmask module. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1530481 13f79535-47bb-0310-9956-ffa450edef68 --- qpid/extras/dispatch/src/bitmask.c | 6 +++--- qpid/extras/dispatch/tests/tool_test.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) (limited to 'qpid/extras/dispatch') diff --git a/qpid/extras/dispatch/src/bitmask.c b/qpid/extras/dispatch/src/bitmask.c index 5341f8d83a..88ba69dde1 100644 --- a/qpid/extras/dispatch/src/bitmask.c +++ b/qpid/extras/dispatch/src/bitmask.c @@ -33,7 +33,7 @@ ALLOC_DECLARE(dx_bitmask_t); ALLOC_DEFINE(dx_bitmask_t); #define MASK_INDEX(num) (num / 64) -#define MASK_ONEHOT(num) (1 << (num % 64)) +#define MASK_ONEHOT(num) (((uint64_t) 1) << (num % 64)) #define FIRST_NONE -1 #define FIRST_UNKNOWN -2 @@ -81,7 +81,7 @@ void dx_bitmask_set_bit(dx_bitmask_t *b, int bitnum) { assert(bitnum < DX_BITMASK_BITS); b->array[MASK_INDEX(bitnum)] |= MASK_ONEHOT(bitnum); - if (b->first_set > bitnum) + if (b->first_set > bitnum || b->first_set < 0) b->first_set = bitnum; } @@ -108,7 +108,7 @@ int dx_bitmask_first_set(dx_bitmask_t *b, int *bitnum) for (int i = 0; i < DX_BITMASK_LONGS; i++) if (b->array[i]) { for (int j = 0; j < 64; j++) - if ((1 << j) & b->array[i]) { + if ((((uint64_t) 1) << j) & b->array[i]) { b->first_set = i * 64 + j; break; } diff --git a/qpid/extras/dispatch/tests/tool_test.c b/qpid/extras/dispatch/tests/tool_test.c index ba047d928c..2c90714b3d 100644 --- a/qpid/extras/dispatch/tests/tool_test.c +++ b/qpid/extras/dispatch/tests/tool_test.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include "alloc_private.h" typedef struct item_t { DEQ_LINKS(struct item_t); @@ -188,12 +190,43 @@ static char* test_deq_basic2(void *context) } +static char* test_bitmask(void *context) +{ + dx_bitmask_t *bm; + int num; + + bm = dx_bitmask(0); + if (!bm) return "Can't allocate a bit mask"; + if (dx_bitmask_first_set(bm, &num)) return "Expected no first set bit"; + + dx_bitmask_set_bit(bm, 3); + dx_bitmask_set_bit(bm, 500); + + if (!dx_bitmask_first_set(bm, &num)) return "Expected first set bit"; + if (num != 3) return "Expected first set bit to be 3"; + + dx_bitmask_clear_bit(bm, num); + + if (!dx_bitmask_first_set(bm, &num)) return "Expected first set bit (2)"; + if (num != 500) return "Expected first set bit to be 500"; + + dx_bitmask_clear_bit(bm, num); + if (dx_bitmask_first_set(bm, &num)) return "Expected no first set bit (2)"; + + dx_bitmask_free(bm); + + return 0; +} + + int tool_tests(void) { int result = 0; + dx_alloc_initialize(); TEST_CASE(test_deq_basic, 0); TEST_CASE(test_deq_basic2, 0); + TEST_CASE(test_bitmask, 0); return result; } -- cgit v1.2.1