From: Peter Hutterer Date: Mon, 14 Oct 2024 04:39:07 +0000 (+1000) Subject: util: add the range handling as separate header file X-Git-Tag: 1.27.0~49 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=817dc42381a3fcf524f46432439b787a3460b3d6;p=platform%2Fupstream%2Flibinput.git util: add the range handling as separate header file Part-of: --- diff --git a/src/libinput-util.h b/src/libinput-util.h index 1f9863c6..0ca522ce 100644 --- a/src/libinput-util.h +++ b/src/libinput-util.h @@ -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 index 00000000..c59b4c2b --- /dev/null +++ b/src/util-range.h @@ -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 + +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 diff --git a/test/litest.h b/test/litest.h index fdd0899f..4697eb68 100644 --- a/test/litest.h +++ b/test/litest.h @@ -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); diff --git a/test/test-utils.c b/test/test-utils.c index cfa3c05f..73a82208 100644 --- a/test/test-utils.c +++ b/test/test-utils.c @@ -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;