File: test_state.c

package info (click to toggle)
lilv 0.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,336 kB
  • sloc: ansic: 11,764; python: 1,698; cpp: 349; makefile: 160; sh: 70
file content (1152 lines) | stat: -rw-r--r-- 35,705 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
// Copyright 2007-2020 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC

#undef NDEBUG

#include "lilv_test_uri_map.h"
#include "lilv_test_utils.h"

#include <lilv/lilv.h>
#include <lv2/core/lv2.h>
#include <lv2/state/state.h>
#include <lv2/urid/urid.h>
#include <serd/serd.h>
#include <zix/allocator.h>
#include <zix/filesystem.h>
#include <zix/path.h>

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TEST_PLUGIN_URI "http://example.org/lilv-test-plugin"

typedef struct {
  LilvTestEnv*        env;
  LilvTestUriMap      uri_map;
  LV2_URID_Map        map;
  LV2_Feature         map_feature;
  LV2_URID_Unmap      unmap;
  LV2_Feature         unmap_feature;
  LV2_State_Free_Path free_path;
  LV2_Feature         free_path_feature;
  const LV2_Feature*  features[4];
  LV2_URID            atom_Float;
  float               in;
  float               out;
  float               control;
} TestContext;

typedef struct {
  char* top;     ///< Temporary directory that contains everything
  char* shared;  ///< Common parent for shared directoryes (top/shared)
  char* scratch; ///< Scratch file directory (top/shared/scratch)
  char* copy;    ///< Copy directory (top/shared/scratch)
  char* link;    ///< Link directory (top/shared/scratch)
} TestDirectories;

static void
lilv_free_path(LV2_State_Free_Path_Handle handle, char* path)
{
  (void)handle;

  lilv_free(path);
}

static TestContext*
test_context_new(void)
{
  TestContext* ctx = (TestContext*)calloc(1, sizeof(TestContext));
  assert(ctx);

  lilv_test_uri_map_init(&ctx->uri_map);

  ctx->env                    = lilv_test_env_new();
  ctx->map.handle             = &ctx->uri_map;
  ctx->map.map                = map_uri;
  ctx->map_feature.URI        = LV2_URID_MAP_URI;
  ctx->map_feature.data       = &ctx->map;
  ctx->unmap.handle           = &ctx->uri_map;
  ctx->unmap.unmap            = unmap_uri;
  ctx->unmap_feature.URI      = LV2_URID_UNMAP_URI;
  ctx->unmap_feature.data     = &ctx->unmap;
  ctx->free_path.free_path    = lilv_free_path;
  ctx->free_path_feature.URI  = LV2_STATE__freePath;
  ctx->free_path_feature.data = &ctx->free_path;
  ctx->features[0]            = &ctx->map_feature;
  ctx->features[1]            = &ctx->unmap_feature;
  ctx->features[2]            = &ctx->free_path_feature;
  ctx->features[3]            = NULL;

  ctx->atom_Float =
    map_uri(&ctx->uri_map, "http://lv2plug.in/ns/ext/atom#Float");

  ctx->in      = 1.0;
  ctx->out     = 42.0;
  ctx->control = 1234.0;

  return ctx;
}

static void
test_context_free(TestContext* ctx)
{
  lilv_test_uri_map_clear(&ctx->uri_map);
  lilv_test_env_free(ctx->env);
  free(ctx);
}

static TestDirectories
create_test_directories(void)
{
  TestDirectories dirs;

  char* const top = lilv_create_temporary_directory("lilv_XXXXXX");
  assert(top);

  /* On MacOS, temporary directories from mkdtemp involve symlinks, so
     resolve it here so that path comparisons in tests work. */

  dirs.top     = zix_canonical_path(NULL, top);
  dirs.shared  = zix_path_join(NULL, dirs.top, "shared");
  dirs.scratch = zix_path_join(NULL, dirs.shared, "scratch");
  dirs.copy    = zix_path_join(NULL, dirs.shared, "copy");
  dirs.link    = zix_path_join(NULL, dirs.shared, "link");

  assert(dirs.top);
  assert(dirs.shared);
  assert(dirs.scratch);
  assert(dirs.copy);
  assert(dirs.link);

  assert(!zix_create_directory(dirs.shared));
  assert(!zix_create_directory(dirs.scratch));
  assert(!zix_create_directory(dirs.copy));
  assert(!zix_create_directory(dirs.link));

  free(top);

  return dirs;
}

static TestDirectories
no_test_directories(void)
{
  TestDirectories dirs = {NULL, NULL, NULL, NULL, NULL};

  return dirs;
}

