Add bitfield helper functions from libdevdev-util.h and some tests

Signed-off-by: Stephen Chandler Paul <thatslyude@gmail.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
This commit is contained in:
Stephen Chandler Paul 2014-05-21 21:52:57 -04:00
parent 6fa0c9aa97
commit 812ea542e7
2 changed files with 61 additions and 0 deletions

View file

@ -84,4 +84,26 @@ zalloc(size_t size)
return calloc(1, size);
}
/* This bitfield helper implementation is taken from from libevdev-util.h,
* except that it has been modified to work with arrays of unsigned chars
*/
static inline int
bit_is_set(const unsigned char *array, int bit)
{
return !!(array[bit / 8] & (1 << (bit % 8)));
}
static inline void
set_bit(unsigned char *array, int bit)
{
array[bit / 8] |= (1 << (bit % 8));
}
static inline void
clear_bit(unsigned char *array, int bit)
{
array[bit / 8] &= ~(1 << (bit % 8));
}
#endif /* LIBINPUT_UTIL_H */

View file

@ -29,6 +29,7 @@
#include <unistd.h>
#include "litest.h"
#include "libinput-util.h"
static int open_restricted(const char *path, int flags, void *data)
{
@ -371,12 +372,50 @@ START_TEST(event_conversion_touch)
}
END_TEST
START_TEST(bitfield_helpers)
{
/* This value has a bit set on all of the word boundaries we want to
* test: 0, 1, 7, 8, 31, 32, and 33
*/
unsigned char read_bitfield[] = { 0x83, 0x1, 0x0, 0x80, 0x3 };
unsigned char write_bitfield[ARRAY_LENGTH(read_bitfield)];
size_t i;
/* Now check that the bitfield we wrote to came out to be the same as
* the bitfield we were writing from */
for (i = 0; i < ARRAY_LENGTH(read_bitfield) * 8; i++) {
switch (i) {
case 0:
case 1:
case 7:
case 8:
case 31:
case 32:
case 33:
ck_assert(bit_is_set(read_bitfield, i));
set_bit(write_bitfield, i);
break;
default:
ck_assert(!bit_is_set(read_bitfield, i));
clear_bit(write_bitfield, i);
break;
}
}
ck_assert_int_eq(memcmp(read_bitfield,
write_bitfield,
sizeof(read_bitfield)),
0);
}
END_TEST
int main (int argc, char **argv) {
litest_add_no_device("events:conversion", event_conversion_device_notify);
litest_add_no_device("events:conversion", event_conversion_pointer);
litest_add_no_device("events:conversion", event_conversion_pointer_abs);
litest_add_no_device("events:conversion", event_conversion_key);
litest_add_no_device("events:conversion", event_conversion_touch);
litest_add_no_device("bitfield_helpers", bitfield_helpers);
return litest_run(argc, argv);
}