--- /dev/null
+/*
+ * 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
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);
#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"
}
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)
{
tcase_add_test(tc, absinfo_normalize_value_test);
+ tcase_add_test(tc, range_test);
+
suite_add_tcase(s, tc);
return s;