static void
remove_file(const char* path, const char* name, void* data)
{
  (void)data;

  char* const full_path = zix_path_join(NULL, path, name);
  assert(!zix_remove(full_path));
  free(full_path);
}

static void
cleanup_test_directories(const TestDirectories dirs)
{
  zix_dir_for_each(dirs.scratch, NULL, remove_file);
  zix_dir_for_each(dirs.copy, NULL, remove_file);
  zix_dir_for_each(dirs.link, NULL, remove_file);

  assert(!zix_remove(dirs.link));
  assert(!zix_remove(dirs.copy));
  assert(!zix_remove(dirs.scratch));
  assert(!zix_remove(dirs.shared));
  assert(!zix_remove(dirs.top));

  zix_free(NULL, dirs.link);
  zix_free(NULL, dirs.copy);
  zix_free(NULL, dirs.scratch);
  zix_free(NULL, dirs.shared);
  free(dirs.top);
}

static const void*
get_port_value(const char* port_symbol,
               void*       user_data,
               uint32_t*   size,
               uint32_t*   type)
{
  TestContext* ctx = (TestContext*)user_data;

  if (!strcmp(port_symbol, "input")) {
    *size = sizeof(float);
    *type = ctx->atom_Float;
    return &ctx->in;
  }

  if (!strcmp(port_symbol, "output")) {
    *size = sizeof(float);
    *type = ctx->atom_Float;
    return &ctx->out;
  }

  if (!strcmp(port_symbol, "control")) {
    *size = sizeof(float);
    *type = ctx->atom_Float;
    return &ctx->control;
  }

  fprintf(
    stderr, "error: get_port_value for nonexistent port `%s'\n", port_symbol);
  *size = *type = 0;
  return NULL;
}

static void
set_port_value(const char* port_symbol,
               void*       user_data,
               const void* value,
               uint32_t    size,
               uint32_t    type)
{
  (void)size;
  (void)type;

  TestContext* ctx = (TestContext*)user_data;

  if (!strcmp(port_symbol, "input")) {
    ctx->in = *(const float*)value;
  } else if (!strcmp(port_symbol, "output")) {
    ctx->out = *(const float*)value;
  } else if (!strcmp(port_symbol, "control")) {
    ctx->control = *(const float*)value;
  } else {
    fprintf(
      stderr, "error: set_port_value for nonexistent port `%s'\n", port_symbol);
  }
}

static char*
make_scratch_path(LV2_State_Make_Path_Handle handle, const char* path)
{
  const TestDirectories* dirs = (TestDirectories*)handle;

  return zix_path_join(NULL, dirs->scratch, path);
}

static const LilvPlugin*
load_test_plugin(const TestContext* const ctx)
{
  LilvWorld* world      = ctx->env->world;
  LilvNode*  bundle_uri = lilv_new_file_uri(world, NULL, LILV_TEST_BUNDLE);
  LilvNode*  plugin_uri = lilv_new_uri(world, TEST_PLUGIN_URI);

  lilv_world_load_bundle(world, bundle_uri);

  const LilvPlugins* plugins = lilv_world_get_all_plugins(world);
  const LilvPlugin*  plugin  = lilv_plugins_get_by_uri(plugins, plugin_uri);

  lilv_node_free(plugin_uri);
  lilv_node_free(bundle_uri);

  assert(plugin);
  return plugin;
}

static LilvState*
state_from_instance(const LilvPlugin* const      plugin,
                    LilvInstance* const          instance,
                    TestContext* const           ctx,
                    const TestDirectories* const dirs,
                    const char* const            bundle_path)
{
  return lilv_state_new_from_instance(plugin,
                                      instance,
                                      &ctx->map,
                                      dirs->scratch,
                                      dirs->copy,
                                      dirs->link,
                                      bundle_path,
                                      get_port_value,
                                      ctx,
                                      0,
                                      NULL);
}

static void
test_instance_state(void)
{
  TestContext* const      ctx    = test_context_new();
  const TestDirectories   dirs   = no_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);
  LilvInstance* const     instance =
    lilv_plugin_instantiate(plugin, 48000.0, ctx->features);

  assert(instance);

  // Get instance state
  LilvState* const state =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Check that state contains properties saved by the plugin
  assert(lilv_state_get_num_properties(state) == 8);

  // Check that state has no URI
  assert(!lilv_state_get_uri(state));

  // Check that state can't be saved without a URI
  assert(!lilv_state_to_string(
    ctx->env->world, &ctx->map, &ctx->unmap, state, NULL, NULL));

  // Check that we can't delete unsaved state
  assert(lilv_state_delete(ctx->env->world, state));

  lilv_state_free(state);
  lilv_instance_free(instance);
  test_context_free(ctx);
}

