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
|
/*
* test-stardict.c: StarDict API test
*
* Copyright (c) 2013, Přemysl Janouch <p@janouch.name>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
#include "stardict.h"
#include "stardict-private.h"
#include "generator.h"
// --- Utilities ---------------------------------------------------------------
// Adapted http://gezeiten.org/post/2009/04/Writing-Your-Own-GIO-Jobs
static gboolean remove_recursive (GFile *file, GError **error);
static gboolean
remove_directory_contents (GFile *file, GError **error)
{
GFileEnumerator *enumerator =
g_file_enumerate_children (file, "standard::*",
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, error);
if (!enumerator)
return FALSE;
gboolean success = TRUE;
do
{
GError *err = NULL;
GFileInfo *child_info =
g_file_enumerator_next_file (enumerator, NULL, &err);
if (!child_info)
{
if (err)
{
g_propagate_error (error, err);
success = FALSE;
}
break;
}
GFile *child = g_file_resolve_relative_path
(file, g_file_info_get_name (child_info));
success = remove_recursive (child, error);
g_object_unref (child);
g_object_unref (child_info);
}
while (success);
g_object_unref (enumerator);
return success;
}
static gboolean
remove_recursive (GFile *file, GError **error)
{
g_return_val_if_fail (G_IS_FILE (file), FALSE);
GFileInfo *info = g_file_query_info (file, "standard::*",
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, error);
if (!info)
return FALSE;
GFileType type = g_file_info_get_file_type (info);
g_object_unref (info);
if (type == G_FILE_TYPE_DIRECTORY &&
!remove_directory_contents (file, error))
return FALSE;
return g_file_delete (file, NULL, error);
}
static gchar *
generate_random_string (gsize length, GRand *rand)
{
GString *s = g_string_sized_new (length);
while (length--)
g_string_append_c (s, g_rand_int_range (rand, 'a', 'z' + 1));
return g_string_free (s, FALSE);
}
static gpointer
generate_random_data (gsize length, GRand *rand)
{
gchar *blob = g_malloc (length), *i = blob;
while (length--)
*i++ = g_rand_int_range (rand, 0, 256);
return blob;
}
// --- Dictionary generation ---------------------------------------------------
typedef struct dictionary Dictionary;
typedef struct test_entry TestEntry;
struct dictionary
{
GFile *tmp_dir; ///< A temporary dictionary
GFile *ifo_file; ///< The dictionary's .ifo file
GArray *data; ///< Array of TestEntry's
};
struct test_entry
{
gchar *word;
gchar *meaning;
gpointer data;
gsize data_size;
};
static void
test_entry_free (TestEntry *te)
{
g_free (te->word);
g_free (te->meaning);
g_free (te->data);
}
static gint
test_entry_word_compare (gconstpointer a, gconstpointer b)
{
return strcmp (((TestEntry *) a)->word, ((TestEntry *) b)->word);
}
static GArray *
generate_dictionary_data (gsize length)
{
GRand *rand = g_rand_new_with_seed (0);
GArray *a = g_array_sized_new (FALSE, FALSE, sizeof (TestEntry), length);
g_array_set_clear_func (a, (GDestroyNotify) test_entry_free);
while (length--)
{
TestEntry te;
te.word = generate_random_string
(g_rand_int_range (rand, 1, 10), rand);
te.meaning = generate_random_string
(g_rand_int_range (rand, 1, 1024), rand);
te.data_size = g_rand_int_range (rand, 0, 1048576);
te.data = generate_random_data (te.data_size, rand);
g_array_append_val (a, te);
}
g_rand_free (rand);
g_array_sort (a, test_entry_word_compare);
return a;
}
static Dictionary *
dictionary_create (void)
{
GError *error = NULL;
gchar *tmp_dir_path = g_dir_make_tmp ("stardict-test-XXXXXX", &error);
if (!tmp_dir_path)
g_error ("Failed to create a directory for the test dictionary: %s",
error->message);
Dictionary *dict = g_malloc (sizeof *dict);
dict->tmp_dir = g_file_new_for_path (tmp_dir_path);
dict->ifo_file = g_file_get_child (dict->tmp_dir, "test.ifo");
gchar *base = g_build_filename (tmp_dir_path, "test", NULL);
Generator *generator = generator_new (base, &error);
g_free (base);
if (!generator)
g_error ("Failed to create a dictionary: %s", error->message);
static const guint dictionary_size = 8;
dict->data = generate_dictionary_data (dictionary_size);
generator->info->version = SD_VERSION_3_0_0;
generator->info->book_name = g_strdup ("Test Book");
generator->info->author = g_strdup ("Lyra Heartstrings");
generator->info->email = g_strdup ("lyra@equestria.net");
generator->info->description = g_strdup ("Test dictionary");
generator->info->date = g_strdup ("21.12.2012");
generator->info->same_type_sequence = g_strdup ("mX");
guint i;
for (i = 0; i < dictionary_size; i++)
{
TestEntry *te = &g_array_index (dict->data, TestEntry, i);
generator_begin_entry (generator);
if (!generator_write_string (generator, te->meaning, TRUE, &error)
|| !generator_write_raw (generator,
te->data, te->data_size, FALSE, &error))
g_error ("Write to dictionary data failed: %s", error->message);
if (!generator_finish_entry (generator, te->word, &error))
g_error ("Write to index failed: %s", error->message);
}
if (!generator_finish (generator, &error))
g_error ("Failed to finish the dictionary: %s", error->message);
g_message ("Successfully created a test dictionary in %s", tmp_dir_path);
generator_free (generator);
g_free (tmp_dir_path);
return dict;
}
static void
dictionary_destroy (Dictionary *dict)
{
GError *error = NULL;
if (!remove_recursive (dict->tmp_dir, &error))
g_error ("Failed to delete the temporary directory: %s",
error->message);
g_message ("The test dictionary has been deleted");
g_object_unref (dict->tmp_dir);
g_object_unref (dict->ifo_file);
g_array_free (dict->data, TRUE);
g_free (dict);
}
// --- Testing -----------------------------------------------------------------
typedef struct dict_fixture DictFixture;
struct dict_fixture
{
StardictDict *dict;
};
static void
dict_setup (DictFixture *fixture, gconstpointer test_data)
{
Dictionary *dict = (Dictionary *) test_data;
gchar *ifo_filename = g_file_get_path (dict->ifo_file);
fixture->dict = stardict_dict_new (ifo_filename, NULL);
g_free (ifo_filename);
}
static void
dict_teardown (DictFixture *fixture, G_GNUC_UNUSED gconstpointer test_data)
{
g_object_unref (fixture->dict);
}
static void
dict_test_list (gconstpointer user_data)
{
Dictionary *dict = (Dictionary *) user_data;
gchar *tmp_path = g_file_get_path (dict->tmp_dir);
GList *dictionaries = stardict_list_dictionaries (tmp_path);
g_free (tmp_path);
g_assert (dictionaries != NULL);
g_assert (dictionaries->next == NULL);
StardictInfo *info = dictionaries->data;
GFile *ifo_file = g_file_new_for_path (stardict_info_get_path (info));
g_assert (g_file_equal (ifo_file, dict->ifo_file) == TRUE);
g_object_unref (ifo_file);
g_list_free_full (dictionaries, (GDestroyNotify) stardict_info_free);
}
static void
dict_test_new (gconstpointer user_data)
{
Dictionary *dict = (Dictionary *) user_data;
gchar *ifo_filename = g_file_get_path (dict->ifo_file);
StardictDict *sd = stardict_dict_new (ifo_filename, NULL);
g_free (ifo_filename);
g_assert (sd != NULL);
g_object_unref (sd);
}
static void
dict_test_data_entry (StardictDict *sd, TestEntry *entry)
{
gboolean success;
StardictIterator *sdi =
stardict_dict_search (sd, entry->word, &success);
g_assert (success == TRUE);
g_assert (sdi != NULL);
g_assert (stardict_iterator_is_valid (sdi));
const gchar *word = stardict_iterator_get_word (sdi);
g_assert_cmpstr (word, ==, entry->word);
StardictEntry *sde = stardict_iterator_get_entry (sdi);
g_assert (sde != NULL);
const GList *fields = stardict_entry_get_fields (sde);
const StardictEntryField *sdef;
g_assert (fields != NULL);
g_assert (fields->data != NULL);
sdef = fields->data;
g_assert (sdef->type == 'm');
g_assert_cmpstr (sdef->data, ==, entry->meaning);
fields = fields->next;
g_assert (fields != NULL);
g_assert (fields->data != NULL);
sdef = fields->data;
g_assert (sdef->type == 'X');
g_assert_cmpuint (sdef->data_size, ==, entry->data_size);
g_assert (memcmp (sdef->data, entry->data, entry->data_size) == 0);
fields = fields->next;
g_assert (fields == NULL);
g_object_unref (sde);
g_object_unref (sdi);
}
static void
dict_test_data (DictFixture *fixture, gconstpointer user_data)
{
Dictionary *dict = (Dictionary *) user_data;
GArray *data = dict->data;
StardictDict *sd = fixture->dict;
guint i;
for (i = 0; i < data->len; i++)
{
TestEntry *entry = &g_array_index (data, TestEntry, i);
dict_test_data_entry (sd, entry);
}
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
if (glib_check_version (2, 36, 0))
g_type_init ();
G_GNUC_END_IGNORE_DEPRECATIONS
Dictionary *dict = dictionary_create ();
g_test_add_data_func ("/dict/list", dict, dict_test_list);
g_test_add_data_func ("/dict/new", dict, dict_test_new);
g_test_add ("/dict/data", DictFixture, dict,
dict_setup, dict_test_data, dict_teardown);
int result = g_test_run ();
dictionary_destroy (dict);
return result;
}
|