aboutsummaryrefslogtreecommitdiff
path: root/test/osc_test.c
blob: 64bd2b375c56be2def4957958f5f4e36c2174217 (plain)
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
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>

#include <osc.lv2/osc.h>
#include <osc.lv2/reader.h>
#include <osc.lv2/writer.h>
#include <osc.lv2/forge.h>
#if !defined(_WIN32)
#	include <osc.lv2/stream.h>
#endif

#define BUF_SIZE 0x100000
#define MAX_URIDS 512

typedef void (*test_t)(LV2_OSC_Writer *writer);
typedef struct _urid_t urid_t;
typedef struct _app_t app_t;

struct _urid_t {
	LV2_URID urid;
	char *uri;
};

struct _app_t {
	urid_t urids [MAX_URIDS];
	LV2_URID urid;
};

static app_t __app;
static uint8_t buf0 [BUF_SIZE];
static uint8_t buf1 [BUF_SIZE];
static uint8_t buf2 [BUF_SIZE];
static const LV2_Atom_Object *obj2= (const LV2_Atom_Object *)buf2;

const uint8_t raw_0 [] = {
	'/', 0x0, 0x0, 0x0,
	',', 0x0, 0x0, 0x0
};

const uint8_t raw_1 [] = {
	'/', 'p', 'i', 'n',
	'g', 0x0, 0x0, 0x0,
	',', 'i', 'f', 's',
	0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0xc,
	0x40, 0x59, 0x99, 0x9a,
	'w', 'o', 'r', 'l',
	'd', 0x0, 0x0, 0x0
};

const uint8_t raw_2 [] = {
	'/', 'p', 'i', 'n',
	'g', 0x0, 0x0, 0x0,
	',', 'h', 'd', 'S',
	0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0xc,
	0x40, 0x0b, 0x33, 0x33,
	0x33, 0x33, 0x33, 0x33,
	'h', 't', 't', 'p',
	':', '/', '/', 'e',
	'x', 'a', 'm', 'p',
	'l', 'e', '.', 'c',
	'o', 'm',  0x0, 0x0
};

const uint8_t raw_3 [] = {
	'/', 'p', 'i', 'n',
	'g', 0x0, 0x0, 0x0,
	',', 'T', 'F', 'N',
	'I', 0x0, 0x0, 0x0
};

const uint8_t raw_4 [] = {
	'/', 'm', 'i', 'd',
	'i', 0x0, 0x0, 0x0,
	',', 'm', 0x0, 0x0,
	0x0, 0x90, 24, 0x7f
};

const uint8_t raw_5 [] = {
	'/', 'b', 'l', 'o',
	'b', 0x0, 0x0, 0x0,
	',', 'b', 0x0, 0x0,
	0x0, 0x0, 0x0, 0x6,
	0x1, 0x2, 0x3, 0x4,
	0x5, 0x6, 0x0, 0x0
};

const uint8_t raw_6 [] = {
	'#', 'b', 'u', 'n',
	'd', 'l', 'e', 0x0,
	0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x1,

	0x0, 0x0, 0x0, 0x8,
		'/', 0x0, 0x0, 0x0,
		',', 0x0, 0x0, 0x0
};

const uint8_t raw_7 [] = {
	'#', 'b', 'u', 'n',
	'd', 'l', 'e', 0x0,
	0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x1,

	0x0, 0x0, 0x0, 0x1c,
		'#', 'b', 'u', 'n',
		'd', 'l', 'e', 0x0,
		0x0, 0x0, 0x0, 0x0,
		0x0, 0x0, 0x0, 0x1,

		0x0, 0x0, 0x0, 0x8,
			'/', 0x0, 0x0, 0x0,
			',', 0x0, 0x0, 0x0,

	0x0, 0x0, 0x0, 0x8,
		'/', 0x0, 0x0, 0x0,
		',', 0x0, 0x0, 0x0
};

const uint8_t raw_8 [] = {
	'/', 'p', 'i', 'n',
	'g', 0x0, 0x0, 0x0,
	',', 't', 'c', 'r',
	0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x1,
	0x0, 0x0, 0x0, 'o',
	0x1, 0x2, 0x3, 0x4
};

