File: test-intervaltree.c

package info (click to toggle)
evolution-data-server 3.56.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,656 kB
  • sloc: ansic: 365,352; xml: 578; cpp: 482; perl: 297; sh: 62; makefile: 60; python: 35; javascript: 29
file content (452 lines) | stat: -rw-r--r-- 10,159 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2008
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Stanislav Slusny <slusnys@gmail.com>
 */
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libedata-cal/libedata-cal.h>

#define NUM_INTERVALS_CLOSED  100
#define NUM_INTERVALS_OPEN  100
#define NUM_SEARCHES  500
#define _TIME_MIN	((time_t) 0)		/* Min valid time_t	*/
#define _TIME_MAX	((time_t) INT_MAX)	/* Max valid time_t	*/

GRand *myrand = NULL;
const double pbality_delete = 0.3;

struct _EInterval
{
	gint start;
	gint end;
	ECalComponent * comp;
};

typedef struct _EInterval EInterval;

GList *list = NULL;

static inline gint
compare_intervals (time_t x_start,
                   time_t x_end,
                   time_t y_start,
                   time_t y_end)
{
	/* assumption: x_start <= x_end */
	/* assumption: y_start <= y_end */

	/* x is left of y */
	if (x_end < y_start)
		return -1;

	/* x is right of y */
	if (y_end < x_start)
		return 1;

	/* x and y overlap */
	return 0;
}

static GList *
search_in_list (GList *l,
                time_t start,
                time_t end)
{
	GList * result = NULL;
	EInterval *i, *i_new;

	while (l) {
		i = (EInterval *) l->data;

		if (compare_intervals (start, end, i->start, i->end) == 0) {
			i_new = g_new (EInterval, 1);

			i_new->start = i->start;
			i_new->end = i->end;
			i_new->comp = i->comp;
			g_object_ref (i->comp);

			result = g_list_insert (result, i_new, -1);
		}

		l = l->next;
	}

	return result;
}

/*
 * list1 .... from list
 * list2 ... from interval tree
 */
static gboolean
compare_interval_lists (GList *list1,
                        GList *list2)
{
	gboolean found;
	GList *l2;
	EInterval *i1;
	ECalComponent *c2, *c1;

	found = TRUE;

	while (list1 != NULL && found) {
		found = FALSE;

		l2 = list2;

		i1 = (EInterval *) list1->data;
		c1 = i1->comp;

		while (!found && l2 != NULL) {
			c2 = (ECalComponent *) l2->data;

			found = (c1 == c2);

			l2 = l2->next;
		}

		if (found)
			list1 = list1->next;
	}

	if (!found) {
		i1 = (EInterval *) list1->data;

		g_message (G_STRLOC ": Not found %d - %d\n", i1->start, i1->end);
	}

	return found;
}

static ECalComponent *
create_test_component (time_t start,
                       time_t end)
{
	ECalComponent *comp = e_cal_component_new ();
	ECalComponentText *summary;
	ICalTime *current;
	gchar *txt;
	/* ECalComponentDateTime *dt; */

	e_cal_component_set_new_vtype (comp, E_CAL_COMPONENT_EVENT);

	/*
	dt = e_cal_component_datetime_new_take (i_cal_time_new_from_timet_with_zone (start, 0, NULL), NULL);
	e_cal_component_set_dtstart (comp, dt);
	e_cal_component_datetime_free (dt);

	dt = e_cal_component_datetime_new_take (i_cal_time_new_from_timet_with_zone (end, 0, NULL), NULL);
	e_cal_component_set_dtend (comp, dt);
	e_cal_component_datetime_free (dt);
	*/

	txt = g_strdup_printf ("%" G_GINT64_FORMAT "- %" G_GINT64_FORMAT, (gint64) start, (gint64) end);

	summary = e_cal_component_text_new (txt, NULL);
	e_cal_component_set_summary (comp, summary);
	e_cal_component_text_free (summary);

	g_free (txt);

	current = i_cal_time_new_from_timet_with_zone (time (NULL), 0, NULL);
	e_cal_component_set_created (comp, current);
	e_cal_component_set_last_modified (comp, current);
	g_clear_object (&current);

	return comp;
}

