1 #include <assert.h>
2 
3 #include <xpress.lv2/xpress.h>
4 
5 #define MAX_NVOICES 32
6 #define MAX_URIDS 512
7 
8 typedef struct _targetI_t targetI_t;
9 typedef struct _plughandle_t plughandle_t;
10 typedef struct _urid_t urid_t;
11 typedef void (*test_t)(xpress_t *xpressI);
12 
13 struct _targetI_t {
14 	int64_t frames;
15 	xpress_state_t state;
16 	xpress_uuid_t uuid;
17 };
18 
19 struct _plughandle_t {
20 	XPRESS_T(xpressI, MAX_NVOICES);
21 	targetI_t targetI [MAX_NVOICES];
22 };
23 
24 static void
_add(void * data,int64_t frames,const xpress_state_t * state,xpress_uuid_t uuid,void * target)25 _add(void *data, int64_t frames, const xpress_state_t *state,
26 	xpress_uuid_t uuid, void *target)
27 {
28 	plughandle_t *handle = data;
29 	targetI_t *src = target;
30 
31 	(void)handle; //FIXME
32 	src->frames = frames;
33 	src->state = *state;
34 	src->uuid = uuid;
35 }
36 
37 static void
_set(void * data,int64_t frames,const xpress_state_t * state,xpress_uuid_t uuid,void * target)38 _set(void *data, int64_t frames, const xpress_state_t *state,
39 	xpress_uuid_t uuid, void *target)
40 {
41 	plughandle_t *handle = data;
42 	targetI_t *src = target;
43 
44 	(void)handle; //FIXME
45 	src->frames = frames;
46 	src->state = *state;
47 	assert(src->uuid == uuid);
48 }
49 
50 static void
_del(void * data,int64_t frames,xpress_uuid_t uuid,void * target)51 _del(void *data, int64_t frames,
52 	xpress_uuid_t uuid, void *target)
53 {
54 	plughandle_t *handle = data;
55 	targetI_t *src = target;
56 
57 	(void)handle; //FIXME
58 	src->frames = frames;
59 	assert(src->uuid == uuid);
60 }
61 
62 static const xpress_iface_t ifaceI = {
63 	.size = sizeof(targetI_t),
64 	.add = _add,
65 	.set = _set,
66 	.del = _del
67 };
68 
69 struct _urid_t {
70 	LV2_URID urid;
71 	char *uri;
72 };
73 
74 static urid_t urids [MAX_URIDS];
75 static LV2_URID urid;
76 
77 static LV2_URID
_map(LV2_URID_Map_Handle instance,const char * uri)78 _map(LV2_URID_Map_Handle instance __attribute__((unused)), const char *uri)
79 {
80 	urid_t *itm;
81 	for(itm=urids; itm->urid; itm++)
82 	{
83 		if(!strcmp(itm->uri, uri))
84 			return itm->urid;
85 	}
86 
87 	assert(urid + 1 < MAX_URIDS);
88 
89 	// create new
90 	itm->urid = ++urid;
91 	itm->uri = strdup(uri);
92 
93 	return itm->urid;
94 }
95 
96 static LV2_URID_Map map = {
97 	.handle = NULL,
98 	.map = _map
99 };
100 
101 static xpress_uuid_t counter = 1;
102 
103 static xpress_uuid_t
_new_uuid(void * handle,uint32_t flag)104 _new_uuid(void *handle __attribute__((unused)),
105 	uint32_t flag __attribute__((unused)))
106 {
107 	return counter++;
108 }
109 
110 static xpress_map_t voice_map = {
111 	.handle = NULL,
112 	.new_uuid = _new_uuid
113 };
114 
115 static void
_test_1(xpress_t * xpressI)116 _test_1(xpress_t *xpressI)
117 {
118 	xpress_uuid_t uuid = 0;
119 	targetI_t *src = xpress_create(xpressI, &uuid);
120 	assert(uuid != 0);
121 	assert(src != NULL);
122 
123 	assert(xpress_get(xpressI, uuid) == src);
124 	assert(xpress_free(xpressI, uuid) == 1);
125 	assert(xpress_get(xpressI, uuid) == NULL);
126 	assert(xpress_free(xpressI, uuid) == 0);
127 }
128 
129 static void
_test_2(xpress_t * xpressI)130 _test_2(xpress_t *xpressI)
131 {
132 	xpress_uuid_t uuids [MAX_NVOICES] = { 0 };
133 
134 	for(unsigned i = 0; i < MAX_NVOICES; i++)
135 	{
136 		xpress_uuid_t uuid = 0;
137 		targetI_t *src = xpress_create(xpressI, &uuid);
138 		assert(uuid != 0);
139 		assert(src != NULL);
140 
141 		assert(xpress_get(xpressI, uuid) == src);
142 
143 		uuids[i] = uuid;
144 	}
145 
146 	// test overflow
147 	{
148 		xpress_uuid_t uuid = 0;
149 		targetI_t *src = xpress_create(xpressI, &uuid);
150 		assert(uuid != 0);
151 		assert(src == NULL);
152 	}
153 
154 	// test uniqueness of uuids and srcs
155 	for(unsigned i = 0; i < MAX_NVOICES; i++)
156 	{
157 		targetI_t *src0 = xpress_get(xpressI, uuids[i]);
158 
159 		for(unsigned j = 0; j < MAX_NVOICES; j++)
160 		{
161 			targetI_t *src1 = xpress_get(xpressI, uuids[j]);
162 
163 			if(i == j)
164 			{
165 				assert(uuids[i] == uuids[j]);
166 				assert(src0 == src1);
167 			}
168 			else
169 			{
170 				assert(uuids[i] != uuids[j]);
171 				assert(src0 != src1);
172 			}
173 		}
174 	}
175 }
176 
177 static const test_t tests [] = {
178 	_test_1,
179 	_test_2,
180 	NULL
181 };
182 
183 int
main(int argc,char ** argv)184 main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
185 {
186 	static plughandle_t handle;
187 
188 	for(const test_t *test = tests; *test; test++)
189 	{
190 		assert(xpress_init(&handle.xpressI, MAX_NVOICES, &map, &voice_map,
191 				XPRESS_EVENT_ALL, &ifaceI, handle.targetI, &handle) == 1);
192 
193 		(*test)(&handle.xpressI);
194 
195 		xpress_deinit(&handle.xpressI);
196 	}
197 
198 	for(unsigned i=0; i<urid; i++)
199 	{
200 		urid_t *itm = &urids[i];
201 
202 		free(itm->uri);
203 	}
204 
205 	return 0;
206 }
207