File: test_walk.c

package info (click to toggle)
python-datrie 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 568 kB
  • sloc: ansic: 3,794; python: 516; makefile: 10; sh: 1
file content (549 lines) | stat: -rw-r--r-- 18,266 bytes parent folder | download | duplicates (5)
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * libdatrie - Double-Array Trie Library
 * Copyright (C) 2013  Theppitak Karoonboonyanan <theppitak@gmail.com>
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * test_walk.c - Test for datrie walking operations
 * Created: 2013-10-16
 * Author:  Theppitak Karoonboonyanan <theppitak@gmail.com>
 */

#include <datrie/trie.h>
#include "utils.h"
#include <stdio.h>

/*
 * Sample trie in http://linux.thai.net/~thep/datrie/datrie.html
 *
 *           +---o-> (3) -o-> (4) -l-> [5]
 *           |
 *           |        +---i-> (7) -z-> (8) -e-> [9]
 *           |        |
 * (1) -p-> (2) -r-> (6) -e-> (10) -v-> (11) -i-> (12) -e-> (13) -w-> [14]
 *                    |         |
 *                    |         +---p-> (15) -a-> (16) -r-> (17) -e-> [18]
 *                    |
 *                    +---o-> (19) -d-> (20) -u-> (21) -c-> (22) -e-> [23]
 *                              |
 *                              +---g-> (24) -r-> (25) -e-> (26) -s-> (27) -s-> [28]
 *
 */
DictRec walk_dict[] = {
    {(AlphaChar *)L"pool",       TRIE_DATA_UNREAD},
    {(AlphaChar *)L"prize",      TRIE_DATA_UNREAD},
    {(AlphaChar *)L"preview",    TRIE_DATA_UNREAD},
    {(AlphaChar *)L"prepare",    TRIE_DATA_UNREAD},
    {(AlphaChar *)L"produce",    TRIE_DATA_UNREAD},
    {(AlphaChar *)L"progress",   TRIE_DATA_UNREAD},
    {(AlphaChar *)NULL,          TRIE_DATA_ERROR},
};

static Bool
is_walkables_include (AlphaChar c, const AlphaChar *walkables, int n_elm)
{
    while (n_elm > 0) {
        if (walkables[--n_elm] == c)
            return TRUE;
    }
    return FALSE;
}

static void
print_walkables (const AlphaChar *walkables, int n_elm)
{
    int i;

    printf ("{");
    for (i = 0; i < n_elm; i++) {
        if (i > 0) {
            printf (", ");
        }
        printf ("'%lc'", walkables[i]);
    }
    printf ("}");
}

#define ALPHABET_SIZE 256