static LV2_URID
_map(LV2_URID_Map_Handle instance, const char *uri)
{
	app_t *app = instance;

	urid_t *itm;
	for(itm=app->urids; itm->urid; itm++)
	{
		if(!strcmp(itm->uri, uri))
			return itm->urid;
	}

	assert(app->urid + 1 < MAX_URIDS);

	// create new
	itm->urid = ++app->urid;
	itm->uri = strdup(uri);

	return itm->urid;
}

static const char *
_unmap(LV2_URID_Unmap_Handle instance, LV2_URID urid)
{
	app_t *app = instance;

	urid_t *itm;
	for(itm=app->urids; itm->urid; itm++)
	{
		if(itm->urid == urid)
			return itm->uri;
	}

	// not found
	return NULL;
}

static LV2_URID_Map map = {
	.handle = &__app,
	.map = _map
};

static LV2_URID_Unmap unmap = {
	.handle = &__app,
	.unmap = _unmap
};

//#define DUMP
#if defined(DUMP)
static void
_dump(const uint8_t *src, const uint8_t *dst, size_t size)
{
	for(size_t i = 0; i < size; i++)
		printf("%zu %02x %02x\n", i, src[i], dst[i]);
	printf("\n");
}
#endif

static void
_clone(LV2_OSC_Reader *reader, LV2_OSC_Writer *writer, size_t size)
{
	if(lv2_osc_reader_is_bundle(reader))
	{
		LV2_OSC_Item *itm = OSC_READER_BUNDLE_BEGIN(reader, size);
		assert(itm);

		LV2_OSC_Writer_Frame frame_bndl = { .ref = 0 };
		assert(lv2_osc_writer_push_bundle(writer, &frame_bndl, itm->timetag));

		OSC_READER_BUNDLE_ITERATE(reader, itm)
		{
			LV2_OSC_Reader reader2;
			lv2_osc_reader_initialize(&reader2, itm->body, itm->size);

			LV2_OSC_Writer_Frame frame_itm = { .ref = 0 };
			assert(lv2_osc_writer_push_item(writer, &frame_itm));
			_clone(&reader2, writer, itm->size);
			assert(lv2_osc_writer_pop_item(writer, &frame_itm));
		}

		assert(lv2_osc_writer_pop_bundle(writer, &frame_bndl));
	}
	else if(lv2_osc_reader_is_message(reader))
	{
		LV2_OSC_Arg *arg = OSC_READER_MESSAGE_BEGIN(reader, size);
		assert(arg);

		assert(lv2_osc_writer_add_path(writer, arg->path));
		assert(lv2_osc_writer_add_format(writer, arg->type));

		OSC_READER_MESSAGE_ITERATE(reader, arg)
		{
			switch((LV2_OSC_Type)*arg->type)
			{
				case LV2_OSC_INT32:
					assert(lv2_osc_writer_add_int32(writer, arg->i));
					break;
				case LV2_OSC_FLOAT:
					assert(lv2_osc_writer_add_float(writer, arg->f));
					break;
				case LV2_OSC_STRING:
					assert(lv2_osc_writer_add_string(writer, arg->s));
					break;
				case LV2_OSC_BLOB:
					assert(lv2_osc_writer_add_blob(writer, arg->size, arg->b));
					break;

				case LV2_OSC_INT64:
					assert(lv2_osc_writer_add_int64(writer, arg->h));
					break;
				case LV2_OSC_DOUBLE:
					assert(lv2_osc_writer_add_double(writer, arg->d));
					break;
				case LV2_OSC_TIMETAG:
					assert(lv2_osc_writer_add_timetag(writer, arg->t));
					break;

				case LV2_OSC_TRUE:
				case LV2_OSC_FALSE:
				case LV2_OSC_NIL:
				case LV2_OSC_IMPULSE:
					break;

				case LV2_OSC_MIDI:
					assert(lv2_osc_writer_add_midi(writer, arg->size, arg->m));
					break;
				case LV2_OSC_SYMBOL:
					assert(lv2_osc_writer_add_symbol(writer, arg->S));
					break;
				case LV2_OSC_CHAR:
					assert(lv2_osc_writer_add_char(writer, arg->c));
					break;
				case LV2_OSC_RGBA:
					assert(lv2_osc_writer_add_rgba(writer, arg->R, arg->G, arg->B, arg->A));
					break;
			}
		}
	}
}

