]> git.ozlabs.org Git - ccan/blob - ccan/json_out/test/run-move_cb.c
json_out: new module for authoring JSON strings.
[ccan] / ccan / json_out / test / run-move_cb.c
1 #include <ccan/json_out/json_out.h>
2 /* Include the C files directly. */
3 #include <ccan/json_out/json_out.c>
4 #include <ccan/tap/tap.h>
5
6 static const char *ptr;
7 static bool called = false;
8
9 static void move_cb(struct json_out *jout, ptrdiff_t delta,
10                     struct json_out *arg)
11 {
12         ptr += delta;
13         called = true;
14         ok1(arg == jout);
15 }
16
17 int main(void)
18 {
19         const tal_t *ctx = tal(NULL, char);
20         struct json_out *jout;
21         char *p;
22         size_t len;
23
24         /* This is how many tests you plan to run */
25         plan_tests(3);
26
27         /* Test nested arrays. */
28         jout = json_out_new(ctx);
29         json_out_call_on_move(jout, move_cb, jout);
30
31         json_out_start(jout, NULL, '{');
32         ptr = json_out_contents(jout, &len);
33
34         p = json_out_member_direct(jout, "fieldname", 102);
35         p[0] = '"';
36         p[101] = '"';
37         memset(p+1, 'p', 100);
38
39         json_out_finished(jout);
40         ok1(called);
41         /* Contents should have moved correctly. */
42         ok1(json_out_contents(jout, &len) == ptr);
43
44         tal_free(ctx);
45
46         /* This exits depending on whether all tests passed */
47         return exit_status();
48 }