util: add the range handling as separate header file
authorPeter Hutterer <peter.hutterer@who-t.net>
Mon, 14 Oct 2024 04:39:07 +0000 (14:39 +1000)
committerMarge Bot <emma+marge@anholt.net>
Wed, 30 Oct 2024 23:20:42 +0000 (23:20 +0000)
Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1067>

src/libinput-util.h
src/util-range.h [new file with mode: 0644]
test/litest.h
test/test-utils.c

index 1f9863c6407a81ba4356d75a2d5c27f4c031c5f4..0ca522ce6633233fcacc03fb02e7672a621cb594 100644 (file)
@@ -39,6 +39,7 @@
 #include "util-matrix.h"
 #include "util-strings.h"
 #include "util-ratelimit.h"
+#include "util-range.h"
 #include "util-prop-parsers.h"
 #include "util-time.h"
 
diff --git a/src/util-range.h b/src/util-range.h
new file mode 100644 (file)
index 0000000..c59b4c2
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright © 2024 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef UTIL_RANGE_H
+#define UTIL_RANGE_H
+
+#include <stdbool.h>
+
+struct range {
+       int lower; /* inclusive */
+       int upper; /* exclusive */
+};
+
+static inline struct range
+range_init_empty(void) {
+       return (struct range){ .lower = 0, .upper = -1 };
+}
+
+static inline struct range
+range_init_inclusive(int lower, int upper) {
+       return (struct range) { .lower = lower, .upper = upper + 1};
+}
+
+static inline struct range
+range_init_exclusive(int lower, int upper) {
+       return (struct range){ .lower = lower, .upper = upper };
+}
+
+static inline bool
+range_is_valid(const struct range *r)
+{
+       return r->upper > r->lower;
+}
+
+#define range_for_each(range_, r_) \
+       for (r_ = (range_)->lower; r < (range_)->upper; r++)
+
+#endif
index fdd0899f92f51fb4e68cf16c57629c13eb179b36..4697eb685ca779b56b842422e3265ca9b312f58e 100644 (file)
@@ -578,14 +578,6 @@ litest_axis_set_value(struct axis_replacement *axes, int code, double value)
        litest_axis_set_value_unchecked(axes, code, value);
 }
 
-/* A loop range, resolves to:
-   for (i = lower; i < upper; i++)
- */
-struct range {
-       int lower; /* inclusive */
-       int upper; /* exclusive */
-};
-
 struct libinput *litest_create_context(void);
 void litest_destroy_context(struct libinput *li);
 void litest_disable_log_handler(struct libinput *libinput);
index cfa3c05f8a663334c97c5d7a47af0f29335eb992..73a82208396050036f74c8823b6aa244165bafc3 100644 (file)
@@ -33,6 +33,7 @@
 #include "util-prop-parsers.h"
 #include "util-macros.h"
 #include "util-bits.h"
+#include "util-range.h"
 #include "util-ratelimit.h"
 #include "util-matrix.h"
 #include "util-input-event.h"
@@ -1686,6 +1687,40 @@ START_TEST(absinfo_normalize_value_test)
 }
 END_TEST
 
+START_TEST(range_test)
+{
+       struct range incl = range_init_inclusive(1, 100);
+       ck_assert_int_eq(incl.lower, 1);
+       ck_assert_int_eq(incl.upper, 101);
+
+       struct range excl = range_init_exclusive(1, 100);
+       ck_assert_int_eq(excl.lower, 1);
+       ck_assert_int_eq(excl.upper, 100);
+
+       struct range zero = range_init_exclusive(0, 0);
+       ck_assert_int_eq(zero.lower, 0);
+       ck_assert_int_eq(zero.upper, 0);
+
+       struct range empty = range_init_empty();
+       ck_assert_int_eq(empty.lower, 0);
+       ck_assert_int_eq(empty.upper, -1);
+
+       ck_assert(range_is_valid(&incl));
+       ck_assert(range_is_valid(&excl));
+       ck_assert(!range_is_valid(&zero));
+       ck_assert(!range_is_valid(&empty));
+
+       int expected = 1;
+       int r = 0;
+       range_for_each(&incl, r) {
+               ck_assert_int_eq(r, expected);
+               expected++;
+       }
+       ck_assert_int_eq(r, 101);
+
+}
+END_TEST
+
 static Suite *
 litest_utils_suite(void)
 {
@@ -1742,6 +1777,8 @@ litest_utils_suite(void)
 
        tcase_add_test(tc, absinfo_normalize_value_test);
 
+       tcase_add_test(tc, range_test);
+
        suite_add_tcase(s, tc);
 
        return s;