Beispiel #1
0
void test_set_ChiEngMode()
{
	const int VALUE[] = {
		CHINESE_MODE,
		SYMBOL_MODE,
	};

	const int INVALID_VALUE[] = {
		-1,
		2,
	};

	ChewingContext *ctx;
	size_t i;
	size_t j;

	ctx = chewing_new();
	start_testcase( ctx, fd );

	for ( i = 0; i < ARRAY_SIZE( VALUE ); ++i ) {
		chewing_set_ChiEngMode( ctx, VALUE[i] );
		ok( chewing_get_ChiEngMode( ctx ) == VALUE[i],
			"ChiEngMode shall be `%d'", VALUE[i] );

		for ( j = 0; j < ARRAY_SIZE( INVALID_VALUE ); ++j ) {
			// mode shall not change when set mode has invalid value.
			chewing_set_ChiEngMode( ctx, INVALID_VALUE[j] );
			ok( chewing_get_ChiEngMode( ctx ) == VALUE[i],
				"ChiEngMode shall be `%d'", VALUE[i] );
		}
	}

	chewing_delete( ctx );
}
Beispiel #2
0
void test_del_bopomofo_as_mode_switch()
{
    ChewingContext *ctx;

    clean_userphrase();

    ctx = chewing_new();
    start_testcase(ctx, fd);

    type_keystroke_by_string(ctx, "2k");        /* ㄉㄜ */
    ok_bopomofo_buffer(ctx, "\xe3\x84\x89\xe3\x84\x9c" /* ㄉㄜ */ );
    ok_preedit_buffer(ctx, "");
    chewing_set_ChiEngMode(ctx, SYMBOL_MODE);
    ok_bopomofo_buffer(ctx, "");
    ok_preedit_buffer(ctx, "");

    chewing_set_ChiEngMode(ctx, CHINESE_MODE);

    type_keystroke_by_string(ctx, "ji");        /* ㄨㄛ */
    ok_bopomofo_buffer(ctx, "\xe3\x84\xa8\xe3\x84\x9b" /* ㄨㄛ */ );
    ok_preedit_buffer(ctx, "");
    chewing_set_ChiEngMode(ctx, SYMBOL_MODE);
    ok_bopomofo_buffer(ctx, "");
    ok_preedit_buffer(ctx, "");

    chewing_delete(ctx);
}
void test_in_fullshape_mode()
{
	ChewingContext *ctx;
	size_t i;


	ctx = chewing_new();

	chewing_set_maxChiSymbolLen( ctx, 16 );
	chewing_set_ChiEngMode( ctx, SYMBOL_MODE );
	chewing_set_ShapeMode( ctx, FULLSHAPE_MODE );

	for ( i = 0; i < ARRAY_SIZE( SPECIAL_SYMBOL_TABLE ); ++i ) {
		// If fullshape symbol is collided with special symbol, use
		// fullshape symbol
		if ( is_fullshape_collision_key( SPECIAL_SYMBOL_TABLE[i].token ) )
			continue;

		type_keystroke_by_string( ctx, SPECIAL_SYMBOL_TABLE[i].token );
		ok_preedit_buffer( ctx, "" );
		ok_commit_buffer( ctx, SPECIAL_SYMBOL_TABLE[i].expected );
	}

	chewing_delete( ctx );
}
void ibus_chewing_engine_property_activate(IBusEngine *engine, const gchar  *prop_name, guint  prop_state){
    G_DEBUG_MSG(3,"[I3] property_activate(-, %s, %u)", prop_name, prop_state);
    Self *self=SELF(engine);
    gboolean needRefresh=TRUE;
    if (strcmp(prop_name,"chewing_chieng_prop")==0){
	/* Toggle Chinese <-> English */
	chewing_set_ChiEngMode(self->context, !chewing_get_ChiEngMode(self->context));
    }else if (strcmp(prop_name,"chewing_alnumSize_prop")==0){
	/* Toggle Full <-> Half */
	chewing_set_ShapeMode(self->context, !chewing_get_ShapeMode(self->context));
    }else if (strcmp(prop_name,"chewing_settings_prop")==0){
	if (self->settings_prop->state==PROP_STATE_UNCHECKED){
	    if (gtk_dialog_run(GTK_DIALOG(self->setting_dialog))==GTK_RESPONSE_OK){
		self_save_config_all(self);
	    }
	    gtk_widget_hide(self->setting_dialog);
	    self->settings_prop->state=PROP_STATE_UNCHECKED;
	}
    }else{
	G_DEBUG_MSG(3,"[I3]  property_activate(-, %s, %u) not recognized",prop_name, prop_state);
	needRefresh=FALSE;
    }
    if (needRefresh)
	self_refresh_property(self,prop_name);
}
Beispiel #5
0
/**
 * @brief initialize the extra input method
 *
 * @param arg
 * @return successful or not
 **/