static void
_test_a(LV2_OSC_Writer *writer, const uint8_t *raw, size_t size)
{
	LV2_OSC_URID osc_urid;
	lv2_osc_urid_init(&osc_urid, &map);

	// check writer against raw bytes
	size_t len;
	assert(lv2_osc_writer_finalize(writer, &len) == buf0);
	assert(len == size);
#if defined(DUMP)
	if(memcmp(raw, buf0, size) != 0)
		_dump(raw, buf0, size);
#endif
	assert(memcmp(raw, buf0, size) == 0);

	// check reader & writer
	LV2_OSC_Reader reader;
	lv2_osc_reader_initialize(&reader, buf0, size);
	lv2_osc_writer_initialize(writer, buf1, BUF_SIZE);
	_clone(&reader, writer, size);

	// check cloned against raw bytes
	assert(lv2_osc_writer_finalize(writer, &len) == buf1);
	assert(len == size);
#if defined(DUMP)
	if(memcmp(raw, buf1, size) != 0)
		_dump(raw, buf1, size);
#endif
	assert(memcmp(raw, buf1, size) == 0);

	// check forge 
	LV2_Atom_Forge forge;
	lv2_atom_forge_init(&forge, &map);
	lv2_atom_forge_set_buffer(&forge, buf2, BUF_SIZE);
	assert(lv2_osc_forge_packet(&forge, &osc_urid, &map, buf0, size));

	// check deforge 
	lv2_osc_writer_initialize(writer, buf1, BUF_SIZE);
	assert(lv2_osc_writer_packet(writer, &osc_urid, &unmap, obj2->atom.size, &obj2->body));

	// check deforged against raw bytes
	assert(lv2_osc_writer_finalize(writer, &len) == buf1);
	assert(len == size);
#if defined(DUMP)
	if(memcmp(raw, buf1, size) != 0)
		_dump(raw, buf1, size);
#endif
	assert(memcmp(raw, buf1, size) == 0);
}

static void
test_0_a(LV2_OSC_Writer *writer)
{
	assert(lv2_osc_writer_message_vararg(writer, "/", ""));
	_test_a(writer, raw_0, sizeof(raw_0));
}

static void
test_1_a(LV2_OSC_Writer *writer)
{
	assert(lv2_osc_writer_message_vararg(writer, "/ping", "ifs",
		12, 3.4f, "world"));
	_test_a(writer, raw_1, sizeof(raw_1));
}

static void
test_2_a(LV2_OSC_Writer *writer)
{
	assert(lv2_osc_writer_message_vararg(writer, "/ping", "hdS",
		(int64_t)12, (double)3.4, "http://example.com"));
	_test_a(writer, raw_2, sizeof(raw_2));
}

static void
test_3_a(LV2_OSC_Writer *writer)
{
	assert(lv2_osc_writer_message_vararg(writer, "/ping", "TFNI"));
	_test_a(writer, raw_3, sizeof(raw_3));
}

static void
test_4_a(LV2_OSC_Writer *writer)
{
	const uint8_t m [] = {0x00, 0x90, 24, 0x7f};
	const int32_t len = sizeof(m);
	assert(lv2_osc_writer_message_vararg(writer, "/midi", "m", len, m));
	_test_a(writer, raw_4, sizeof(raw_4));
}

static void
test_5_a(LV2_OSC_Writer *writer)
{
	const uint8_t b [] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6};
	const int32_t len = sizeof(b);
	assert(lv2_osc_writer_message_vararg(writer, "/blob", "b", len, b));
	_test_a(writer, raw_5, sizeof(raw_5));
}