int
main ()
{
    Trie       *test_trie;
    DictRec    *dict_p;
    TrieState  *s, *t, *u;
    AlphaChar   walkables[ALPHABET_SIZE];
    int         n;
    Bool        is_failed;
    TrieData    data;

    msg_step ("Preparing trie");
    test_trie = en_trie_new ();
    if (!test_trie) {
        fprintf (stderr, "Fail to create test trie\n");
        goto err_trie_not_created;
    }

    /* store */
    for (dict_p = walk_dict; dict_p->key; dict_p++) {
        if (!trie_store (test_trie, dict_p->key, dict_p->data)) {
            printf ("Failed to add key '%ls', data %d.\n",
                    dict_p->key, dict_p->data);
            goto err_trie_created;
        }
    }

    printf (
        "Now the trie structure is supposed to be:\n"
        "\n"
        "          +---o-> (3) -o-> (4) -l-> [5]\n"
        "          |\n"
        "          |        +---i-> (7) -z-> (8) -e-> [9]\n"
        "          |        |\n"
        "(1) -p-> (2) -r-> (6) -e-> (10) -v-> (11) -i-> (12) -e-> (13) -w-> [14]\n"
        "                   |         |\n"
        "                   |         +---p-> (15) -a-> (16) -r-> (17) -e-> [18]\n"
        "                   |\n"
        "                   +---o-> (19) -d-> (20) -u-> (21) -c-> (22) -e-> [23]\n"
        "                             |\n"
        "                             +---g-> (24) -r-> (25) -e-> (26) -s-> (27) -s-> [28]\n"
        "\n"
    );

    /* walk */
    msg_step ("Test walking");
    s = trie_root (test_trie);
    if (!s) {
        printf ("Failed to get trie root state\n");
        goto err_trie_created;
    }

    msg_step ("Test walking with 'p'");
    if (!trie_state_is_walkable (s, L'p')) {
        printf ("Trie state is not walkable with 'p'\n");
        goto err_trie_state_s_created;
    }
    if (!trie_state_walk (s, L'p')) {
        printf ("Failed to walk with 'p'\n");
        goto err_trie_state_s_created;
    }

    msg_step ("Now at (2), walkable chars should be {'o', 'r'}");
    is_failed = FALSE;
    n = trie_state_walkable_chars (s, walkables, ALPHABET_SIZE);
    if (2 != n) {
        printf ("Walkable chars should be exactly 2, got %d\n", n);
        is_failed = TRUE;
    }
    if (!is_walkables_include (L'o', walkables, n)) {
        printf ("Walkable chars do not include 'o'\n");
        is_failed = TRUE;
    }
    if (!is_walkables_include (L'r', walkables, n)) {
        printf ("Walkable chars do not include 'r'\n");
        is_failed = TRUE;
    }
    if (is_failed) {
        printf ("Walkables = ");
        print_walkables (walkables, n);
        printf ("\n");
        goto err_trie_state_s_created;
    }

    msg_step ("Try walking from (2) with 'o' to (3)");
    t = trie_state_clone (s);
    if (!t) {
        printf ("Failed to clone trie state\n");
        goto err_trie_state_s_created;
    }
    if (!trie_state_walk (t, L'o')) {
        printf ("Failed to walk from (2) with 'o' to (3)\n");
        goto err_trie_state_t_created;
    }
    if (!trie_state_is_single (t)) {
        printf ("(3) should be single, but isn't.\n");
        goto err_trie_state_t_created;
    }

    msg_step ("Try walking from (3) with 'o' to (4)");
    if (!trie_state_walk (t, L'o')) {
        printf ("Failed to walk from (3) with 'o' to (4)\n");
        goto err_trie_state_t_created;
    }
    if (!trie_state_is_single (t)) {
        printf ("(4) should be single, but isn't.\n");
        goto err_trie_state_t_created;
    }

    msg_step ("Try walking from (4) with 'l' to (5)");
    if (!trie_state_walk (t, L'l')) {
        printf ("Failed to walk from (4) with 'l' to (5)\n");
        goto err_trie_state_t_created;
    }
    if (!trie_state_is_terminal (t)) {
        printf ("(5) should be terminal, but isn't.\n");
        goto err_trie_state_t_created;
    }

    /* get key & data */
    msg_step ("Try getting data from (5)");
    data = trie_state_get_data (t);
    if (TRIE_DATA_ERROR == data) {
        printf ("Failed to get data from (5)\n");
        goto err_trie_state_t_created;
    }
    if (TRIE_DATA_UNREAD != data) {
        printf ("Mismatched data from (5), expected %d, got %d\n",
                TRIE_DATA_UNREAD, data);
        goto err_trie_state_t_created;
    }

    /* walk s from (2) with 'r' to (6) */
    msg_step ("Try walking from (2) with 'r' to (6)");
    if (!trie_state_walk (s, L'r')) {
        printf ("Failed to walk from (2) with 'r' to (6)\n");
        goto err_trie_state_t_created;
    }

    msg_step ("Now at (6), walkable chars should be {'e', 'i', 'o'}");
    is_failed = FALSE;
    n = trie_state_walkable_chars (s, walkables, ALPHABET_SIZE);
    if (3 != n) {
        printf ("Walkable chars should be exactly 3, got %d\n", n);
        is_failed = TRUE;
    }
    if (!is_walkables_include (L'e', walkables, n)) {
        printf ("Walkable chars do not include 'e'\n");
        is_failed = TRUE;
    }
    if (!is_walkables_include (L'i', walkables, n)) {
        printf ("Walkable chars do not include 'i'\n");
        is_failed = TRUE;
    }
    if (!is_walkables_include (L'o', walkables, n)) {
        printf ("Walkable chars do not include 'o'\n");
        is_failed = TRUE;
    }
    if (is_failed) {
        printf ("Walkables = ");
        print_walkables (walkables, n);
        printf ("\n");
        goto err_trie_state_t_created;
    }

    /* walk from s (6) with "ize" */
    msg_step ("Try walking from (6) with 'i' to (7)");
    trie_state_copy (t, s);
    if (!trie_state_walk (t, L'i')) {
        printf ("Failed to walk from (6) with 'i' to (7)\n");
        goto err_trie_state_t_created;
    }
    msg_step ("Try walking from (7) with 'z' to (8)");
    if (!trie_state_walk (t, L'z')) {
        printf ("Failed to walk from (7) with 'z' to (8)\n");
        goto err_trie_state_t_created;
    }
    if (!trie_state_is_single (t)) {
        printf ("(7) should be single, but isn't.\n");
        goto err_trie_state_t_created;
    }
    msg_step ("Try walking from (8) with 'e' to (9)");
    if (!trie_state_walk (t, L'e')) {
        printf ("Failed to walk from (8) with 'e' to (9)\n");
        goto err_trie_state_t_created;
    }
    if (!trie_state_is_terminal (t)) {
        printf ("(9) should be terminal, but isn't.\n");
        goto err_trie_state_t_created;
    }

    msg_step ("Try getting data from (9)");
    data = trie_state_get_data (t);
    if (TRIE_DATA_ERROR == data) {
        printf ("Failed to get data from (9)\n");
        goto err_trie_state_t_created;
    }
    if (TRIE_DATA_UNREAD != data) {
        printf ("Mismatched data from (9), expected %d, got %d\n",
                TRIE_DATA_UNREAD, data);
        goto err_trie_state_t_created;
    }

    /* walk from u = s (6) with 'e' to (10) */
    msg_step ("Try walking from (6) with 'e' to (10)");
    u = trie_state_clone (s);
    if (!u) {
        printf ("Failed to clone trie state\n");
        goto err_trie_state_t_created;
    }
    if (!trie_state_walk (u, L'e')) {
        printf ("Failed to walk from (6) with 'e' to (10)\n");
        goto err_trie_state_u_created;
    }

    /* walkable chars from (10) should be {'p', 'v'} */
    msg_step ("Now at (10), walkable chars should be {'p', 'v'}");
    is_failed = FALSE;
    n = trie_state_walkable_chars (u, walkables, ALPHABET_SIZE);
    if (2 != n) {
        printf ("Walkable chars should be exactly 2, got %d\n", n);
        is_failed = TRUE;
    }
    if (!is_walkables_include (L'p', walkables, n)) {
        printf ("Walkable chars do not include 'p'\n");
        is_failed = TRUE;
    }
    if (!is_walkables_include (L'v', walkables, n)) {
        printf ("Walkable chars do not include 'v'\n");
        is_failed = TRUE;
    }
    if (is_failed) {
        printf ("Walkables = ");
        print_walkables (walkables, n);
        printf ("\n");
        goto err_trie_state_u_created;
    }

    /* walk from u (10) with "view" */
    msg_step ("Try walking from (10) with 'v' to (11)");
    trie_state_copy (t, u);
    if (!trie_state_walk (t, L'v')) {
        printf ("Failed to walk from (10) with 'v' to (11)\n");
        goto err_trie_state_u_created;
    }
    if (!trie_state_is_single (t)) {
        printf ("(11) should be single, but isn't.\n");
        goto err_trie_state_u_created;
    }
    msg_step ("Try walking from (11) with 'i' to (12)");
    if (!trie_state_walk (t, L'i')) {
        printf ("Failed to walk from (11) with 'i' to (12)\n");
        goto err_trie_state_u_created;
    }
    msg_step ("Try walking from (12) with 'e' to (13)");
    if (!trie_state_walk (t, L'e')) {
        printf ("Failed to walk from (12) with 'e' to (13)\n");
        goto err_trie_state_u_created;
    }
    msg_step ("Try walking from (13) with 'w' to (14)");
    if (!trie_state_walk (t, L'w')) {
        printf ("Failed to walk from (13) with 'w' to (14)\n");
        goto err_trie_state_u_created;
    }
    if (!trie_state_is_terminal (t)) {
        printf ("(14) should be terminal, but isn't.\n");
        goto err_trie_state_u_created;
    }

    msg_step ("Try getting data from (14)");
    data = trie_state_get_data (t);
    if (TRIE_DATA_ERROR == data) {
        printf ("Failed to get data from (14)\n");
        goto err_trie_state_u_created;
    }
    if (TRIE_DATA_UNREAD != data) {
        printf ("Mismatched data from (14), expected %d, got %d\n",
                TRIE_DATA_UNREAD, data);
        goto err_trie_state_u_created;
    }

    /* walk from u (10) with "pare" */
    msg_step ("Try walking from (10) with 'p' to (15)");
    trie_state_copy (t, u);
    if (!trie_state_walk (t, L'p')) {
        printf ("Failed to walk from (10) with 'p' to (15)\n");
        goto err_trie_state_u_created;
    }
    if (!trie_state_is_single (t)) {
        printf ("(15) should be single, but isn't.\n");
        goto err_trie_state_u_created;
    }
    msg_step ("Try walking from (15) with 'a' to (16)");
    if (!trie_state_walk (t, L'a')) {
        printf ("Failed to walk from (15) with 'a' to (16)\n");
        goto err_trie_state_u_created;
    }
    msg_step ("Try walking from (16) with 'r' to (17)");
    if (!trie_state_walk (t, L'r')) {
        printf ("Failed to walk from (16) with 'r' to (17)\n");
        goto err_trie_state_u_created;
    }
    msg_step ("Try walking from (17) with 'e' to (18)");
    if (!trie_state_walk (t, L'e')) {
        printf ("Failed to walk from (17) with 'e' to (18)\n");
        goto err_trie_state_u_created;
    }
    if (!trie_state_is_terminal (t)) {
        printf ("(18) should be terminal, but isn't.\n");
        goto err_trie_state_u_created;
    }

    msg_step ("Try getting data from (18)");
    data = trie_state_get_data (t);
    if (TRIE_DATA_ERROR == data) {
        printf ("Failed to get data from (18)\n");
        goto err_trie_state_u_created;
    }
    if (TRIE_DATA_UNREAD != data) {
        printf ("Mismatched data from (18), expected %d, got %d\n",
                TRIE_DATA_UNREAD, data);
        goto err_trie_state_u_created;
    }

    trie_state_free (u);

    /* walk s from (6) with 'o' to (19) */
    msg_step ("Try walking from (6) with 'o' to (19)");
    if (!trie_state_walk (s, L'o')) {
        printf ("Failed to walk from (6) with 'o' to (19)\n");
        goto err_trie_state_t_created;
    }

    msg_step ("Now at (19), walkable chars should be {'d', 'g'}");
    is_failed = FALSE;
    n = trie_state_walkable_chars (s, walkables, ALPHABET_SIZE);
    if (2 != n) {
        printf ("Walkable chars should be exactly 2, got %d\n", n);
        is_failed = TRUE;
    }
    if (!is_walkables_include (L'd', walkables, n)) {
        printf ("Walkable chars do not include 'd'\n");
        is_failed = TRUE;
    }
    if (!is_walkables_include (L'g', walkables, n)) {
        printf ("Walkable chars do not include 'g'\n");
        is_failed = TRUE;
    }
    if (is_failed) {
        printf ("Walkables = ");
        print_walkables (walkables, n);
        printf ("\n");
        goto err_trie_state_t_created;
    }

    /* walk from s (19) with "duce" */
    msg_step ("Try walking from (19) with 'd' to (20)");
    trie_state_copy (t, s);
    if (!trie_state_walk (t, L'd')) {
        printf ("Failed to walk from (19) with 'd' to (20)\n");
        goto err_trie_state_t_created;
    }
    if (!trie_state_is_single (t)) {
        printf ("(20) should be single, but isn't.\n");
        goto err_trie_state_t_created;
    }
    msg_step ("Try walking from (20) with 'u' to (21)");
    if (!trie_state_walk (t, L'u')) {
        printf ("Failed to walk from (20) with 'u' to (21)\n");
        goto err_trie_state_t_created;
    }
    msg_step ("Try walking from (21) with 'c' to (22)");
    if (!trie_state_walk (t, L'c')) {
        printf ("Failed to walk from (21) with 'c' to (22)\n");
        goto err_trie_state_t_created;
    }
    msg_step ("Try walking from (22) with 'e' to (23)");
    if (!trie_state_walk (t, L'e')) {
        printf ("Failed to walk from (22) with 'e' to (23)\n");
        goto err_trie_state_t_created;
    }
    if (!trie_state_is_terminal (t)) {
        printf ("(23) should be terminal, but isn't.\n");
        goto err_trie_state_t_created;
    }

    msg_step ("Try getting data from (23)");
    data = trie_state_get_data (t);
    if (TRIE_DATA_ERROR == data) {
        printf ("Failed to get data from (23)\n");
        goto err_trie_state_t_created;
    }
    if (TRIE_DATA_UNREAD != data) {
        printf ("Mismatched data from (23), expected %d, got %d\n",
                TRIE_DATA_UNREAD, data);
        goto err_trie_state_t_created;
    }

    trie_state_free (t);

    /* walk from s (19) with "gress" */
    msg_step ("Try walking from (19) with 'g' to (24)");
    if (!trie_state_walk (s, L'g')) {
        printf ("Failed to walk from (19) with 'g' to (24)\n");
        goto err_trie_state_s_created;
    }
    if (!trie_state_is_single (s)) {
        printf ("(24) should be single, but isn't.\n");
        goto err_trie_state_s_created;
    }
    msg_step ("Try walking from (24) with 'r' to (25)");
    if (!trie_state_walk (s, L'r')) {
        printf ("Failed to walk from (24) with 'r' to (25)\n");
        goto err_trie_state_s_created;
    }
    msg_step ("Try walking from (25) with 'e' to (26)");
    if (!trie_state_walk (s, L'e')) {
        printf ("Failed to walk from (25) with 'e' to (26)\n");
        goto err_trie_state_s_created;
    }
    msg_step ("Try walking from (26) with 's' to (27)");
    if (!trie_state_walk (s, L's')) {
        printf ("Failed to walk from (26) with 's' to (27)\n");
        goto err_trie_state_s_created;
    }
    msg_step ("Try walking from (27) with 's' to (28)");
    if (!trie_state_walk (s, L's')) {
        printf ("Failed to walk from (27) with 's' to (28)\n");
        goto err_trie_state_s_created;
    }
    if (!trie_state_is_terminal (s)) {
        printf ("(28) should be terminal, but isn't.\n");
        goto err_trie_state_s_created;
    }

    msg_step ("Try getting data from (28)");
    data = trie_state_get_data (s);
    if (TRIE_DATA_ERROR == data) {
        printf ("Failed to get data from (28)\n");
        goto err_trie_state_s_created;
    }
    if (TRIE_DATA_UNREAD != data) {
        printf ("Mismatched data from (28), expected %d, got %d\n",
                TRIE_DATA_UNREAD, data);
        goto err_trie_state_s_created;
    }

    trie_state_free (s);
    trie_free (test_trie);
    return 0;

err_trie_state_u_created:
    trie_state_free (u);
err_trie_state_t_created:
    trie_state_free (t);
err_trie_state_s_created:
    trie_state_free (s);
err_trie_created:
    trie_free (test_trie);
err_trie_not_created:
    return 1;
}

/*
vi:ts=4:ai:expandtab
*/