File: testchewing.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 (264 lines) | stat: -rw-r--r-- 5,567 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
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
/**
 * testchewing.c
 *
 * Copyright (c) 2004, 2005, 2008
 *	libchewing Core Team. See ChangeLog for details.
 *
 * See the file "COPYING" for information on usage and redistribution
 * of this file.
 */

#include "chewing.h"

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

#define KEY_SLEFT 896
#define KEY_SRIGHT 897	
#define KEY_LEFT 898
#define KEY_RIGHT 899	
#define KEY_UP 990 
#define KEY_DOWN 991
#define KEY_SPACE ' '
#define KEY_ENTER 992
#define KEY_BACKSPACE 993
#define KEY_ESC	994
#define KEY_DELETE 995
#define KEY_HOME 996
#define KEY_END 997
#define KEY_TAB 998
#define KEY_CAPSLOCK 999
#define KEY_CTRL_BASE 1000
#define END 2000

#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 */

int get_keystroke()
{
	char ch;
	int result;
	int flag = 0;
	while ( ( ch = getchar() ) != EOF ) {
		if ( ( ch != '<' ) && ( flag != 1 ) )
			return (int) ch;
		else if ( ch == '>' ) {
			flag = 0;
			return result;
		}
		else {
			flag = 1;
			ch = getchar();
			switch ( ch ) {
				case 'L':
					result = KEY_LEFT;
					break;
				case 'R':
					result = KEY_RIGHT;
					break;
				case 'U':
					result = KEY_UP;
					break;
				case 'D':
					if ( ( ch = getchar() ) == '>' )
						return result = KEY_DOWN;
					else {
						getchar();
						return result = KEY_DELETE;
					}
					break;
				case 'E':
					if ( ( ch = getchar() ) == '>' )
						return result = KEY_ENTER;
					else if ( ch == 'E' )
						result = KEY_ESC;
					else
						result = KEY_END;
					break;
				case 'C':
					if ( ( ch = getchar() ) != '>' ) {
						if ( ( ch == 'B' ))
							result = ( KEY_CAPSLOCK );
						else
							result = ( KEY_CTRL_BASE + ch );
					}
					break;
				case 'B':
					result = KEY_BACKSPACE;
					break;
				case 'H':
					result = KEY_HOME;
					break;
				case 'S':
					if ( ( ch = getchar() ) == 'L' )
						result = KEY_SLEFT;
					else
						result = KEY_SRIGHT;
					break;
				case 'T':
					result = KEY_TAB;
					break;
			}
		}
	}
	return result = END;
}

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( int argc, char *argv[] )
#endif
{
	ChewingContext *ctx;
	char *prefix = CHEWING_DATA_PREFIX;
	int i;
	int ctrl_shifted;

	/* Initialize libchewing */
	/* for the sake of testing, we should not change existing hash data */
	chewing_Init( prefix, 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();
		switch ( i ) {
			case KEY_LEFT:
				chewing_handle_Left( ctx );
				break;
			case KEY_SLEFT:
				chewing_handle_ShiftLeft( ctx );
				break;
			case KEY_RIGHT:
				chewing_handle_Right( ctx );
				break;
			case KEY_SRIGHT:
				chewing_handle_ShiftRight( ctx );
				break;
			case KEY_UP:
				chewing_handle_Up( ctx );
				break;
			case KEY_DOWN:
				chewing_handle_Down( ctx );
				break;
			case KEY_SPACE:
				chewing_handle_Space( ctx );
				break;
			case KEY_ENTER:
				chewing_handle_Enter( ctx );
				break;
			case KEY_BACKSPACE:
				chewing_handle_Backspace( ctx );
				break;
			case KEY_ESC:
				chewing_handle_Esc( ctx );
				break;
			case KEY_DELETE:
				chewing_handle_Del( ctx );
				break;
			case KEY_HOME:
				chewing_handle_Home( ctx );
				break;
			case KEY_END:
				chewing_handle_End( ctx );
				break;
			case KEY_TAB:
				chewing_handle_Tab( ctx );
				break;			
			case KEY_CAPSLOCK:
				chewing_handle_Capslock( ctx );
				break;
			case END:
				goto end;
			default:
				ctrl_shifted = ( i - KEY_CTRL_BASE );
				if ( ( ctrl_shifted >= '0' ) && ( ctrl_shifted <= '9' ) ) {
					chewing_handle_CtrlNum( ctx, ctrl_shifted );
				} else {
					chewing_handle_Default( ctx, (char) i );
				}
				break;
		}
		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 */
	chewing_Terminate();
#ifndef USED_IN_SIMULATION
	printf( "\n" );
#endif
	return 0;
}