summaryrefslogtreecommitdiff
path: root/testsuite/gtk/filter.c
blob: 0a109e6fa3a14bfccc25e7b7011f2c2400ade85b (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
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
/* GtkRBTree tests.
 *
 * Copyright (C) 2011, Red Hat, Inc.
 * Authors: Benjamin Otte <otte@gnome.org>
 *
 * 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 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/>.
 */

#include <locale.h>

#include <gtk/gtk.h>

static GQuark number_quark;

static guint
get (GListModel *model,
     guint       position)
{
  GObject *object = g_list_model_get_item (model, position);
  guint ret;
  g_assert (object != NULL);
  ret = GPOINTER_TO_UINT (g_object_get_qdata (object, number_quark));
  g_object_unref (object);
  return ret;
}

static char *
get_string (gpointer object)
{
  return g_strdup_printf ("%u", GPOINTER_TO_UINT (g_object_get_qdata (object, number_quark)));
}

static void
append_digit (GString *s,
              guint    digit)
{
  static const char *names[10] = { NULL, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

  if (digit == 0)
    return;

  g_assert (digit < 10);

  if (s->len)
    g_string_append_c (s, ' ');
  g_string_append (s, names[digit]);
}

static void
append_below_thousand (GString *s,
                       guint    n)
{
  if (n >= 100)
    {
      append_digit (s, n / 100);
      g_string_append (s, " hundred");
      n %= 100;
    }

  if (n >= 20)
    {
      const char *names[10] = { NULL, NULL, "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
      if (s->len)
        g_string_append_c (s, ' ');
      g_string_append (s, names [n / 10]);
      n %= 10;
    }

  if (n >= 10)
    {
      const char *names[10] = { "ten", "eleven", "twelve", "thirteen", "fourteen",
                                "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
      if (s->len)
        g_string_append_c (s, ' ');
      g_string_append (s, names [n - 10]);
    }
  else
    {
      append_digit (s, n);
    }
}

static char *
get_spelled_out (gpointer object)
{
  guint n = GPOINTER_TO_UINT (g_object_get_qdata (object, number_quark));
  GString *s;

  g_assert (n < 1000000);

  if (n == 0)
    return g_strdup ("Zero");

  s = g_string_new (NULL);

  if (n >= 1000)
    {
      append_below_thousand (s, n / 1000);
      g_string_append (s, " thousand");
      n %= 1000;
    }

  append_below_thousand (s, n);

  /* Capitalize first letter so we can do case-sensitive matching */
  s->str[0] = g_ascii_toupper (s->str[0]);

  return g_string_free (s, FALSE);
}

static char *
model_to_string (GListModel *model)
{
  GString *string = g_string_new (NULL);
  guint i;

  for (i = 0; i < g_list_model_get_n_items (model); i++)
    {
      if (i > 0)
        g_string_append (string, " ");
      g_string_append_printf (string, "%u", get (model, i));
    }

  return g_string_free (string, FALSE);
}

static GListStore *
new_store (guint start,
           guint end,
           guint step);

static void
add (GListStore *store,
     guint       number)
{
  GObject *object;

  /* 0 cannot be differentiated from NULL, so don't use it */
  g_assert (number != 0);

  object = g_object_new (G_TYPE_OBJECT, NULL);
  g_object_set_qdata (object, number_quark, GUINT_TO_POINTER (number));
  g_list_store_append (store, object);
  g_object_unref (object);
}

#define assert_model(model, expected) G_STMT_START{ \
  char *s = model_to_string (G_LIST_MODEL (model)); \
  if (!g_str_equal (s, expected)) \
     g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
         #model " == " #expected, s, "==", expected); \
  g_free (s); \
}G_STMT_END

static GListStore *
new_empty_store (void)
{
  return g_list_store_new (G_TYPE_OBJECT);
}

static GListStore *
new_store (guint start,
           guint end,
           guint step)
{
  GListStore *store = new_empty_store ();
  guint i;

  for (i = start; i <= end; i += step)
    add (store, i);

  return store;
}

static GtkFilterListModel *
new_model (guint      size,
           GtkFilter *filter)
{
  GtkFilterListModel *result;

  result = gtk_filter_list_model_new (g_object_ref (G_LIST_MODEL (new_store (1, size, 1))), g_object_ref (filter));

  return result;
}

static gboolean
divisible_by (gpointer item,
              gpointer data)
{
  return GPOINTER_TO_UINT (g_object_get_qdata (item, number_quark)) % GPOINTER_TO_UINT (data) == 0;
}

static void
test_simple (void)
{
  GtkFilterListModel *model;
  GtkFilter *filter;

  filter = gtk_custom_filter_new (divisible_by, GUINT_TO_POINTER (3), NULL);
  model = new_model (20, filter);
  g_object_unref (filter);
  assert_model (model, "3 6 9 12 15 18");
  g_object_unref (model);
}

static void
test_any_simple (void)
{
  GtkFilterListModel *model;
  GtkFilter *any, *filter1, *filter2;

  any = gtk_any_filter_new ();
  filter1 = gtk_custom_filter_new (divisible_by, GUINT_TO_POINTER (3), NULL);
  filter2 = gtk_custom_filter_new (divisible_by, GUINT_TO_POINTER (5), NULL);

  model = new_model (20, any);
  assert_model (model, "");

  gtk_multi_filter_append (GTK_MULTI_FILTER (any), filter1);
  assert_model (model, "3 6 9 12 15 18");

  gtk_multi_filter_append (GTK_MULTI_FILTER (any), filter2);
  assert_model (model, "3 5 6 9 10 12 15 18 20");

  gtk_multi_filter_remove (GTK_MULTI_FILTER (any), 0);
  assert_model (model, "5 10 15 20");

  /* doesn't exist */
  gtk_multi_filter_remove (GTK_MULTI_FILTER (any), 10);
  assert_model (model, "5 10 15 20");

  gtk_multi_filter_remove (GTK_MULTI_FILTER (any), 0);
  assert_model (model, "");

  g_object_unref (model);
  g_object_unref (any);
}

static void
test_string_simple (void)
{
  GtkFilterListModel *model;
  GtkFilter *filter;

  filter = gtk_string_filter_new (
               gtk_cclosure_expression_new (G_TYPE_STRING,
                                            NULL,
                                            0, NULL,
                                            G_CALLBACK (get_string),
                                            NULL, NULL));

  model = new_model (20, filter);
  assert_model (model, "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20");

  gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "1");
  assert_model (model, "1 10 11 12 13 14 15 16 17 18 19");

  g_object_unref (model);
  g_object_unref (filter);
}

static void
test_string_properties (void)
{
  GtkFilterListModel *model;
  GtkFilter *filter;

  filter = gtk_string_filter_new (
               gtk_cclosure_expression_new (G_TYPE_STRING,
                                            NULL,
                                            0, NULL,
                                            G_CALLBACK (get_spelled_out),
                                            NULL, NULL));

  model = new_model (1000, filter);
  gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "thirte");
  assert_model (model, "13 113 213 313 413 513 613 713 813 913");

  gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "thirteen");
  assert_model (model, "13 113 213 313 413 513 613 713 813 913");

  gtk_string_filter_set_ignore_case (GTK_STRING_FILTER (filter), FALSE);
  assert_model (model, "113 213 313 413 513 613 713 813 913");

  gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "Thirteen");
  assert_model (model, "13");

  gtk_string_filter_set_match_mode (GTK_STRING_FILTER (filter), GTK_STRING_FILTER_MATCH_MODE_EXACT);
  assert_model (model, "13");

  gtk_string_filter_set_ignore_case (GTK_STRING_FILTER (filter), TRUE);
  assert_model (model, "13");

  gtk_string_filter_set_match_mode (GTK_STRING_FILTER (filter), GTK_STRING_FILTER_MATCH_MODE_SUBSTRING);
  assert_model (model, "13 113 213 313 413 513 613 713 813 913");

  g_object_unref (model);
  g_object_unref (filter);
}

static void
test_every_dispose (void)
{
  GtkFilter *filter, *filter1, *filter2;

  filter = gtk_every_filter_new ();

  filter1 = gtk_custom_filter_new (divisible_by, GUINT_TO_POINTER (3), NULL);
  filter2 = gtk_custom_filter_new (divisible_by, GUINT_TO_POINTER (5), NULL);

  g_object_ref (filter1);
  g_object_ref (filter2);

  gtk_multi_filter_append (GTK_MULTI_FILTER (filter), filter1);
  gtk_multi_filter_append (GTK_MULTI_FILTER (filter), filter2);

  g_object_unref (filter);

  g_object_unref (filter1);
  g_object_unref (filter2);
}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);
  setlocale (LC_ALL, "C");

  number_quark = g_quark_from_static_string ("Hell and fire was spawned to be released.");

  g_test_add_func ("/filter/simple", test_simple);
  g_test_add_func ("/filter/any/simple", test_any_simple);
  g_test_add_func ("/filter/string/simple", test_string_simple);
  g_test_add_func ("/filter/string/properties", test_string_properties);
  g_test_add_func ("/filter/every/dispose", test_every_dispose);

  return g_test_run ();
}