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.

sratom_test.c 12KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. Copyright 2012-2016 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include <ctype.h>
  15. #include <stdarg.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
  19. #include "lv2/lv2plug.in/ns/ext/atom/util.h"
  20. #include "lv2/lv2plug.in/ns/ext/midi/midi.h"
  21. #include "sratom/sratom.h"
  22. #define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  23. #define USTR(s) ((const uint8_t*)(s))
  24. static char** uris = NULL;
  25. static size_t n_uris = 0;
  26. static char*
  27. copy_string(const char* str)
  28. {
  29. const size_t len = strlen(str);
  30. char* dup = (char*)malloc(len + 1);
  31. memcpy(dup, str, len + 1);
  32. return dup;
  33. }
  34. static LV2_URID
  35. urid_map(LV2_URID_Map_Handle handle, const char* uri)
  36. {
  37. for (size_t i = 0; i < n_uris; ++i) {
  38. if (!strcmp(uris[i], uri)) {
  39. return i + 1;
  40. }
  41. }
  42. uris = (char**)realloc(uris, ++n_uris * sizeof(char*));
  43. uris[n_uris - 1] = copy_string(uri);
  44. return n_uris;
  45. }
  46. static const char*
  47. urid_unmap(LV2_URID_Unmap_Handle handle,
  48. LV2_URID urid)
  49. {
  50. if (urid > 0 && urid <= n_uris) {
  51. return uris[urid - 1];
  52. }
  53. return NULL;
  54. }
  55. static int
  56. test_fail(const char* fmt, ...)
  57. {
  58. va_list args;
  59. va_start(args, fmt);
  60. fprintf(stderr, "error: ");
  61. vfprintf(stderr, fmt, args);
  62. va_end(args);
  63. return 1;
  64. }
  65. static int
  66. test(SerdEnv* env, bool top_level, bool pretty_numbers)
  67. {
  68. LV2_URID_Map map = { NULL, urid_map };
  69. LV2_URID_Unmap unmap = { NULL, urid_unmap };
  70. LV2_Atom_Forge forge;
  71. lv2_atom_forge_init(&forge, &map);
  72. Sratom* sratom = sratom_new(&map);
  73. sratom_set_env(sratom, env);
  74. sratom_set_pretty_numbers(sratom, pretty_numbers);
  75. sratom_set_object_mode(
  76. sratom,
  77. top_level ? SRATOM_OBJECT_MODE_BLANK_SUBJECT : SRATOM_OBJECT_MODE_BLANK);
  78. LV2_URID eg_Object = urid_map(NULL, "http://example.org/Object");
  79. LV2_URID eg_one = urid_map(NULL, "http://example.org/a-one");
  80. LV2_URID eg_two = urid_map(NULL, "http://example.org/b-two");
  81. LV2_URID eg_three = urid_map(NULL, "http://example.org/c-three");
  82. LV2_URID eg_four = urid_map(NULL, "http://example.org/d-four");
  83. LV2_URID eg_true = urid_map(NULL, "http://example.org/e-true");
  84. LV2_URID eg_false = urid_map(NULL, "http://example.org/f-false");
  85. LV2_URID eg_path = urid_map(NULL, "http://example.org/g-path");
  86. LV2_URID eg_winpath = urid_map(NULL, "http://example.org/h-winpath");
  87. LV2_URID eg_relpath = urid_map(NULL, "http://example.org/i-relpath");
  88. LV2_URID eg_urid = urid_map(NULL, "http://example.org/j-urid");
  89. LV2_URID eg_string = urid_map(NULL, "http://example.org/k-string");
  90. LV2_URID eg_langlit = urid_map(NULL, "http://example.org/l-langlit");
  91. LV2_URID eg_typelit = urid_map(NULL, "http://example.org/m-typelit");
  92. LV2_URID eg_null = urid_map(NULL, "http://example.org/n-null");
  93. LV2_URID eg_chunk = urid_map(NULL, "http://example.org/o-chunk");
  94. LV2_URID eg_blob = urid_map(NULL, "http://example.org/p-blob");
  95. LV2_URID eg_blank = urid_map(NULL, "http://example.org/q-blank");
  96. LV2_URID eg_tuple = urid_map(NULL, "http://example.org/r-tuple");
  97. LV2_URID eg_rectup = urid_map(NULL, "http://example.org/s-rectup");
  98. LV2_URID eg_ivector = urid_map(NULL, "http://example.org/t-ivector");
  99. LV2_URID eg_lvector = urid_map(NULL, "http://example.org/u-lvector");
  100. LV2_URID eg_fvector = urid_map(NULL, "http://example.org/v-fvector");
  101. LV2_URID eg_dvector = urid_map(NULL, "http://example.org/w-dvector");
  102. LV2_URID eg_bvector = urid_map(NULL, "http://example.org/x-bvector");
  103. LV2_URID eg_fseq = urid_map(NULL, "http://example.org/y-fseq");
  104. LV2_URID eg_bseq = urid_map(NULL, "http://example.org/z-bseq");
  105. uint8_t buf[1024];
  106. lv2_atom_forge_set_buffer(&forge, buf, sizeof(buf));
  107. const char* obj_uri = "http://example.org/obj";
  108. LV2_URID obj_id = urid_map(NULL, obj_uri);
  109. LV2_Atom_Forge_Frame obj_frame;
  110. if (top_level) {
  111. lv2_atom_forge_object(&forge, &obj_frame, obj_id, eg_Object);
  112. } else {
  113. lv2_atom_forge_object(&forge, &obj_frame, 0, eg_Object);
  114. }
  115. LV2_Atom* obj = lv2_atom_forge_deref(&forge, obj_frame.ref);
  116. // eg_one = (Int32)1
  117. lv2_atom_forge_key(&forge, eg_one);
  118. lv2_atom_forge_int(&forge, 1);
  119. // eg_two = (Int64)2
  120. lv2_atom_forge_key(&forge, eg_two);
  121. lv2_atom_forge_long(&forge, 2);
  122. // eg_three = (Float)3.0
  123. lv2_atom_forge_key(&forge, eg_three);
  124. lv2_atom_forge_float(&forge, 3.0f);
  125. // eg_four = (Double)4.0
  126. lv2_atom_forge_key(&forge, eg_four);
  127. lv2_atom_forge_double(&forge, 4.0);
  128. // eg_true = (Bool)1
  129. lv2_atom_forge_key(&forge, eg_true);
  130. lv2_atom_forge_bool(&forge, true);
  131. // eg_false = (Bool)0
  132. lv2_atom_forge_key(&forge, eg_false);
  133. lv2_atom_forge_bool(&forge, false);
  134. // eg_path = (Path)"/absolute/path"
  135. const char* pstr = "/absolute/path";
  136. const size_t pstr_len = strlen(pstr);
  137. lv2_atom_forge_key(&forge, eg_path);
  138. lv2_atom_forge_path(&forge, pstr, pstr_len);
  139. // eg_winpath = (Path)"C:\Stupid\File System"
  140. const char* wpstr = "C:/Stupid/File System";
  141. const size_t wpstr_len = strlen(wpstr);
  142. lv2_atom_forge_key(&forge, eg_winpath);
  143. lv2_atom_forge_path(&forge, wpstr, wpstr_len);
  144. // eg_relpath = (Path)"foo/bar"
  145. const char* rpstr = "foo/bar";
  146. const size_t rpstr_len = strlen(rpstr);
  147. lv2_atom_forge_key(&forge, eg_relpath);
  148. lv2_atom_forge_path(&forge, rpstr, rpstr_len);
  149. // eg_urid = (URID)"http://example.org/value"
  150. LV2_URID eg_value = urid_map(NULL, "http://example.org/value");
  151. lv2_atom_forge_key(&forge, eg_urid);
  152. lv2_atom_forge_urid(&forge, eg_value);
  153. // eg_string = (String)"hello"
  154. lv2_atom_forge_key(&forge, eg_string);
  155. lv2_atom_forge_string(&forge, "hello", strlen("hello"));
  156. // eg_langlit = (Literal)"ni hao"@cmn (but in actual mandarin)
  157. const uint8_t ni_hao[] = { 0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD };
  158. lv2_atom_forge_key(&forge, eg_langlit);
  159. lv2_atom_forge_literal(
  160. &forge, (const char*)ni_hao, 6,
  161. 0, urid_map(NULL, "http://lexvo.org/id/iso639-3/cmn"));
  162. // eg_typelit = (Literal)"value"^^<http://example.org/Type>
  163. lv2_atom_forge_key(&forge, eg_typelit);
  164. lv2_atom_forge_literal(
  165. &forge, "value", strlen("value"),
  166. urid_map(NULL, "http://example.org/Type"), 0);
  167. // eg_null = null
  168. lv2_atom_forge_key(&forge, eg_null);
  169. lv2_atom_forge_atom(&forge, 0, 0);
  170. // eg_chunk = 0xBEEFDEAD
  171. uint8_t chunk_buf[] = { 0xBE, 0xEF, 0xDE, 0xAD };
  172. lv2_atom_forge_key(&forge, eg_chunk);
  173. lv2_atom_forge_atom(&forge, sizeof(chunk_buf), forge.Chunk);
  174. lv2_atom_forge_write(&forge, chunk_buf, sizeof(chunk_buf));
  175. // eg_blob = 0xDEADBEEF
  176. uint32_t blob_type = map.map(map.handle, "http://example.org/Blob");
  177. uint8_t blob_buf[] = { 0xDE, 0xAD, 0xBE, 0xEF };
  178. lv2_atom_forge_key(&forge, eg_blob);
  179. lv2_atom_forge_atom(&forge, sizeof(blob_buf), blob_type);
  180. lv2_atom_forge_write(&forge, blob_buf, sizeof(blob_buf));
  181. // eg_blank = [ a <http://example.org/Object> ]
  182. lv2_atom_forge_key(&forge, eg_blank);
  183. LV2_Atom_Forge_Frame blank_frame;
  184. lv2_atom_forge_object(&forge, &blank_frame, 0, eg_Object);
  185. lv2_atom_forge_pop(&forge, &blank_frame);
  186. // eg_tuple = "foo",true
  187. lv2_atom_forge_key(&forge, eg_tuple);
  188. LV2_Atom_Forge_Frame tuple_frame;
  189. lv2_atom_forge_tuple(&forge, &tuple_frame);
  190. lv2_atom_forge_string(&forge, "foo", strlen("foo"));
  191. lv2_atom_forge_bool(&forge, true);
  192. lv2_atom_forge_pop(&forge, &tuple_frame);
  193. // eg_rectup = "foo",true,("bar",false)
  194. lv2_atom_forge_key(&forge, eg_rectup);
  195. LV2_Atom_Forge_Frame rectup_frame;
  196. lv2_atom_forge_tuple(&forge, &rectup_frame);
  197. lv2_atom_forge_string(&forge, "foo", strlen("foo"));
  198. lv2_atom_forge_bool(&forge, true);
  199. LV2_Atom_Forge_Frame subrectup_frame;
  200. lv2_atom_forge_tuple(&forge, &subrectup_frame);
  201. lv2_atom_forge_string(&forge, "bar", strlen("bar"));
  202. lv2_atom_forge_bool(&forge, false);
  203. lv2_atom_forge_pop(&forge, &subrectup_frame);
  204. lv2_atom_forge_pop(&forge, &rectup_frame);
  205. // eg_ivector = (Vector<Int>)1,2,3,4,5
  206. lv2_atom_forge_key(&forge, eg_ivector);
  207. int32_t ielems[] = { 1, 2, 3, 4, 5 };
  208. lv2_atom_forge_vector(&forge, sizeof(int32_t), forge.Int, 5, ielems);
  209. // eg_lvector = (Vector<Long>)1,2,3,4
  210. lv2_atom_forge_key(&forge, eg_lvector);
  211. int64_t lelems[] = { 1, 2, 3, 4 };
  212. lv2_atom_forge_vector(&forge, sizeof(int64_t), forge.Long, 4, lelems);
  213. // eg_fvector = (Vector<Float>)1.0,2.0,3.0,4.0,5.0
  214. lv2_atom_forge_key(&forge, eg_fvector);
  215. float felems[] = { 1, 2, 3, 4, 5 };
  216. lv2_atom_forge_vector(&forge, sizeof(float), forge.Float, 5, felems);
  217. // eg_dvector = (Vector<Double>)1.0,2.0,3.0,4.0
  218. lv2_atom_forge_key(&forge, eg_dvector);
  219. double delems[] = { 1, 2, 3, 4 };
  220. lv2_atom_forge_vector(&forge, sizeof(double), forge.Double, 4, delems);
  221. // eg_bvector = (Vector<Bool>)1,0,1
  222. lv2_atom_forge_key(&forge, eg_bvector);
  223. int32_t belems[] = { true, false , true };
  224. lv2_atom_forge_vector(&forge, sizeof(int32_t), forge.Bool, 3, belems);
  225. // eg_fseq = (Sequence)1, 2
  226. LV2_URID midi_midiEvent = map.map(map.handle, LV2_MIDI__MidiEvent);
  227. lv2_atom_forge_key(&forge, eg_fseq);
  228. LV2_Atom_Forge_Frame fseq_frame;
  229. lv2_atom_forge_sequence_head(&forge, &fseq_frame, 0);
  230. const uint8_t ev1[3] = { 0x90, 0x1A, 0x1 };
  231. lv2_atom_forge_frame_time(&forge, 1);
  232. lv2_atom_forge_atom(&forge, sizeof(ev1), midi_midiEvent);
  233. lv2_atom_forge_raw(&forge, ev1, sizeof(ev1));
  234. lv2_atom_forge_pad(&forge, sizeof(ev1));
  235. const uint8_t ev2[3] = { 0x90, 0x2B, 0x2 };
  236. lv2_atom_forge_frame_time(&forge, 3);
  237. lv2_atom_forge_atom(&forge, sizeof(ev2), midi_midiEvent);
  238. lv2_atom_forge_raw(&forge, ev2, sizeof(ev2));
  239. lv2_atom_forge_pad(&forge, sizeof(ev2));
  240. lv2_atom_forge_pop(&forge, &fseq_frame);
  241. // eg_bseq = (Sequence)1.1, 2.2
  242. LV2_URID atom_beatTime = map.map(map.handle, LV2_ATOM__beatTime);
  243. lv2_atom_forge_key(&forge, eg_bseq);
  244. LV2_Atom_Forge_Frame bseq_frame;
  245. lv2_atom_forge_sequence_head(&forge, &bseq_frame, atom_beatTime);
  246. lv2_atom_forge_beat_time(&forge, 1.0);
  247. lv2_atom_forge_atom(&forge, sizeof(ev1), midi_midiEvent);
  248. lv2_atom_forge_raw(&forge, ev1, sizeof(ev1));
  249. lv2_atom_forge_pad(&forge, sizeof(ev1));
  250. lv2_atom_forge_beat_time(&forge, 2.0);
  251. lv2_atom_forge_atom(&forge, sizeof(ev2), midi_midiEvent);
  252. lv2_atom_forge_raw(&forge, ev2, sizeof(ev2));
  253. lv2_atom_forge_pad(&forge, sizeof(ev2));
  254. lv2_atom_forge_pop(&forge, &bseq_frame);
  255. lv2_atom_forge_pop(&forge, &obj_frame);
  256. const char* base_uri = "file:///tmp/base/";
  257. SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/obj"));
  258. SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_RDF "value"));
  259. SerdNode* subj = top_level ? NULL : &s;
  260. SerdNode* pred = top_level ? NULL : &p;
  261. char* outstr = sratom_to_turtle(
  262. sratom, &unmap, base_uri, subj, pred,
  263. obj->type, obj->size, LV2_ATOM_BODY(obj));
  264. printf("# Atom => Turtle\n\n%s", outstr);
  265. LV2_Atom* parsed = NULL;
  266. if (top_level) {
  267. SerdNode o = serd_node_from_string(SERD_URI, (const uint8_t*)obj_uri);
  268. parsed = sratom_from_turtle(sratom, base_uri, &o, NULL, outstr);
  269. } else {
  270. parsed = sratom_from_turtle(sratom, base_uri, subj, pred, outstr);
  271. }
  272. if (!pretty_numbers) {
  273. if (!lv2_atom_equals(obj, parsed)) {
  274. return test_fail("Parsed atom does not match original\n");
  275. }
  276. char* instr = sratom_to_turtle(
  277. sratom, &unmap, base_uri, subj, pred,
  278. parsed->type, parsed->size, LV2_ATOM_BODY(parsed));
  279. printf("# Turtle => Atom\n\n%s", instr);
  280. if (strcmp(outstr, instr)) {
  281. return test_fail("Re-serialised string differs from original\n");
  282. }
  283. free(instr);
  284. }
  285. printf("All tests passed.\n");
  286. free(parsed);
  287. free(outstr);
  288. sratom_free(sratom);
  289. for (uint32_t i = 0; i < n_uris; ++i) {
  290. free(uris[i]);
  291. }
  292. free(uris);
  293. uris = NULL;
  294. n_uris = 0;
  295. return 0;
  296. }
  297. static int
  298. test_env(SerdEnv* env)
  299. {
  300. if (test(env, false, false)) {
  301. return 1;
  302. } else if (test(env, true, false)) {
  303. return 1;
  304. } else if (test(env, false, true)) {
  305. return 1;
  306. } else if (test(env, true, true)) {
  307. return 1;
  308. }
  309. return 0;
  310. }
  311. int
  312. main(void)
  313. {
  314. // Test with no environment
  315. if (test_env(NULL)) {
  316. return 1;
  317. }
  318. // Test with a prefix defined
  319. SerdEnv* env = serd_env_new(NULL);
  320. serd_env_set_prefix_from_strings(
  321. env, (const uint8_t*)"eg", (const uint8_t*)"http://example.org/");
  322. test_env(env);
  323. serd_env_free(env);
  324. return 0;
  325. }