File: testchewing.c

package info (click to toggle)
libchewing 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 14,220 kB
  • ctags: 10,179
  • sloc: ansic: 103,539; sh: 11,563; makefile: 316; python: 98
file content (129 lines) | stat: -rw-r--r-- 3,006 bytes parent folder | download
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
/**
 * testchewing.c
 *
 * Copyright (c) 2004, 2005, 2008, 2011
 *	libchewing Core Team. See ChangeLog for details.
 *
 * See the file "COPYING" for information on usage and redistribution
 * of this file.
 */

#include "chewing.h"
#include "testhelper.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef USED_IN_SIMULATION
#    define MAXLEN 149
char commit_string_buf[MAXLEN];
char expect_string_buf[MAXLEN];

#    define getchar fake_getchar
int fake_getchar();

#    include "internal/chewing-utf8-util.h"
int tested_word_count = 0;
int failed_word_count = 0;
#endif

static int selKey_define[11] = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0 }; /* Default */

static int get_char(void *param UNUSED)
{
    int ch = getchar();

    if (ch == EOF)
        return END;
    return ch;
}

void commit_string(ChewingContext *ctx)
{
    char *s;

    if (chewing_commit_Check(ctx)) {
        s = chewing_commit_String(ctx);
#ifdef USED_IN_SIMULATION
        strcat(commit_string_buf, s);
#else
        printf("%s", s);
#endif
        free(s);
    }
}

#ifdef USED_IN_SIMULATION
void compare_per_run()
{
    int i, len;
    char utf8buf_expect[16];
    char utf8buf_commit[16];

    printf("Expected:  %s", expect_string_buf);
    printf("Committed: ");

    tested_word_count += (len = ueStrLen(expect_string_buf) - 1);
    /* omit the suffix character */
    for (i = 0; i < len; i++) {
        ueStrNCpy(utf8buf_expect, ueStrSeek(expect_string_buf, i), 1, STRNCPY_CLOSE);
        ueStrNCpy(utf8buf_commit, ueStrSeek(commit_string_buf, i), 1, STRNCPY_CLOSE);
        if (!strcmp(utf8buf_expect, utf8buf_commit))
            printf("%s", utf8buf_commit);
        else {
            printf("\033[44;37m%s\033[m", utf8buf_commit);
            failed_word_count++;
        }
    }
    memset(commit_string_buf, 0, MAXLEN);
    printf("\n\n");
}

/* entry point for simulation */
int chewing_test_Main()
#else
int main()
#endif
{
    ChewingContext *ctx;
    int i;

    /* Initialize libchewing */
    putenv("CHEWING_PATH=" CHEWING_DATA_PREFIX);
    /* for the sake of testing, we should not change existing hash data */
    putenv("CHEWING_USER_PATH=" TEST_HASH_DIR);

    /* Request handle to ChewingContext */
    ctx = chewing_new();

    /* Set keyboard type */
    chewing_set_KBType(ctx, chewing_KBStr2Num("KB_DEFAULT"));

    chewing_set_candPerPage(ctx, 9);
    chewing_set_maxChiSymbolLen(ctx, 16);
    chewing_set_addPhraseDirection(ctx, 1);
    chewing_set_selKey(ctx, selKey_define, 10);
    chewing_set_spaceAsSelection(ctx, 1);

    while (1) {
        i = get_keystroke(get_char, NULL);
        if (i == END)
            goto end;
        type_single_keystroke(ctx, i);
        commit_string(ctx);
#ifdef USED_IN_SIMULATION
        if (i == KEY_ENTER)
            compare_per_run();
#endif
    }
  end:
    /* Free Chewing IM handle */
    chewing_delete(ctx);

    /* Termate Chewing services */
#ifndef USED_IN_SIMULATION
    printf("\n");
#endif
    return 0;
}