__EXPORT_API
void* FcitxChewingCreate(FcitxInstance* instance)
{
    if (GetFcitxChewingConfigDesc() == NULL)
        return NULL;
    
    char* user_path = NULL;
    FILE* fp = FcitxXDGGetFileUserWithPrefix("chewing", ".place_holder", "w", NULL);
    if (fp)
        fclose(fp);
    FcitxXDGGetFileUserWithPrefix("chewing", "", NULL, &user_path);
    FcitxLog(INFO, "Chewing storage path %s", user_path);
    if (0 == chewing_Init(CHEWING_DATADIR, user_path)) {
        FcitxLog(DEBUG, "chewing init ok");
    } else {
        FcitxLog(DEBUG, "chewing init failed");
        return NULL;
    }
    
    FcitxChewing* chewing = (FcitxChewing*) fcitx_utils_malloc0(sizeof(FcitxChewing));
    FcitxGlobalConfig* config = FcitxInstanceGetGlobalConfig(instance);
    FcitxInputState *input = FcitxInstanceGetInputState(instance);
    FcitxCandidateWordSetChoose(FcitxInputStateGetCandidateList(input), DIGIT_STR_CHOOSE);
    
    bindtextdomain("fcitx-chewing", LOCALEDIR);

    chewing->context = chewing_new();
    ChewingContext * c = chewing->context;
    chewing->owner = instance;
    chewing_set_ChiEngMode(c, CHINESE_MODE);
    chewing_set_maxChiSymbolLen(c, 16);
    // chewing will crash without set page
    chewing_set_candPerPage(c, config->iMaxCandWord);
    FcitxCandidateWordSetPageSize(FcitxInputStateGetCandidateList(input), config->iMaxCandWord);
    chewing_set_selKey(c, selKey, 10);
    LoadChewingConfig(&chewing->config);
    ConfigChewing(chewing);

    FcitxInstanceRegisterIM(
        instance,
        chewing,
        "chewing",
        _("Chewing"),
        "chewing",
        FcitxChewingInit,
        FcitxChewingReset,
        FcitxChewingDoInput,
        FcitxChewingGetCandWords,
        NULL,
        NULL,
        FcitxChewingReloadConfig,
        NULL,
        1,
        "zh_TW"
    );
    return chewing;
}
Beispiel #6
0
void test_fullshape_input()
{
	ChewingContext *ctx;
	size_t i;


	ctx = chewing_new();

	chewing_set_ChiEngMode( ctx, SYMBOL_MODE );
	chewing_set_ShapeMode( ctx, FULLSHAPE_MODE );

	for ( i = 0; i < ARRAY_SIZE( FULLSHAPE_DATA ); ++i ) {
		type_keystroke_by_string( ctx, FULLSHAPE_DATA[i].token );
		// fullshape symbol does not present in preedit buffer.
		ok_preedit_buffer( ctx, "" );
		ok_commit_buffer( ctx, FULLSHAPE_DATA[i].expected );
	}

	chewing_delete( ctx );
}
Beispiel #7
0
void test_ShiftSpace()
{
    ChewingContext *ctx;
    int mode;

    ctx = chewing_new();
    start_testcase(ctx, fd);

    mode = chewing_get_ShapeMode(ctx);
    ok(mode == HALFSHAPE_MODE, "mode shall be HALFSHAPE_MODE");

    type_keystroke_by_string(ctx, "<SS>");
    mode = chewing_get_ShapeMode(ctx);
    ok(mode == FULLSHAPE_MODE, "mode shall be FULLSHAPE_MODE");

    type_keystroke_by_string(ctx, " ");
    ok_commit_buffer(ctx, "\xE3\x80\x80"); /* Fullshape Space (U+3000) */

    chewing_set_ChiEngMode(ctx, SYMBOL_MODE);
    type_keystroke_by_string(ctx, "a");
    ok_commit_buffer(ctx, "\xEF\xBD\x81"); /* Fullshape a */

    chewing_delete(ctx);
}
gboolean ibus_chewing_engine_process_key_event(IBusEngine *engine,
	guint keysym,  guint keycode, guint modifiers){
    gboolean result=TRUE;
    if (modifiers & IBUS_RELEASE_MASK){
	/* Skip release event */
	return TRUE;
    }
    IBusChewingEngine *self=IBUS_CHEWING_ENGINE(engine);
    guint kSym=ibus_chewing_engine_keycode_to_keysym(self,keysym, keycode, modifiers);

    G_DEBUG_MSG(2,"***[I2] process_key_event(-, %x(%s), %x, %x) orig keysym=%x... proceed.",kSym, keyName_get(kSym), keycode, modifiers,keysym);
    guint state= modifiers & (IBUS_SHIFT_MASK | IBUS_CONTROL_MASK | IBUS_MOD1_MASK);
    self->_priv->key_last=kSym;
    if (state==0){
	guint kSym_tmp=keysym_KP_to_normal(kSym);
	if (kSym_tmp){
	    G_DEBUG_MSG(3,"***[I3] process_key_event(): %x is from keypad.", kSym_tmp);
	    /* Is keypad key */
	    if ((self->chewingFlags & CHEWING_FLAG_NUMPAD_ALWAYS_NUMBER) && chewing_get_ChiEngMode(self->context)){
		chewing_set_ChiEngMode(self->context, 0);
		self_handle_Default(self,kSym_tmp,FALSE);
		chewing_set_ChiEngMode(self->context,CHINESE_MODE);
	    }else{
		/* Convert kp numbers to normal */
		self_handle_Default(self,kSym_tmp,FALSE);
	    }
	}else{
	    switch (kSym){
		case IBUS_Return:
		case IBUS_KP_Enter:
		    chewing_handle_Enter(self->context);
		    break;
		case IBUS_Escape:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    chewing_handle_Esc(self->context);
		    break;
		case IBUS_BackSpace:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    chewing_handle_Backspace(self->context);
		    break;
		case IBUS_Delete:
		case IBUS_KP_Delete:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    chewing_handle_Del(self->context);
		    break;
		case IBUS_space:
		case IBUS_KP_Space:
		    if (self->chewingFlags & CHEWING_FLAG_PLAIN_ZHUYIN) {
			if (chewing_cand_TotalChoice(self->context) == 0) {
			    chewing_handle_Space(self->context);
			}
		    } else {
			/**
			 * Fix for space in Temporary English mode.
			 */
			chewing_handle_Space(self->context);
		    }
		    if (self->inputMode==CHEWING_INPUT_MODE_SELECTION_DONE ||
			    self->inputMode==CHEWING_INPUT_MODE_BYPASS )
			ibus_chewing_engine_set_status_flag(self,ENGINE_STATUS_NEED_COMMIT);
		    break;
		case IBUS_Page_Up:
		case IBUS_KP_Page_Up:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    IBUS_ENGINE_GET_CLASS(engine)->page_up(engine);
		    break;
		case IBUS_Page_Down:
		case IBUS_KP_Page_Down:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    IBUS_ENGINE_GET_CLASS(engine)->page_down(engine);
		    break;
		case IBUS_Up:
		case IBUS_KP_Up:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    IBUS_ENGINE_GET_CLASS(engine)->cursor_up(engine);
		    break;
		case IBUS_Down:
		case IBUS_KP_Down:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    IBUS_ENGINE_GET_CLASS(engine)->cursor_down(engine);
		    break;
		case IBUS_Left:
		case IBUS_KP_Left:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    chewing_handle_Left(self->context);
		    break;
		case IBUS_Right:
		case IBUS_KP_Right:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    chewing_handle_Right(self->context);
		    break;
		case IBUS_Home:
		case IBUS_KP_Home:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    chewing_handle_Home(self->context);
		    break;
		case IBUS_End:
		case IBUS_KP_End:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    chewing_handle_End(self->context);
		    break;
		case IBUS_Tab:
		    if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
			return FALSE;
		    chewing_handle_Tab(self->context);
		    break;
		case IBUS_Caps_Lock:
		    /* When Chi->Eng with incomplete character */
		    if (chewing_get_ChiEngMode(self->context) && !chewing_zuin_Check(self->context)){
			/* chewing_zuin_Check==0 means incomplete character */
			/* Send a space to finish the character */
			chewing_handle_Space(self->context);
		    }
		    chewing_handle_Capslock(self->context);
		    self_refresh_property(self,"chewing_chieng_prop");
		    break;
		case IBUS_Shift_L:
		case IBUS_Shift_R:
		    /* Some QT application will sneak these through */
		    return FALSE;
		case IBUS_Alt_L:
		case IBUS_Alt_R:
		    /* Some QT application will sneak these through */
		    return FALSE;
		case IBUS_Control_L:
		case IBUS_Control_R:
		    /* Some QT application will sneak these through */
		    return FALSE;
		default:
		    self_handle_Default(self,kSym,FALSE);
		    break;
	    }
	}
    }else if (state==IBUS_SHIFT_MASK){
	switch(kSym){
	    case IBUS_Left:
		if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
		    return FALSE;
		chewing_handle_ShiftLeft(self->context);
		break;
	    case IBUS_Right:
		if (self->inputMode==CHEWING_INPUT_MODE_BYPASS)
		    return FALSE;
		chewing_handle_ShiftRight(self->context);
		break;
	    case IBUS_Up:
	    case IBUS_KP_Up:
	    case IBUS_Down:
	    case IBUS_KP_Down:
	    case IBUS_Page_Up:
	    case IBUS_KP_Page_Up:
	    case IBUS_Page_Down:
	    case IBUS_KP_Page_Down:
	    case IBUS_Home:
	    case IBUS_End:
		if (self->_priv->statusFlags & ENGINE_STATUS_NEED_COMMIT)
		    self_force_commit(self);
		return FALSE;
	    case IBUS_space:
	    case IBUS_KP_Space:
		chewing_handle_ShiftSpace(self->context);
		chewing_set_ShapeMode(self->context, !chewing_get_ShapeMode(self->context));
		self_refresh_property(self,"chewing_alnumSize_prop");
		break;
	    default:
		if (kSym>127 || kSym<0){
		    /* Special keys, must let it through */
		    return FALSE;
		}
		self_handle_Default(self,kSym,TRUE);
		break;
	}
    }else if (state==IBUS_CONTROL_MASK){
	if (kSym>=IBUS_0 && kSym<=IBUS_9){
	    chewing_handle_CtrlNum(self->context,kSym);
//	}else if (kSym==IBUS_v || kSym==IBUS_V){
//	    chewing_handle_Right(self->context);
	}else{
	    result=FALSE;
	}

    }else{
	result=FALSE;
    }
    if (!result){
	return FALSE;
    }
    return self_update(self);
}
void test_null()
{
    int ret;
    char *buf;
    const char *const_buf;
    int *key;
    unsigned short *phone;

    start_testcase(NULL, fd);

    chewing_Reset(NULL);        // shall not crash

    ret = chewing_set_KBType(NULL, 0);
    ok(ret == -1, "chewing_set_KBType() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_get_KBType(NULL);
    ok(ret == -1, "chewing_get_KBType() returns `%d' shall be `%d'", ret, -1);

    buf = chewing_get_KBString(NULL);
    ok(strcmp(buf, "") == 0, "chewing_get_KBString() returns `%s' shall be `%s'", buf, "");
    chewing_free(buf);

    chewing_delete(NULL);       // shall not crash

    chewing_free(NULL);         // shall not crash

    chewing_set_candPerPage(NULL, 0);   // shall not crash

    ret = chewing_get_candPerPage(NULL);
    ok(ret == -1, "chewing_get_candPerPage() returns `%d' shall be `%d'", ret, -1);

    chewing_set_maxChiSymbolLen(NULL, 0);       // shall not crash

    ret = chewing_get_maxChiSymbolLen(NULL);
    ok(ret == -1, "chewing_get_maxChiSymbolLen() returns `%d' shall be `%d'", ret, -1);

    chewing_set_selKey(NULL, NULL, 0);  // shall not crash

    key = chewing_get_selKey(NULL);
    ok(key == NULL, "chewing_get_selKey() returns NULL");
    chewing_free(key);

    chewing_set_addPhraseDirection(NULL, 0);    // shall not crash

    ret = chewing_get_addPhraseDirection(NULL);
    ok(ret == -1, "chewing_get_addPhraseDirection() returns `%d' shall be `%d'", ret, -1);

    chewing_set_spaceAsSelection(NULL, 0);      // shall not crash

    ret = chewing_get_spaceAsSelection(NULL);
    ok(ret == -1, "chewing_get_spaceAsSelection() returns `%d' shall be `%d'", ret, -1);

    chewing_set_escCleanAllBuf(NULL, 0);        // shall not crash

    ret = chewing_get_escCleanAllBuf(NULL);
    ok(ret == -1, "chewing_get_escCleanAllBuf() returns `%d' shall be `%d'", ret, -1);

    chewing_set_autoShiftCur(NULL, 0);  // shall not crash

    ret = chewing_get_autoShiftCur(NULL);
    ok(ret == -1, "chewing_get_autoShiftCur() returns `%d' shall be `%d'", ret, -1);

    chewing_set_easySymbolInput(NULL, 0);       // shall not crash

    ret = chewing_get_easySymbolInput(NULL);
    ok(ret == -1, "chewing_get_easySymbolInput() returns `%d' shall be `%d'", ret, -1);

    chewing_set_phraseChoiceRearward(NULL, 0);

    ret = chewing_get_phraseChoiceRearward(NULL);
    ok(ret == -1, "chewing_get_phraseChoiceRearward() returns `%d' shall be `%d'", ret, -1);

    chewing_set_ChiEngMode(NULL, 0);    // shall not crash

    ret = chewing_get_ChiEngMode(NULL);
    ok(ret == -1, "chewing_get_ChiEngMode() returns `%d' shall be `%d'", ret, -1);

    chewing_set_ShapeMode(NULL, 0);     // shall not crash

    ret = chewing_handle_Space(NULL);
    ok(ret == -1, "chewing_handle_Space() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Esc(NULL);
    ok(ret == -1, "chewing_handle_Esc() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Enter(NULL);
    ok(ret == -1, "chewing_handle_Enter() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Del(NULL);
    ok(ret == -1, "chewing_handle_Del() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Backspace(NULL);
    ok(ret == -1, "chewing_handle_Backspace() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Up(NULL);
    ok(ret == -1, "chewing_handle_Up() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Down(NULL);
    ok(ret == -1, "chewing_handle_Down() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_ShiftLeft(NULL);
    ok(ret == -1, "chewing_handle_ShiftLeft() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Left(NULL);
    ok(ret == -1, "chewing_handle_Left() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_ShiftRight(NULL);
    ok(ret == -1, "chewing_handle_ShiftRight() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Right(NULL);
    ok(ret == -1, "chewing_handle_Right() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Tab(NULL);
    ok(ret == -1, "chewing_handle_Tab() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_DblTab(NULL);
    ok(ret == -1, "chewing_handle_DblTab() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Capslock(NULL);
    ok(ret == -1, "chewing_handle_Capslock() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Home(NULL);
    ok(ret == -1, "chewing_handle_Home() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_PageUp(NULL);
    ok(ret == -1, "chewing_handle_PageUp() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_PageDown(NULL);
    ok(ret == -1, "chewing_handle_PageDown() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Default(NULL, 0);
    ok(ret == -1, "chewing_handle_Default() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_CtrlNum(NULL, 0);
    ok(ret == -1, "chewing_handle_CtrlNum() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_ShiftSpace(NULL);
    ok(ret == -1, "chewing_handle_ShiftSpace() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_handle_Numlock(NULL, 0);
    ok(ret == -1, "chewing_handle_Numlock() returns `%d' shall be `%d'", ret, -1);

    phone = chewing_get_phoneSeq(NULL);
    ok(phone == NULL, "chewing_get_phoneSeq() returns NULL");
    chewing_free(phone);

    ret = chewing_get_phoneSeqLen(NULL);
    ok(ret == -1, "chewing_get_phoneSeqLen() returns `%d' shall be `%d'", ret, -1);

    chewing_set_logger(NULL, NULL, NULL);

    ret = chewing_userphrase_enumerate(NULL);
    ok(ret == -1, "chewing_userphrase_enumerate() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_userphrase_has_next(NULL, NULL, NULL);
    ok(ret == 0, "chewing_userphrase_has_next() returns `%d' shall be `%d'", ret, 0);

    ret = chewing_userphrase_get(NULL, NULL, 0, NULL, 0);
    ok(ret == -1, "chewing_userphrase_get() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_userphrase_add(NULL, NULL, NULL);
    ok(ret == -1, "chewing_userphrase_add() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_userphrase_remove(NULL, NULL, NULL);
    ok(ret == -1, "chewing_userphrase_remove() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_userphrase_lookup(NULL, NULL, NULL);
    ok(ret == 0, "chewing_userphrase_lookup() returns `%d' shall be `%d'", ret, 0);

    ret = chewing_cand_open(NULL);
    ok(ret == -1, "chewing_cand_open() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_close(NULL);
    ok(ret == -1, "chewing_cand_open() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_choose_by_index(NULL, 0);
    ok(ret == -1, "chewing_cand_choose_by_index() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_list_first(NULL);
    ok(ret == -1, "chewing_cand_list_first() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_list_last(NULL);
    ok(ret == -1, "chewing_cand_list_last() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_list_has_next(NULL);
    ok(ret == 0, "chewing_cand_list_has_next() returns `%d' shall be `%d'", ret, 0);

    ret = chewing_cand_list_has_prev(NULL);
    ok(ret == 0, "chewing_cand_list_has_prev() returns `%d' shall be `%d'", ret, 0);

    ret = chewing_cand_list_next(NULL);
    ok(ret == -1, "chewing_cand_list_next() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_list_prev(NULL);
    ok(ret == -1, "chewing_cand_list_prev() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_commit_preedit_buf(NULL);
    ok(ret == -1, "chewing_commit_preedit_buf() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_clean_preedit_buf(NULL);
    ok(ret == -1, "chewing_clean_preedit_buf() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_clean_bopomofo_buf(NULL);
    ok(ret == -1, "chewing_clean_bopomofo_buf() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_commit_Check(NULL);
    ok(ret == -1, "chewing_commit_Check() returns `%d' shall be `%d'", ret, -1);

    buf = chewing_commit_String(NULL);
    ok(strcmp(buf, "") == 0, "chewing_commit_String() returns `%s' shall be `%s'", buf, "");
    chewing_free(buf);

    const_buf = chewing_commit_String_static(NULL);
    ok(strcmp(const_buf, "") == 0, "chewing_commit_String() returns `%s' shall be `%s'", const_buf, "");

    ret = chewing_buffer_Check(NULL);
    ok(ret == -1, "chewing_buffer_Check() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_buffer_Len(NULL);
    ok(ret == -1, "chewing_buffer_Len() returns `%d' shall be `%d'", ret, -1);

    buf = chewing_buffer_String(NULL);
    ok(strcmp(buf, "") == 0, "chewing_buffer_String() returns `%s' shall be `%s'", buf, "");
    chewing_free(buf);

    const_buf = chewing_buffer_String_static(NULL);
    ok(strcmp(const_buf, "") == 0, "chewing_buffer_String_static() returns `%s' shall be `%s'", const_buf, "");

    const_buf = chewing_bopomofo_String_static(NULL);
    ok(strcmp(const_buf, "") == 0, "chewing_bopomofo_String_static() returns `%s' shall be `%s'", const_buf, "");

BEGIN_IGNORE_DEPRECATIONS
    buf = chewing_zuin_String(NULL, NULL);
END_IGNORE_DEPRECATIONS
    ok(strcmp(buf, "") == 0, "chewing_zuin_String() returns `%s' shall be `%s'", buf, "");
    chewing_free(buf);

    ret = chewing_bopomofo_Check(NULL);
    ok(ret == -1, "chewing_bopomofo_Check() returns `%d' shall be `%d'", ret, -1);

BEGIN_IGNORE_DEPRECATIONS
    chewing_zuin_Check(NULL); // shall not crash
END_IGNORE_DEPRECATIONS

    ret = chewing_cursor_Current(NULL);
    ok(ret == -1, "chewing_cursor_Current() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_CheckDone(NULL);
    ok(ret == -1, "chewing_cand_CheckDone() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_TotalPage(NULL);
    ok(ret == -1, "chewing_cand_TotalPage() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_ChoicePerPage(NULL);
    ok(ret == -1, "chewing_cand_ChoicePerPage() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_TotalChoice(NULL);
    ok(ret == -1, "chewing_cand_TotalChoice() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_cand_CurrentPage(NULL);
    ok(ret == -1, "chewing_cand_CurrentPage() returns `%d' shall be `%d'", ret, -1);

    chewing_cand_Enumerate(NULL);       // shall not crash

    ret = chewing_cand_hasNext(NULL);
    ok(ret == -1, "chewing_cand_hasNext() returns `%d' shall be `%d'", ret, -1);

    const_buf = chewing_cand_String_static(NULL);
    ok(strcmp(const_buf, "") == 0, "chewing_cand_String_static() returns `%s' shall be `%s'", const_buf, "");

    buf = chewing_cand_String(NULL);
    ok(strcmp(buf, "") == 0, "chewing_cand_String() returns `%s' shall be `%s'", buf, "");
    chewing_free(buf);

    chewing_interval_Enumerate(NULL);   // shall not crash

    ret = chewing_interval_hasNext(NULL);
    ok(ret == -1, "chewing_interval_hasNext() returns `%d' shall be `%d'", ret, -1);

    chewing_interval_Get(NULL, NULL);   // shall not crash

    ret = chewing_aux_Check(NULL);
    ok(ret == -1, "chewing_aux_Check() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_aux_Length(NULL);
    ok(ret == -1, "chewing_aux_Length() returns `%d' shall be `%d'", ret, -1);

    const_buf = chewing_aux_String_static(NULL);
    ok(strcmp(const_buf, "") == 0, "chewing_aux_String_static() returns `%s' shall be `%s'", const_buf, "");

    buf = chewing_aux_String(NULL);
    ok(strcmp(buf, "") == 0, "chewing_aux_String() returns `%s' shall be `%s'", buf, "");
    chewing_free(buf);

    ret = chewing_keystroke_CheckIgnore(NULL);
    ok(ret == -1, "chewing_keystroke_CheckIgnore() returns `%d' shall be `%d'", ret, -1);

    ret = chewing_keystroke_CheckAbsorb(NULL);
    ok(ret == -1, "chewing_keystroke_CheckAbsorb() returns `%d' shall be `%d'", ret, -1);

    chewing_kbtype_Enumerate(NULL);     // shall not crash

    ret = chewing_kbtype_hasNext(NULL);
    ok(ret == -1, "chewing_kbtype_hasNext() returns `%d' shall be `%d'", ret, -1);

    const_buf = chewing_kbtype_String_static(NULL);
    ok(strcmp(const_buf, "") == 0, "chewing_kbtype_String_static() returns `%s' shall be `%s'", const_buf, "");

    buf = chewing_kbtype_String(NULL);
    ok(strcmp(buf, "") == 0, "chewing_kbtype_String() returns `%s' shall be `%s'", buf, "");
    chewing_free(buf);
}
Beispiel #10
0
int main(int argc, char *argv[])
{
    size_t i;
    int flag_random_init = 0;
    int flag_random_extra = 0;
    int flag_loop = -1;
    int (*get_input)() = &random256;

    for (i = 1; i < argc; i++) {
	if (strcmp(argv[i], "-init") == 0)
	    flag_random_init = 1;
	else if (strcmp(argv[i], "-extra") == 0 && argv[i + 1])
	    flag_random_extra = 1;
	else if (strcmp(argv[i], "-loop") == 0 && argv[i + 1])
	    flag_loop = atoi(argv[++i]);
	else if (strcmp(argv[i], "-stdin") == 0)
	    get_input = &get_stdin;
	else {
	    printf("Usage: %s [-init] [-extra] [-loop N] [-stdin]\n", argv[0]);
	    printf("\t-init           Random initial configuration\n");
	    printf("\t-extra          Random change all configurations during input.\n");
	    printf("\t                This is usually unexpected.\n");
	    printf("\t-stdin          Get random input from stdin\n");
	    printf("\t-loop N         How many iterations to test (default infinite=-1)\n");
	    exit(1);
	}
    }

    /* 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);

    for (i = 0; i != flag_loop; i++) {
	ChewingContext *ctx = chewing_new();

	/* typical configuration */
	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);

	if (flag_random_init) {
	    chewing_set_KBType(ctx, get_input());
	    chewing_set_candPerPage(ctx, get_input());
	    chewing_set_maxChiSymbolLen(ctx, get_input());
	    chewing_set_addPhraseDirection(ctx, get_input());
	    chewing_set_selKey(ctx, selKey_define, get_input() % 11);
	    chewing_set_spaceAsSelection(ctx, get_input());
	    chewing_set_escCleanAllBuf(ctx, get_input());
	    chewing_set_autoShiftCur(ctx, get_input());
	    chewing_set_easySymbolInput(ctx, get_input());
	    chewing_set_phraseChoiceRearward(ctx, get_input());
	}

	while (1) {
	    /* Random value: [0, max_key) for keys, [max_key, 0xff] for
	     * configurations. Use a fixed range here because I don't want the
	     * meaning of input changed a lot frequently if we add more keys in
	     * the future. */
	    const int max_key = 192;  /* arbitrary number */
	    int v = get_input();
	    if (v == EOF)
		break;
	    assert(max_key * sizeof(all_key[0]) >= sizeof(all_key));
	    if (v >= max_key) {
		const int typical = 2;
		int handled = 1;
		v = v - max_key;
		if (flag_random_extra || v < typical) {
		    switch (v) {
			/* typical configurations may be changed during input */
			case 0:
			    chewing_set_ChiEngMode(ctx, get_input());
			    break;
			case 1:
			    chewing_set_ShapeMode(ctx, get_input());
			    break;

			/* usually not changed during input */
			case 2:
			    chewing_set_KBType(ctx, get_input());
			    break;
			case 3:
			    chewing_set_candPerPage(ctx, get_input());
			    break;
			case 4:
			    chewing_set_maxChiSymbolLen(ctx, get_input());
			    break;
			case 5:
			    chewing_set_addPhraseDirection(ctx, get_input());
			    break;
			case 6:
			    chewing_set_selKey(ctx, selKey_define, get_input() % 11);
			    break;
			case 7:
			    chewing_set_spaceAsSelection(ctx, get_input());
			    break;
			case 8:
			    chewing_set_escCleanAllBuf(ctx, get_input());
			    break;
			case 9:
			    chewing_set_autoShiftCur(ctx, get_input());
			    break;
			case 10:
			    chewing_set_easySymbolInput(ctx, get_input());
			    break;
			case 11:
			    chewing_set_phraseChoiceRearward(ctx, get_input());
			    break;
			default:
			    handled = 0;
			    break;
		    }
		} else {
		    handled = 0;
		}
		if (!handled)
		    break;
	    } else {
		if (0 <= v && v < max_key) {
		    int key = all_key[v];
		    type_single_keystroke(ctx, key);
		} else {
		    break;
		}
	    }
	    commit_string(ctx);
	}
	chewing_delete(ctx);

	if (i % 10000 == 0)
	    printf("%zu\n", i);
	if (getenv("AFL_PERSISTENT"))
	    raise(SIGSTOP);
    }

    return 0;
}