aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests/intset.c
blob: df7567f01eea89a7ad195ebe2b8e895f98d5a56e (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
/* main.vala
 *
 * Copyright (C) 2008  Rob Taylor
 *
 * This library 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, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library 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 library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author:
 *	Rob Taylor <rob.taylor@codethink.co.uk>
 */

#include <tests/unit_tests/intset.h>
#include <intset.h>




static void _test_intset_new_gcallback (void);
static void _test_intset_add_gcallback (void);
static void _test_intset_intersect_gcallback (void);
static void _test_intset_disjoint_gcallback (void);
static int _vala_strcmp0 (const char * str1, const char * str2);




GType test_values_get_type (void) {
	static GType test_values_type_id = 0;
	if (G_UNLIKELY (test_values_type_id == 0)) {
		static const GEnumValue values[] = {{TEST_VALUES_A, "TEST_VALUES_A", "a"}, {TEST_VALUES_B, "TEST_VALUES_B", "b"}, {TEST_VALUES_C, "TEST_VALUES_C", "c"}, {TEST_VALUES_D, "TEST_VALUES_D", "d"}, {TEST_VALUES_E, "TEST_VALUES_E", "e"}, {0, NULL, NULL}};
		test_values_type_id = g_enum_register_static ("TestValues", values);
	}
	return test_values_type_id;
}


void test_intset_new (void) {
	ContextProviderIntSet* intset;
	intset = context_provider_intset_new ();
	g_assert (_vala_strcmp0 (context_provider_intset_dump (intset), "") == 0);
	(intset == NULL) ? NULL : (intset = (context_provider_intset_destroy (intset), NULL));
}


void test_intset_add (void) {
	ContextProviderIntSet* intset;
	intset = context_provider_intset_new ();
	context_provider_intset_add (intset, (guint) TEST_VALUES_A);
	context_provider_intset_add (intset, (guint) TEST_VALUES_B);
	context_provider_intset_add (intset, (guint) TEST_VALUES_C);
	context_provider_intset_add (intset, (guint) TEST_VALUES_D);
	context_provider_intset_add (intset, (guint) TEST_VALUES_E);
	g_assert (context_provider_intset_is_member (intset, (guint) TEST_VALUES_A));
	g_assert (context_provider_intset_is_member (intset, (guint) TEST_VALUES_B));
	g_assert (context_provider_intset_is_member (intset, (guint) TEST_VALUES_C));
	g_assert (context_provider_intset_is_member (intset, (guint) TEST_VALUES_D));
	g_assert (context_provider_intset_is_member (intset, (guint) TEST_VALUES_E));
	(intset == NULL) ? NULL : (intset = (context_provider_intset_destroy (intset), NULL));
}


void test_intset_intersect (void) {
	ContextProviderIntSet* i1;
	ContextProviderIntSet* i2;
	ContextProviderIntSet* i3;
	i1 = context_provider_intset_new ();
	i2 = context_provider_intset_new ();
	context_provider_intset_add (i1, (guint) TEST_VALUES_A);
	context_provider_intset_add (i1, (guint) TEST_VALUES_B);
	context_provider_intset_add (i1, (guint) TEST_VALUES_C);
	context_provider_intset_add (i2, (guint) TEST_VALUES_C);
	context_provider_intset_add (i2, (guint) TEST_VALUES_D);
	context_provider_intset_add (i2, (guint) TEST_VALUES_E);
	i3 = context_provider_intset_intersection (i1, i2);
	g_assert (context_provider_intset_is_member (i3, (guint) TEST_VALUES_C));
	g_assert (!context_provider_intset_is_member (i3, (guint) TEST_VALUES_A));
	g_assert (!context_provider_intset_is_member (i3, (guint) TEST_VALUES_B));
	g_assert (!context_provider_intset_is_member (i3, (guint) TEST_VALUES_D));
	g_assert (!context_provider_intset_is_member (i3, (guint) TEST_VALUES_E));
	(i1 == NULL) ? NULL : (i1 = (context_provider_intset_destroy (i1), NULL));
	(i2 == NULL) ? NULL : (i2 = (context_provider_intset_destroy (i2), NULL));
	(i3 == NULL) ? NULL : (i3 = (context_provider_intset_destroy (i3), NULL));
}


void test_intset_disjoint (void) {
	ContextProviderIntSet* i1;
	ContextProviderIntSet* i2;
	i1 = context_provider_intset_new ();
	i2 = context_provider_intset_new ();
	context_provider_intset_add (i1, (guint) TEST_VALUES_A);
	context_provider_intset_add (i1, (guint) TEST_VALUES_B);
	context_provider_intset_add (i1, (guint) TEST_VALUES_C);
	context_provider_intset_add (i2, (guint) TEST_VALUES_C);
	context_provider_intset_add (i2, (guint) TEST_VALUES_D);
	context_provider_intset_add (i2, (guint) TEST_VALUES_E);
	g_assert (!context_provider_intset_is_disjoint (i1, i2));
	context_provider_intset_remove (i1, (guint) TEST_VALUES_C);
	g_assert (context_provider_intset_is_disjoint (i1, i2));
	(i1 == NULL) ? NULL : (i1 = (context_provider_intset_destroy (i1), NULL));
	(i2 == NULL) ? NULL : (i2 = (context_provider_intset_destroy (i2), NULL));
}


static void _test_intset_new_gcallback (void) {
	test_intset_new ();
}


static void _test_intset_add_gcallback (void) {
	test_intset_add ();
}


static void _test_intset_intersect_gcallback (void) {
	test_intset_intersect ();
}


static void _test_intset_disjoint_gcallback (void) {
	test_intset_disjoint ();
}


void _main (char** args, int args_length1) {
	g_test_init (&args_length1, &args, NULL);
	g_test_add_func ("/contextkit/intset/new", _test_intset_new_gcallback);
	g_test_add_func ("/contextkit/intset/add", _test_intset_add_gcallback);
	g_test_add_func ("/contextkit/intset/intersect", _test_intset_intersect_gcallback);
	g_test_add_func ("/contextkit/intset/disjoint", _test_intset_disjoint_gcallback);
	g_test_run ();
}


int main (int argc, char ** argv) {
	g_type_init ();
	_main (argv, argc);
	return 0;
}


static int _vala_strcmp0 (const char * str1, const char * str2) {
	if (str1 == NULL) {
		return -(str1 != str2);
	}
	if (str2 == NULL) {
		return str1 != str2;
	}
	return strcmp (str1, str2);
}