static void
test_6_a(LV2_OSC_Writer *writer)
{
	LV2_OSC_Writer_Frame frame_bndl = { .ref = 0 };
	LV2_OSC_Writer_Frame frame_itm = { .ref = 0 };

	assert(lv2_osc_writer_push_bundle(writer, &frame_bndl, LV2_OSC_IMMEDIATE));
	{
		assert(lv2_osc_writer_push_item(writer, &frame_itm));
		{
			assert(lv2_osc_writer_message_vararg(writer, "/", ""));
		}
		assert(lv2_osc_writer_pop_item(writer, &frame_itm));
	}
	assert(lv2_osc_writer_pop_bundle(writer, &frame_bndl));

	_test_a(writer, raw_6, sizeof(raw_6));
}

static void
test_7_a(LV2_OSC_Writer *writer)
{
	LV2_OSC_Writer_Frame frame_bndl[2] = { { .ref = 0 }, { .ref = 0 } };
	LV2_OSC_Writer_Frame frame_itm[2] = { { .ref = 0 }, { .ref = 0 } };;

	assert(lv2_osc_writer_push_bundle(writer, &frame_bndl[0], LV2_OSC_IMMEDIATE));
	{
		assert(lv2_osc_writer_push_item(writer, &frame_itm[0]));
		{
			assert(lv2_osc_writer_push_bundle(writer, &frame_bndl[1], LV2_OSC_IMMEDIATE));
			{
				assert(lv2_osc_writer_push_item(writer, &frame_itm[1]));
				{
					assert(lv2_osc_writer_message_vararg(writer, "/", ""));
				}
				assert(lv2_osc_writer_pop_item(writer, &frame_itm[1]));
			}
			assert(lv2_osc_writer_pop_bundle(writer, &frame_bndl[1]));
		}
		assert(lv2_osc_writer_pop_item(writer, &frame_itm[0]));

		assert(lv2_osc_writer_push_item(writer, &frame_itm[0]));
		{
			assert(lv2_osc_writer_message_vararg(writer, "/", ""));
		}
		assert(lv2_osc_writer_pop_item(writer, &frame_itm[0]));
	}
	assert(lv2_osc_writer_pop_bundle(writer, &frame_bndl[0]));

	_test_a(writer, raw_7, sizeof(raw_7));
}

static void
test_8_a(LV2_OSC_Writer *writer)
{
	assert(lv2_osc_writer_message_vararg(writer, "/ping", "tcr",
		1ULL,
		'o',
		0x1, 0x2, 0x3, 0x4));
	_test_a(writer, raw_8, sizeof(raw_8));
}

static test_t tests [] = {
	test_0_a,
	test_1_a,
	test_2_a,
	test_3_a,
	test_4_a,
	test_5_a,
	test_6_a,
	test_7_a,
	test_8_a,

	NULL
}
;
static int
_run_tests()
{
	LV2_OSC_Writer writer;

	for(test_t *test=tests; *test; test++)
	{
		test_t cb = *test;

		memset(buf0, 0x0, BUF_SIZE);
		memset(buf1, 0x0, BUF_SIZE);

		lv2_osc_writer_initialize(&writer, buf0, BUF_SIZE);

		cb(&writer);
	}

	assert(unmap.unmap(unmap.handle, 0)== NULL);

	return 0;
}

#if !defined(_WIN32)
typedef struct _item_t item_t;
typedef struct _stash_t stash_t;

struct _item_t {
	size_t size;
	uint8_t buf [];
};

struct _stash_t {
	size_t size;
	item_t **items;
	item_t *rsvd;
};

static uint8_t *
_stash_write_req(stash_t *stash, size_t minimum, size_t *maximum)
{
	if(!stash->rsvd || (stash->rsvd->size < minimum))
	{
		const size_t sz = sizeof(item_t) + minimum;
		stash->rsvd = realloc(stash->rsvd, sz);
		assert(stash->rsvd);
		stash->rsvd->size = minimum;
	}

	if(maximum)
	{
		*maximum = stash->rsvd->size;
	}

	return stash->rsvd->buf;
}

