summaryrefslogtreecommitdiffstats
path: root/app/tests/test-gimpidtable.c
blob: 8de21308423769444908e86168dd0840d8e72b6e (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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 2011 Martin Nordholts <martinn@src.gnome.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib-object.h>

#include "core/core-types.h"

#include "core/gimpidtable.h"


#define ADD_TEST(function) \
  g_test_add ("/gimpidtable/" #function, \
              GimpTestFixture, \
              NULL, \
              gimp_test_id_table_setup, \
              gimp_test_id_table_ ## function, \
              gimp_test_id_table_teardown);


typedef struct
{
  GimpIdTable *id_table;
} GimpTestFixture;


static gpointer data1 = (gpointer) 0x00000001;
static gpointer data2 = (gpointer) 0x00000002;


static void
gimp_test_id_table_setup (GimpTestFixture *fixture,
                          gconstpointer    data)
{
  fixture->id_table = gimp_id_table_new ();
}

static void
gimp_test_id_table_teardown (GimpTestFixture *fixture,
                             gconstpointer    data)
{
  g_object_unref (fixture->id_table);
  fixture->id_table = NULL;
}

/**
 * gimp_test_id_table_insert_and_lookup:
 *
 * Test that insert and lookup works.
 **/
static void
gimp_test_id_table_insert_and_lookup (GimpTestFixture *f,
                                      gconstpointer    data)
{
  gint     ret_id   = gimp_id_table_insert (f->id_table, data1);
  gpointer ret_data = gimp_id_table_lookup (f->id_table, ret_id);

  g_assert (ret_data == data1);
}

/**
 * gimp_test_id_table_insert_twice:
 *
 * Test that two consecutive inserts generates different IDs.
 **/
static void
gimp_test_id_table_insert_twice (GimpTestFixture *f,
                                 gconstpointer    data)
{
  gint     ret_id    = gimp_id_table_insert (f->id_table, data1);
  gpointer ret_data  = gimp_id_table_lookup (f->id_table, ret_id);
  gint     ret_id2   = gimp_id_table_insert (f->id_table, data2);
  gpointer ret_data2 = gimp_id_table_lookup (f->id_table, ret_id2);

  g_assert (ret_id    != ret_id2);
  g_assert (ret_data  == data1);
  g_assert (ret_data2 == data2);
}

/**
 * gimp_test_id_table_insert_with_id:
 *
 * Test that it is possible to insert data with a specific ID.
 **/
static void
gimp_test_id_table_insert_with_id (GimpTestFixture *f,
                                   gconstpointer    data)
{
  const int id = 10;

  int      ret_id   = gimp_id_table_insert_with_id (f->id_table, id, data1);
  gpointer ret_data = gimp_id_table_lookup (f->id_table, id);

  g_assert (ret_id   == id);
  g_assert (ret_data == data1);
}

/**
 * gimp_test_id_table_insert_with_id_existing:
 *
 * Test that it is not possible to insert data with a specific ID if
 * that ID already is inserted.
 **/
static void
gimp_test_id_table_insert_with_id_existing (GimpTestFixture *f,
                                            gconstpointer    data)
{
  const int id = 10;

  int      ret_id    = gimp_id_table_insert_with_id (f->id_table, id, data1);
  gpointer ret_data  = gimp_id_table_lookup (f->id_table, ret_id);
  int      ret_id2   = gimp_id_table_insert_with_id (f->id_table, id, data2);
  gpointer ret_data2 = gimp_id_table_lookup (f->id_table, ret_id2);

  g_assert (id        == ret_id);
  g_assert (ret_id2   == -1);
  g_assert (ret_data  == data1);
  g_assert (ret_data2 == NULL);
}

/**
 * gimp_test_id_table_replace:
 *
 * Test that it is possible to replace data with a given ID with
 * different data.
 **/
static void
gimp_test_id_table_replace (GimpTestFixture *f,
                            gconstpointer    data)
{
  int ret_id = gimp_id_table_insert (f->id_table, data1);
  gpointer ret_data;

  gimp_id_table_replace (f->id_table, ret_id, data2);
  ret_data = gimp_id_table_lookup (f->id_table, ret_id);

  g_assert (ret_data  == data2);
}

/**
 * gimp_test_id_table_replace_as_insert:
 *
 * Test that replace works like insert when there is no data to
 * replace.
 **/
static void
gimp_test_id_table_replace_as_insert (GimpTestFixture *f,
                                      gconstpointer    data)
{
  const int id = 10;

  gpointer ret_data;

  gimp_id_table_replace (f->id_table, id, data1);
  ret_data = gimp_id_table_lookup (f->id_table, id);

  g_assert (ret_data  == data1);
}

/**
 * gimp_test_id_table_remove:
 *
 * Test that it is possible to remove data identified by the ID:
 **/
static void
gimp_test_id_table_remove (GimpTestFixture *f,
                           gconstpointer    data)
{
  gint     ret_id            = gimp_id_table_insert (f->id_table, data1);
  void    *ret_data          = gimp_id_table_lookup (f->id_table, ret_id);
  gboolean remove_successful = gimp_id_table_remove (f->id_table, ret_id);
  void    *ret_data2         = gimp_id_table_lookup (f->id_table, ret_id);

  g_assert (remove_successful);
  g_assert (ret_data == data1);
  g_assert (ret_data2 == NULL);
}

/**
 * gimp_test_id_table_remove_non_existing:
 *
 * Tests that things work properly when trying to remove data with an
 * ID that doesn't exist.
 **/
static void
gimp_test_id_table_remove_non_existing (GimpTestFixture *f,
                                        gconstpointer    data)
{
  const int id = 10;

  gboolean remove_successful = gimp_id_table_remove (f->id_table, id);
  void    *ret_data          = gimp_id_table_lookup (f->id_table, id);

  g_assert (! remove_successful);
  g_assert (ret_data == NULL);
}

int main(int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);

  ADD_TEST (insert_and_lookup);
  ADD_TEST (insert_twice);
  ADD_TEST (insert_with_id);
  ADD_TEST (insert_with_id_existing);
  ADD_TEST (replace);
  ADD_TEST (replace_as_insert);
  ADD_TEST (remove);
  ADD_TEST (remove_non_existing);

  return g_test_run ();
}