File: test_thinp.c

package info (click to toggle)
libthai 0.1.14-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,600 kB
  • ctags: 798
  • sloc: sh: 10,137; ansic: 4,205; makefile: 313
file content (88 lines) | stat: -rw-r--r-- 2,267 bytes parent folder | download | duplicates (2)
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
/*
 * test_thinp.c : Test suite for <thai/thinp.h> functions
 * Created: 2001-08-09
 * Author:  Theppitak Karoonboonyanan
 */

#include <thai/thinp.h>
#include <string.h>
#include <stdio.h>

const thchar_t test_keys[] = "ӹաءءѹѹԹ";
const thchar_t res_level0[] = "ӹաءءѹѹԹ";
const thchar_t res_level1[] = "ӹӡ";
const thchar_t res_level2[] = "ӹӡ";
const thchar_t res_validate[] = "ӹӡѹѹ";

static int test_simple_input(const thchar_t keys[],
                             const thchar_t ans[], int level)
{
    thchar_t buffer[80];
    int      cur_pos = 0;
    int      err_no = 0;

    while (*keys) {
        thchar_t prev_c = cur_pos ? buffer[cur_pos-1] : 0;
        if (th_isaccept(prev_c, *keys, level)) {
            buffer[cur_pos++] = *keys;
        }
        ++keys;
    }
    buffer[cur_pos] = 0;

    err_no = strcmp((const char*)buffer, (const char*)ans);
    if (err_no != 0) {
        fprintf(stderr, "(%s)!=(%s)\n", buffer, ans);
    }
    return err_no;
}

static int test_th_isaccept()
{
    int err_no = 0;

    err_no += test_simple_input(test_keys, res_level0, 0);
    err_no += test_simple_input(test_keys, res_level1, 1);
    err_no += test_simple_input(test_keys, res_level2, 2);

    return err_no;
}

static int test_th_validate()
{
    int      err_no = 0;
    thchar_t buffer[80];
    int      cur_pos = 0;
    const thchar_t *keys = test_keys;

    while (*keys) {
        struct thcell_t prev_cell;
        struct thinpconv_t conv;

        th_prev_cell(buffer, cur_pos, &prev_cell, 1);
        if (th_validate(prev_cell, *keys, &conv)) {
            strcpy(&buffer[cur_pos + conv.offset], conv.conv);
            cur_pos += conv.offset + strlen(conv.conv);
        }
        ++keys;
    }
    buffer[cur_pos] = 0;

    err_no = strcmp((const char*)buffer, (const char*)res_validate);
    if (err_no != 0) {
        fprintf(stderr, "(%s)!=(%s)\n", buffer, res_validate);
    }

    return err_no;
}

int main()
{
    int err_no = 0;

    err_no += test_th_isaccept();
    err_no += test_th_validate();

    return err_no ? 1 : 0;
}