File: simulate.c

package info (click to toggle)
libchewing 0.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,588 kB
  • ctags: 878
  • sloc: sh: 9,738; ansic: 7,644; makefile: 234; python: 49
file content (84 lines) | stat: -rw-r--r-- 1,477 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
/**
 * simulate.c
 *
 * Copyright (c) 2008
 *	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"

#define FN_MATERIALS "materials.txt"

static FILE *fp = NULL;

int init_sim()
{
	fp = fopen( FN_MATERIALS, "r" );
	return (fp != NULL);
}

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

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 ) == EOF )
			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;
}