summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-12-01 10:23:49 +0100
committerDavid Robillard <d@drobilla.net>2020-12-01 10:23:49 +0100
commit1a3f6ebb018fe0c685aca0ea6f4db306b288c4c1 (patch)
tree27410dbb2b373fc5d0f84e4ef6bbbe60286a3ff2 /test
parent4eee2e929cb273845cf4b00938968641caa0dfd4 (diff)
downloadlilv-1a3f6ebb018fe0c685aca0ea6f4db306b288c4c1.tar.gz
lilv-1a3f6ebb018fe0c685aca0ea6f4db306b288c4c1.tar.bz2
lilv-1a3f6ebb018fe0c685aca0ea6f4db306b288c4c1.zip
Factor out test URI map
Diffstat (limited to 'test')
-rw-r--r--test/lilv_test_uri_map.h86
-rw-r--r--test/lilv_test_utils.h5
-rw-r--r--test/test_state.c42
3 files changed, 98 insertions, 35 deletions
diff --git a/test/lilv_test_uri_map.h b/test/lilv_test_uri_map.h
new file mode 100644
index 0000000..d6badc1
--- /dev/null
+++ b/test/lilv_test_uri_map.h
@@ -0,0 +1,86 @@
+/*
+ Copyright 2007-2020 David Robillard <http://drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef LILV_TEST_URI_MAP_H
+#define LILV_TEST_URI_MAP_H
+
+#include "lilv_test_utils.h"
+
+#include "../src/lilv_internal.h"
+
+#include "serd/serd.h"
+
+#include <assert.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct
+{
+ char** uris;
+ size_t n_uris;
+} LilvTestUriMap;
+
+static inline void
+lilv_test_uri_map_init(LilvTestUriMap* const map)
+{
+ map->uris = NULL;
+ map->n_uris = 0;
+}
+
+static inline void
+lilv_test_uri_map_clear(LilvTestUriMap* const map)
+{
+ for (size_t i = 0; i < map->n_uris; ++i) {
+ free(map->uris[i]);
+ }
+
+ free(map->uris);
+ map->uris = NULL;
+ map->n_uris = 0;
+}
+
+static inline LV2_URID
+map_uri(LV2_URID_Map_Handle handle, const char* uri)
+{
+ LilvTestUriMap* map = (LilvTestUriMap*)handle;
+
+ for (size_t i = 0; i < map->n_uris; ++i) {
+ if (!strcmp(map->uris[i], uri)) {
+ return i + 1;
+ }
+ }
+
+ assert(serd_uri_string_has_scheme((const uint8_t*)uri));
+
+ map->uris = (char**)realloc(map->uris, ++map->n_uris * sizeof(char*));
+ map->uris[map->n_uris - 1] = lilv_strdup(uri);
+ return map->n_uris;
+}
+
+static inline const char*
+unmap_uri(LV2_URID_Map_Handle handle, LV2_URID urid)
+{
+ LilvTestUriMap* map = (LilvTestUriMap*)handle;
+
+ if (urid > 0 && urid <= map->n_uris) {
+ return map->uris[urid - 1];
+ }
+
+ return NULL;
+}
+
+#endif // LILV_TEST_URI_MAP_H
diff --git a/test/lilv_test_utils.h b/test/lilv_test_utils.h
index f027c80..13fd939 100644
--- a/test/lilv_test_utils.h
+++ b/test/lilv_test_utils.h
@@ -14,6 +14,9 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#ifndef LILV_TEST_UTILS_H
+#define LILV_TEST_UTILS_H
+
#include "lilv/lilv.h"
#define MANIFEST_PREFIXES \
@@ -83,3 +86,5 @@ delete_bundle(LilvTestEnv* env);
// Set an environment variable so it is immediately visible in this process
void
set_env(const char* name, const char* value);
+
+#endif // LILV_TEST_UTILS_H
diff --git a/test/test_state.c b/test/test_state.c
index 5a2e5cb..9d39181 100644
--- a/test/test_state.c
+++ b/test/test_state.c
@@ -16,6 +16,7 @@
#undef NDEBUG
+#include "lilv_test_uri_map.h"
#include "lilv_test_utils.h"
#include "../src/filesystem.h"
@@ -93,33 +94,6 @@ set_port_value(const char* port_symbol,
}
}
-static char** uris = NULL;
-static size_t n_uris = 0;
-
-static LV2_URID
-map_uri(LV2_URID_Map_Handle handle, const char* uri)
-{
- for (size_t i = 0; i < n_uris; ++i) {
- if (!strcmp(uris[i], uri)) {
- return i + 1;
- }
- }
-
- assert(serd_uri_string_has_scheme((const uint8_t*)uri));
- uris = (char**)realloc(uris, ++n_uris * sizeof(char*));
- uris[n_uris - 1] = lilv_strdup(uri);
- return n_uris;
-}
-
-static const char*
-unmap_uri(LV2_URID_Map_Handle handle, LV2_URID urid)
-{
- if (urid > 0 && urid <= n_uris) {
- return uris[urid - 1];
- }
- return NULL;
-}
-
static char* temp_dir = NULL;
static char*
@@ -140,6 +114,9 @@ main(void)
LilvTestEnv* const env = lilv_test_env_new();
LilvWorld* const world = env->world;
+ LilvTestUriMap uri_map;
+ lilv_test_uri_map_init(&uri_map);
+
uint8_t* abs_bundle = (uint8_t*)lilv_path_absolute(LILV_TEST_BUNDLE);
SerdNode bundle = serd_node_new_file_uri(abs_bundle, 0, 0, true);
LilvNode* bundle_uri = lilv_new_uri(world, (const char*)bundle.buf);
@@ -153,9 +130,9 @@ main(void)
const LilvPlugin* plugin = lilv_plugins_get_by_uri(plugins, plugin_uri);
assert(plugin);
- LV2_URID_Map map = {NULL, map_uri};
+ LV2_URID_Map map = {&uri_map, map_uri};
LV2_Feature map_feature = {LV2_URID_MAP_URI, &map};
- LV2_URID_Unmap unmap = {NULL, unmap_uri};
+ LV2_URID_Unmap unmap = {&uri_map, unmap_uri};
LV2_Feature unmap_feature = {LV2_URID_UNMAP_URI, &unmap};
const LV2_Feature* features[] = {&map_feature, &unmap_feature, NULL};
@@ -562,12 +539,7 @@ main(void)
lilv_remove("state");
- // Free URI map
- for (size_t i = 0; i < n_uris; ++i) {
- free(uris[i]);
- }
- free(uris);
- n_uris = 0;
+ lilv_test_uri_map_clear(&uri_map);
lilv_node_free(plugin_uri);
lilv_node_free(bundle_uri);