static void
_stash_write_adv(stash_t *stash, size_t written)
{
	assert(stash->rsvd);
	assert(stash->rsvd->size >= written);
	stash->rsvd->size = written;
	stash->size += 1;
	stash->items = realloc(stash->items, sizeof(item_t *) * stash->size);
	stash->items[stash->size - 1] = stash->rsvd;
	stash->rsvd = NULL;
}

static const uint8_t *
_stash_read_req(stash_t *stash, size_t *size)
{
	if(stash->size == 0)
	{
		if(size)
		{
			*size = 0;
		}

		return NULL;
	}

	item_t *item = stash->items[0];

	if(size)
	{
		*size = item->size;
	}

	return item->buf;
}

static void
_stash_read_adv(stash_t *stash)
{
	assert(stash->size);

	free(stash->items[0]);
	stash->size -= 1;

	for(unsigned i = 0; i < stash->size; i++)
	{
		stash->items[i] = stash->items[i+1];
	}

	stash->items = realloc(stash->items, sizeof(item_t *) * stash->size);
}

static void *
_write_req(void *data, size_t minimum, size_t *maximum)
{
	stash_t *stash = data;

	return _stash_write_req(&stash[0], minimum, maximum);
}

static void
_write_adv(void *data, size_t written)
{
	stash_t *stash = data;

	_stash_write_adv(&stash[0], written);
}

static const void *
_read_req(void *data, size_t *toread)
{
	stash_t *stash = data;

	return _stash_read_req(&stash[1], toread);
}

static void
_read_adv(void *data)
{
	stash_t *stash = data;

	_stash_read_adv(&stash[1]);
}

static const LV2_OSC_Driver driv = {
	.write_req = _write_req,
	.write_adv = _write_adv,
	.read_req = _read_req,
	.read_adv = _read_adv
};

#define COUNT 128

typedef struct _pair_t pair_t;

struct _pair_t {
	const char *server;
	const char *client;
	bool lossy;
};

static void *
_thread_1(void *data)
{
	const pair_t *pair = data;
	const char *uri = pair->server;

	LV2_OSC_Stream stream;
	stash_t stash [2];
	uint8_t check [COUNT];

	memset(&stream, 0x0, sizeof(stream));
	memset(stash, 0x0, sizeof(stash));
	memset(check, 0x0, sizeof(check));

	assert(lv2_osc_stream_init(&stream, uri, &driv, stash) == 0);

	time_t t0 = time(NULL);
	unsigned count = 0;
	while(true)
	{
		const time_t t1 = time(NULL);
		const LV2_OSC_Enum ev = lv2_osc_stream_run(&stream);

		if(ev & LV2_OSC_ERR)
		{
			fprintf(stderr, "%s: %s\n", __func__, strerror(ev & LV2_OSC_ERR));
		}

		if(ev & LV2_OSC_RECV)
		{
			const uint8_t *buf_rx;
			size_t reat;

			while( (buf_rx = _stash_read_req(&stash[0], &reat)) )
			{
				LV2_OSC_Reader reader;

				lv2_osc_reader_initialize(&reader, buf_rx, reat);
				assert(lv2_osc_reader_is_message(&reader));

				OSC_READER_MESSAGE_FOREACH(&reader, arg, reat)
				{
					assert(strcmp(arg->path, "/trip") == 0);
					assert(*arg->type == 'i');
					assert(arg->size == sizeof(int32_t));
					assert(check[arg->i] == 0);
					check[arg->i] = 1;
				}

				count++;

				while(true)
				{
					// send back
					uint8_t *buf_tx;
					if( (buf_tx = _stash_write_req(&stash[1], reat, NULL)) )
					{
						memcpy(buf_tx, buf_rx, reat);

						_stash_write_adv(&stash[1], reat);
						break;
					}
				}

				_stash_read_adv(&stash[0]);
			}

			t0 = t1;
		}

		if(count >= COUNT)
		{
			break;
		}
		else if(pair->lossy && (difftime(t1, t0) >= 1.0) )
		{
			fprintf(stderr, "%s: timeout: %i\n", __func__, count);
			break;
		}
	}

	LV2_OSC_Enum ev;
	do
	{
		ev = lv2_osc_stream_run(&stream);

		if(ev & LV2_OSC_ERR)
		{
			fprintf(stderr, "%s: %s\n", __func__, strerror(ev & LV2_OSC_ERR));
		}
	} while( (ev & LV2_OSC_SEND) || (ev & LV2_OSC_CONN) );

	assert(pair->lossy || (count == COUNT) );

	assert(lv2_osc_stream_deinit(&stream) == 0);

	free(stash[0].rsvd);
	while(stash[0].size)
	{
		_stash_read_adv(&stash[0]);
	}
	free(stash[0].items);

	free(stash[1].rsvd);
	while(stash[1].size)
	{
		_stash_read_adv(&stash[1]);
	}
	free(stash[1].items);

	return NULL;
}

