| 1 |
// Copyright 2012-2026 David Robillard <[email protected]> |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "forge_test_object.h" |
| 5 |
#include "test_uri_map.h" |
| 6 |
|
| 7 |
#include <lv2/atom/atom.h> |
| 8 |
#include <lv2/atom/forge.h> |
| 9 |
#include <lv2/atom/util.h> |
| 10 |
#include <lv2/urid/urid.h> |
| 11 |
#include <serd/serd.h> |
| 12 |
#include <sratom/sratom.h> |
| 13 |
|
| 14 |
#include <stdbool.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
| 21 |
|
| 22 |
#define USTR(s) ((const uint8_t*)(s)) |
| 23 |
|
| 24 |
static int |
| 25 |
test_fail(const char* const msg) |
| 26 |
{ |
| 27 |
fprintf(stderr, "error: %s\n", msg); |
| 28 |
return 1; |
| 29 |
} |
| 30 |
|
| 31 |
static int |
| 32 |
test(SerdEnv* env, const char* base_uri, bool top_level, bool pretty_numbers) |
| 33 |
{ |
| 34 |
Uris uris = {NULL, 0}; |
| 35 |
LV2_URID_Map map = {&uris, urid_map}; |
| 36 |
LV2_URID_Unmap unmap = {&uris, urid_unmap}; |
| 37 |
LV2_Atom_Forge forge; |
| 38 |
lv2_atom_forge_init(&forge, &map); |
| 39 |
|
| 40 |
Sratom* sratom = sratom_new(&map); |
| 41 |
sratom_set_env(sratom, env); |
| 42 |
sratom_set_pretty_numbers(sratom, pretty_numbers); |
| 43 |
sratom_set_object_mode(sratom, |
| 44 |
top_level ? SRATOM_OBJECT_MODE_BLANK_SUBJECT |
| 45 |
: SRATOM_OBJECT_MODE_BLANK); |
| 46 |
|
| 47 |
LV2_Atom buf[144]; |
| 48 |
lv2_atom_forge_set_buffer(&forge, (uint8_t*)buf, sizeof(buf)); |
| 49 |
|
| 50 |
const char* const obj_uri = "http://example.org/obj"; |
| 51 |
const LV2_URID obj_id = urid_map(&uris, obj_uri); |
| 52 |
forge_test_object(&forge, &map, &uris, top_level ? obj_id : 0U); |
| 53 |
|
| 54 |
SerdNode s = serd_node_from_string(SERD_URI, USTR("http://example.org/obj")); |
| 55 |
SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_RDF "value")); |
| 56 |
|
| 57 |
const SerdNode* subj = top_level ? NULL : &s; |
| 58 |
const SerdNode* pred = top_level ? NULL : &p; |
| 59 |
|
| 60 |
char* outstr = sratom_to_turtle(sratom, |
| 61 |
&unmap, |
| 62 |
base_uri, |
| 63 |
subj, |
| 64 |
pred, |
| 65 |
buf->type, |
| 66 |
buf->size, |
| 67 |
LV2_ATOM_BODY(buf)); |
| 68 |
|
| 69 |
printf("# Atom => Turtle\n\n%s", outstr); |
| 70 |
|
| 71 |
LV2_Atom* parsed = NULL; |
| 72 |
if (top_level) { |
| 73 |
SerdNode o = serd_node_from_string(SERD_URI, (const uint8_t*)obj_uri); |
| 74 |
parsed = sratom_from_turtle(sratom, base_uri, &o, NULL, outstr); |
| 75 |
} else { |
| 76 |
parsed = sratom_from_turtle(sratom, base_uri, subj, pred, outstr); |
| 77 |
} |
| 78 |
|
| 79 |
if (!pretty_numbers) { |
| 80 |
if (!lv2_atom_equals(buf, parsed)) { |
| 81 |
return test_fail("Parsed atom does not match original"); |
| 82 |
} |
| 83 |
|
| 84 |
char* instr = sratom_to_turtle(sratom, |
| 85 |
&unmap, |
| 86 |
base_uri, |
| 87 |
subj, |
| 88 |
pred, |
| 89 |
parsed->type, |
| 90 |
parsed->size, |
| 91 |
LV2_ATOM_BODY(parsed)); |
| 92 |
printf("# Turtle => Atom\n\n%s", instr); |
| 93 |
|
| 94 |
if (!!strcmp(outstr, instr)) { |
| 95 |
return test_fail("Re-serialized string differs from original"); |
| 96 |
} |
| 97 |
free(instr); |
| 98 |
} |
| 99 |
|
| 100 |
printf("All tests passed.\n"); |
| 101 |
|
| 102 |
free(parsed); |
| 103 |
free(outstr); |
| 104 |
sratom_free(sratom); |
| 105 |
free_uris(&uris); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
static int |
| 110 |
test_env(SerdEnv* env) |
| 111 |
{ |
| 112 |
if (test(env, "file:///tmp/base/", false, false) || // |
| 113 |
test(env, "file:///tmp/base/", true, false) || // |
| 114 |
test(env, "file:///tmp/base/", false, true) || // |
| 115 |
test(env, "file:///tmp/base/", true, true) || // |
| 116 |
test(env, "http://example.org/", true, true)) { |
| 117 |
return 1; |
| 118 |
} |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
int |
| 124 |
main(void) |
| 125 |
{ |
| 126 |
// Test with no environment |
| 127 |
if (test_env(NULL)) { |
| 128 |
return 1; |
| 129 |
} |
| 130 |
|
| 131 |
// Test with a prefix defined |
| 132 |
SerdEnv* env = serd_env_new(NULL); |
| 133 |
serd_env_set_prefix_from_strings( |
| 134 |
env, (const uint8_t*)"eg", (const uint8_t*)"http://example.org/"); |
| 135 |
|
| 136 |
test_env(env); |
| 137 |
serd_env_free(env); |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |