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.

lilv_test.c 52KB

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