[PATCH libevdev 1/2] Add queue_slice to remove elements from the internal event queue

Peter Hutterer peter.hutterer at who-t.net
Mon Feb 24 20:26:08 PST 2014


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 libevdev/libevdev-int.h |  18 +++++
 test/test-int-queue.c   | 183 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 201 insertions(+)

diff --git a/libevdev/libevdev-int.h b/libevdev/libevdev-int.h
index 847fe56..44c5f4a 100644
--- a/libevdev/libevdev-int.h
+++ b/libevdev/libevdev-int.h
@@ -279,5 +279,23 @@ queue_set_num_elements(struct libevdev *dev, size_t nelem)
 
 	return 0;
 }
+
+static inline int
+queue_slice(struct libevdev *dev, size_t from, size_t nelem)
+{
+	size_t i;
+
+	if (from >= dev->queue_size)
+		return 1;
+
+	if (from + nelem > dev->queue_size)
+		nelem = dev->queue_size - from;
+
+	for (i = 0; i < dev->queue_size - nelem - from; i++)
+		dev->queue[from + i] = dev->queue[from + nelem + i];
+
+	queue_set_num_elements(dev, dev->queue_size - nelem);
+	return 0;
+}
 #endif
 
diff --git a/test/test-int-queue.c b/test/test-int-queue.c
index d45fee0..04b9fd3 100644
--- a/test/test-int-queue.c
+++ b/test/test-int-queue.c
@@ -329,6 +329,180 @@ START_TEST(test_queue_set_num_elements)
 }
 END_TEST
 
+static void
+test_queue_setup_simple(struct libevdev *dev, size_t size)
+{
+	int i;
+
+	queue_alloc(dev, size);
+
+	for (i = 0; i < size; i++) {
+		struct input_event *e;
+		e = queue_push(dev);
+
+		e->type = EV_REL;
+		e->code = REL_X;
+		e->value = i;
+	}
+
+	ck_assert_int_eq(queue_num_elements(dev), size);
+}
+
+START_TEST(test_queue_slice_front)
+{
+	struct libevdev dev = {0};
+	int rc;
+	size_t i, remaining;
+	unsigned int elems = 0;
+	size_t queue_size = 32;
+	size_t slice_size = 9;
+
+	test_queue_setup_simple(&dev, queue_size);
+	rc = queue_slice(&dev, 0, slice_size);
+	ck_assert_int_eq(rc, 0);
+
+	remaining = queue_num_elements(&dev);
+	ck_assert_int_eq(remaining, queue_size - slice_size);
+
+	for (i = 0; i < remaining; i++) {
+		struct input_event e;
+		queue_shift(&dev, &e);
+		elems |= 1 << e.value;
+	}
+
+	ck_assert_int_eq(elems, 0xFFFFFe00);
+}
+END_TEST
+
+START_TEST(test_queue_slice_end)
+{
+	struct libevdev dev = {0};
+	int rc;
+	size_t i, remaining;
+	unsigned int elems = 0;
+	size_t queue_size = 32;
+	size_t slice_size = 8;
+
+	test_queue_setup_simple(&dev, queue_size);
+	rc = queue_slice(&dev, queue_size - slice_size, slice_size);
+	ck_assert_int_eq(rc, 0);
+
+	remaining = queue_num_elements(&dev);
+	ck_assert_int_eq(remaining, queue_size - slice_size);
+
+	for (i = 0; i < remaining; i++) {
+		struct input_event e;
+		queue_shift(&dev, &e);
+		elems |= 1 << e.value;
+	}
+
+	ck_assert_int_eq(elems, 0x00FFFFFF);
+}
+END_TEST
+
+START_TEST(test_queue_slice_middle)
+{
+	struct libevdev dev = {0};
+	int rc;
+	size_t i, remaining;
+	unsigned int elems = 0;
+	size_t queue_size = 32;
+	size_t slice_size = 3;
+
+	test_queue_setup_simple(&dev, queue_size);
+
+	rc = queue_slice(&dev, 8, slice_size);
+	ck_assert_int_eq(rc, 0);
+
+	remaining = queue_num_elements(&dev);
+	ck_assert_int_eq(remaining, queue_size - slice_size);
+
+	for (i = 0; i < remaining; i++) {
+		struct input_event e;
+		queue_shift(&dev, &e);
+		elems |= 1 << e.value;
+	}
+
+	ck_assert_int_eq(elems, 0xFFFFF8FF);
+}
+END_TEST
+
+START_TEST(test_queue_slice_front_invalid)
+{
+	struct libevdev dev = {0};
+	int rc;
+	size_t i, remaining;
+	unsigned int elems = 0;
+	size_t queue_size = 32;
+	size_t slice_size = 3;
+
+	test_queue_setup_simple(&dev, queue_size);
+
+	rc = queue_slice(&dev, -1, slice_size);
+	ck_assert_int_eq(rc, 1);
+
+	remaining = queue_num_elements(&dev);
+	ck_assert_int_eq(remaining, queue_size);
+
+	for (i = 0; i < remaining; i++) {
+		struct input_event e;
+		queue_shift(&dev, &e);
+		elems |= 1 << e.value;
+	}
+
+	ck_assert_int_eq(elems, 0xFFFFFFFF);
+}
+END_TEST
+
+START_TEST(test_queue_slice_end_invalid)
+{
+	struct libevdev dev = {0};
+	int rc;
+	size_t i, remaining;
+	unsigned int elems = 0;
+	size_t queue_size = 32;
+	size_t slice_size = 30;
+
+	test_queue_setup_simple(&dev, queue_size);
+
+	rc = queue_slice(&dev, 24, slice_size);
+	ck_assert_int_eq(rc, 0);
+
+	remaining = queue_num_elements(&dev);
+	ck_assert_int_eq(remaining, 24);
+
+	for (i = 0; i < remaining; i++) {
+		struct input_event e;
+		queue_shift(&dev, &e);
+		elems |= 1 << e.value;
+	}
+
+	ck_assert_int_eq(elems, 0x00FFFFFF);
+}
+END_TEST
+
+START_TEST(test_queue_slice_all)
+{
+	struct libevdev dev = {0};
+	int rc;
+	size_t remaining;
+	size_t queue_size = 32;
+	size_t slice_size = 32;
+	struct input_event e;
+
+	test_queue_setup_simple(&dev, queue_size);
+
+	rc = queue_slice(&dev, 0, slice_size);
+	ck_assert_int_eq(rc, 0);
+
+	remaining = queue_num_elements(&dev);
+	ck_assert_int_eq(remaining, 0);
+
+	rc = queue_shift(&dev, &e);
+	ck_assert_int_eq(rc, 1);
+}
+END_TEST
+
 Suite *
 queue_suite(void)
 {
@@ -355,6 +529,15 @@ queue_suite(void)
 	tcase_add_test(tc, test_queue_set_num_elements);
 	suite_add_tcase(s, tc);
 
+	tc = tcase_create("Queue slice");
+	tcase_add_test(tc, test_queue_slice_front);
+	tcase_add_test(tc, test_queue_slice_end);
+	tcase_add_test(tc, test_queue_slice_middle);
+	tcase_add_test(tc, test_queue_slice_front_invalid);
+	tcase_add_test(tc, test_queue_slice_end_invalid);
+	tcase_add_test(tc, test_queue_slice_all);
+	suite_add_tcase(s, tc);
+
 	return s;
 }
 
-- 
1.8.4.2



More information about the Input-tools mailing list