static void
test_default_state(void)
{
  TestContext* const      ctx        = test_context_new();
  const LilvPlugin* const plugin     = load_test_plugin(ctx);
  const LilvNode* const   plugin_uri = lilv_plugin_get_uri(plugin);

  LilvState* const state =
    lilv_state_new_from_world(ctx->env->world, &ctx->map, plugin_uri);

  assert(lilv_state_get_num_properties(state) == 0);
  assert(lilv_node_equals(lilv_state_get_uri(state), plugin_uri));

  char* const bundle_path = lilv_file_uri_parse(
    lilv_node_as_string(lilv_plugin_get_bundle_uri(plugin)), NULL);

  LilvState* const bundle_state =
    lilv_state_new_from_file(ctx->env->world, &ctx->map, NULL, bundle_path);
  assert(bundle_state);
  assert(lilv_state_equals(bundle_state, state));

  lilv_state_free(bundle_state);
  lilv_free(bundle_path);
  lilv_state_free(state);
  test_context_free(ctx);
}

static void
test_equal(void)
{
  TestContext* const      ctx    = test_context_new();
  const TestDirectories   dirs   = no_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);
  LilvInstance* const     instance =
    lilv_plugin_instantiate(plugin, 48000.0, ctx->features);

  assert(instance);

  // Get instance state
  LilvState* const state_1 =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Get another instance state
  LilvState* const state_2 =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Ensure they are equal
  assert(lilv_state_equals(state_1, state_2));

  // Set a label on the second state
  assert(lilv_state_get_label(state_2) == NULL);
  lilv_state_set_label(state_2, "Test State Old Label");

  // Ensure they are no longer equal
  assert(!lilv_state_equals(state_1, state_2));

  lilv_state_free(state_2);
  lilv_state_free(state_1);
  lilv_instance_free(instance);
  test_context_free(ctx);
}

static void
test_changed_plugin_data(void)
{
  TestContext* const      ctx    = test_context_new();
  const TestDirectories   dirs   = no_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);
  LilvInstance* const     instance =
    lilv_plugin_instantiate(plugin, 48000.0, ctx->features);

  assert(instance);

  // Get initial state
  LilvState* const initial_state =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Run plugin to change internal state
  lilv_instance_activate(instance);
  lilv_instance_connect_port(instance, 0, &ctx->in);
  lilv_instance_connect_port(instance, 1, &ctx->out);
  lilv_instance_run(instance, 1);
  assert(ctx->in == 1.0);
  assert(ctx->out == 1.0);

  // Get a new instance state (which should now differ)
  LilvState* const changed_state =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Ensure state has changed
  assert(!lilv_state_equals(initial_state, changed_state));

  lilv_state_free(changed_state);
  lilv_state_free(initial_state);
  lilv_instance_free(instance);
  test_context_free(ctx);
}

static void
test_changed_metadata(void)
{
  TestContext* const      ctx    = test_context_new();
  const TestDirectories   dirs   = no_test_directories();
  LV2_URID_Map* const     map    = &ctx->map;
  const LilvPlugin* const plugin = load_test_plugin(ctx);
  LilvInstance* const     instance =
    lilv_plugin_instantiate(plugin, 48000.0, ctx->features);

  assert(instance);

  // Get initial state
  LilvState* const initial_state =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Save initial state to a string
  char* const initial_string =
    lilv_state_to_string(ctx->env->world,
                         &ctx->map,
                         &ctx->unmap,
                         initial_state,
                         "http://example.org/initial",
                         NULL);

  // Get another state
  LilvState* const changed_state =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Set a metadata property
  const LV2_URID key   = map->map(map->handle, "http://example.org/extra");
  const int32_t  value = 1;
  const LV2_URID type =
    map->map(map->handle, "http://lv2plug.in/ns/ext/atom#Int");
  lilv_state_set_metadata(changed_state,
                          key,
                          &value,
                          sizeof(value),
                          type,
                          LV2_STATE_IS_PORTABLE | LV2_STATE_IS_POD);

  // Save changed state to a string
  char* const changed_string =
    lilv_state_to_string(ctx->env->world,
                         &ctx->map,
                         &ctx->unmap,
                         changed_state,
                         "http://example.org/changed",
                         NULL);

  // Ensure that strings differ (metadata does not affect state equality)
  assert(strcmp(initial_string, changed_string));

  free(changed_string);
  lilv_state_free(changed_state);
  free(initial_string);
  lilv_state_free(initial_state);
  lilv_instance_free(instance);
  test_context_free(ctx);
}