static void *
_thread_2(void *data)
{
	const pair_t *pair = data;
	const char *uri = pair->client;

	LV2_OSC_Stream stream;
	stash_t stash [2];
	uint8_t check [COUNT];

	memset(&stream, 0x0, sizeof(stream));
	memset(stash, 0x0, sizeof(stash));
	memset(check, 0x0, sizeof(check));

	assert(lv2_osc_stream_init(&stream, uri, &driv, stash) == 0);

	unsigned count = 0;
	for(int32_t i = 0; i < COUNT; i++)
	{
		LV2_OSC_Writer writer;

		while(true)
		{
			uint8_t *buf_tx;
			size_t max;
			if( (buf_tx = _stash_write_req(&stash[1], 1024, &max)) )
			{
				size_t writ;
				lv2_osc_writer_initialize(&writer, buf_tx, max);
				assert(lv2_osc_writer_message_vararg(&writer, "/trip", "i", i));
				assert(lv2_osc_writer_finalize(&writer, &writ) == buf_tx);
				assert(writ == 16);
				assert(check[i] == 0);
				check[i] = 1;

				_stash_write_adv(&stash[1], writ);
				break;
			}
		}

		const LV2_OSC_Enum ev = lv2_osc_stream_run(&stream);

		if(ev & LV2_OSC_ERR)
		{
			fprintf(stderr, "%s: %s\n", __func__, strerror(ev & LV2_OSC_ERR));
		}

		if(ev & LV2_OSC_RECV)
		{
			const uint8_t *buf_rx;
			size_t reat;

			while( (buf_rx = _stash_read_req(&stash[0], &reat)) )
			{
				LV2_OSC_Reader reader;

				lv2_osc_reader_initialize(&reader, buf_rx, reat);
				assert(lv2_osc_reader_is_message(&reader));

				OSC_READER_MESSAGE_FOREACH(&reader, arg, reat)
				{
					assert(strcmp(arg->path, "/trip") == 0);
					assert(*arg->type == 'i');
					assert(arg->size == sizeof(int32_t));
					assert(check[arg->i] == 1);
					check[arg->i] = 2;
				}

				count++;

				_stash_read_adv(&stash[0]);
			}
		}
	}

	time_t t0 = time(NULL);
	while(true)
	{
		const time_t t1 = time(NULL);
		const LV2_OSC_Enum ev = lv2_osc_stream_run(&stream);

		if(ev & LV2_OSC_ERR)
		{
			fprintf(stderr, "%s: %s\n", __func__, strerror(ev & LV2_OSC_ERR));
		}

		if(ev & LV2_OSC_RECV)
		{
			const uint8_t *buf_rx;
			size_t reat;

			while( (buf_rx = _stash_read_req(&stash[0], &reat)) )
			{
				LV2_OSC_Reader reader;

				lv2_osc_reader_initialize(&reader, buf_rx, reat);
				assert(lv2_osc_reader_is_message(&reader));

				OSC_READER_MESSAGE_FOREACH(&reader, arg, reat)
				{
					assert(strcmp(arg->path, "/trip") == 0);
					assert(*arg->type == 'i');
					assert(arg->size == sizeof(int32_t));
					assert(check[arg->i] == 1);
					check[arg->i] = 2;
				}

				count++;

				_stash_read_adv(&stash[0]);
			}

			t0 = t1;
		}

		if(count >= COUNT)
		{
			break;
		}
		else if(pair->lossy && (difftime(t1, t0) >= 1.0) )
		{
			fprintf(stderr, "%s: timeout: %i\n", __func__, count);
			break;
		}
	}

	assert(pair->lossy || (count == COUNT) );

	assert(lv2_osc_stream_deinit(&stream) == 0);

	free(stash[0].rsvd);
	while(stash[0].size)
	{
		_stash_read_adv(&stash[0]);
	}
	free(stash[0].items);

	free(stash[1].rsvd);
	while(stash[1].size)
	{
		_stash_read_adv(&stash[1]);
	}
	free(stash[1].items);

	return NULL;
}