static void
unref_comp (gpointer data,
            gpointer user_data)
{
	EInterval *interval = (EInterval *) data;
	g_object_unref (interval->comp);
	g_free (data);
}

/* Not used at the moment. Use it later 
static void
print_nodes_list (GList *l1)
{
	while (l1) {
		ECalComponent *comp = l1->data;
		ECalComponentText *summary;

		summary = e_cal_component_get_summary (comp);
		g_print ("%s\n", summary ? e_cal_component_text_get_value (summary) : NULL);
		e_cal_component_text_free (summary);
		l1 = l1->next;
	}
}

static void
print_list (GList *l2)
{
	while (l2) {
		EInterval *i = l2->data;

		g_print ("%d - %d\n", i->start, i->end);
		l2 = l2->next;
	}
}
*/

static void
random_test (void)
{
	/*
	 * outline:
	 * 1. create new tree and empty list of intervals
	 * 2. insert some intervals into tree and list
	 * 3. do various searches, compare results of both structures
	 * 4. delete some intervals 
	 * 5. do various searches, compare results of both structures
	 * 6. free memory
	 */
	gint i, start, end;
	EInterval *interval = NULL;
	EIntervalTree *tree;
	GList *l1, *l2, *next;
	gint num_deleted = 0;

	tree = e_intervaltree_new ();

	for (i = 0; i < NUM_INTERVALS_CLOSED; i++) {
		ECalComponent *comp;
		start = g_rand_int_range (myrand, 0, 1000);
		end = g_rand_int_range (myrand, start, 2000);
		comp = create_test_component (start, end);
		if (!comp) {
			g_message (G_STRLOC ": error");
			exit (-1);
		}

		interval = g_new (EInterval, 1);
		interval->start = start;
		interval->end = end;
		interval->comp = comp;

		list = g_list_insert (list, interval, -1);

		e_intervaltree_insert (tree, start, end, comp);
	}

	/* insert open ended intervals */
	for (i = 0; i < NUM_INTERVALS_OPEN; i++) {
		ECalComponent *comp;
		start = g_rand_int_range (myrand, 0, 1000);
		comp = create_test_component (start, end);
		if (!comp)
		{
			g_message (G_STRLOC ": error");
			exit (-1);
		}

		interval = g_new (EInterval, 1);
		interval->start = start;
		interval->end = _TIME_MAX;
		interval->comp = comp;
		list = g_list_insert (list, interval, -1);

		e_intervaltree_insert (tree, start, interval->end, comp);
		/* g_print ("%d - %d\n", start, interval->end); */
	}

	g_print ("Number of intervals inserted: %d\n", NUM_INTERVALS_CLOSED + NUM_INTERVALS_OPEN);

	for (i = 0; i < NUM_SEARCHES; i++) {
		start = g_rand_int_range (myrand, 0, 1000);
		end = g_rand_int_range (myrand, 2000, _TIME_MAX);

		/*
		 * g_print ("Search for : %d - %d\n", start, end);
		 * */

		l1 = e_intervaltree_search (tree, start, end);

		l2 = search_in_list (list, start, end);

		if (!compare_interval_lists (l2, l1)) {
			e_intervaltree_dump (tree);
			g_message (G_STRLOC "Error");
			exit (-1);
		}

		/* g_print ("OK\n"); */
		g_list_foreach (l1, (GFunc) g_object_unref, NULL);
		g_list_foreach (l2, (GFunc) unref_comp, NULL);
		g_list_free (l1);
		g_list_free (l2);
	}

	/* open-ended intervals */
	for (i = 0; i < 20; i++) {
		start = g_rand_int_range (myrand, 0, 1000);
		end = _TIME_MAX;

		/*
		 * g_print ("Search for : %d - %d\n", start, end);
		 * */

		l1 = e_intervaltree_search (tree, start, end);

		l2 = search_in_list (list, start, end);

		if (!compare_interval_lists (l2, l1)) {
			e_intervaltree_dump (tree);
			g_message (G_STRLOC "Error");
			exit (-1);
		}

		/* g_print ("OK\n"); */
		g_list_foreach (l1, (GFunc) g_object_unref, NULL);
		g_list_foreach (l2, (GFunc) unref_comp, NULL);
		g_list_free (l1);
		g_list_free (l2);
	}

	l1 = list;

	while (l1) {
		/* perhaps we will delete l1 */
		next = l1->next;

		if (g_rand_double (myrand) < pbality_delete) {
			ECalComponent *comp;
			const gchar *uid;
			gchar *rid;
			interval = (EInterval *) l1->data;
			comp = interval->comp;

			/* delete l1 */
			/*
			 * g_print ("Deleting interval %d - %d\n", interval->start,
			 * interval->end);
			 * */

			rid = e_cal_component_get_recurid_as_string (comp);
			uid = e_cal_component_get_uid (comp);
			if (!e_intervaltree_remove (tree, uid, rid)) {
				g_free (rid);
				e_intervaltree_dump (tree);
				g_print (
					"Deleting interval %d - %d ERROR\n", interval->start,
					interval->end);
				exit (-1);
			}

			g_free (rid);
			g_object_unref (interval->comp);
			g_free (l1->data);
			list = g_list_delete_link (list, l1);
			num_deleted++;
		}

		l1 = next;
	}

	g_print ("Number of intervals deleted: %d\n", num_deleted);

	for (i = 0; i < NUM_SEARCHES; i++)
	{
		start = g_rand_int_range (myrand, 0, 1000);
		end = g_rand_int_range (myrand, start, 2000);

		/*
		 * g_print ("Search for : %d - %d\n", start, end);
		 * */

		l1 = e_intervaltree_search (tree, start, end);

		/*
		 * g_print ("Results from tree:\n");
		 * print_nodes_list (l1);
		 */

		l2 = search_in_list (list, start, end);

		if (!compare_interval_lists (l2, l1))
		{
			g_print ("ERROR!\n\n");
			return;
		}

		g_list_foreach (l1, (GFunc) g_object_unref, NULL);
		g_list_foreach (l2, (GFunc) unref_comp, NULL);
		g_list_free (l1);
		g_list_free (l2);

		/* g_print ("OK\n"); */
	}

	e_intervaltree_destroy (tree);
	g_list_foreach (list, (GFunc) unref_comp, NULL);
	g_list_free (list);
}

