1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 2011 Martin Nordholts <martinn@src.gnome.org>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 3 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include <glib-object.h>
22
#include "core/core-types.h"
24
#include "core/gimpidtable.h"
27
#define ADD_TEST(function) \
28
g_test_add ("/gimpidtable/" #function, \
31
gimp_test_id_table_setup, \
33
gimp_test_id_table_teardown);
38
GimpIdTable *id_table;
42
static gpointer data1 = (gpointer) 0x00000001;
43
static gpointer data2 = (gpointer) 0x00000002;
47
gimp_test_id_table_setup (GimpTestFixture *fixture,
50
fixture->id_table = gimp_id_table_new ();
54
gimp_test_id_table_teardown (GimpTestFixture *fixture,
57
g_object_unref (fixture->id_table);
58
fixture->id_table = NULL;
64
* Test that insert and lookup works.
67
insert_and_lookup (GimpTestFixture *f,
70
gint ret_id = gimp_id_table_insert (f->id_table, data1);
71
gpointer ret_data = gimp_id_table_lookup (f->id_table, ret_id);
73
g_assert (ret_data == data1);
79
* Test that two consecutive inserts generates different IDs.
82
insert_twice (GimpTestFixture *f,
85
gint ret_id = gimp_id_table_insert (f->id_table, data1);
86
gpointer ret_data = gimp_id_table_lookup (f->id_table, ret_id);
87
gint ret_id2 = gimp_id_table_insert (f->id_table, data2);
88
gpointer ret_data2 = gimp_id_table_lookup (f->id_table, ret_id2);
90
g_assert (ret_id != ret_id2);
91
g_assert (ret_data == data1);
92
g_assert (ret_data2 == data2);
98
* Test that it is possible to insert data with a specific ID.
101
insert_with_id (GimpTestFixture *f,
106
int ret_id = gimp_id_table_insert_with_id (f->id_table, id, data1);
107
gpointer ret_data = gimp_id_table_lookup (f->id_table, id);
109
g_assert (ret_id == id);
110
g_assert (ret_data == data1);
114
* insert_with_id_existing:
116
* Test that it is not possible to insert data with a specific ID if
117
* that ID already is inserted.
120
insert_with_id_existing (GimpTestFixture *f,
125
int ret_id = gimp_id_table_insert_with_id (f->id_table, id, data1);
126
gpointer ret_data = gimp_id_table_lookup (f->id_table, ret_id);
127
int ret_id2 = gimp_id_table_insert_with_id (f->id_table, id, data2);
128
gpointer ret_data2 = gimp_id_table_lookup (f->id_table, ret_id2);
130
g_assert (id == ret_id);
131
g_assert (ret_id2 == -1);
132
g_assert (ret_data == data1);
133
g_assert (ret_data2 == NULL);
139
* Test that it is possible to replace data with a given ID with
143
replace (GimpTestFixture *f,
146
int ret_id = gimp_id_table_insert (f->id_table, data1);
149
gimp_id_table_replace (f->id_table, ret_id, data2);
150
ret_data = gimp_id_table_lookup (f->id_table, ret_id);
152
g_assert (ret_data == data2);
158
* Test that replace works like insert when there is no data to
162
replace_as_insert (GimpTestFixture *f,
169
gimp_id_table_replace (f->id_table, id, data1);
170
ret_data = gimp_id_table_lookup (f->id_table, id);
172
g_assert (ret_data == data1);
178
* Test that it is possible to remove data identified by the ID:
181
remove (GimpTestFixture *f,
184
gint ret_id = gimp_id_table_insert (f->id_table, data1);
185
void *ret_data = gimp_id_table_lookup (f->id_table, ret_id);
186
gboolean remove_successful = gimp_id_table_remove (f->id_table, ret_id);
187
void *ret_data2 = gimp_id_table_lookup (f->id_table, ret_id);
189
g_assert (remove_successful);
190
g_assert (ret_data == data1);
191
g_assert (ret_data2 == NULL);
195
* remove_non_existing:
197
* Tests that things work properly when trying to remove data with an
198
* ID that doesn't exist.
201
remove_non_existing (GimpTestFixture *f,
206
gboolean remove_successful = gimp_id_table_remove (f->id_table, id);
207
void *ret_data = gimp_id_table_lookup (f->id_table, id);
209
g_assert (! remove_successful);
210
g_assert (ret_data == NULL);
213
int main(int argc, char **argv)
216
g_test_init (&argc, &argv, NULL);
218
ADD_TEST (insert_and_lookup);
219
ADD_TEST (insert_twice);
220
ADD_TEST (insert_with_id);
221
ADD_TEST (insert_with_id_existing);
223
ADD_TEST (replace_as_insert);
225
ADD_TEST (remove_non_existing);
227
return g_test_run ();