static void
test_to_string(void)
{
  TestContext* const      ctx    = test_context_new();
  const TestDirectories   dirs   = no_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);
  LilvInstance* const     instance =
    lilv_plugin_instantiate(plugin, 48000.0, ctx->features);

  assert(instance);

  // Get initial state
  LilvState* const initial_state =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Run plugin to change internal state
  lilv_instance_activate(instance);
  lilv_instance_connect_port(instance, 0, &ctx->in);
  lilv_instance_connect_port(instance, 1, &ctx->out);
  lilv_instance_run(instance, 1);
  assert(ctx->in == 1.0);
  assert(ctx->out == 1.0);

  // Restore instance state to original state
  lilv_state_restore(initial_state, instance, set_port_value, ctx, 0, NULL);

  // Take a new snapshot of the state
  LilvState* const restored =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Check that new state matches the initial state
  assert(lilv_state_equals(initial_state, restored));

  lilv_state_free(restored);
  lilv_state_free(initial_state);
  lilv_instance_free(instance);
  test_context_free(ctx);
}

static void
test_string_round_trip(void)
{
  TestContext* const      ctx    = test_context_new();
  const TestDirectories   dirs   = no_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);
  LilvInstance* const     instance =
    lilv_plugin_instantiate(plugin, 48000.0, ctx->features);

  assert(instance);

  // Get initial state
  LilvState* const initial_state =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Save state to a string
  char* const string = lilv_state_to_string(ctx->env->world,
                                            &ctx->map,
                                            &ctx->unmap,
                                            initial_state,
                                            "http://example.org/string",
                                            NULL);

  // Restore from string
  LilvState* const restored =
    lilv_state_new_from_string(ctx->env->world, &ctx->map, string);

  // Ensure they are equal
  assert(restored);
  assert(lilv_state_equals(initial_state, restored));

  // Check that the restored state refers to the correct plugin
  const LilvNode* state_plugin_uri = lilv_state_get_plugin_uri(restored);
  assert(!strcmp(lilv_node_as_string(state_plugin_uri), TEST_PLUGIN_URI));

  lilv_state_free(restored);
  free(string);
  lilv_state_free(initial_state);
  lilv_instance_free(instance);
  test_context_free(ctx);
}

static SerdStatus
count_sink(void* const              handle,
           const SerdStatementFlags flags,
           const SerdNode* const    graph,
           const SerdNode* const    subject,
           const SerdNode* const    predicate,
           const SerdNode* const    object,
           const SerdNode* const    object_datatype,
           const SerdNode* const    object_lang)
{
  (void)flags;
  (void)graph;
  (void)subject;
  (void)predicate;
  (void)object;
  (void)object_datatype;
  (void)object_lang;

  size_t* const n_statements = (size_t*)handle;

  ++(*n_statements);

  return SERD_SUCCESS;
}

static size_t
count_statements(const char* path)
{
  size_t n_statements = 0;

  SerdReader* reader = serd_reader_new(
    SERD_TURTLE, &n_statements, NULL, NULL, NULL, count_sink, NULL);

  SerdNode uri = serd_node_new_file_uri((const uint8_t*)path, NULL, NULL, true);

  assert(uri.buf);
  assert(!serd_reader_read_file(reader, (const uint8_t*)uri.buf));

  serd_node_free(&uri);
  serd_reader_free(reader);

  return n_statements;
}