static void
mem_test (void)
{
	EIntervalTree *tree;
	time_t start = 10, end = 50;
	ECalComponent *comp = create_test_component (start, end), *clone_comp;
	const gchar *uid;
	gchar *rid;

	tree = e_intervaltree_new ();

	g_assert_true (((GObject *) comp)->ref_count == 1);
	e_intervaltree_insert (tree, start, end, comp);
	g_assert_true (((GObject *) comp)->ref_count == 2);

	uid = e_cal_component_get_uid (comp);
	rid = e_cal_component_get_recurid_as_string (comp);
	e_intervaltree_remove (tree, uid, rid);
	g_free (rid);
	g_assert_true (((GObject *) comp)->ref_count == 1);

	e_intervaltree_insert (tree, start, end, comp);
	g_assert_true (((GObject *) comp)->ref_count == 2);

	clone_comp = e_cal_component_clone (comp);
	e_intervaltree_insert (tree, start, end, clone_comp);

	g_assert_true (((GObject *) comp)->ref_count == 1);
	g_assert_true (((GObject *) clone_comp)->ref_count == 2);

	e_intervaltree_destroy (tree);

	g_assert_true (((GObject *) comp)->ref_count == 1);
	g_assert_true (((GObject *) clone_comp)->ref_count == 1);

	g_object_unref (comp);
	g_object_unref (clone_comp);
}

gint
main (gint argc,
      gchar **argv)
{
	myrand = g_rand_new ();
	mem_test ();
	random_test ();
	g_print ("Everything OK\n");

	return 0;
}