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

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