tests: add qtest_add_data_func_full
PATCH v5 1/1 tests: add qtest_add_data_func_full
Allows one to specify a destroy function for the test data.
Add a fallback using glib g_test_add_vtable() internal function, whose signature changed over time. Tested with glib 2.22, 2.26 and 2.48, which according to git log should be enough to cover all variations.
--- tests/libqtest.c | 19 +++++++++++++++++++ tests/libqtest.h | 17 +++++++++++++++++ 2 files changed, 36 insertions(+)
tests/libqtest.c +19 -0
@@ -758,6 +758,25 @@ void qtest_add_func(const char *str, void (*fn)(void))g_free (path); } +void qtest_add_data_func_full (const char *str,void *data, +void (*fn)(const void *), +GDestroyNotify data_free_func) +{ +gchar *path =g_strdup_printf ("/%s/%s" ,qtest_get_arch (), str); +#if GLIB_CHECK_VERSION (2 ,34 ,0 ) +g_test_add_data_func_full (path, data, fn, data_free_func); +#elif GLIB_CHECK_VERSION (2 ,26 ,0 ) +/* back-compat casts, remove this once we can require new-enough glib */ +g_test_add_vtable (path,0 , data,NULL , + (GTestFixtureFunc )fn, (GTestFixtureFunc ) data_free_func); +#elif GLIB_CHECK_VERSION (2 ,22 ,0 ) +/* back-compat casts, remove this once we can require new-enough glib */ +g_test_add_vtable (path,0 , data,NULL , + (void (*)(void )) fn, (void (*)(void )) data_free_func); +#endif +g_free (path); +} +void qtest_add_data_func (const char *str,const void *data,void (*fn)(const void *)) {
tests/libqtest.h +17 -0
@@ -425,6 +425,23 @@ void qtest_add_func(const char *str, void (*fn)(void));void qtest_add_data_func (const char *str,const void *data,void (*fn)(const void *)); +/** +* qtest_add_data_func_full: +* @str: Test case path. +* @data: Test case data +* @fn: Test case function +* @data_free_func: GDestroyNotify for data +* +* Add a GTester testcase with the given name, data and function. +* The path is prefixed with the architecture under test, as +* returned by qtest_get_arch(). +* +* @data is passed to @data_free_func() on test completion. +*/ +void qtest_add_data_func_full (const char *str,void *data, +void (*fn)(const void *), +GDestroyNotify data_free_func); +/** * qtest_add: * @testpath: Test case path
Signature · 1 line
2.10.0
Quoted 22 lines · Marc-André Lureau
Allows one to specify a destroy function for the test data.
Add a fallback using glib
g_test_add_vtable()internal function, whose signature changed over time. Tested with glib 2.22, 2.26 and 2.48, which according to git log should be enough to cover all variations.Signed-off-by Marc-André Lureau <[email protected]>--- tests/libqtest.c | 19 +++++++++++++++++++ tests/libqtest.h | 17 +++++++++++++++++ 2 files changed, 36 insertions(+)tests/libqtest.c +2
-0@@ -758,6 +758,25 @@ void qtest_add_func(const char *str, void (*fn)(void))g_free (path); } +void qtest_add_data_func_full(const char *str, void *data, + void (*fn)(const void *),
tests/libqtest.c void qtest_add_func(const char *str, void (*fn)(void)) · line 772
Quoted 10 lines
tests/libqtest.c +10
-0+ GDestroyNotify data_free_func) +{ +gchar *path =g_strdup_printf ("/%s/%s" ,qtest_get_arch (), str); +#if GLIB_CHECK_VERSION (2 ,34 ,0 ) +g_test_add_data_func_full (path, data, fn, data_free_func); +#elif GLIB_CHECK_VERSION (2 ,26 ,0 ) +/* back-compat casts, remove this once we can require new-enough glib */ +g_test_add_vtable (path,0 , data,NULL , + (GTestFixtureFunc )fn, (GTestFixtureFunc ) data_free_func); +#elif GLIB_CHECK_VERSION (2 ,22 ,0 )
Perhaps this could be written as #else, since we have no further alternatives (2.22.0 being our minimum). Up to the maintainer if it is worth changing. Otherwise I think the end result is reasonable, and you were able to avoid the leaks.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Hi
----- Original Message -----
Quoted 41 lines · Marc-André Lureau
Quoted 24 lines
Allows one to specify a destroy function for the test data.
Add a fallback using glib
g_test_add_vtable()internal function, whose signature changed over time. Tested with glib 2.22, 2.26 and 2.48, which according to git log should be enough to cover all variations.Signed-off-by Marc-André Lureau <[email protected]>--- tests/libqtest.c | 19 +++++++++++++++++++ tests/libqtest.h | 17 +++++++++++++++++ 2 files changed, 36 insertions(+)tests/libqtest.c +4
-0@@ -758,6 +758,25 @@ void qtest_add_func(const char *str, void (*fn)(void))g_free (path); } +void qtest_add_data_func_full (const char *str,void *data, +void (*fn)(const void *), +GDestroyNotify data_free_func) +{tests/libqtest.c void qtest_add_func(const char *str, void (*fn)(void)) · line 774
Quoted 10 lines
tests/libqtest.c +8
-0+gchar *path =g_strdup_printf ("/%s/%s" ,qtest_get_arch (), str); +#if GLIB_CHECK_VERSION (2 ,34 ,0 ) +g_test_add_data_func_full (path, data, fn, data_free_func); +#elif GLIB_CHECK_VERSION (2 ,26 ,0 ) +/* back-compat casts, remove this once we can require new-enough glib */ +g_test_add_vtable (path,0 , data,NULL , + (GTestFixtureFunc )fn, (GTestFixtureFunc ) data_free_func); +#elif GLIB_CHECK_VERSION (2 ,22 ,0 )Perhaps this could be written as #else, since we have no further alternatives (2.22.0 being our minimum). Up to the maintainer if it is worth changing. Otherwise I think the end result is reasonable, and you were able to avoid the leaks.
Quoted 1 line
Reviewed-by Eric Blake <[email protected]>
Ack, I'll change it.
thanks