Skip to content
parse-pinyin-test.cpp 2.03 KiB
Newer Older
#include "parse-pinyin.h"
Yuanle Song's avatar
Yuanle Song committed
#include <iostream>
#include <glib.h>
#include <glib/gprintf.h>
Yuanle Song's avatar
Yuanle Song committed

Yuanle Song's avatar
Yuanle Song committed
test_parse_pinyin()
Yuanle Song's avatar
Yuanle Song committed
	GList *result = NULL;
	Pinyin *thispy = NULL;
	result = parse_pinyin("liyifeng", 15, PINYIN_FLAGS_NONE);
Yuanle Song's avatar
Yuanle Song committed
	g_assert_cmpint(g_list_length(result), ==, 3);
Yuanle Song's avatar
Yuanle Song committed
	thispy = (Pinyin *) g_list_nth_data(result, 0);
	g_assert_cmpint(thispy->shengmu_i, ==, 10);
	g_assert_cmpint(thispy->yunmu_i, ==, 34);
	g_assert_cmpint(thispy->length, ==, 2);
Yuanle Song's avatar
Yuanle Song committed
	thispy = (Pinyin *) g_list_nth_data(result, 1);
	g_assert_cmpint(thispy->shengmu_i, ==, 21);
	g_assert_cmpint(thispy->yunmu_i, ==, 34);
	g_assert_cmpint(thispy->length, ==, 2);
Yuanle Song's avatar
Yuanle Song committed
	thispy = (Pinyin *) g_list_nth_data(result, 2);
	g_assert_cmpint(thispy->shengmu_i, ==, 5);
	g_assert_cmpint(thispy->yunmu_i, ==, 32);
	g_assert_cmpint(thispy->length, ==, 4);
Yuanle Song's avatar
Yuanle Song committed
	g_list_free_full(result, g_free);
/**
 * print GList of Pinyin.
 */
static void
Yuanle Song's avatar
Yuanle Song committed
print_parse_result(GList *result)
Yuanle Song's avatar
Yuanle Song committed
	GList *iter = result;
	Pinyin *thispy = NULL;
	while (iter != NULL) {
Yuanle Song's avatar
Yuanle Song committed
		thispy = (Pinyin *) iter->data;
		g_printf("shengmu_i=%i yunmu_i=%i length=%u\n",
			 thispy->shengmu_i, thispy->yunmu_i, thispy->length);
		iter = iter->next;
	}
}

static void
Yuanle Song's avatar
Yuanle Song committed
test_parse_pinyin_incomplete_pinyin()
Yuanle Song's avatar
Yuanle Song committed
	GList *result = NULL;
	Pinyin *thispy = NULL;
	result = parse_pinyin("zhey", 15, PINYIN_INCOMPLETE_PINYIN);
Yuanle Song's avatar
Yuanle Song committed
	print_parse_result(result);
	g_assert_cmpint(g_list_length(result), ==, 2);
Yuanle Song's avatar
Yuanle Song committed
	thispy = (Pinyin *) g_list_nth_data(result, 0);
	g_assert_cmpint(thispy->shengmu_i, ==, 23);
	g_assert_cmpint(thispy->yunmu_i, ==, 29);
	g_assert_cmpint(thispy->length, ==, 3);
Yuanle Song's avatar
Yuanle Song committed
	thispy = (Pinyin *) g_list_nth_data(result, 1);
	g_assert_cmpint(thispy->shengmu_i, ==, 21);
	g_assert_cmpint(thispy->yunmu_i, ==, 0);
	g_assert_cmpint(thispy->length, ==, 1);
Yuanle Song's avatar
Yuanle Song committed
	g_list_free_full(result, g_free);
Yuanle Song's avatar
Yuanle Song committed
int
Yuanle Song's avatar
Yuanle Song committed
main(int argc, char *argv[])
Yuanle Song's avatar
Yuanle Song committed
{
Yuanle Song's avatar
Yuanle Song committed
	g_test_init(&argc, &argv, NULL);
	g_test_add_func("/zero/test_parse_pinyin",
			test_parse_pinyin);
	g_test_add_func("/zero/test_parse_pinyin incomplete pinyin",
			test_parse_pinyin_incomplete_pinyin);
Yuanle Song's avatar
Yuanle Song committed

Yuanle Song's avatar
Yuanle Song committed
	return g_test_run();
Yuanle Song's avatar
Yuanle Song committed
}