Skip to content
parse-pinyin-test.cpp 2.02 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

static void
test_parse_pinyin ()
{
	GList* result = NULL;
	Pinyin* thispy = NULL;

	result = parse_pinyin ("liyifeng", 15);
	g_assert_cmpint (g_list_length (result), ==, 3);

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

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

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

	g_list_free_full (result, g_free);
}

/**
 * print GList of Pinyin.
 */
static void
print_parse_result (GList* result)
{
	GList* iter = result;
	Pinyin* thispy = NULL;
	while (iter != NULL) {
		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
test_parse_pinyin_incomplete_pinyin ()
{
	GList* result = NULL;
	Pinyin* thispy = NULL;

	result = parse_pinyin ("zhey", 15);
	print_parse_result (result);
	g_assert_cmpint (g_list_length (result), ==, 2);

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

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

	g_list_free_full (result, g_free);
}

Yuanle Song's avatar
Yuanle Song committed
int
main (int argc, char *argv[])
{
	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

	return g_test_run ();
}