[PATCH v2] include: let BitIsOn() return a boolean value.

Julien Cristau jcristau at debian.org
Thu Nov 25 06:08:53 PST 2010


On Thu, Nov 25, 2010 at 08:24:25 +1000, Peter Hutterer wrote:

> Simply returning the mask bit breaks checks like
>     BitIsOn(mask, 0) != BitIsOn(mask, 1);
> as used in 048e93593e3f7a99a7d2a219e1ce2bdc9d407807.
> 
> The naming of this macro suggests that it should return boolean values
> anyway. This patch also adds a few simple tests for these macros to make
> sure they don't accidentally break in the future.
> 
> Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
> ---
> Changes to v1;
> - add another set of parens. they're cheap, we can afford it.
> 
>  include/inputstr.h |    2 +-
>  test/input.c       |   16 ++++++++++++++++
>  2 files changed, 17 insertions(+), 1 deletions(-)
> 
> diff --git a/include/inputstr.h b/include/inputstr.h
> index d4c253e..44de9c4 100644
> --- a/include/inputstr.h
> +++ b/include/inputstr.h
> @@ -57,7 +57,7 @@ SOFTWARE.
>  #include "geext.h"
>  #include "privates.h"
>  
> -#define BitIsOn(ptr, bit) (((BYTE *) (ptr))[(bit)>>3] & (1 << ((bit) & 7)))
> +#define BitIsOn(ptr, bit) (!!(((BYTE *) (ptr))[(bit)>>3] & (1 << ((bit) & 7))))
>  #define SetBit(ptr, bit)  (((BYTE *) (ptr))[(bit)>>3] |= (1 << ((bit) & 7)))
>  #define ClearBit(ptr, bit) (((BYTE *)(ptr))[(bit)>>3] &= ~(1 << ((bit) & 7)))
>  extern _X_EXPORT int CountBits(const uint8_t *mask, int len);
> diff --git a/test/input.c b/test/input.c
> index 4ccfaff..1fe228c 100644
> --- a/test/input.c
> +++ b/test/input.c
> @@ -1050,7 +1050,22 @@ static void dix_valuator_mode(void)
>          g_assert(valuator_get_mode(&dev, i) == Relative);
>  }
>  
> +static void include_bit_test_macros(void)
> +{
> +    uint8_t mask[9] = { 0 };
> +    int i;
>  
> +    for (i = 0; i < sizeof(mask)/sizeof(mask[0]); i++)

I was going to suggest using ARRAY_SIZE, but that's only in
glx/glxdricommon.c, so...

> +    {
> +        g_assert(BitIsOn(mask, i) == 0);
> +        SetBit(mask, i);
> +        g_assert(BitIsOn(mask, i) == 1);
> +        g_assert(!!(mask[i/8] & (1 << (i % 8))));
> +        g_assert(CountBits(mask, sizeof(mask)) == 1);
> +        ClearBit(mask, i);
> +        g_assert(BitIsOn(mask, i) == 0);
> +    }
> +}
>  
>  int main(int argc, char** argv)
>  {
> @@ -1066,6 +1081,7 @@ int main(int argc, char** argv)
>      g_test_add_func("/dix/input/grab_matching", dix_grab_matching);
>      g_test_add_func("/dix/input/valuator_mode", dix_valuator_mode);
>      g_test_add_func("/include/byte_padding_macros", include_byte_padding_macros);
> +    g_test_add_func("/include/bit_test_macros", include_bit_test_macros);
>      g_test_add_func("/Xi/xiproperty/register-unregister", xi_unregister_handlers);
>  
Reviewed-by: Julien Cristau <jcristau at debian.org>

Cheers,
Julien


More information about the xorg-devel mailing list