static const pair_t pairs [] = {
	{
		.server = "osc.udp://:2222",
		.client = "osc.udp://localhost:2222",
		.lossy = true
	},
	{
		.server = "osc.udp://[]:3333",
		.client = "osc.udp://[::1]:3333",
		.lossy = true
	},

	{
		.server = "osc.udp://:3344",
		.client = "osc.udp://255.255.255.255:3344",
		.lossy = true
	},

	{
		.server = "osc.tcp://:4444",
		.client = "osc.tcp://localhost:4444",
		.lossy = false
	},
	{
		.server = "osc.tcp://[]:5555",
		.client = "osc.tcp://[::1]:5555",
		.lossy = false
	},

	{
		.server = "osc.slip.tcp://:6666",
		.client = "osc.slip.tcp://localhost:6666",
		.lossy = false
	},
	{
		.server = "osc.slip.tcp://[]:7777",
		.client = "osc.slip.tcp://[::1]:7777",
		.lossy = false
	},

	{
		.server = "osc.prefix.tcp://:8888",
		.client = "osc.prefix.tcp://localhost:8888",
		.lossy = false
	},
	{
		.server = "osc.prefix.tcp://[%lo]:9999",
		.client = "osc.prefix.tcp://[::1%lo]:9999",
		.lossy = false
	},

#if 0
	{
		.server = "osc.serial:///dev/pts/4", //FIXME baudrate
		.client = "osc.serial:///dev/pts/5",
		.lossy = false
	},
#endif

	{
		.server = NULL,
		.client = NULL,
		.lossy = false
	}
};
#endif

#if !defined(_WIN32)
static void
_one(const char *path, const LV2_Atom_Tuple *arguments __attribute__((unused)),
	void *data)
{
	bool *flag = data;
	*flag = true;

	assert(!strcmp(path, "/sub/one")
		|| !strcmp(path, "/*/one")
		|| !strcmp(path, "/s*/one")
		|| !strcmp(path, "/su*/one")
		|| !strcmp(path, "/sub*/one")
		|| !strcmp(path, "/sub/*")
		|| !strcmp(path, "/*sub/one")
		|| !strcmp(path, "/*s*u*b*/one")
		|| !strcmp(path, "/su[ab]/one")
		|| !strcmp(path, "/su[a-b]/[!a-np-z]ne")
		|| !strcmp(path, "/su[a-b]/one")
		|| !strcmp(path, "/s?b/?ne")
		|| !strcmp(path, "/s?*/?ne")
		|| !strcmp(path, "/s?*/*?e")
		|| !strcmp(path, "/sub/{one,two}"));
}

static void
_two(const char *path, const LV2_Atom_Tuple *arguments __attribute__((unused)),
	void *data)
{
	bool *flag = data;
	*flag = true;

	assert(!strcmp(path, "/sub/two")
		|| !strcmp(path, "/sub/*")
		|| !strcmp(path, "/sub/{one,two}"));
}

static void
_foo(const char *path, const LV2_Atom_Tuple *arguments __attribute__((unused)),
	void *data)
{
	bool *flag = data;
	*flag = true;

	assert(!strcmp(path, "/foo")
		|| !strcmp(path, "/{foo,bar}"));
}

static void
_bar(const char *path, const LV2_Atom_Tuple *arguments __attribute__((unused)),
	void *data)
{
	bool *flag = data;
	*flag = true;

	assert(!strcmp(path, "/bar")
		|| !strcmp(path, "/{foo,bar}"));
}