static void
test_to_files(void)
{
  TestContext* const      ctx    = test_context_new();
  TestDirectories         dirs   = create_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);

  LV2_State_Make_Path make_path         = {&dirs, make_scratch_path};
  LV2_Feature         make_path_feature = {LV2_STATE__makePath, &make_path};

  const LV2_Feature* const instance_features[] = {
    &ctx->map_feature, &ctx->free_path_feature, &make_path_feature, NULL};

  LilvInstance* const instance =
    lilv_plugin_instantiate(plugin, 48000.0, instance_features);

  assert(instance);

  // Run plugin to generate some recording file data
  lilv_instance_activate(instance);
  lilv_instance_connect_port(instance, 0, &ctx->in);
  lilv_instance_connect_port(instance, 1, &ctx->out);
  lilv_instance_run(instance, 1);
  lilv_instance_run(instance, 2);
  assert(ctx->in == 1.0);
  assert(ctx->out == 1.0);

  // Check that the test plugin has made its recording scratch file
  char* const recfile_path = zix_path_join(NULL, dirs.scratch, "recfile");
  assert(zix_file_type(recfile_path) == ZIX_FILE_TYPE_REGULAR);

  // Get state
  char* const      bundle_1_path = zix_path_join(NULL, dirs.top, "state1.lv2");
  LilvState* const state_1 =
    state_from_instance(plugin, instance, ctx, &dirs, bundle_1_path);

  // Check that state contains properties saved by the plugin (with files)
  assert(lilv_state_get_num_properties(state_1) == 10);

  // Check that a snapshop of the recfile was created
  char* const recfile_copy_1 = zix_path_join(NULL, dirs.copy, "recfile");
  assert(zix_file_type(recfile_copy_1) == ZIX_FILE_TYPE_REGULAR);

  // Save state to a bundle
  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          state_1,
                          "http://example.org/state1",
                          bundle_1_path,
                          "state.ttl"));

  // Check that a manifest exists
  char* const manifest_path =
    zix_path_join(NULL, bundle_1_path, "manifest.ttl");
  assert(zix_file_type(manifest_path) == ZIX_FILE_TYPE_REGULAR);

  // Check that the expected statements are in the manifest file
  assert(count_statements(manifest_path) == 3);

  // Check that a link to the recfile exists in the saved bundle
  char* const recfile_link_1 = zix_path_join(NULL, bundle_1_path, "recfile");
  assert(zix_file_type(recfile_link_1) == ZIX_FILE_TYPE_REGULAR);
#ifndef _WIN32
  assert(zix_symlink_type(recfile_link_1) == ZIX_FILE_TYPE_SYMLINK);
#endif

  // Check that link points to the corresponding copy
  assert(zix_file_equals(NULL, recfile_link_1, recfile_copy_1));

  // Run plugin again to modify recording file data
  lilv_instance_run(instance, 2);

  // Get updated state
  char* const      bundle_2_path = zix_path_join(NULL, dirs.top, "state2.lv2");
  LilvState* const state_2 =
    state_from_instance(plugin, instance, ctx, &dirs, bundle_2_path);

  // Save updated state to a bundle
  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          state_2,
                          NULL,
                          bundle_2_path,
                          "state.ttl"));

  // Check that a new snapshop of the recfile was created
  char* const recfile_copy_2 = zix_path_join(NULL, dirs.copy, "recfile.2");
  assert(zix_file_type(recfile_copy_2) == ZIX_FILE_TYPE_REGULAR);

  // Check that a link to the recfile exists in the updated bundle
  char* const recfile_link_2 = zix_path_join(NULL, bundle_2_path, "recfile");
  assert(zix_file_type(recfile_link_2) == ZIX_FILE_TYPE_REGULAR);
#ifndef _WIN32
  assert(zix_symlink_type(recfile_link_2) == ZIX_FILE_TYPE_SYMLINK);
#endif

  // Check that link points to the corresponding copy
  assert(zix_file_equals(NULL, recfile_link_2, recfile_copy_2));

  lilv_instance_free(instance);
  zix_dir_for_each(bundle_2_path, NULL, remove_file);
  zix_dir_for_each(bundle_1_path, NULL, remove_file);
  assert(!zix_remove(bundle_2_path));
  assert(!zix_remove(bundle_1_path));
  cleanup_test_directories(dirs);

  zix_free(NULL, recfile_link_2);
  zix_free(NULL, recfile_copy_2);
  lilv_state_free(state_2);
  zix_free(NULL, bundle_2_path);
  zix_free(NULL, recfile_link_1);
  zix_free(NULL, manifest_path);
  zix_free(NULL, recfile_copy_1);
  lilv_state_free(state_1);
  zix_free(NULL, bundle_1_path);
  zix_free(NULL, recfile_path);
  test_context_free(ctx);
}

