File: simulate.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 (88 lines) | stat: -rw-r--r-- 1,803 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
/**
 * simulate.c
 *
 * Copyright (c) 2008, 2010
 *	libchewing Core Team. See ChangeLog for details.
 *
 * See the file "COPYING" for information on usage and redistribution
 * of this file.
 */

#define USED_IN_SIMULATION
#include "testchewing.c"
#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#else
#    include "plat_types.h"
#endif

#define FN_MATERIALS "materials.txt"

static FILE *fp = NULL;

int init_sim()
{
    if (0 == access(FN_MATERIALS "-random", R_OK))
        fp = fopen(FN_MATERIALS "-random", "r");
    else
        fp = fopen(FN_MATERIALS, "r");
    return (fp != NULL);
}

int fini_sim()
{
    if (fp)
        fclose(fp);
    fflush(stdout);
    return 0;
}

static char linebuf[MAXLEN];
int fake_getchar()
{
    static int remainder = 0;
    static int idx = 0;
    char *pos;

    if (feof(fp))
        return EOF;

    if (remainder == 0) {
      start:
        if (fgets(linebuf, MAXLEN, fp) == NULL)
            return EOF;
        if (linebuf[0] == '#' || linebuf[0] == ' ')
            goto start;

        pos = strstr(linebuf, "<E>");
        if (!pos)
            return EOF;
        *(pos + 3) = '\0';
        remainder = pos - linebuf + 3;
        idx = 0;

        pos += 4;
        while (*pos == '\t' || *pos == ' ')
            pos++;
        strcpy(expect_string_buf, pos);
    }
    remainder--;
    return linebuf[idx++];
}

int main()
{
    if (!init_sim())
        return 1;

    chewing_test_Main();

    {
        printf("_________________________________________________________________________\n" "[ Report ]\n");
        printf("Checks: %d words,  Failures: %d words\n", tested_word_count, failed_word_count);
        printf("Ratio: %.2f%%\n", (float) (tested_word_count - failed_word_count) / tested_word_count * 100);
    }

    fini_sim();
    return 0;
}