static bool foo_sub_one = false;
static bool foo_sub_two [2] = { false, false };
static bool foo = false;
static bool bar = false;

static LV2_OSC_Hook sub [] = {
	{ .name = "one", .method = _one, .data = &foo_sub_one },
	{ .name = "two", .method = _two, .data = &foo_sub_two[0] },
	{ .name = "two", .method = _two, .data = &foo_sub_two[1] },
	{ .name = NULL }
};

static LV2_OSC_Hook root [] = {
	{ .name = "foo", .method = _foo, .data = &foo },
	{ .name = "bar", .method = _bar, .data = &bar },
	{ .name = "sub", .hooks = sub },
	{ .name = NULL }
};

static bool
_run_test_hooks_internal(const char *path)
{
	LV2_OSC_URID osc_urid;
	LV2_Atom_Forge forge;

	lv2_osc_urid_init(&osc_urid, &map);
	lv2_atom_forge_init(&forge, &map);

	lv2_atom_forge_set_buffer(&forge, buf0, BUF_SIZE);
	assert(lv2_osc_forge_message_vararg(&forge, &osc_urid, path, ""));

	foo_sub_one = foo_sub_two[0] = foo_sub_two[1] = foo = bar = false;

	const LV2_Atom_Object *obj = (const LV2_Atom_Object *)buf0;;
	return lv2_osc_unroll(&osc_urid, obj, lv2_osc_hooks, root);
}

static int
_run_test_hooks()
{
	{
		assert(_run_test_hooks_internal("/nil") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == false);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/foo") == true);
		assert(foo == true);
		assert(bar == false);
		assert(foo_sub_one == false);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/bar") == true);
		assert(foo == false);
		assert(bar == true);
		assert(foo_sub_one == false);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/sub/nil") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == false);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/sub/one") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/sub/two") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == false);
		assert(foo_sub_two[0] == true);
		assert(foo_sub_two[1] == true);
	}

	{
		assert(_run_test_hooks_internal("/sub/*") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == true);
		assert(foo_sub_two[1] == true);
	}

	{
		assert(_run_test_hooks_internal("/*/one") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/s*/one") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/su*/one") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/sub*/one") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/*sub/one") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/*s*u*b*/one") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/su[ab]/one") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/su[a-b]/[!a-np-z]ne") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/su[!a-b]/one") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == false);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/s?b/?ne") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/s?*/*?e") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/{foo,bar}") == true);
		assert(foo == true);
		assert(bar == true);
		assert(foo_sub_one == false);
		assert(foo_sub_two[0] == false);
		assert(foo_sub_two[1] == false);
	}

	{
		assert(_run_test_hooks_internal("/sub/{one,two}") == true);
		assert(foo == false);
		assert(bar == false);
		assert(foo_sub_one == true);
		assert(foo_sub_two[0] == true);
		assert(foo_sub_two[1] == true);
	}

	return 0;
}
#endif

int
main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
{
#if !defined(_WIN32)
	(void)lv2_osc_stream_pollin; //FIXME
#endif

	fprintf(stdout, "running main tests:\n");
	assert(_run_tests() == 0);

#if !defined(_WIN32)
	fprintf(stdout, "running hook tests:\n");
	assert(_run_test_hooks() == 0);
#else
	(void)lv2_osc_hooks; //FIXME
#endif

#if !defined(_WIN32)
	for(const pair_t *pair = pairs; pair->server; pair++)
	{
		pthread_t thread_1;
		pthread_t thread_2;

		fprintf(stdout, "running stream test: <%s> <%s> %i\n",
			pair->server, pair->client, pair->lossy);

		assert(pthread_create(&thread_1, NULL, _thread_1, (void *)pair) == 0);
		assert(pthread_create(&thread_2, NULL, _thread_2, (void *)pair) == 0);

		assert(pthread_join(thread_1, NULL) == 0);
		assert(pthread_join(thread_2, NULL) == 0);
	}
#endif

	for(unsigned i=0; i<__app.urid; i++)
	{
		urid_t *itm = &__app.urids[i];

		free(itm->uri);
	}

	return 0;
}