Debian Codegraph @

test/test_write.c

raw
1 // Copyright 2026 David Robillard <[email protected]>
2 // SPDX-License-Identifier: ISC
3
4 #undef NDEBUG
5
6 #include "forge_test_object.h"
7 #include "test_uri_map.h"
8
9 #include <lv2/atom/atom.h>
10 #include <lv2/atom/forge.h>
11 #include <lv2/urid/urid.h>
12 #include <serd/serd.h>
13 #include <sratom/sratom.h>
14
15 #include <assert.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #define NS_EG "http://example.org/"
21
22 #define USTR(s) ((const uint8_t*)(s))
23
24 typedef struct {
25 unsigned fail_on; ///< Index of statement/end to fail on
26 unsigned n_events; ///< Number of statements so far
27 } WriteContext;
28
29 static SerdStatus
30 on_statement(void* const handle,
31 const SerdStatementFlags flags,
32 const SerdNode* const graph,
33 const SerdNode* const subject,
34 const SerdNode* const predicate,
35 const SerdNode* const object,
36 const SerdNode* const object_datatype,
37 const SerdNode* const object_lang)
38 {
39 (void)flags;
40 (void)graph;
41 (void)subject;
42 (void)predicate;
43 (void)object;
44 (void)object_datatype;
45 (void)object_lang;
46
47 WriteContext* const ctx = (WriteContext*)handle;
48
49 return (ctx->n_events++ == ctx->fail_on) ? SERD_ERR_BAD_WRITE : SERD_SUCCESS;
50 }
51
52 static SerdStatus
53 on_end(void* const handle, const SerdNode* const node)
54 {
55 (void)handle;
56 (void)node;
57
58 WriteContext* const ctx = (WriteContext*)handle;
59
60 return (ctx->n_events++ == ctx->fail_on) ? SERD_ERR_BAD_WRITE : SERD_SUCCESS;
61 }
62
63 static void
64 check_turtle(Sratom* const sratom,
65 LV2_URID_Unmap* const unmap,
66 const LV2_Atom* const atom,
67 const char* const expected)
68 {
69 const SerdNode s = serd_node_from_string(SERD_URI, USTR(NS_EG "s"));
70 const SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_EG "p"));
71
72 char* const ttl = sratom_to_turtle(
73 sratom, unmap, NS_EG, &s, &p, atom->type, atom->size, LV2_ATOM_BODY(atom));
74
75 assert(ttl);
76 assert(!strcmp(ttl, expected));
77 free(ttl);
78 }
79
80 // Not covered by round-trip test because it comes back as a string
81 static void
82 test_bare_literal(void)
83 {
84 Uris uris = {NULL, 0};
85 LV2_URID_Map map = {&uris, urid_map};
86 LV2_URID_Unmap unmap = {&uris, urid_unmap};
87 Sratom* const sratom = sratom_new(&map);
88
89 LV2_Atom_Forge forge;
90 LV2_Atom buf[8];
91 lv2_atom_forge_init(&forge, &map);
92 lv2_atom_forge_set_buffer(&forge, (uint8_t*)buf, sizeof(buf));
93 lv2_atom_forge_literal(&forge, "test", 4, 0, 0);
94
95 check_turtle(sratom,
96 &unmap,
97 buf,
98 "<http://example.org/s>\n\t<http://example.org/p> \"test\" .\n");
99
100 sratom_free(sratom);
101 free_uris(&uris);
102 }
103
104 // Not covered by round-trip test because it comes back as a URID
105 static void
106 test_uri(void)
107 {
108 Uris uris = {NULL, 0};
109 LV2_URID_Map map = {&uris, urid_map};
110 LV2_URID_Unmap unmap = {&uris, urid_unmap};
111 Sratom* const sratom = sratom_new(&map);
112
113 LV2_Atom_Forge forge;
114 LV2_Atom buf[8];
115 lv2_atom_forge_init(&forge, &map);
116 lv2_atom_forge_set_buffer(&forge, (uint8_t*)buf, sizeof(buf));
117 lv2_atom_forge_uri(&forge, NS_EG "o", strlen(NS_EG "o"));
118
119 check_turtle(sratom,
120 &unmap,
121 buf,
122 "<http://example.org/s>\n\t<http://example.org/p> "
123 "<http://example.org/o> .\n");
124
125 sratom_free(sratom);
126 free_uris(&uris);
127 }
128
129 static void
130 test_bad_language(void)
131 {
132 Uris uris = {NULL, 0};
133 LV2_URID_Map map = {&uris, urid_map};
134 LV2_URID_Unmap unmap = {&uris, urid_unmap};
135 const LV2_URID lang = urid_map(&uris, NS_EG "l");
136 WriteContext ctx = {0, 0};
137 Sratom* const sratom = sratom_new(&map);
138
139 sratom_set_sink(sratom, NS_EG, on_statement, on_end, &ctx);
140
141 LV2_Atom_Forge forge;
142 LV2_Atom buf[8];
143 lv2_atom_forge_init(&forge, &map);
144 lv2_atom_forge_set_buffer(&forge, (uint8_t*)buf, sizeof(buf));
145 lv2_atom_forge_literal(&forge, "test", 4, 0, lang);
146
147 const SerdNode s = serd_node_from_string(SERD_URI, USTR(NS_EG "s"));
148 const SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_EG "p"));
149 const LV2_Atom* const obj = buf;
150 assert(
151 sratom_write(
152 sratom, &unmap, 0U, &s, &p, obj->type, obj->size, LV2_ATOM_BODY(obj)) ==
153 SERD_ERR_BAD_ARG);
154
155 assert(!ctx.n_events);
156 sratom_free(sratom);
157 free_uris(&uris);
158 }
159
160 static void
161 test_bad_vector_child_size(void)
162 {
163 Uris uris = {NULL, 0};
164 LV2_URID_Map map = {&uris, urid_map};
165 LV2_URID_Unmap unmap = {&uris, urid_unmap};
166 const LV2_URID atom_Int = map.map(map.handle, LV2_ATOM__Int);
167 const LV2_URID atom_Vector = map.map(map.handle, LV2_ATOM__Vector);
168 WriteContext ctx = {0, 0};
169 Sratom* const sratom = sratom_new(&map);
170
171 sratom_set_sink(sratom, NS_EG, on_statement, on_end, &ctx);
172
173 const SerdNode s = serd_node_from_string(SERD_URI, USTR(NS_EG "s"));
174 const SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_EG "p"));
175 const LV2_Atom_Vector vec = {{sizeof(LV2_Atom_Vector), atom_Vector},
176 {0U, atom_Int}};
177
178 assert(sratom_write(sratom,
179 &unmap,
180 0U,
181 &s,
182 &p,
183 vec.atom.type,
184 vec.atom.size,
185 LV2_ATOM_BODY(&vec)) == SERD_ERR_BAD_ARG);
186
187 assert(!ctx.n_events);
188 sratom_free(sratom);
189 free_uris(&uris);
190 }
191
192 static void
193 test_write_errors(void)
194 {
195 Uris uris = {NULL, 0};
196 LV2_URID_Map map = {&uris, urid_map};
197 LV2_URID_Unmap unmap = {&uris, urid_unmap};
198 WriteContext ctx = {0, 0};
199 Sratom* const sratom = sratom_new(&map);
200
201 sratom_set_sink(sratom, NS_EG, on_statement, on_end, &ctx);
202
203 LV2_Atom_Forge forge;
204 LV2_Atom buf[192];
205 lv2_atom_forge_init(&forge, &map);
206 lv2_atom_forge_set_buffer(&forge, (uint8_t*)buf, sizeof(buf));
207 forge_test_object(&forge, &map, &uris, 0U);
208
209 const SerdNode s = serd_node_from_string(SERD_URI, USTR(NS_EG "s"));
210 const SerdNode p = serd_node_from_string(SERD_URI, USTR(NS_EG "p"));
211
212 for (unsigned i = 0U; i < 157; ++i) {
213 ctx.fail_on = i;
214 ctx.n_events = 0;
215
216 const int st = sratom_write(
217 sratom, &unmap, 0U, &s, &p, buf->type, buf->size, LV2_ATOM_BODY(buf));
218
219 assert(st == SERD_ERR_BAD_WRITE);
220 assert(ctx.n_events == ctx.fail_on + 1U);
221 }
222
223 sratom_free(sratom);
224 free_uris(&uris);
225 }
226
227 int
228 main(void)
229 {
230 test_bare_literal();
231 test_uri();
232 test_bad_language();
233 test_bad_vector_child_size();
234 test_write_errors();
235 return 0;
236 }