static void
test_multi_save(void)
{
  TestContext* const      ctx    = test_context_new();
  TestDirectories         dirs   = create_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);

  LV2_State_Make_Path make_path         = {&dirs, make_scratch_path};
  LV2_Feature         make_path_feature = {LV2_STATE__makePath, &make_path};

  const LV2_Feature* const instance_features[] = {
    &ctx->map_feature, &ctx->free_path_feature, &make_path_feature, NULL};

  LilvInstance* const instance =
    lilv_plugin_instantiate(plugin, 48000.0, instance_features);

  assert(instance);

  // Get state
  char* const      bundle_1_path = zix_path_join(NULL, dirs.top, "state1.lv2");
  LilvState* const state_1 =
    state_from_instance(plugin, instance, ctx, &dirs, bundle_1_path);

  // Save state to a bundle
  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          state_1,
                          "http://example.org/state1",
                          bundle_1_path,
                          "state.ttl"));

  // Check that a manifest exists
  char* const manifest_path =
    zix_path_join(NULL, bundle_1_path, "manifest.ttl");
  assert(zix_file_type(manifest_path) == ZIX_FILE_TYPE_REGULAR);

  // Check that the state file exists
  char* const state_path = zix_path_join(NULL, bundle_1_path, "state.ttl");
  assert(zix_file_type(state_path) == ZIX_FILE_TYPE_REGULAR);

  // Check that the expected statements are in the files
  assert(count_statements(manifest_path) == 3);
  assert(count_statements(state_path) == 21);

  // Save state again to the same bundle
  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          state_1,
                          "http://example.org/state1",
                          bundle_1_path,
                          "state.ttl"));

  // Check that everything is the same
  assert(zix_file_type(manifest_path) == ZIX_FILE_TYPE_REGULAR);
  assert(zix_file_type(state_path) == ZIX_FILE_TYPE_REGULAR);
  assert(count_statements(manifest_path) == 3);
  assert(count_statements(state_path) == 21);

  lilv_instance_free(instance);
  zix_dir_for_each(bundle_1_path, NULL, remove_file);
  assert(!zix_remove(bundle_1_path));
  cleanup_test_directories(dirs);

  free(state_path);
  free(manifest_path);
  lilv_state_free(state_1);
  free(bundle_1_path);
  test_context_free(ctx);
}

static void
test_files_round_trip(void)
{
  TestContext* const      ctx    = test_context_new();
  TestDirectories         dirs   = create_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);

  LV2_State_Make_Path make_path         = {&dirs, make_scratch_path};
  LV2_Feature         make_path_feature = {LV2_STATE__makePath, &make_path};

  const LV2_Feature* const instance_features[] = {
    &ctx->map_feature, &ctx->free_path_feature, &make_path_feature, NULL};

  LilvInstance* const instance =
    lilv_plugin_instantiate(plugin, 48000.0, instance_features);

  assert(instance);

  // Run plugin to generate some recording file data
  lilv_instance_activate(instance);
  lilv_instance_connect_port(instance, 0, &ctx->in);
  lilv_instance_connect_port(instance, 1, &ctx->out);
  lilv_instance_run(instance, 1);
  lilv_instance_run(instance, 2);
  assert(ctx->in == 1.0);
  assert(ctx->out == 1.0);

  // Save first state to a bundle
  char* const bundle_1_1_path = zix_path_join(NULL, dirs.top, "state1_1.lv2");
  LilvState* const state_1_1 =
    state_from_instance(plugin, instance, ctx, &dirs, bundle_1_1_path);

  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          state_1_1,
                          NULL,
                          bundle_1_1_path,
                          "state.ttl"));

  // Save first state to another bundle
  char* const bundle_1_2_path = zix_path_join(NULL, dirs.top, "state1_2.lv2");
  LilvState* const state_1_2 =
    state_from_instance(plugin, instance, ctx, &dirs, bundle_1_2_path);

  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          state_1_2,
                          NULL,
                          bundle_1_2_path,
                          "state.ttl"));

  // Load both first state bundles and check that the results are equal
  char* const state_1_1_path =
    zix_path_join(NULL, bundle_1_1_path, "state.ttl");
  char* const state_1_2_path =
    zix_path_join(NULL, bundle_1_2_path, "state.ttl");

  LilvState* state_1_1_loaded =
    lilv_state_new_from_file(ctx->env->world, &ctx->map, NULL, state_1_1_path);

  LilvState* state_1_2_loaded =
    lilv_state_new_from_file(ctx->env->world, &ctx->map, NULL, state_1_2_path);

  assert(state_1_1_loaded);
  assert(state_1_2_loaded);
  assert(lilv_state_equals(state_1_1_loaded, state_1_2_loaded));

  // Run plugin again to modify recording file data
  lilv_instance_run(instance, 2);

  // Save updated state to a bundle
  char* const      bundle_2_path = zix_path_join(NULL, dirs.top, "state2.lv2");
  LilvState* const state_2 =
    state_from_instance(plugin, instance, ctx, &dirs, bundle_2_path);

  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          state_2,
                          NULL,
                          bundle_2_path,
                          "state.ttl"));

  // Load updated state bundle and check that it differs from the others
  LilvState* state_2_loaded =
    lilv_state_new_from_file(ctx->env->world, &ctx->map, NULL, bundle_2_path);

  assert(state_2_loaded);
  assert(!lilv_state_equals(state_1_1_loaded, state_2_loaded));

  lilv_instance_free(instance);
  zix_dir_for_each(bundle_1_1_path, NULL, remove_file);
  zix_dir_for_each(bundle_1_2_path, NULL, remove_file);
  zix_dir_for_each(bundle_2_path, NULL, remove_file);
  assert(!zix_remove(bundle_1_1_path));
  assert(!zix_remove(bundle_1_2_path));
  assert(!zix_remove(bundle_2_path));
  cleanup_test_directories(dirs);

  lilv_state_free(state_2_loaded);
  lilv_state_free(state_2);
  free(bundle_2_path);
  lilv_state_free(state_1_2_loaded);
  lilv_state_free(state_1_1_loaded);
  free(state_1_2_path);
  free(state_1_1_path);
  lilv_state_free(state_1_2);
  free(bundle_1_2_path);
  lilv_state_free(state_1_1);
  free(bundle_1_1_path);
  test_context_free(ctx);
}

