test: make the TEST_COLLECTION() macro re-usable in the same file
authorPeter Hutterer <peter.hutterer@who-t.net>
Thu, 17 Oct 2024 09:25:39 +0000 (19:25 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Fri, 18 Oct 2024 00:49:47 +0000 (10:49 +1000)
Concat the line number to the generated variable names, this way we can
have more than one TEST_COLLECTION() in the same file.

Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1065>

src/util-macros.h
test/litest.h

index d56a8a3fd767b61e728769049f992f1ef6cb5834..29a34f6afe781f90ce2bace801cadcf1c4f0b572 100644 (file)
 
 #define CASE_RETURN_STRING(a) case a: return #a
 
+/**
+ * Concatenate two macro args into one, e.g.:
+ *     int CONCAT(foo_, __LINE__);
+ * will produce:
+ *     int foo_123;
+ */
+#define CONCAT2(X,Y) X##Y
+#define CONCAT(X,Y) CONCAT2(X,Y)
+
 #define _fallthrough_ __attribute__((fallthrough))
index 448faf47e1451f009e23aed33b12c542e3c0cbbb..c1bf8b7779ec74e496e2ede632d3f7f6d9b26fd3 100644 (file)
@@ -74,14 +74,14 @@ struct test_collection {
        void (*setup)(void);
 } __attribute__((aligned(16)));
 
-#define TEST_COLLECTION(name) \
-       static void (name##_setup)(void); \
-       static const struct test_collection _test_collection \
+#define TEST_COLLECTION(name_) \
+       static void (CONCAT(name_ , __LINE__))(void); \
+       static const struct test_collection CONCAT(_test_collection_, __LINE__) \
        __attribute__ ((used)) \
        __attribute__ ((section ("test_collection_section"))) = { \
-               #name, name##_setup \
+               #name_, CONCAT(name_, __LINE__) \
        }; \
-       static void (name##_setup)(void)
+       static void (CONCAT(name_, __LINE__))(void)
 
 __attribute__ ((format (printf, 3, 0)))
 void _litest_checkpoint(const char *func,