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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. Copyright 2012-2014 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(bool top_level)
  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_object_mode(
  74. sratom,
  75. top_level ? SRATOM_OBJECT_MODE_BLANK_SUBJECT : SRATOM_OBJECT_MODE_BLANK);
  76. LV2_URID eg_Object = urid_map(NULL, "http://example.org/Object");
  77. LV2_URID eg_one = urid_map(NULL, "http://example.org/a-one");
  78. LV2_URID eg_two = urid_map(NULL, "http://example.org/b-two");
  79. LV2_URID eg_three = urid_map(NULL, "http://example.org/c-three");
  80. LV2_URID eg_four = urid_map(NULL, "http://example.org/d-four");
  81. LV2_URID eg_true = urid_map(NULL, "http://example.org/e-true");
  82. LV2_URID eg_false = urid_map(NULL, "http://example.org/f-false");
  83. LV2_URID eg_path = urid_map(NULL, "http://example.org/g-path");
  84. LV2_URID eg_urid = urid_map(NULL, "http://example.org/h-urid");
  85. LV2_URID eg_string = urid_map(NULL, "http://example.org/i-string");
  86. LV2_URID eg_langlit = urid_map(NULL, "http://example.org/j-langlit");
  87. LV2_URID eg_typelit = urid_map(NULL, "http://example.org/k-typelit");
  88. LV2_URID eg_null = urid_map(NULL, "http://example.org/l-null");
  89. LV2_URID eg_chunk = urid_map(NULL, "http://example.org/m-chunk");
  90. LV2_URID eg_blob = urid_map(NULL, "http://example.org/n-blob");
  91. LV2_URID eg_blank = urid_map(NULL, "http://example.org/o-blank");
  92. LV2_URID eg_tuple = urid_map(NULL, "http://example.org/p-tuple");
  93. LV2_URID eg_rectup = urid_map(NULL, "http://example.org/q-rectup");
  94. LV2_URID eg_vector = urid_map(NULL, "http://example.org/r-vector");
  95. LV2_URID eg_seq = urid_map(NULL, "http://example.org/s-seq");
  96. uint8_t buf[1024];
  97. lv2_atom_forge_set_buffer(&forge, buf, sizeof(buf));
  98. const char* obj_uri = "http://example.org/obj";
  99. LV2_URID obj_id = urid_map(NULL, obj_uri);
  100. LV2_Atom_Forge_Frame obj_frame;
  101. if (top_level) {
  102. lv2_atom_forge_object(&forge, &obj_frame, obj_id, eg_Object);
  103. } else {
  104. lv2_atom_forge_object(&forge, &obj_frame, 0, eg_Object);
  105. }
  106. LV2_Atom* obj = lv2_atom_forge_deref(&forge, obj_frame.ref);
  107. // eg_one = (Int32)1
  108. lv2_atom_forge_key(&forge, eg_one);
  109. lv2_atom_forge_int(&forge, 1);
  110. // eg_two = (Int64)2
  111. lv2_atom_forge_key(&forge, eg_two);
  112. lv2_atom_forge_long(&forge, 2);
  113. // eg_three = (Float)3.0
  114. lv2_atom_forge_key(&forge, eg_three);
  115. lv2_atom_forge_float(&forge, 3.0f);
  116. // eg_four = (Double)4.0
  117. lv2_atom_forge_key(&forge, eg_four);
  118. lv2_atom_forge_double(&forge, 4.0);
  119. // eg_true = (Bool)1
  120. lv2_atom_forge_key(&forge, eg_true);
  121. lv2_atom_forge_bool(&forge, true);
  122. // eg_false = (Bool)0
  123. lv2_atom_forge_key(&forge, eg_false);
  124. lv2_atom_forge_bool(&forge, false);
  125. // eg_path = (Path)"/absolute/path"
  126. const char* pstr = "/absolute/path";
  127. const size_t pstr_len = strlen(pstr);
  128. lv2_atom_forge_key(&forge, eg_path);
  129. lv2_atom_forge_path(&forge, pstr, pstr_len);
  130. // eg_urid = (URID)"http://example.org/value"
  131. LV2_URID eg_value = urid_map(NULL, "http://example.org/value");
  132. lv2_atom_forge_key(&forge, eg_urid);
  133. lv2_atom_forge_urid(&forge, eg_value);
  134. // eg_string = (String)"hello"
  135. lv2_atom_forge_key(&forge, eg_string);
  136. lv2_atom_forge_string(&forge, "hello", strlen("hello"));
  137. // eg_langlit = (Literal)"bonjour"@fr
  138. lv2_atom_forge_key(&forge, eg_langlit);
  139. lv2_atom_forge_literal(
  140. &forge, "bonjour", strlen("bonjour"),
  141. 0, urid_map(NULL, "http://lexvo.org/id/iso639-3/fra"));
  142. // eg_typelit = (Literal)"bonjour"@fr
  143. lv2_atom_forge_key(&forge, eg_typelit);
  144. lv2_atom_forge_literal(
  145. &forge, "value", strlen("value"),
  146. urid_map(NULL, "http://example.org/Type"), 0);
  147. // eg_null = null
  148. lv2_atom_forge_key(&forge, eg_null);
  149. lv2_atom_forge_atom(&forge, 0, 0);
  150. // eg_chunk = 0xBEEFDEAD
  151. uint8_t chunk_buf[] = { 0xBE, 0xEF, 0xDE, 0xAD };
  152. lv2_atom_forge_key(&forge, eg_chunk);
  153. lv2_atom_forge_atom(&forge, sizeof(chunk_buf), forge.Chunk);
  154. lv2_atom_forge_write(&forge, chunk_buf, sizeof(chunk_buf));
  155. // eg_blob = 0xDEADBEEF
  156. uint32_t blob_type = map.map(map.handle, "http://example.org/Blob");
  157. uint8_t blob_buf[] = { 0xDE, 0xAD, 0xBE, 0xEF };
  158. lv2_atom_forge_key(&forge, eg_blob);
  159. lv2_atom_forge_atom(&forge, sizeof(blob_buf), blob_type);
  160. lv2_atom_forge_write(&forge, blob_buf, sizeof(blob_buf));
  161. // eg_blank = [ a <http://example.org/Object> ]
  162. lv2_atom_forge_key(&forge, eg_blank);
  163. LV2_Atom_Forge_Frame blank_frame;
  164. lv2_atom_forge_object(&forge, &blank_frame, 0, eg_Object);
  165. lv2_atom_forge_pop(&forge, &blank_frame);
  166. // eg_tuple = "foo",true
  167. lv2_atom_forge_key(&forge, eg_tuple);
  168. LV2_Atom_Forge_Frame tuple_frame;
  169. lv2_atom_forge_tuple(&forge, &tuple_frame);
  170. lv2_atom_forge_string(&forge, "foo", strlen("foo"));
  171. lv2_atom_forge_bool(&forge, true);
  172. lv2_atom_forge_pop(&forge, &tuple_frame);
  173. // eg_rectup = "foo",true,("bar",false)
  174. lv2_atom_forge_key(&forge, eg_rectup);
  175. LV2_Atom_Forge_Frame rectup_frame;
  176. lv2_atom_forge_tuple(&forge, &rectup_frame);
  177. lv2_atom_forge_string(&forge, "foo", strlen("foo"));
  178. lv2_atom_forge_bool(&forge, true);
  179. LV2_Atom_Forge_Frame subrectup_frame;
  180. lv2_atom_forge_tuple(&forge, &subrectup_frame);
  181. lv2_atom_forge_string(&forge, "bar", strlen("bar"));
  182. lv2_atom_forge_bool(&forge, false);
  183. lv2_atom_forge_pop(&forge, &subrectup_frame);
  184. lv2_atom_forge_pop(&forge, &rectup_frame);
  185. // eg_vector = (Vector<Int32>)1,2,3,4
  186. lv2_atom_forge_key(&forge, eg_vector);
  187. int32_t elems[] = { 1, 2, 3, 4 };
  188. lv2_atom_forge_vector(&forge, 4, forge.Int, sizeof(int32_t), elems);
  189. // eg_seq = (Sequence)1, 2
  190. LV2_URID midi_midiEvent = map.map(map.handle, LV2_MIDI__MidiEvent);
  191. lv2_atom_forge_key(&forge, eg_seq);
  192. LV2_Atom_Forge_Frame seq_frame;
  193. lv2_atom_forge_sequence_head(&forge, &seq_frame, 0);
  194. const uint8_t ev1[3] = { 0x90, 0x1A, 0x1 };
  195. lv2_atom_forge_frame_time(&forge, 1);
  196. lv2_atom_forge_atom(&forge, sizeof(ev1), midi_midiEvent);
  197. lv2_atom_forge_raw(&forge, ev1, sizeof(ev1));
  198. lv2_atom_forge_pad(&forge, sizeof(ev1));
  199. const uint8_t ev2[3] = { 0x90, 0x2B, 0x2 };
  200. lv2_atom_forge_frame_time(&forge, 3);
  201. lv2_atom_forge_atom(&forge, sizeof(ev2), midi_midiEvent);
  202. lv2_atom_forge_raw(&forge, ev2, sizeof(ev2));
  203. lv2_atom_forge_pad(&forge, sizeof(ev2));
  204. lv2_atom_forge_pop(&forge, &seq_frame);
  205. lv2_atom_forge_pop(&forge, &obj_frame);
  206. const char* base_uri = "file:///tmp/base/";
  207. SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/obj"));
  208. SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_RDF "value"));
  209. SerdNode* subj = top_level ? NULL : &s;
  210. SerdNode* pred = top_level ? NULL : &p;
  211. char* outstr = sratom_to_turtle(
  212. sratom, &unmap, base_uri, subj, pred,
  213. obj->type, obj->size, LV2_ATOM_BODY(obj));
  214. printf("# Atom => Turtle\n\n%s", outstr);
  215. LV2_Atom* parsed = NULL;
  216. if (top_level) {
  217. SerdNode o= serd_node_from_string(SERD_URI, (const uint8_t*)obj_uri);
  218. parsed = sratom_from_turtle(sratom, base_uri, &o, NULL, outstr);
  219. } else {
  220. parsed = sratom_from_turtle(sratom, base_uri, subj, pred, outstr);
  221. }
  222. if (!lv2_atom_equals(obj, parsed)) {
  223. return test_fail("Parsed atom does not match original\n");
  224. }
  225. char* instr = sratom_to_turtle(
  226. sratom, &unmap, base_uri, subj, pred,
  227. parsed->type, parsed->size, LV2_ATOM_BODY(parsed));
  228. printf("# Turtle => Atom\n\n%s", instr);
  229. if (strcmp(outstr, instr)) {
  230. return test_fail("Re-serialised string differs from original\n");
  231. }
  232. printf("All tests passed.\n");
  233. free(parsed);
  234. free(instr);
  235. free(outstr);
  236. sratom_free(sratom);
  237. for (uint32_t i = 0; i < n_uris; ++i) {
  238. free(uris[i]);
  239. }
  240. free(uris);
  241. uris = NULL;
  242. n_uris = 0;
  243. return 0;
  244. }
  245. int
  246. main(void)
  247. {
  248. if (test(false)) {
  249. return 1;
  250. } else if (test(true)) {
  251. return 1;
  252. }
  253. return 0;
  254. }