static void
test_world_round_trip(void)
{
  TestContext* const       ctx       = test_context_new();
  LilvWorld* const         world     = ctx->env->world;
  TestDirectories          dirs      = create_test_directories();
  const LilvPlugin* const  plugin    = load_test_plugin(ctx);
  static const char* const state_uri = "http://example.org/worldState";

  LV2_State_Make_Path make_path         = {&dirs, make_scratch_path};
  LV2_Feature         make_path_feature = {LV2_STATE__makePath, &make_path};

  const LV2_Feature* const instance_features[] = {
    &ctx->map_feature, &ctx->free_path_feature, &make_path_feature, NULL};

  LilvInstance* const instance =
    lilv_plugin_instantiate(plugin, 48000.0, instance_features);

  assert(instance);

  // Run plugin to generate some recording file data
  lilv_instance_activate(instance);
  lilv_instance_connect_port(instance, 0, &ctx->in);
  lilv_instance_connect_port(instance, 1, &ctx->out);
  lilv_instance_run(instance, 1);
  lilv_instance_run(instance, 2);
  assert(ctx->in == 1.0);
  assert(ctx->out == 1.0);

  // Save state to a bundle
  char* const      bundle_path = zix_path_join(NULL, dirs.top, "state.lv2/");
  LilvState* const start_state =
    state_from_instance(plugin, instance, ctx, &dirs, bundle_path);

  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          start_state,
                          state_uri,
                          bundle_path,
                          "state.ttl"));

  // Load state bundle into world
  SerdNode bundle_uri =
    serd_node_new_file_uri((const uint8_t*)bundle_path, 0, 0, true);
  LilvNode* const bundle_node =
    lilv_new_uri(world, (const char*)bundle_uri.buf);
  LilvNode* const state_node = lilv_new_uri(world, state_uri);
  lilv_world_load_bundle(world, bundle_node);
  lilv_world_load_resource(world, state_node);

  // Ensure the state loaded from the world matches
  LilvState* const restored =
    lilv_state_new_from_world(world, &ctx->map, state_node);
  assert(restored);
  assert(lilv_state_equals(start_state, restored));

  // Unload state from world
  lilv_world_unload_resource(world, state_node);
  lilv_world_unload_bundle(world, bundle_node);

  // Ensure that it is no longer present
  assert(!lilv_state_new_from_world(world, &ctx->map, state_node));

  lilv_instance_free(instance);
  lilv_state_delete(world, start_state);
  cleanup_test_directories(dirs);

  lilv_state_free(restored);
  lilv_node_free(state_node);
  lilv_node_free(bundle_node);
  serd_node_free(&bundle_uri);
  lilv_state_free(start_state);
  free(bundle_path);
  test_context_free(ctx);
}

