Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1924 lines
63KB

  1. /*
  2. Copyright 2007-2014 David Robillard <http://drobilla.net>
  3. Copyright 2008 Krzysztof Foltman
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #define _POSIX_C_SOURCE 200112L /* for setenv */
  16. #include <assert.h>
  17. #include <ctype.h>
  18. #include <errno.h>
  19. #include <float.h>
  20. #include <limits.h>
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #ifdef _WIN32
  27. # include <direct.h>
  28. # define mkdir(path, flags) _mkdir(path)
  29. # define setenv(n, v, r) SetEnvironmentVariable((n), (v))
  30. # define unsetenv(n) SetEnvironmentVariable((n), NULL)
  31. #else
  32. # include <dirent.h>
  33. # include <unistd.h>
  34. #endif
  35. #include "lilv/lilv.h"
  36. #include "../src/lilv_internal.h"
  37. #include "lv2/lv2plug.in/ns/ext/presets/presets.h"
  38. #include "lv2/lv2plug.in/ns/ext/state/state.h"
  39. #include "lv2/lv2plug.in/ns/ext/urid/urid.h"
  40. #define TEST_PATH_MAX 1024
  41. #if defined(__APPLE__)
  42. # define SHLIB_EXT ".dylib"
  43. #elif defined(_WIN32)
  44. # define SHLIB_EXT ".dll"
  45. #else
  46. # define SHLIB_EXT ".so"
  47. #endif
  48. static char bundle_dir_name[TEST_PATH_MAX];
  49. static char bundle_dir_uri[TEST_PATH_MAX];
  50. static char manifest_name[TEST_PATH_MAX];
  51. static char content_name[TEST_PATH_MAX];
  52. static LilvWorld* world;
  53. int test_count = 0;
  54. int error_count = 0;
  55. static void
  56. delete_bundle(void)
  57. {
  58. unlink(content_name);
  59. unlink(manifest_name);
  60. remove(bundle_dir_name);
  61. }
  62. static void
  63. init_tests(void)
  64. {
  65. strncpy(bundle_dir_name, getenv("HOME"), 900);
  66. strcat(bundle_dir_name, "/.lv2");
  67. mkdir(bundle_dir_name, 0700);
  68. strcat(bundle_dir_name, "/lilv-test.lv2");
  69. sprintf(bundle_dir_uri, "file://%s/", bundle_dir_name);
  70. sprintf(manifest_name, "%s/manifest.ttl", bundle_dir_name);
  71. sprintf(content_name, "%s/plugin.ttl", bundle_dir_name);
  72. delete_bundle();
  73. }
  74. static void
  75. fatal_error(const char* err, const char* arg)
  76. {
  77. /* TODO: possibly change to vfprintf later */
  78. fprintf(stderr, err, arg);
  79. /* IMHO, the bundle should be left in place after an error, for possible investigation */
  80. /* delete_bundle(); */
  81. exit(1);
  82. }
  83. static void
  84. write_file(const char* name, const char* content)
  85. {
  86. FILE* f = fopen(name, "w");
  87. size_t len = strlen(content);
  88. if (fwrite(content, 1, len, f) != len)
  89. fatal_error("Cannot write file %s\n", name);
  90. fclose(f);
  91. }
  92. static int
  93. init_world(void)
  94. {
  95. world = lilv_world_new();
  96. return world != NULL;
  97. }
  98. static int
  99. load_all_bundles(void)
  100. {
  101. if (!init_world())
  102. return 0;
  103. lilv_world_load_all(world);
  104. return 1;
  105. }
  106. static void
  107. create_bundle(const char* manifest, const char* content)
  108. {
  109. if (mkdir(bundle_dir_name, 0700) && errno != EEXIST)
  110. fatal_error("Cannot create directory %s\n", bundle_dir_name);
  111. write_file(manifest_name, manifest);
  112. write_file(content_name, content);
  113. }
  114. static int
  115. start_bundle(const char* manifest, const char* content)
  116. {
  117. create_bundle(manifest, content);
  118. return load_all_bundles();
  119. }
  120. static void
  121. unload_bundle(void)
  122. {
  123. if (world)
  124. lilv_world_free(world);
  125. world = NULL;
  126. }
  127. static void
  128. cleanup(void)
  129. {
  130. delete_bundle();
  131. }
  132. /*****************************************************************************/
  133. #define TEST_CASE(name) { #name, test_##name }
  134. #define TEST_ASSERT(check) do {\
  135. test_count++;\
  136. if (!(check)) {\
  137. assert(false);\
  138. error_count++;\
  139. fprintf(stderr, "lilv_test.c:%d: error: %s\n", __LINE__, #check);\
  140. }\
  141. } while (0)
  142. typedef int (*TestFunc)(void);
  143. struct TestCase {
  144. const char* title;
  145. TestFunc func;
  146. };
  147. #define PREFIX_ATOM "@prefix atom: <http://lv2plug.in/ns/ext/atom#> . \n"
  148. #define PREFIX_LINE "@prefix : <http://example.org/> .\n"
  149. #define PREFIX_LV2 "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n"
  150. #define PREFIX_LV2EV "@prefix lv2ev: <http://lv2plug.in/ns/ext/event#> . \n"
  151. #define PREFIX_LV2UI "@prefix lv2ui: <http://lv2plug.in/ns/extensions/ui#> .\n"
  152. #define PREFIX_RDF "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"
  153. #define PREFIX_RDFS "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
  154. #define PREFIX_FOAF "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n"
  155. #define PREFIX_DOAP "@prefix doap: <http://usefulinc.com/ns/doap#> .\n"
  156. #define PREFIX_PSET "@prefix pset: <http://lv2plug.in/ns/ext/presets#> .\n"
  157. #define MANIFEST_PREFIXES PREFIX_LINE PREFIX_LV2 PREFIX_RDFS
  158. #define BUNDLE_PREFIXES PREFIX_ATOM PREFIX_LINE PREFIX_LV2 PREFIX_RDF PREFIX_RDFS PREFIX_FOAF PREFIX_DOAP PREFIX_PSET
  159. #define PLUGIN_NAME(name) "doap:name \"" name "\""
  160. #define LICENSE_GPL "doap:license <http://usefulinc.com/doap/licenses/gpl>"
  161. static const char* uris_plugin = "http://example.org/plug";
  162. static LilvNode* plugin_uri_value;
  163. static LilvNode* plugin2_uri_value;
  164. /*****************************************************************************/
  165. static void
  166. init_uris(void)
  167. {
  168. plugin_uri_value = lilv_new_uri(world, uris_plugin);
  169. plugin2_uri_value = lilv_new_uri(world, "http://example.org/foobar");
  170. TEST_ASSERT(plugin_uri_value);
  171. TEST_ASSERT(plugin2_uri_value);
  172. }
  173. static void
  174. cleanup_uris(void)
  175. {
  176. lilv_node_free(plugin2_uri_value);
  177. lilv_node_free(plugin_uri_value);
  178. plugin2_uri_value = NULL;
  179. plugin_uri_value = NULL;
  180. }
  181. /*****************************************************************************/
  182. static int
  183. test_utils(void)
  184. {
  185. TEST_ASSERT(!strcmp(lilv_uri_to_path("file:///tmp/blah"), "/tmp/blah"));
  186. TEST_ASSERT(!lilv_uri_to_path("file:/example.org/blah"));
  187. TEST_ASSERT(!lilv_uri_to_path("http://example.org/blah"));
  188. return 1;
  189. }
  190. /*****************************************************************************/
  191. static int
  192. test_value(void)
  193. {
  194. if (!start_bundle(MANIFEST_PREFIXES
  195. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  196. BUNDLE_PREFIXES
  197. ":plug a lv2:Plugin ; a lv2:CompressorPlugin ; "
  198. PLUGIN_NAME("Test plugin") " ; "
  199. LICENSE_GPL " ; "
  200. "lv2:port [ "
  201. " a lv2:ControlPort ; a lv2:InputPort ; "
  202. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"Foo\" ; "
  203. "] ."))
  204. return 0;
  205. init_uris();
  206. LilvNode* uval = lilv_new_uri(world, "http://example.org");
  207. LilvNode* sval = lilv_new_string(world, "Foo");
  208. LilvNode* ival = lilv_new_int(world, 42);
  209. LilvNode* fval = lilv_new_float(world, 1.6180);
  210. TEST_ASSERT(lilv_node_is_uri(uval));
  211. TEST_ASSERT(lilv_node_is_string(sval));
  212. TEST_ASSERT(lilv_node_is_int(ival));
  213. TEST_ASSERT(lilv_node_is_float(fval));
  214. TEST_ASSERT(!lilv_node_is_literal(uval));
  215. TEST_ASSERT(lilv_node_is_literal(sval));
  216. TEST_ASSERT(lilv_node_is_literal(ival));
  217. TEST_ASSERT(lilv_node_is_literal(fval));
  218. TEST_ASSERT(!strcmp(lilv_node_as_uri(uval), "http://example.org"));
  219. TEST_ASSERT(!strcmp(lilv_node_as_string(sval), "Foo"));
  220. TEST_ASSERT(lilv_node_as_int(ival) == 42);
  221. TEST_ASSERT(fabs(lilv_node_as_float(fval) - 1.6180) < FLT_EPSILON);
  222. LilvNode* loc_abs = lilv_new_file_uri(world, NULL, "/foo/bar");
  223. LilvNode* loc_rel = lilv_new_file_uri(world, NULL, "foo");
  224. LilvNode* host_abs = lilv_new_file_uri(world, "host", "/foo/bar");
  225. LilvNode* host_rel = lilv_new_file_uri(world, "host", "foo");
  226. TEST_ASSERT(!strcmp(lilv_node_as_uri(loc_abs), "file:///foo/bar"));
  227. TEST_ASSERT(!strncmp(lilv_node_as_uri(loc_rel), "file:///", 8));
  228. TEST_ASSERT(!strcmp(lilv_node_as_uri(host_abs), "file://host/foo/bar"));
  229. TEST_ASSERT(!strncmp(lilv_node_as_uri(host_rel), "file://host/", 12));
  230. lilv_node_free(host_rel);
  231. lilv_node_free(host_abs);
  232. lilv_node_free(loc_rel);
  233. lilv_node_free(loc_abs);
  234. char* tok = lilv_node_get_turtle_token(uval);
  235. TEST_ASSERT(!strcmp(tok, "<http://example.org>"));
  236. free(tok);
  237. tok = lilv_node_get_turtle_token(sval);
  238. TEST_ASSERT(!strcmp(tok, "Foo"));
  239. free(tok);
  240. tok = lilv_node_get_turtle_token(ival);
  241. TEST_ASSERT(!strcmp(tok, "42"));
  242. free(tok);
  243. tok = lilv_node_get_turtle_token(fval);
  244. TEST_ASSERT(!strncmp(tok, "1.6180", 6));
  245. free(tok);
  246. LilvNode* uval_e = lilv_new_uri(world, "http://example.org");
  247. LilvNode* sval_e = lilv_new_string(world, "Foo");
  248. LilvNode* ival_e = lilv_new_int(world, 42);
  249. LilvNode* fval_e = lilv_new_float(world, 1.6180);
  250. LilvNode* uval_ne = lilv_new_uri(world, "http://no-example.org");
  251. LilvNode* sval_ne = lilv_new_string(world, "Bar");
  252. LilvNode* ival_ne = lilv_new_int(world, 24);
  253. LilvNode* fval_ne = lilv_new_float(world, 3.14159);
  254. TEST_ASSERT(lilv_node_equals(uval, uval_e));
  255. TEST_ASSERT(lilv_node_equals(sval, sval_e));
  256. TEST_ASSERT(lilv_node_equals(ival, ival_e));
  257. TEST_ASSERT(lilv_node_equals(fval, fval_e));
  258. TEST_ASSERT(!lilv_node_equals(uval, uval_ne));
  259. TEST_ASSERT(!lilv_node_equals(sval, sval_ne));
  260. TEST_ASSERT(!lilv_node_equals(ival, ival_ne));
  261. TEST_ASSERT(!lilv_node_equals(fval, fval_ne));
  262. TEST_ASSERT(!lilv_node_equals(uval, sval));
  263. TEST_ASSERT(!lilv_node_equals(sval, ival));
  264. TEST_ASSERT(!lilv_node_equals(ival, fval));
  265. LilvNode* uval_dup = lilv_node_duplicate(uval);
  266. TEST_ASSERT(lilv_node_equals(uval, uval_dup));
  267. LilvNode* ifval = lilv_new_float(world, 42.0);
  268. TEST_ASSERT(!lilv_node_equals(ival, ifval));
  269. lilv_node_free(ifval);
  270. LilvNode* nil = NULL;
  271. TEST_ASSERT(!lilv_node_equals(uval, nil));
  272. TEST_ASSERT(!lilv_node_equals(nil, uval));
  273. TEST_ASSERT(lilv_node_equals(nil, nil));
  274. LilvNode* nil2 = lilv_node_duplicate(nil);
  275. TEST_ASSERT(lilv_node_equals(nil, nil2));
  276. lilv_node_free(uval);
  277. lilv_node_free(sval);
  278. lilv_node_free(ival);
  279. lilv_node_free(fval);
  280. lilv_node_free(uval_e);
  281. lilv_node_free(sval_e);
  282. lilv_node_free(ival_e);
  283. lilv_node_free(fval_e);
  284. lilv_node_free(uval_ne);
  285. lilv_node_free(sval_ne);
  286. lilv_node_free(ival_ne);
  287. lilv_node_free(fval_ne);
  288. lilv_node_free(uval_dup);
  289. lilv_node_free(nil2);
  290. cleanup_uris();
  291. return 1;
  292. }
  293. /*****************************************************************************/
  294. static int discovery_plugin_found = 0;
  295. static void
  296. discovery_verify_plugin(const LilvPlugin* plugin)
  297. {
  298. const LilvNode* value = lilv_plugin_get_uri(plugin);
  299. if (lilv_node_equals(value, plugin_uri_value)) {
  300. const LilvNode* lib_uri = NULL;
  301. TEST_ASSERT(!lilv_node_equals(value, plugin2_uri_value));
  302. discovery_plugin_found = 1;
  303. lib_uri = lilv_plugin_get_library_uri(plugin);
  304. TEST_ASSERT(lib_uri);
  305. TEST_ASSERT(lilv_node_is_uri(lib_uri));
  306. TEST_ASSERT(lilv_node_as_uri(lib_uri));
  307. TEST_ASSERT(strstr(lilv_node_as_uri(lib_uri), "foo" SHLIB_EXT));
  308. TEST_ASSERT(lilv_plugin_verify(plugin));
  309. }
  310. }
  311. static int
  312. test_discovery(void)
  313. {
  314. if (!start_bundle(MANIFEST_PREFIXES
  315. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  316. BUNDLE_PREFIXES
  317. ":plug a lv2:Plugin ;"
  318. PLUGIN_NAME("Test plugin") " ; "
  319. LICENSE_GPL " ; "
  320. "lv2:port [ a lv2:ControlPort ; a lv2:InputPort ;"
  321. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"bar\" ; ] ."))
  322. return 0;
  323. init_uris();
  324. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  325. TEST_ASSERT(lilv_plugins_size(plugins) > 0);
  326. const LilvPlugin* explug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  327. TEST_ASSERT(explug != NULL);
  328. const LilvPlugin* explug2 = lilv_plugins_get_by_uri(plugins, plugin2_uri_value);
  329. TEST_ASSERT(explug2 == NULL);
  330. if (explug) {
  331. LilvNode* name = lilv_plugin_get_name(explug);
  332. TEST_ASSERT(!strcmp(lilv_node_as_string(name), "Test plugin"));
  333. lilv_node_free(name);
  334. }
  335. discovery_plugin_found = 0;
  336. LILV_FOREACH(plugins, i, plugins)
  337. discovery_verify_plugin(lilv_plugins_get(plugins, i));
  338. TEST_ASSERT(discovery_plugin_found);
  339. plugins = NULL;
  340. cleanup_uris();
  341. return 1;
  342. }
  343. /*****************************************************************************/
  344. static int
  345. test_lv2_path(void)
  346. {
  347. #ifndef _WIN32
  348. char* orig_lv2_path = lilv_strdup(getenv("LV2_PATH"));
  349. setenv("LV2_PATH", "~/.lv2:/usr/local/lib/lv2:/usr/lib/lv2", 1);
  350. world = lilv_world_new();
  351. lilv_world_load_all(world);
  352. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  353. const size_t n_plugins = lilv_plugins_size(plugins);
  354. lilv_world_free(world);
  355. setenv("LV2_PATH", "$HOME/.lv2:/usr/local/lib/lv2:/usr/lib/lv2", 1);
  356. world = lilv_world_new();
  357. lilv_world_load_all(world);
  358. plugins = lilv_world_get_all_plugins(world);
  359. TEST_ASSERT(lilv_plugins_size(plugins) == n_plugins);
  360. lilv_world_free(world);
  361. world = NULL;
  362. if (orig_lv2_path) {
  363. setenv("LV2_PATH", orig_lv2_path, 1);
  364. } else {
  365. unsetenv("LV2_PATH");
  366. }
  367. free(orig_lv2_path);
  368. #endif
  369. return 1;
  370. }
  371. /*****************************************************************************/
  372. static int
  373. test_verify(void)
  374. {
  375. if (!start_bundle(MANIFEST_PREFIXES
  376. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  377. BUNDLE_PREFIXES
  378. ":plug a lv2:Plugin ; "
  379. PLUGIN_NAME("Test plugin") " ; "
  380. LICENSE_GPL " ; "
  381. "lv2:port [ a lv2:ControlPort ; a lv2:InputPort ;"
  382. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"bar\" ] ."))
  383. return 0;
  384. init_uris();
  385. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  386. const LilvPlugin* explug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  387. TEST_ASSERT(explug);
  388. TEST_ASSERT(lilv_plugin_verify(explug));
  389. cleanup_uris();
  390. return 1;
  391. }
  392. /*****************************************************************************/
  393. static int
  394. test_no_verify(void)
  395. {
  396. if (!start_bundle(MANIFEST_PREFIXES
  397. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  398. BUNDLE_PREFIXES
  399. ":plug a lv2:Plugin . "))
  400. return 0;
  401. init_uris();
  402. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  403. const LilvPlugin* explug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  404. TEST_ASSERT(explug);
  405. TEST_ASSERT(!lilv_plugin_verify(explug));
  406. cleanup_uris();
  407. return 1;
  408. }
  409. /*****************************************************************************/
  410. static int
  411. test_classes(void)
  412. {
  413. if (!start_bundle(MANIFEST_PREFIXES
  414. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  415. BUNDLE_PREFIXES
  416. ":plug a lv2:Plugin ; a lv2:CompressorPlugin ; "
  417. PLUGIN_NAME("Test plugin") " ; "
  418. LICENSE_GPL " ; "
  419. "lv2:port [ "
  420. " a lv2:ControlPort ; a lv2:InputPort ; "
  421. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"Foo\" ; "
  422. "] ."))
  423. return 0;
  424. init_uris();
  425. const LilvPluginClass* plugin = lilv_world_get_plugin_class(world);
  426. const LilvPluginClasses* classes = lilv_world_get_plugin_classes(world);
  427. LilvPluginClasses* children = lilv_plugin_class_get_children(plugin);
  428. TEST_ASSERT(lilv_plugin_class_get_parent_uri(plugin) == NULL);
  429. TEST_ASSERT(lilv_plugin_classes_size(classes) > lilv_plugin_classes_size(children));
  430. TEST_ASSERT(!strcmp(lilv_node_as_string(lilv_plugin_class_get_label(plugin)), "Plugin"));
  431. TEST_ASSERT(!strcmp(lilv_node_as_string(lilv_plugin_class_get_uri(plugin)),
  432. "http://lv2plug.in/ns/lv2core#Plugin"));
  433. LILV_FOREACH(plugin_classes, i, children) {
  434. TEST_ASSERT(lilv_node_equals(
  435. lilv_plugin_class_get_parent_uri(lilv_plugin_classes_get(children, i)),
  436. lilv_plugin_class_get_uri(plugin)));
  437. }
  438. LilvNode* some_uri = lilv_new_uri(world, "http://example.org/whatever");
  439. TEST_ASSERT(lilv_plugin_classes_get_by_uri(classes, some_uri) == NULL);
  440. lilv_node_free(some_uri);
  441. lilv_plugin_classes_free(children);
  442. cleanup_uris();
  443. return 1;
  444. }
  445. /*****************************************************************************/
  446. static int
  447. test_plugin(void)
  448. {
  449. if (!start_bundle(MANIFEST_PREFIXES
  450. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  451. BUNDLE_PREFIXES
  452. ":plug a lv2:Plugin ; a lv2:CompressorPlugin ; "
  453. PLUGIN_NAME("Test plugin") " ; "
  454. LICENSE_GPL " ; "
  455. "lv2:optionalFeature lv2:hardRTCapable ; "
  456. "lv2:requiredFeature <http://lv2plug.in/ns/ext/event> ; "
  457. "lv2:extensionData <http://example.org/extdata> ;"
  458. ":foo 1.6180 ; "
  459. ":bar true ; "
  460. ":baz false ; "
  461. ":blank [ a <http://example.org/blank> ] ; "
  462. "doap:maintainer [ foaf:name \"David Robillard\" ; "
  463. " foaf:homepage <http://drobilla.net> ; foaf:mbox <mailto:d@drobilla.net> ] ; "
  464. "lv2:port [ "
  465. " a lv2:ControlPort ; a lv2:InputPort ; "
  466. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"bar\" ; "
  467. " lv2:minimum -1.0 ; lv2:maximum 1.0 ; lv2:default 0.5 "
  468. "] , [ "
  469. " a lv2:ControlPort ; a lv2:InputPort ; "
  470. " lv2:index 1 ; lv2:symbol \"bar\" ; lv2:name \"Baz\" ; "
  471. " lv2:minimum -2.0 ; lv2:maximum 2.0 ; lv2:default 1.0 "
  472. "] , [ "
  473. " a lv2:ControlPort ; a lv2:OutputPort ; "
  474. " lv2:index 2 ; lv2:symbol \"latency\" ; lv2:name \"Latency\" ; "
  475. " lv2:portProperty lv2:reportsLatency ; "
  476. " lv2:designation lv2:latency "
  477. "] . \n"
  478. ":thing doap:name \"Something else\" .\n"))
  479. return 0;
  480. init_uris();
  481. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  482. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  483. TEST_ASSERT(plug);
  484. const LilvPluginClass* klass = lilv_plugin_get_class(plug);
  485. const LilvNode* klass_uri = lilv_plugin_class_get_uri(klass);
  486. TEST_ASSERT(!strcmp(lilv_node_as_string(klass_uri),
  487. "http://lv2plug.in/ns/lv2core#CompressorPlugin"));
  488. LilvNode* rdf_type = lilv_new_uri(
  489. world, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
  490. TEST_ASSERT(lilv_world_ask(world,
  491. lilv_plugin_get_uri(plug),
  492. rdf_type,
  493. klass_uri));
  494. lilv_node_free(rdf_type);
  495. TEST_ASSERT(!lilv_plugin_is_replaced(plug));
  496. TEST_ASSERT(!lilv_plugin_get_related(plug, NULL));
  497. const LilvNode* plug_bundle_uri = lilv_plugin_get_bundle_uri(plug);
  498. TEST_ASSERT(!strcmp(lilv_node_as_string(plug_bundle_uri), bundle_dir_uri));
  499. const LilvNodes* data_uris = lilv_plugin_get_data_uris(plug);
  500. TEST_ASSERT(lilv_nodes_size(data_uris) == 2);
  501. LilvNode* project = lilv_plugin_get_project(plug);
  502. TEST_ASSERT(!project);
  503. char* manifest_uri = (char*)malloc(TEST_PATH_MAX);
  504. char* data_uri = (char*)malloc(TEST_PATH_MAX);
  505. snprintf(manifest_uri, TEST_PATH_MAX, "%s%s",
  506. lilv_node_as_string(plug_bundle_uri), "manifest.ttl");
  507. snprintf(data_uri, TEST_PATH_MAX, "%s%s",
  508. lilv_node_as_string(plug_bundle_uri), "plugin.ttl");
  509. LilvNode* manifest_uri_val = lilv_new_uri(world, manifest_uri);
  510. TEST_ASSERT(lilv_nodes_contains(data_uris, manifest_uri_val));
  511. lilv_node_free(manifest_uri_val);
  512. LilvNode* data_uri_val = lilv_new_uri(world, data_uri);
  513. TEST_ASSERT(lilv_nodes_contains(data_uris, data_uri_val));
  514. lilv_node_free(data_uri_val);
  515. free(manifest_uri);
  516. free(data_uri);
  517. float mins[3];
  518. float maxs[3];
  519. float defs[3];
  520. lilv_plugin_get_port_ranges_float(plug, mins, maxs, defs);
  521. TEST_ASSERT(mins[0] == -1.0f);
  522. TEST_ASSERT(maxs[0] == 1.0f);
  523. TEST_ASSERT(defs[0] == 0.5f);
  524. LilvNode* audio_class = lilv_new_uri(world,
  525. "http://lv2plug.in/ns/lv2core#AudioPort");
  526. LilvNode* control_class = lilv_new_uri(world,
  527. "http://lv2plug.in/ns/lv2core#ControlPort");
  528. LilvNode* in_class = lilv_new_uri(world,
  529. "http://lv2plug.in/ns/lv2core#InputPort");
  530. LilvNode* out_class = lilv_new_uri(world,
  531. "http://lv2plug.in/ns/lv2core#OutputPort");
  532. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, control_class, NULL) == 3);
  533. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, audio_class, NULL) == 0);
  534. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, in_class, NULL) == 2);
  535. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, out_class, NULL) == 1);
  536. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, control_class, in_class, NULL) == 2);
  537. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, control_class, out_class, NULL) == 1);
  538. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, audio_class, in_class, NULL) == 0);
  539. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, audio_class, out_class, NULL) == 0);
  540. TEST_ASSERT(lilv_plugin_has_latency(plug));
  541. TEST_ASSERT(lilv_plugin_get_latency_port_index(plug) == 2);
  542. LilvNode* lv2_latency = lilv_new_uri(world,
  543. "http://lv2plug.in/ns/lv2core#latency");
  544. const LilvPort* latency_port = lilv_plugin_get_port_by_designation(
  545. plug, out_class, lv2_latency);
  546. lilv_node_free(lv2_latency);
  547. TEST_ASSERT(latency_port);
  548. TEST_ASSERT(lilv_port_get_index(plug, latency_port) == 2);
  549. LilvNode* rt_feature = lilv_new_uri(world,
  550. "http://lv2plug.in/ns/lv2core#hardRTCapable");
  551. LilvNode* event_feature = lilv_new_uri(world,
  552. "http://lv2plug.in/ns/ext/event");
  553. LilvNode* pretend_feature = lilv_new_uri(world,
  554. "http://example.org/solvesWorldHunger");
  555. TEST_ASSERT(lilv_plugin_has_feature(plug, rt_feature));
  556. TEST_ASSERT(lilv_plugin_has_feature(plug, event_feature));
  557. TEST_ASSERT(!lilv_plugin_has_feature(plug, pretend_feature));
  558. lilv_node_free(rt_feature);
  559. lilv_node_free(event_feature);
  560. lilv_node_free(pretend_feature);
  561. LilvNodes* supported = lilv_plugin_get_supported_features(plug);
  562. LilvNodes* required = lilv_plugin_get_required_features(plug);
  563. LilvNodes* optional = lilv_plugin_get_optional_features(plug);
  564. TEST_ASSERT(lilv_nodes_size(supported) == 2);
  565. TEST_ASSERT(lilv_nodes_size(required) == 1);
  566. TEST_ASSERT(lilv_nodes_size(optional) == 1);
  567. lilv_nodes_free(supported);
  568. lilv_nodes_free(required);
  569. lilv_nodes_free(optional);
  570. LilvNode* foo_p = lilv_new_uri(world, "http://example.org/foo");
  571. LilvNodes* foos = lilv_plugin_get_value(plug, foo_p);
  572. TEST_ASSERT(lilv_nodes_size(foos) == 1);
  573. TEST_ASSERT(fabs(lilv_node_as_float(lilv_nodes_get_first(foos)) - 1.6180) < FLT_EPSILON);
  574. lilv_node_free(foo_p);
  575. lilv_nodes_free(foos);
  576. LilvNode* bar_p = lilv_new_uri(world, "http://example.org/bar");
  577. LilvNodes* bars = lilv_plugin_get_value(plug, bar_p);
  578. TEST_ASSERT(lilv_nodes_size(bars) == 1);
  579. TEST_ASSERT(lilv_node_as_bool(lilv_nodes_get_first(bars)) == true);
  580. lilv_node_free(bar_p);
  581. lilv_nodes_free(bars);
  582. LilvNode* baz_p = lilv_new_uri(world, "http://example.org/baz");
  583. LilvNodes* bazs = lilv_plugin_get_value(plug, baz_p);
  584. TEST_ASSERT(lilv_nodes_size(bazs) == 1);
  585. TEST_ASSERT(lilv_node_as_bool(lilv_nodes_get_first(bazs)) == false);
  586. lilv_node_free(baz_p);
  587. lilv_nodes_free(bazs);
  588. LilvNode* blank_p = lilv_new_uri(world, "http://example.org/blank");
  589. LilvNodes* blanks = lilv_plugin_get_value(plug, blank_p);
  590. TEST_ASSERT(lilv_nodes_size(blanks) == 1);
  591. LilvNode* blank = lilv_nodes_get_first(blanks);
  592. TEST_ASSERT(lilv_node_is_blank(blank));
  593. const char* blank_str = lilv_node_as_blank(blank);
  594. char* blank_tok = lilv_node_get_turtle_token(blank);
  595. TEST_ASSERT(!strncmp(blank_tok, "_:", 2));
  596. TEST_ASSERT(!strcmp(blank_tok + 2, blank_str));
  597. free(blank_tok);
  598. lilv_node_free(blank_p);
  599. lilv_nodes_free(blanks);
  600. LilvNode* author_name = lilv_plugin_get_author_name(plug);
  601. TEST_ASSERT(!strcmp(lilv_node_as_string(author_name), "David Robillard"));
  602. lilv_node_free(author_name);
  603. LilvNode* author_email = lilv_plugin_get_author_email(plug);
  604. TEST_ASSERT(!strcmp(lilv_node_as_string(author_email), "mailto:d@drobilla.net"));
  605. lilv_node_free(author_email);
  606. LilvNode* author_homepage = lilv_plugin_get_author_homepage(plug);
  607. TEST_ASSERT(!strcmp(lilv_node_as_string(author_homepage), "http://drobilla.net"));
  608. lilv_node_free(author_homepage);
  609. LilvNode* thing_uri = lilv_new_uri(world, "http://example.org/thing");
  610. LilvNode* name_p = lilv_new_uri(world, "http://usefulinc.com/ns/doap#name");
  611. LilvNodes* thing_names = lilv_world_find_nodes(world, thing_uri, name_p, NULL);
  612. TEST_ASSERT(lilv_nodes_size(thing_names) == 1);
  613. LilvNode* thing_name = lilv_nodes_get_first(thing_names);
  614. TEST_ASSERT(thing_name);
  615. TEST_ASSERT(lilv_node_is_string(thing_name));
  616. TEST_ASSERT(!strcmp(lilv_node_as_string(thing_name), "Something else"));
  617. LilvNode* thing_name2 = lilv_world_get(world, thing_uri, name_p, NULL);
  618. TEST_ASSERT(lilv_node_equals(thing_name, thing_name2));
  619. LilvUIs* uis = lilv_plugin_get_uis(plug);
  620. TEST_ASSERT(lilv_uis_size(uis) == 0);
  621. lilv_uis_free(uis);
  622. LilvNode* extdata = lilv_new_uri(world, "http://example.org/extdata");
  623. LilvNode* noextdata = lilv_new_uri(world, "http://example.org/noextdata");
  624. LilvNodes* extdatas = lilv_plugin_get_extension_data(plug);
  625. TEST_ASSERT(lilv_plugin_has_extension_data(plug, extdata));
  626. TEST_ASSERT(!lilv_plugin_has_extension_data(plug, noextdata));
  627. TEST_ASSERT(lilv_nodes_size(extdatas) == 1);
  628. TEST_ASSERT(lilv_node_equals(lilv_nodes_get_first(extdatas), extdata));
  629. lilv_node_free(noextdata);
  630. lilv_node_free(extdata);
  631. lilv_nodes_free(extdatas);
  632. lilv_nodes_free(thing_names);
  633. lilv_node_free(thing_uri);
  634. lilv_node_free(thing_name2);
  635. lilv_node_free(name_p);
  636. lilv_node_free(control_class);
  637. lilv_node_free(audio_class);
  638. lilv_node_free(in_class);
  639. lilv_node_free(out_class);
  640. cleanup_uris();
  641. return 1;
  642. }
  643. /*****************************************************************************/
  644. static int
  645. test_project(void)
  646. {
  647. if (!start_bundle(MANIFEST_PREFIXES
  648. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  649. BUNDLE_PREFIXES
  650. ":plug a lv2:Plugin ; a lv2:CompressorPlugin ; "
  651. PLUGIN_NAME("Test plugin with project") " ; "
  652. LICENSE_GPL " ; "
  653. "lv2:project [ "
  654. " doap:maintainer [ "
  655. " foaf:name \"David Robillard\" ; "
  656. " foaf:homepage <http://drobilla.net> ; foaf:mbox <mailto:d@drobilla.net> ] ; "
  657. "] ; "
  658. "lv2:port [ "
  659. " a lv2:ControlPort ; a lv2:InputPort ; "
  660. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"bar\" ; "
  661. " lv2:minimum -1.0 ; lv2:maximum 1.0 ; lv2:default 0.5 "
  662. "] , [ "
  663. " a lv2:ControlPort ; a lv2:InputPort ; "
  664. " lv2:index 1 ; lv2:symbol \"bar\" ; lv2:name \"Baz\" ; "
  665. " lv2:minimum -2.0 ; lv2:maximum 2.0 ; lv2:default 1.0 "
  666. "] , [ "
  667. " a lv2:ControlPort ; a lv2:OutputPort ; "
  668. " lv2:index 2 ; lv2:symbol \"latency\" ; lv2:name \"Latency\" ; "
  669. " lv2:portProperty lv2:reportsLatency ; "
  670. " lv2:designation lv2:latency "
  671. "] . \n"
  672. ":thing doap:name \"Something else\" .\n"))
  673. return 0;
  674. init_uris();
  675. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  676. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  677. TEST_ASSERT(plug);
  678. LilvNode* author_name = lilv_plugin_get_author_name(plug);
  679. TEST_ASSERT(!strcmp(lilv_node_as_string(author_name), "David Robillard"));
  680. lilv_node_free(author_name);
  681. LilvNode* author_email = lilv_plugin_get_author_email(plug);
  682. TEST_ASSERT(!strcmp(lilv_node_as_string(author_email), "mailto:d@drobilla.net"));
  683. lilv_node_free(author_email);
  684. LilvNode* author_homepage = lilv_plugin_get_author_homepage(plug);
  685. TEST_ASSERT(!strcmp(lilv_node_as_string(author_homepage), "http://drobilla.net"));
  686. lilv_node_free(author_homepage);
  687. cleanup_uris();
  688. return 1;
  689. }
  690. /*****************************************************************************/
  691. static int
  692. test_no_author(void)
  693. {
  694. if (!start_bundle(MANIFEST_PREFIXES
  695. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  696. BUNDLE_PREFIXES
  697. ":plug a lv2:Plugin ; a lv2:CompressorPlugin ; "
  698. PLUGIN_NAME("Test plugin with project") " ; "
  699. LICENSE_GPL " ; "
  700. "lv2:port [ "
  701. " a lv2:ControlPort ; a lv2:InputPort ; "
  702. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"bar\" ; "
  703. " lv2:minimum -1.0 ; lv2:maximum 1.0 ; lv2:default 0.5 "
  704. "] , [ "
  705. " a lv2:ControlPort ; a lv2:InputPort ; "
  706. " lv2:index 1 ; lv2:symbol \"bar\" ; lv2:name \"Baz\" ; "
  707. " lv2:minimum -2.0 ; lv2:maximum 2.0 ; lv2:default 1.0 "
  708. "] , [ "
  709. " a lv2:ControlPort ; a lv2:OutputPort ; "
  710. " lv2:index 2 ; lv2:symbol \"latency\" ; lv2:name \"Latency\" ; "
  711. " lv2:portProperty lv2:reportsLatency ; "
  712. " lv2:designation lv2:latency "
  713. "] . \n"
  714. ":thing doap:name \"Something else\" .\n"))
  715. return 0;
  716. init_uris();
  717. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  718. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  719. TEST_ASSERT(plug);
  720. LilvNode* author_name = lilv_plugin_get_author_name(plug);
  721. TEST_ASSERT(!author_name);
  722. LilvNode* author_email = lilv_plugin_get_author_email(plug);
  723. TEST_ASSERT(!author_email);
  724. LilvNode* author_homepage = lilv_plugin_get_author_homepage(plug);
  725. TEST_ASSERT(!author_homepage);
  726. cleanup_uris();
  727. return 1;
  728. }
  729. /*****************************************************************************/
  730. static int
  731. test_project_no_author(void)
  732. {
  733. if (!start_bundle(MANIFEST_PREFIXES
  734. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  735. BUNDLE_PREFIXES
  736. ":plug a lv2:Plugin ; a lv2:CompressorPlugin ; "
  737. PLUGIN_NAME("Test plugin with project") " ; "
  738. LICENSE_GPL " ; "
  739. "lv2:project [ "
  740. " doap:name \"Fake project\" ;"
  741. "] ; "
  742. "lv2:port [ "
  743. " a lv2:ControlPort ; a lv2:InputPort ; "
  744. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"bar\" ; "
  745. " lv2:minimum -1.0 ; lv2:maximum 1.0 ; lv2:default 0.5 "
  746. "] , [ "
  747. " a lv2:ControlPort ; a lv2:InputPort ; "
  748. " lv2:index 1 ; lv2:symbol \"bar\" ; lv2:name \"Baz\" ; "
  749. " lv2:minimum -2.0 ; lv2:maximum 2.0 ; lv2:default 1.0 "
  750. "] , [ "
  751. " a lv2:ControlPort ; a lv2:OutputPort ; "
  752. " lv2:index 2 ; lv2:symbol \"latency\" ; lv2:name \"Latency\" ; "
  753. " lv2:portProperty lv2:reportsLatency ; "
  754. " lv2:designation lv2:latency "
  755. "] . \n"
  756. ":thing doap:name \"Something else\" .\n"))
  757. return 0;
  758. init_uris();
  759. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  760. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  761. TEST_ASSERT(plug);
  762. LilvNode* author_name = lilv_plugin_get_author_name(plug);
  763. TEST_ASSERT(!author_name);
  764. LilvNode* author_email = lilv_plugin_get_author_email(plug);
  765. TEST_ASSERT(!author_email);
  766. LilvNode* author_homepage = lilv_plugin_get_author_homepage(plug);
  767. TEST_ASSERT(!author_homepage);
  768. cleanup_uris();
  769. return 1;
  770. }
  771. /*****************************************************************************/
  772. static int
  773. test_preset(void)
  774. {
  775. if (!start_bundle(MANIFEST_PREFIXES
  776. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  777. BUNDLE_PREFIXES
  778. ":plug a lv2:Plugin ; a lv2:CompressorPlugin ; "
  779. PLUGIN_NAME("Test plugin with project") " ; "
  780. LICENSE_GPL " ; "
  781. "lv2:project [ "
  782. " doap:name \"Fake project\" ;"
  783. "] ; "
  784. "lv2:port [ "
  785. " a lv2:ControlPort ; a lv2:InputPort ; "
  786. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"bar\" ; "
  787. " lv2:minimum -1.0 ; lv2:maximum 1.0 ; lv2:default 0.5 "
  788. "] , [ "
  789. " a lv2:ControlPort ; a lv2:InputPort ; "
  790. " lv2:index 1 ; lv2:symbol \"bar\" ; lv2:name \"Baz\" ; "
  791. " lv2:minimum -2.0 ; lv2:maximum 2.0 ; lv2:default 1.0 "
  792. "] , [ "
  793. " a lv2:ControlPort ; a lv2:OutputPort ; "
  794. " lv2:index 2 ; lv2:symbol \"latency\" ; lv2:name \"Latency\" ; "
  795. " lv2:portProperty lv2:reportsLatency ; "
  796. " lv2:designation lv2:latency "
  797. "] . \n"
  798. "<http://example.org/preset> a pset:Preset ;"
  799. " lv2:appliesTo :plug ;"
  800. " rdfs:label \"some preset\" .\n"))
  801. return 0;
  802. init_uris();
  803. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  804. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  805. TEST_ASSERT(plug);
  806. LilvNode* pset_Preset = lilv_new_uri(world, LV2_PRESETS__Preset);
  807. LilvNodes* related = lilv_plugin_get_related(plug, pset_Preset);
  808. TEST_ASSERT(lilv_nodes_size(related) == 1);
  809. lilv_node_free(pset_Preset);
  810. lilv_nodes_free(related);
  811. cleanup_uris();
  812. return 1;
  813. }
  814. /*****************************************************************************/
  815. static int
  816. test_prototype(void)
  817. {
  818. if (!start_bundle(MANIFEST_PREFIXES
  819. ":prot a lv2:PluginBase ; rdfs:seeAlso <plugin.ttl> .\n"
  820. ":plug a lv2:Plugin ; lv2:binary <inst" SHLIB_EXT "> ; lv2:prototype :prot .\n",
  821. BUNDLE_PREFIXES
  822. ":prot a lv2:Plugin ; a lv2:CompressorPlugin ; "
  823. LICENSE_GPL " ; "
  824. "lv2:project [ "
  825. " doap:name \"Fake project\" ;"
  826. "] ; "
  827. "lv2:port [ "
  828. " a lv2:ControlPort ; a lv2:InputPort ; "
  829. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"bar\" ; "
  830. " lv2:minimum -1.0 ; lv2:maximum 1.0 ; lv2:default 0.5 "
  831. "] , [ "
  832. " a lv2:ControlPort ; a lv2:InputPort ; "
  833. " lv2:index 1 ; lv2:symbol \"bar\" ; lv2:name \"Baz\" ; "
  834. " lv2:minimum -2.0 ; lv2:maximum 2.0 ; lv2:default 1.0 "
  835. "] , [ "
  836. " a lv2:ControlPort ; a lv2:OutputPort ; "
  837. " lv2:index 2 ; lv2:symbol \"latency\" ; lv2:name \"Latency\" ; "
  838. " lv2:portProperty lv2:reportsLatency ; "
  839. " lv2:designation lv2:latency "
  840. "] . \n"
  841. ":plug doap:name \"Instance\" .\n"))
  842. return 0;
  843. init_uris();
  844. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  845. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  846. TEST_ASSERT(plug);
  847. // Test non-inherited property
  848. LilvNode* name = lilv_plugin_get_name(plug);
  849. TEST_ASSERT(!strcmp(lilv_node_as_string(name), "Instance"));
  850. lilv_node_free(name);
  851. // Test inherited property
  852. const LilvNode* binary = lilv_plugin_get_library_uri(plug);
  853. TEST_ASSERT(strstr(lilv_node_as_string(binary), "inst" SHLIB_EXT));
  854. cleanup_uris();
  855. return 1;
  856. }
  857. /*****************************************************************************/
  858. static int
  859. test_port(void)
  860. {
  861. if (!start_bundle(MANIFEST_PREFIXES
  862. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  863. BUNDLE_PREFIXES PREFIX_LV2EV
  864. ":plug a lv2:Plugin ; "
  865. PLUGIN_NAME("Test plugin") " ; "
  866. LICENSE_GPL " ; "
  867. "doap:homepage <http://example.org/someplug> ; "
  868. "lv2:port [ "
  869. " a lv2:ControlPort ; a lv2:InputPort ; "
  870. " lv2:index 0 ; lv2:symbol \"foo\" ; "
  871. " lv2:name \"store\" ; "
  872. " lv2:name \"dépanneur\"@fr-ca ; lv2:name \"épicerie\"@fr-fr ; "
  873. " lv2:name \"tienda\"@es ; "
  874. " rdfs:comment \"comment\"@en , \"commentaires\"@fr ; "
  875. " lv2:portProperty lv2:integer ; "
  876. " lv2:minimum -1.0 ; lv2:maximum 1.0 ; lv2:default 0.5 ; "
  877. " lv2:scalePoint [ rdfs:label \"Sin\"; rdf:value 3 ] ; "
  878. " lv2:scalePoint [ rdfs:label \"Cos\"; rdf:value 4 ] "
  879. "] , [\n"
  880. " a lv2:EventPort ; a lv2:InputPort ; "
  881. " lv2:index 1 ; lv2:symbol \"event_in\" ; "
  882. " lv2:name \"Event Input\" ; "
  883. " lv2ev:supportsEvent <http://example.org/event> ;"
  884. " atom:supports <http://example.org/atomEvent> "
  885. "] , [\n"
  886. " a lv2:AudioPort ; a lv2:InputPort ; "
  887. " lv2:index 2 ; lv2:symbol \"audio_in\" ; "
  888. " lv2:name \"Audio Input\" ; "
  889. "] , [\n"
  890. " a lv2:AudioPort ; a lv2:OutputPort ; "
  891. " lv2:index 3 ; lv2:symbol \"audio_out\" ; "
  892. " lv2:name \"Audio Output\" ; "
  893. "] ."))
  894. return 0;
  895. init_uris();
  896. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  897. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  898. TEST_ASSERT(plug);
  899. LilvNode* psym = lilv_new_string(world, "foo");
  900. const LilvPort* p = lilv_plugin_get_port_by_index(plug, 0);
  901. const LilvPort* p2 = lilv_plugin_get_port_by_symbol(plug, psym);
  902. lilv_node_free(psym);
  903. TEST_ASSERT(p != NULL);
  904. TEST_ASSERT(p2 != NULL);
  905. TEST_ASSERT(p == p2);
  906. LilvNode* nopsym = lilv_new_string(world, "thisaintnoportfoo");
  907. const LilvPort* p3 = lilv_plugin_get_port_by_symbol(plug, nopsym);
  908. TEST_ASSERT(p3 == NULL);
  909. lilv_node_free(nopsym);
  910. LilvNode* audio_class = lilv_new_uri(world,
  911. "http://lv2plug.in/ns/lv2core#AudioPort");
  912. LilvNode* control_class = lilv_new_uri(world,
  913. "http://lv2plug.in/ns/lv2core#ControlPort");
  914. LilvNode* in_class = lilv_new_uri(world,
  915. "http://lv2plug.in/ns/lv2core#InputPort");
  916. LilvNode* out_class = lilv_new_uri(world,
  917. "http://lv2plug.in/ns/lv2core#OutputPort");
  918. TEST_ASSERT(lilv_nodes_size(lilv_port_get_classes(plug, p)) == 2);
  919. TEST_ASSERT(lilv_plugin_get_num_ports(plug) == 4);
  920. TEST_ASSERT(lilv_port_is_a(plug, p, control_class));
  921. TEST_ASSERT(lilv_port_is_a(plug, p, in_class));
  922. TEST_ASSERT(!lilv_port_is_a(plug, p, audio_class));
  923. LilvNodes* port_properties = lilv_port_get_properties(plug, p);
  924. TEST_ASSERT(lilv_nodes_size(port_properties) == 1);
  925. lilv_nodes_free(port_properties);
  926. // Untranslated name (current locale is set to "C" in main)
  927. TEST_ASSERT(!strcmp(lilv_node_as_string(lilv_port_get_symbol(plug, p)), "foo"));
  928. LilvNode* name = lilv_port_get_name(plug, p);
  929. TEST_ASSERT(!strcmp(lilv_node_as_string(name), "store"));
  930. lilv_node_free(name);
  931. // Exact language match
  932. setenv("LANG", "fr_FR", 1);
  933. name = lilv_port_get_name(plug, p);
  934. TEST_ASSERT(!strcmp(lilv_node_as_string(name), "épicerie"));
  935. lilv_node_free(name);
  936. // Exact language match (with charset suffix)
  937. setenv("LANG", "fr_CA.utf8", 1);
  938. name = lilv_port_get_name(plug, p);
  939. TEST_ASSERT(!strcmp(lilv_node_as_string(name), "dépanneur"));
  940. lilv_node_free(name);
  941. // Partial language match (choose value translated for different country)
  942. setenv("LANG", "fr_BE", 1);
  943. name = lilv_port_get_name(plug, p);
  944. TEST_ASSERT((!strcmp(lilv_node_as_string(name), "dépanneur"))
  945. ||(!strcmp(lilv_node_as_string(name), "épicerie")));
  946. lilv_node_free(name);
  947. // Partial language match (choose country-less language tagged value)
  948. setenv("LANG", "es_MX", 1);
  949. name = lilv_port_get_name(plug, p);
  950. TEST_ASSERT(!strcmp(lilv_node_as_string(name), "tienda"));
  951. lilv_node_free(name);
  952. // No language match (choose untranslated value)
  953. setenv("LANG", "cn", 1);
  954. name = lilv_port_get_name(plug, p);
  955. TEST_ASSERT(!strcmp(lilv_node_as_string(name), "store"));
  956. lilv_node_free(name);
  957. setenv("LANG", "en_CA.utf-8", 1);
  958. // Language tagged value with no untranslated values
  959. LilvNode* rdfs_comment = lilv_new_uri(world, LILV_NS_RDFS "comment");
  960. LilvNodes* comments = lilv_port_get_value(plug, p, rdfs_comment);
  961. TEST_ASSERT(!strcmp(lilv_node_as_string(lilv_nodes_get_first(comments)),
  962. "comment"));
  963. LilvNode* comment = lilv_port_get(plug, p, rdfs_comment);
  964. TEST_ASSERT(!strcmp(lilv_node_as_string(comment), "comment"));
  965. lilv_node_free(comment);
  966. lilv_nodes_free(comments);
  967. setenv("LANG", "fr", 1);
  968. comments = lilv_port_get_value(plug, p, rdfs_comment);
  969. TEST_ASSERT(!strcmp(lilv_node_as_string(lilv_nodes_get_first(comments)),
  970. "commentaires"));
  971. lilv_nodes_free(comments);
  972. setenv("LANG", "cn", 1);
  973. comments = lilv_port_get_value(plug, p, rdfs_comment);
  974. TEST_ASSERT(!comments);
  975. lilv_nodes_free(comments);
  976. lilv_node_free(rdfs_comment);
  977. setenv("LANG", "C", 1); // Reset locale
  978. LilvScalePoints* points = lilv_port_get_scale_points(plug, p);
  979. TEST_ASSERT(lilv_scale_points_size(points) == 2);
  980. LilvIter* sp_iter = lilv_scale_points_begin(points);
  981. const LilvScalePoint* sp0 = lilv_scale_points_get(points, sp_iter);
  982. TEST_ASSERT(sp0);
  983. sp_iter = lilv_scale_points_next(points, sp_iter);
  984. const LilvScalePoint* sp1 = lilv_scale_points_get(points, sp_iter);
  985. TEST_ASSERT(sp1);
  986. TEST_ASSERT(
  987. ((!strcmp(lilv_node_as_string(lilv_scale_point_get_label(sp0)), "Sin")
  988. && lilv_node_as_float(lilv_scale_point_get_value(sp0)) == 3)
  989. &&
  990. (!strcmp(lilv_node_as_string(lilv_scale_point_get_label(sp1)), "Cos")
  991. && lilv_node_as_float(lilv_scale_point_get_value(sp1)) == 4))
  992. ||
  993. ((!strcmp(lilv_node_as_string(lilv_scale_point_get_label(sp0)), "Cos")
  994. && lilv_node_as_float(lilv_scale_point_get_value(sp0)) == 4)
  995. &&
  996. (!strcmp(lilv_node_as_string(lilv_scale_point_get_label(sp1)), "Sin")
  997. && lilv_node_as_float(lilv_scale_point_get_value(sp1)) == 3)));
  998. LilvNode* homepage_p = lilv_new_uri(world, "http://usefulinc.com/ns/doap#homepage");
  999. LilvNodes* homepages = lilv_plugin_get_value(plug, homepage_p);
  1000. TEST_ASSERT(lilv_nodes_size(homepages) == 1);
  1001. TEST_ASSERT(!strcmp(lilv_node_as_string(lilv_nodes_get_first(homepages)),
  1002. "http://example.org/someplug"));
  1003. LilvNode *min, *max, *def;
  1004. lilv_port_get_range(plug, p, &def, &min, &max);
  1005. TEST_ASSERT(def);
  1006. TEST_ASSERT(min);
  1007. TEST_ASSERT(max);
  1008. TEST_ASSERT(lilv_node_as_float(def) == 0.5);
  1009. TEST_ASSERT(lilv_node_as_float(min) == -1.0);
  1010. TEST_ASSERT(lilv_node_as_float(max) == 1.0);
  1011. LilvNode* integer_prop = lilv_new_uri(world, "http://lv2plug.in/ns/lv2core#integer");
  1012. LilvNode* toggled_prop = lilv_new_uri(world, "http://lv2plug.in/ns/lv2core#toggled");
  1013. TEST_ASSERT(lilv_port_has_property(plug, p, integer_prop));
  1014. TEST_ASSERT(!lilv_port_has_property(plug, p, toggled_prop));
  1015. const LilvPort* ep = lilv_plugin_get_port_by_index(plug, 1);
  1016. LilvNode* event_type = lilv_new_uri(world, "http://example.org/event");
  1017. LilvNode* event_type_2 = lilv_new_uri(world, "http://example.org/otherEvent");
  1018. LilvNode* atom_event = lilv_new_uri(world, "http://example.org/atomEvent");
  1019. TEST_ASSERT(lilv_port_supports_event(plug, ep, event_type));
  1020. TEST_ASSERT(!lilv_port_supports_event(plug, ep, event_type_2));
  1021. TEST_ASSERT(lilv_port_supports_event(plug, ep, atom_event));
  1022. LilvNode* name_p = lilv_new_uri(world, "http://lv2plug.in/ns/lv2core#name");
  1023. LilvNodes* names = lilv_port_get_value(plug, p, name_p);
  1024. TEST_ASSERT(lilv_nodes_size(names) == 1);
  1025. TEST_ASSERT(!strcmp(lilv_node_as_string(lilv_nodes_get_first(names)),
  1026. "store"));
  1027. lilv_nodes_free(names);
  1028. LilvNode* true_val = lilv_new_bool(world, true);
  1029. LilvNode* false_val = lilv_new_bool(world, false);
  1030. TEST_ASSERT(!lilv_node_equals(true_val, false_val));
  1031. lilv_world_set_option(world, LILV_OPTION_FILTER_LANG, false_val);
  1032. names = lilv_port_get_value(plug, p, name_p);
  1033. TEST_ASSERT(lilv_nodes_size(names) == 4);
  1034. lilv_nodes_free(names);
  1035. lilv_world_set_option(world, LILV_OPTION_FILTER_LANG, true_val);
  1036. lilv_node_free(false_val);
  1037. lilv_node_free(true_val);
  1038. names = lilv_port_get_value(plug, ep, name_p);
  1039. TEST_ASSERT(lilv_nodes_size(names) == 1);
  1040. TEST_ASSERT(!strcmp(lilv_node_as_string(lilv_nodes_get_first(names)),
  1041. "Event Input"));
  1042. const LilvPort* ap_in = lilv_plugin_get_port_by_index(plug, 2);
  1043. TEST_ASSERT(lilv_port_is_a(plug, ap_in, in_class));
  1044. TEST_ASSERT(!lilv_port_is_a(plug, ap_in, out_class));
  1045. TEST_ASSERT(lilv_port_is_a(plug, ap_in, audio_class));
  1046. TEST_ASSERT(!lilv_port_is_a(plug, ap_in, control_class));
  1047. const LilvPort* ap_out = lilv_plugin_get_port_by_index(plug, 3);
  1048. TEST_ASSERT(lilv_port_is_a(plug, ap_out, out_class));
  1049. TEST_ASSERT(!lilv_port_is_a(plug, ap_out, in_class));
  1050. TEST_ASSERT(lilv_port_is_a(plug, ap_out, audio_class));
  1051. TEST_ASSERT(!lilv_port_is_a(plug, ap_out, control_class));
  1052. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, control_class, in_class , NULL) == 1);
  1053. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, audio_class , in_class , NULL) == 1);
  1054. TEST_ASSERT(lilv_plugin_get_num_ports_of_class(plug, audio_class , out_class, NULL) == 1);
  1055. lilv_nodes_free(names);
  1056. lilv_node_free(name_p);
  1057. lilv_node_free(integer_prop);
  1058. lilv_node_free(toggled_prop);
  1059. lilv_node_free(event_type);
  1060. lilv_node_free(event_type_2);
  1061. lilv_node_free(atom_event);
  1062. lilv_node_free(min);
  1063. lilv_node_free(max);
  1064. lilv_node_free(def);
  1065. lilv_node_free(homepage_p);
  1066. lilv_nodes_free(homepages);
  1067. lilv_scale_points_free(points);
  1068. lilv_node_free(control_class);
  1069. lilv_node_free(audio_class);
  1070. lilv_node_free(out_class);
  1071. lilv_node_free(in_class);
  1072. cleanup_uris();
  1073. return 1;
  1074. }
  1075. /*****************************************************************************/
  1076. static unsigned
  1077. ui_supported(const char* container_type_uri,
  1078. const char* ui_type_uri)
  1079. {
  1080. return !strcmp(container_type_uri, ui_type_uri);
  1081. }
  1082. static int
  1083. test_ui(void)
  1084. {
  1085. if (!start_bundle(MANIFEST_PREFIXES
  1086. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  1087. BUNDLE_PREFIXES PREFIX_LV2UI
  1088. ":plug a lv2:Plugin ; a lv2:CompressorPlugin ; "
  1089. PLUGIN_NAME("Test plugin") " ; "
  1090. LICENSE_GPL " ; "
  1091. "lv2:optionalFeature lv2:hardRTCapable ; "
  1092. "lv2:requiredFeature <http://lv2plug.in/ns/ext/event> ; "
  1093. "lv2ui:ui :ui , :ui2 , :ui3 , :ui4 ; "
  1094. "doap:maintainer [ foaf:name \"David Robillard\" ; "
  1095. " foaf:homepage <http://drobilla.net> ; foaf:mbox <mailto:d@drobilla.net> ] ; "
  1096. "lv2:port [ "
  1097. " a lv2:ControlPort ; a lv2:InputPort ; "
  1098. " lv2:index 0 ; lv2:symbol \"foo\" ; lv2:name \"bar\" ; "
  1099. " lv2:minimum -1.0 ; lv2:maximum 1.0 ; lv2:default 0.5 "
  1100. "] , [ "
  1101. " a lv2:ControlPort ; a lv2:InputPort ; "
  1102. " lv2:index 1 ; lv2:symbol \"bar\" ; lv2:name \"Baz\" ; "
  1103. " lv2:minimum -2.0 ; lv2:maximum 2.0 ; lv2:default 1.0 "
  1104. "] , [ "
  1105. " a lv2:ControlPort ; a lv2:OutputPort ; "
  1106. " lv2:index 2 ; lv2:symbol \"latency\" ; lv2:name \"Latency\" ; "
  1107. " lv2:portProperty lv2:reportsLatency "
  1108. "] .\n"
  1109. ":ui a lv2ui:GtkUI ; "
  1110. " lv2ui:requiredFeature lv2ui:makeResident ; "
  1111. " lv2ui:binary <ui" SHLIB_EXT "> ; "
  1112. " lv2ui:optionalFeature lv2ui:ext_presets . "
  1113. ":ui2 a lv2ui:GtkUI ; lv2ui:binary <ui2" SHLIB_EXT "> . "
  1114. ":ui3 a lv2ui:GtkUI ; lv2ui:binary <ui3" SHLIB_EXT "> . "
  1115. ":ui4 a lv2ui:GtkUI ; lv2ui:binary <ui4" SHLIB_EXT "> . "))
  1116. return 0;
  1117. init_uris();
  1118. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  1119. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  1120. TEST_ASSERT(plug);
  1121. LilvUIs* uis = lilv_plugin_get_uis(plug);
  1122. TEST_ASSERT(lilv_uis_size(uis) == 4);
  1123. const LilvUI* ui0 = lilv_uis_get(uis, lilv_uis_begin(uis));
  1124. TEST_ASSERT(ui0);
  1125. LilvNode* ui_uri = lilv_new_uri(world, "http://example.org/ui");
  1126. LilvNode* ui2_uri = lilv_new_uri(world, "http://example.org/ui3");
  1127. LilvNode* ui3_uri = lilv_new_uri(world, "http://example.org/ui4");
  1128. LilvNode* noui_uri = lilv_new_uri(world, "http://example.org/notaui");
  1129. const LilvUI* ui0_2 = lilv_uis_get_by_uri(uis, ui_uri);
  1130. TEST_ASSERT(ui0 == ui0_2);
  1131. TEST_ASSERT(lilv_node_equals(lilv_ui_get_uri(ui0_2), ui_uri));
  1132. const LilvUI* ui2 = lilv_uis_get_by_uri(uis, ui2_uri);
  1133. TEST_ASSERT(ui2 != ui0);
  1134. const LilvUI* ui3 = lilv_uis_get_by_uri(uis, ui3_uri);
  1135. TEST_ASSERT(ui3 != ui0);
  1136. const LilvUI* noui = lilv_uis_get_by_uri(uis, noui_uri);
  1137. TEST_ASSERT(noui == NULL);
  1138. const LilvNodes* classes = lilv_ui_get_classes(ui0);
  1139. TEST_ASSERT(lilv_nodes_size(classes) == 1);
  1140. LilvNode* ui_class_uri = lilv_new_uri(world,
  1141. "http://lv2plug.in/ns/extensions/ui#GtkUI");
  1142. TEST_ASSERT(lilv_node_equals(lilv_nodes_get_first(classes), ui_class_uri));
  1143. TEST_ASSERT(lilv_ui_is_a(ui0, ui_class_uri));
  1144. const LilvNode* ui_type = NULL;
  1145. TEST_ASSERT(lilv_ui_is_supported(ui0, ui_supported, ui_class_uri, &ui_type));
  1146. TEST_ASSERT(lilv_node_equals(ui_type, ui_class_uri));
  1147. const LilvNode* plug_bundle_uri = lilv_plugin_get_bundle_uri(plug);
  1148. const LilvNode* ui_bundle_uri = lilv_ui_get_bundle_uri(ui0);
  1149. TEST_ASSERT(lilv_node_equals(plug_bundle_uri, ui_bundle_uri));
  1150. char* ui_binary_uri_str = (char*)malloc(TEST_PATH_MAX);
  1151. snprintf(ui_binary_uri_str, TEST_PATH_MAX, "%s%s",
  1152. lilv_node_as_string(plug_bundle_uri), "ui" SHLIB_EXT);
  1153. const LilvNode* ui_binary_uri = lilv_ui_get_binary_uri(ui0);
  1154. LilvNode* expected_uri = lilv_new_uri(world, ui_binary_uri_str);
  1155. TEST_ASSERT(lilv_node_equals(expected_uri, ui_binary_uri));
  1156. free(ui_binary_uri_str);
  1157. lilv_node_free(ui_class_uri);
  1158. lilv_node_free(ui_uri);
  1159. lilv_node_free(ui2_uri);
  1160. lilv_node_free(ui3_uri);
  1161. lilv_node_free(noui_uri);
  1162. lilv_node_free(expected_uri);
  1163. lilv_uis_free(uis);
  1164. cleanup_uris();
  1165. return 1;
  1166. }
  1167. /*****************************************************************************/
  1168. uint32_t atom_Float = 0;
  1169. float in = 1.0;
  1170. float out = 42.0;
  1171. static const void*
  1172. get_port_value(const char* port_symbol,
  1173. void* user_data,
  1174. uint32_t* size,
  1175. uint32_t* type)
  1176. {
  1177. if (!strcmp(port_symbol, "input")) {
  1178. *size = sizeof(float);
  1179. *type = atom_Float;
  1180. return &in;
  1181. } else if (!strcmp(port_symbol, "output")) {
  1182. *size = sizeof(float);
  1183. *type = atom_Float;
  1184. return &out;
  1185. } else {
  1186. fprintf(stderr, "error: get_port_value for nonexistent port `%s'\n",
  1187. port_symbol);
  1188. *size = *type = 0;
  1189. return NULL;
  1190. }
  1191. }
  1192. static void
  1193. set_port_value(const char* port_symbol,
  1194. void* user_data,
  1195. const void* value,
  1196. uint32_t size,
  1197. uint32_t type)
  1198. {
  1199. if (!strcmp(port_symbol, "input")) {
  1200. in = *(const float*)value;
  1201. } else if (!strcmp(port_symbol, "output")) {
  1202. out = *(const float*)value;
  1203. } else {
  1204. fprintf(stderr, "error: set_port_value for nonexistent port `%s'\n",
  1205. port_symbol);
  1206. }
  1207. }
  1208. char** uris = NULL;
  1209. size_t n_uris = 0;
  1210. static LV2_URID
  1211. map_uri(LV2_URID_Map_Handle handle,
  1212. const char* uri)
  1213. {
  1214. for (size_t i = 0; i < n_uris; ++i) {
  1215. if (!strcmp(uris[i], uri)) {
  1216. return i + 1;
  1217. }
  1218. }
  1219. assert(serd_uri_string_has_scheme((const uint8_t*)uri));
  1220. uris = (char**)realloc(uris, ++n_uris * sizeof(char*));
  1221. uris[n_uris - 1] = lilv_strdup(uri);
  1222. return n_uris;
  1223. }
  1224. static const char*
  1225. unmap_uri(LV2_URID_Map_Handle handle,
  1226. LV2_URID urid)
  1227. {
  1228. if (urid > 0 && urid <= n_uris) {
  1229. return uris[urid - 1];
  1230. }
  1231. return NULL;
  1232. }
  1233. static char* temp_dir = NULL;
  1234. static char*
  1235. lilv_make_path(LV2_State_Make_Path_Handle handle,
  1236. const char* path)
  1237. {
  1238. return lilv_path_join(temp_dir, path);
  1239. }
  1240. static int
  1241. test_state(void)
  1242. {
  1243. init_world();
  1244. uint8_t* abs_bundle = (uint8_t*)lilv_path_absolute(LILV_TEST_BUNDLE);
  1245. SerdNode bundle = serd_node_new_file_uri(abs_bundle, 0, 0, true);
  1246. LilvNode* bundle_uri = lilv_new_uri(world, (const char*)bundle.buf);
  1247. LilvNode* plugin_uri = lilv_new_uri(world,
  1248. "http://example.org/lilv-test-plugin");
  1249. lilv_world_load_bundle(world, bundle_uri);
  1250. free(abs_bundle);
  1251. serd_node_free(&bundle);
  1252. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  1253. const LilvPlugin* plugin = lilv_plugins_get_by_uri(plugins, plugin_uri);
  1254. TEST_ASSERT(plugin);
  1255. LV2_URID_Map map = { NULL, map_uri };
  1256. LV2_Feature map_feature = { LV2_URID_MAP_URI, &map };
  1257. LV2_URID_Unmap unmap = { NULL, unmap_uri };
  1258. LV2_Feature unmap_feature = { LV2_URID_UNMAP_URI, &unmap };
  1259. const LV2_Feature* features[] = { &map_feature, &unmap_feature, NULL };
  1260. atom_Float = map.map(map.handle, "http://lv2plug.in/ns/ext/atom#Float");
  1261. LilvNode* num = lilv_new_int(world, 5);
  1262. LilvState* nostate = lilv_state_new_from_file(world, &map, num, "/junk");
  1263. TEST_ASSERT(!nostate);
  1264. LilvInstance* instance = lilv_plugin_instantiate(plugin, 48000.0, features);
  1265. TEST_ASSERT(instance);
  1266. lilv_instance_activate(instance);
  1267. lilv_instance_connect_port(instance, 0, &in);
  1268. lilv_instance_connect_port(instance, 1, &out);
  1269. lilv_instance_run(instance, 1);
  1270. TEST_ASSERT(in == 1.0);
  1271. TEST_ASSERT(out == 1.0);
  1272. temp_dir = lilv_realpath("temp");
  1273. const char* file_dir = NULL;
  1274. char* copy_dir = NULL;
  1275. char* link_dir = NULL;
  1276. char* save_dir = NULL;
  1277. // Get instance state state
  1278. LilvState* state = lilv_state_new_from_instance(
  1279. plugin, instance, &map,
  1280. file_dir, copy_dir, link_dir, save_dir,
  1281. get_port_value, world, 0, NULL);
  1282. // Get another instance state
  1283. LilvState* state2 = lilv_state_new_from_instance(
  1284. plugin, instance, &map,
  1285. file_dir, copy_dir, link_dir, save_dir,
  1286. get_port_value, world, 0, NULL);
  1287. // Ensure they are equal
  1288. TEST_ASSERT(lilv_state_equals(state, state2));
  1289. // Check that we can't save a state with no URI
  1290. char* bad_state_str = lilv_state_to_string(
  1291. world, &map, &unmap, state, NULL, NULL);
  1292. TEST_ASSERT(!bad_state_str);
  1293. // Check that we can't restore the NULL string (and it doesn't crash)
  1294. LilvState* bad_state = lilv_state_new_from_string(world, &map, NULL);
  1295. TEST_ASSERT(!bad_state);
  1296. // Save state to a string
  1297. char* state1_str = lilv_state_to_string(
  1298. world, &map, &unmap, state, "http://example.org/state1", NULL);
  1299. // Restore from string
  1300. LilvState* from_str = lilv_state_new_from_string(world, &map, state1_str);
  1301. // Ensure they are equal
  1302. TEST_ASSERT(lilv_state_equals(state, from_str));
  1303. free(state1_str);
  1304. const LilvNode* state_plugin_uri = lilv_state_get_plugin_uri(state);
  1305. TEST_ASSERT(lilv_node_equals(state_plugin_uri, plugin_uri));
  1306. // Tinker with the label of the first state
  1307. TEST_ASSERT(lilv_state_get_label(state) == NULL);
  1308. lilv_state_set_label(state, "Test State Old Label");
  1309. TEST_ASSERT(!strcmp(lilv_state_get_label(state), "Test State Old Label"));
  1310. lilv_state_set_label(state, "Test State");
  1311. TEST_ASSERT(!strcmp(lilv_state_get_label(state), "Test State"));
  1312. TEST_ASSERT(!lilv_state_equals(state, state2)); // Label changed
  1313. // Run and get a new instance state (which should now differ)
  1314. lilv_instance_run(instance, 1);
  1315. LilvState* state3 = lilv_state_new_from_instance(
  1316. plugin, instance, &map,
  1317. file_dir, copy_dir, link_dir, save_dir,
  1318. get_port_value, world, 0, NULL);
  1319. TEST_ASSERT(!lilv_state_equals(state2, state3)); // num_runs changed
  1320. // Restore instance state to original state
  1321. lilv_state_restore(state2, instance, set_port_value, NULL, 0, NULL);
  1322. // Take a new snapshot and ensure it matches the set state
  1323. LilvState* state4 = lilv_state_new_from_instance(
  1324. plugin, instance, &map,
  1325. file_dir, copy_dir, link_dir, save_dir,
  1326. get_port_value, world, 0, NULL);
  1327. TEST_ASSERT(lilv_state_equals(state2, state4));
  1328. // Save state to a directory
  1329. int ret = lilv_state_save(world, &map, &unmap, state, NULL,
  1330. "state/state.lv2", "state.ttl");
  1331. TEST_ASSERT(!ret);
  1332. // Load state from directory
  1333. LilvState* state5 = lilv_state_new_from_file(world, &map, NULL,
  1334. "state/state.lv2/state.ttl");
  1335. TEST_ASSERT(lilv_state_equals(state, state5)); // Round trip accuracy
  1336. // Save state with URI to a directory
  1337. const char* state_uri = "http://example.org/state";
  1338. ret = lilv_state_save(world, &map, &unmap, state, state_uri,
  1339. "state/state6.lv2", "state6.ttl");
  1340. TEST_ASSERT(!ret);
  1341. // Load default bundle into world and load state from it
  1342. uint8_t* state6_path = (uint8_t*)lilv_path_absolute("state/state6.lv2/");
  1343. SerdNode state6_uri = serd_node_new_file_uri(state6_path, 0, 0, true);
  1344. LilvNode* test_state_bundle = lilv_new_uri(world, (const char*)state6_uri.buf);
  1345. LilvNode* test_state_node = lilv_new_uri(world, state_uri);
  1346. lilv_world_load_bundle(world, test_state_bundle);
  1347. lilv_world_load_resource(world, test_state_node);
  1348. serd_node_free(&state6_uri);
  1349. free(state6_path);
  1350. LilvState* state6 = lilv_state_new_from_world(world, &map, test_state_node);
  1351. TEST_ASSERT(lilv_state_equals(state, state6)); // Round trip accuracy
  1352. lilv_world_unload_resource(world, test_state_node);
  1353. lilv_world_unload_bundle(world, test_state_bundle);
  1354. LilvState* state6_2 = lilv_state_new_from_world(world, &map, test_state_node);
  1355. TEST_ASSERT(!state6_2); // No longer present
  1356. lilv_state_free(state6_2);
  1357. lilv_node_free(test_state_bundle);
  1358. lilv_node_free(test_state_node);
  1359. unsetenv("LV2_STATE_BUNDLE");
  1360. // Make directories and test files support
  1361. mkdir("temp", 0700);
  1362. file_dir = temp_dir;
  1363. mkdir("files", 0700);
  1364. copy_dir = lilv_realpath("files");
  1365. mkdir("links", 0700);
  1366. link_dir = lilv_realpath("links");
  1367. LV2_State_Make_Path make_path = { NULL, lilv_make_path };
  1368. LV2_Feature make_path_feature = { LV2_STATE__makePath, &make_path };
  1369. const LV2_Feature* ffeatures[] = { &make_path_feature, &map_feature, NULL };
  1370. lilv_instance_deactivate(instance);
  1371. lilv_instance_free(instance);
  1372. instance = lilv_plugin_instantiate(plugin, 48000.0, ffeatures);
  1373. lilv_instance_activate(instance);
  1374. lilv_instance_connect_port(instance, 0, &in);
  1375. lilv_instance_connect_port(instance, 1, &out);
  1376. lilv_instance_run(instance, 1);
  1377. // Get instance state state
  1378. LilvState* fstate = lilv_state_new_from_instance(
  1379. plugin, instance, &map,
  1380. file_dir, copy_dir, link_dir, "state/fstate.lv2",
  1381. get_port_value, world, 0, ffeatures);
  1382. // Get another instance state
  1383. LilvState* fstate2 = lilv_state_new_from_instance(
  1384. plugin, instance, &map,
  1385. file_dir, copy_dir, link_dir, "state/fstate2.lv2",
  1386. get_port_value, world, 0, ffeatures);
  1387. // Should be identical
  1388. TEST_ASSERT(lilv_state_equals(fstate, fstate2));
  1389. // Run, writing more to rec file
  1390. lilv_instance_run(instance, 2);
  1391. // Get yet another instance state
  1392. LilvState* fstate3 = lilv_state_new_from_instance(
  1393. plugin, instance, &map, file_dir, copy_dir, link_dir, "state/fstate3.lv2",
  1394. get_port_value, world, 0, ffeatures);
  1395. // Should be different
  1396. TEST_ASSERT(!lilv_state_equals(fstate, fstate3));
  1397. // Save state to a directory
  1398. ret = lilv_state_save(world, &map, &unmap, fstate, NULL,
  1399. "state/fstate.lv2", "fstate.ttl");
  1400. TEST_ASSERT(!ret);
  1401. // Load state from directory
  1402. LilvState* fstate4 = lilv_state_new_from_file(world, &map, NULL,
  1403. "state/fstate.lv2/fstate.ttl");
  1404. TEST_ASSERT(lilv_state_equals(fstate, fstate4)); // Round trip accuracy
  1405. // Restore instance state to loaded state
  1406. lilv_state_restore(fstate4, instance, set_port_value, NULL, 0, ffeatures);
  1407. // Take a new snapshot and ensure it matches
  1408. LilvState* fstate5 = lilv_state_new_from_instance(
  1409. plugin, instance, &map,
  1410. file_dir, copy_dir, link_dir, "state/fstate5.lv2",
  1411. get_port_value, world, 0, ffeatures);
  1412. TEST_ASSERT(lilv_state_equals(fstate3, fstate5));
  1413. // Save state to a (different) directory again
  1414. ret = lilv_state_save(world, &map, &unmap, fstate, NULL,
  1415. "state/fstate6.lv2", "fstate6.ttl");
  1416. TEST_ASSERT(!ret);
  1417. // Reload it and ensure it's identical to the other loaded version
  1418. LilvState* fstate6 = lilv_state_new_from_file(world, &map, NULL,
  1419. "state/fstate6.lv2/fstate6.ttl");
  1420. TEST_ASSERT(lilv_state_equals(fstate4, fstate6));
  1421. // Run, changing rec file (without changing size)
  1422. lilv_instance_run(instance, 3);
  1423. // Take a new snapshot
  1424. LilvState* fstate7 = lilv_state_new_from_instance(
  1425. plugin, instance, &map,
  1426. file_dir, copy_dir, link_dir, "state/fstate7.lv2",
  1427. get_port_value, world, 0, ffeatures);
  1428. TEST_ASSERT(!lilv_state_equals(fstate6, fstate7));
  1429. // Save the changed state to a (different) directory again
  1430. ret = lilv_state_save(world, &map, &unmap, fstate7, NULL,
  1431. "state/fstate7.lv2", "fstate7.ttl");
  1432. TEST_ASSERT(!ret);
  1433. // Reload it and ensure it's changed
  1434. LilvState* fstate72 = lilv_state_new_from_file(world, &map, NULL,
  1435. "state/fstate7.lv2/fstate7.ttl");
  1436. TEST_ASSERT(lilv_state_equals(fstate72, fstate7));
  1437. TEST_ASSERT(!lilv_state_equals(fstate6, fstate72));
  1438. lilv_instance_deactivate(instance);
  1439. lilv_instance_free(instance);
  1440. lilv_node_free(num);
  1441. lilv_state_free(state);
  1442. lilv_state_free(from_str);
  1443. lilv_state_free(state2);
  1444. lilv_state_free(state3);
  1445. lilv_state_free(state4);
  1446. lilv_state_free(state5);
  1447. lilv_state_free(state6);
  1448. lilv_state_free(fstate);
  1449. lilv_state_free(fstate2);
  1450. lilv_state_free(fstate3);
  1451. lilv_state_free(fstate4);
  1452. lilv_state_free(fstate5);
  1453. lilv_state_free(fstate6);
  1454. lilv_state_free(fstate7);
  1455. lilv_state_free(fstate72);
  1456. // Free URI map
  1457. for (size_t i = 0; i < n_uris; ++i) {
  1458. free(uris[i]);
  1459. }
  1460. free(uris);
  1461. n_uris = 0;
  1462. lilv_node_free(plugin_uri);
  1463. lilv_node_free(bundle_uri);
  1464. free(link_dir);
  1465. free(copy_dir);
  1466. free(temp_dir);
  1467. cleanup_uris();
  1468. return 1;
  1469. }
  1470. /*****************************************************************************/
  1471. static int
  1472. test_bad_port_symbol(void)
  1473. {
  1474. if (!start_bundle(MANIFEST_PREFIXES
  1475. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  1476. BUNDLE_PREFIXES PREFIX_LV2EV
  1477. ":plug a lv2:Plugin ; "
  1478. PLUGIN_NAME("Test plugin") " ; "
  1479. LICENSE_GPL " ; "
  1480. "doap:homepage <http://example.org/someplug> ; "
  1481. "lv2:port [ "
  1482. " a lv2:ControlPort ; a lv2:InputPort ; "
  1483. " lv2:index 0 ; lv2:symbol \"0invalid\" ;"
  1484. " lv2:name \"Invalid\" ; "
  1485. "] ."))
  1486. return 0;
  1487. init_uris();
  1488. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  1489. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  1490. uint32_t n_ports = lilv_plugin_get_num_ports(plug);
  1491. TEST_ASSERT(n_ports == 0);
  1492. cleanup_uris();
  1493. return 1;
  1494. }
  1495. /*****************************************************************************/
  1496. static int
  1497. test_bad_port_index(void)
  1498. {
  1499. if (!start_bundle(MANIFEST_PREFIXES
  1500. ":plug a lv2:Plugin ; lv2:binary <foo" SHLIB_EXT "> ; rdfs:seeAlso <plugin.ttl> .\n",
  1501. BUNDLE_PREFIXES PREFIX_LV2EV
  1502. ":plug a lv2:Plugin ; "
  1503. PLUGIN_NAME("Test plugin") " ; "
  1504. LICENSE_GPL " ; "
  1505. "doap:homepage <http://example.org/someplug> ; "
  1506. "lv2:port [ "
  1507. " a lv2:ControlPort ; a lv2:InputPort ; "
  1508. " lv2:index \"notaninteger\" ; lv2:symbol \"invalid\" ;"
  1509. " lv2:name \"Invalid\" ; "
  1510. "] ."))
  1511. return 0;
  1512. init_uris();
  1513. const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  1514. const LilvPlugin* plug = lilv_plugins_get_by_uri(plugins, plugin_uri_value);
  1515. uint32_t n_ports = lilv_plugin_get_num_ports(plug);
  1516. TEST_ASSERT(n_ports == 0);
  1517. cleanup_uris();
  1518. return 1;
  1519. }
  1520. /*****************************************************************************/
  1521. static int
  1522. test_string(void)
  1523. {
  1524. char* s = NULL;
  1525. TEST_ASSERT(!strcmp((s = lilv_dirname("/foo/bar")), "/foo")); free(s);
  1526. TEST_ASSERT(!strcmp((s = lilv_dirname("/foo/bar/")), "/foo")); free(s);
  1527. TEST_ASSERT(!strcmp((s = lilv_dirname("/foo///bar/")), "/foo")); free(s);
  1528. TEST_ASSERT(!strcmp((s = lilv_dirname("/foo///bar//")), "/foo")); free(s);
  1529. TEST_ASSERT(!strcmp((s = lilv_dirname("foo")), ".")); free(s);
  1530. TEST_ASSERT(!strcmp((s = lilv_dirname("/foo")), "/")); free(s);
  1531. TEST_ASSERT(!strcmp((s = lilv_dirname("/")), "/")); free(s);
  1532. TEST_ASSERT(!strcmp((s = lilv_dirname("//")), "/")); free(s);
  1533. TEST_ASSERT(!strcmp((s = lilv_path_relative_to("/a/b", "/a/")), "b")); free(s);
  1534. TEST_ASSERT(!strcmp((s = lilv_path_relative_to("/a", "/b/c/")), "/a")); free(s);
  1535. TEST_ASSERT(!strcmp((s = lilv_path_relative_to("/a/b/c", "/a/b/d/")), "../c")); free(s);
  1536. TEST_ASSERT(!strcmp((s = lilv_path_relative_to("/a/b/c", "/a/b/d/e/")), "../../c")); free(s);
  1537. TEST_ASSERT(!strcmp((s = lilv_path_join("/a", "b")), "/a/b")); free(s);
  1538. TEST_ASSERT(!strcmp((s = lilv_path_join("/a", "/b")), "/a/b")); free(s);
  1539. TEST_ASSERT(!strcmp((s = lilv_path_join("/a/", "/b")), "/a/b")); free(s);
  1540. TEST_ASSERT(!strcmp((s = lilv_path_join("/a/", "b")), "/a/b")); free(s);
  1541. TEST_ASSERT(!strcmp((s = lilv_path_join("/a", NULL)), "/a/")); free(s);
  1542. #ifndef _WIN32
  1543. setenv("LILV_TEST_1", "test", 1);
  1544. char* home_foo = lilv_strjoin(getenv("HOME"), "/foo", NULL);
  1545. TEST_ASSERT(!strcmp((s = lilv_expand("$LILV_TEST_1")), "test")); free(s);
  1546. TEST_ASSERT(!strcmp((s = lilv_expand("~")), getenv("HOME"))); free(s);
  1547. TEST_ASSERT(!strcmp((s = lilv_expand("~foo")), "~foo")); free(s);
  1548. TEST_ASSERT(!strcmp((s = lilv_expand("~/foo")), home_foo)); free(s);
  1549. TEST_ASSERT(!strcmp((s = lilv_expand("$NOT_A_VAR")), "$NOT_A_VAR")); free(s);
  1550. free(home_foo);
  1551. unsetenv("LILV_TEST_1");
  1552. #endif
  1553. return 1;
  1554. }
  1555. /*****************************************************************************/
  1556. /* add tests here */
  1557. static struct TestCase tests[] = {
  1558. TEST_CASE(utils),
  1559. TEST_CASE(value),
  1560. TEST_CASE(verify),
  1561. TEST_CASE(no_verify),
  1562. TEST_CASE(discovery),
  1563. TEST_CASE(lv2_path),
  1564. TEST_CASE(classes),
  1565. TEST_CASE(plugin),
  1566. TEST_CASE(project),
  1567. TEST_CASE(no_author),
  1568. TEST_CASE(project_no_author),
  1569. TEST_CASE(preset),
  1570. TEST_CASE(prototype),
  1571. TEST_CASE(port),
  1572. TEST_CASE(ui),
  1573. TEST_CASE(bad_port_symbol),
  1574. TEST_CASE(bad_port_index),
  1575. TEST_CASE(bad_port_index),
  1576. TEST_CASE(string),
  1577. TEST_CASE(state),
  1578. { NULL, NULL }
  1579. };
  1580. static void
  1581. run_tests(void)
  1582. {
  1583. int i;
  1584. for (i = 0; tests[i].title; i++) {
  1585. printf("*** Test %s\n", tests[i].title);
  1586. if (!tests[i].func()) {
  1587. printf("\nTest failed\n");
  1588. /* test case that wasn't able to be executed at all counts as 1 test + 1 error */
  1589. error_count++;
  1590. test_count++;
  1591. }
  1592. unload_bundle();
  1593. cleanup();
  1594. }
  1595. }
  1596. int
  1597. main(int argc, char* argv[])
  1598. {
  1599. if (argc != 1) {
  1600. printf("Syntax: %s\n", argv[0]);
  1601. return 0;
  1602. }
  1603. setenv("LANG", "C", 1);
  1604. init_tests();
  1605. run_tests();
  1606. cleanup();
  1607. printf("\n*** Test Results: %d tests, %d errors\n\n", test_count, error_count);
  1608. return error_count ? 1 : 0;
  1609. }