static void
test_label_round_trip(void)
{
  TestContext* const      ctx    = test_context_new();
  const TestDirectories   dirs   = create_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);
  LilvInstance* const     instance =
    lilv_plugin_instantiate(plugin, 48000.0, ctx->features);

  assert(instance);

  // Get initial state
  LilvState* const state =
    state_from_instance(plugin, instance, ctx, &dirs, NULL);

  // Set a label
  lilv_state_set_label(state, "Monopoly on violence");

  // Save to a bundle
  char* const bundle_path = zix_path_join(NULL, dirs.top, "state.lv2/");
  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          state,
                          NULL,
                          bundle_path,
                          "state.ttl"));

  // Load bundle and check the label and that the states are equal
  char* const state_path = zix_path_join(NULL, bundle_path, "state.ttl");

  LilvState* const loaded =
    lilv_state_new_from_file(ctx->env->world, &ctx->map, NULL, state_path);

  assert(loaded);
  assert(lilv_state_equals(state, loaded));
  assert(!strcmp(lilv_state_get_label(loaded), "Monopoly on violence"));

  lilv_instance_free(instance);
  lilv_state_delete(ctx->env->world, state);
  cleanup_test_directories(dirs);

  lilv_state_free(loaded);
  free(state_path);
  free(bundle_path);
  lilv_state_free(state);
  test_context_free(ctx);
}

static void
test_bad_subject(void)
{
  TestContext* const ctx    = test_context_new();
  LilvNode* const    string = lilv_new_string(ctx->env->world, "Not a URI");

  const LilvState* const file_state = lilv_state_new_from_file(
    ctx->env->world, &ctx->map, string, "/I/do/not/matter");

  assert(!file_state);

  const LilvState* const world_state =
    lilv_state_new_from_world(ctx->env->world, &ctx->map, string);

  assert(!world_state);

  lilv_node_free(string);
  test_context_free(ctx);
}

static void
test_missing_path(void)
{
  TestContext* const     ctx   = test_context_new();
  const LilvState* const state = lilv_state_new_from_file(
    ctx->env->world, &ctx->map, NULL, "/does/not/exist");

  assert(!state);

  test_context_free(ctx);
}

static void
count_file(const char* path, const char* name, void* data)
{
  (void)path;
  (void)name;

  *(unsigned*)data += 1;
}

static void
test_delete(void)
{
  TestContext* const      ctx    = test_context_new();
  TestDirectories         dirs   = create_test_directories();
  const LilvPlugin* const plugin = load_test_plugin(ctx);

  LV2_State_Make_Path make_path         = {&dirs, make_scratch_path};
  LV2_Feature         make_path_feature = {LV2_STATE__makePath, &make_path};

  const LV2_Feature* const instance_features[] = {
    &ctx->map_feature, &ctx->free_path_feature, &make_path_feature, NULL};

  LilvInstance* const instance =
    lilv_plugin_instantiate(plugin, 48000.0, instance_features);

  assert(instance);

  // Run plugin to generate some recording file data
  lilv_instance_activate(instance);
  lilv_instance_connect_port(instance, 0, &ctx->in);
  lilv_instance_connect_port(instance, 1, &ctx->out);
  lilv_instance_run(instance, 1);
  lilv_instance_run(instance, 2);
  assert(ctx->in == 1.0);
  assert(ctx->out == 1.0);

  // Save state to a bundle
  char* const      bundle_path = zix_path_join(NULL, dirs.top, "state.lv2/");
  LilvState* const state =
    state_from_instance(plugin, instance, ctx, &dirs, bundle_path);

  assert(!lilv_state_save(ctx->env->world,
                          &ctx->map,
                          &ctx->unmap,
                          state,
                          NULL,
                          bundle_path,
                          "state.ttl"));

  // Count the number of shared files before doing anything
  unsigned n_shared_files_before = 0;
  zix_dir_for_each(dirs.shared, &n_shared_files_before, count_file);

  lilv_instance_free(instance);

  // Delete the state
  assert(!lilv_state_delete(ctx->env->world, state));

  // Ensure the number of shared files is the same after deletion
  unsigned n_shared_files_after = 0;
  zix_dir_for_each(dirs.shared, &n_shared_files_after, count_file);
  assert(n_shared_files_before == n_shared_files_after);

  // Ensure the state directory has been deleted
  assert(zix_file_type(bundle_path) == ZIX_FILE_TYPE_NONE);

  cleanup_test_directories(dirs);

  lilv_state_free(state);
  free(bundle_path);
  test_context_free(ctx);
}

int
main(void)
{
  test_instance_state();
  test_default_state();
  test_equal();
  test_changed_plugin_data();
  test_changed_metadata();
  test_to_string();
  test_string_round_trip();
  test_to_files();
  test_multi_save();
  test_files_round_trip();
  test_world_round_trip();
  test_label_round_trip();
  test_bad_subject();
  test_missing_path();
  test_delete();

  return 0;
}