From 4c42f29cb8863cc5d6810a8a2a25c478e16292f7 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Thu, 14 Jun 2012 11:13:51 +0800 Subject: refine tests --- tests/storage/test_chewing_table.cpp | 23 +++------------- tests/storage/test_phrase_index.cpp | 52 +++++++++++++----------------------- tests/storage/test_phrase_table.cpp | 20 +++----------- tests/tests_helper.h | 30 +++++++++++++++++++++ 4 files changed, 55 insertions(+), 70 deletions(-) (limited to 'tests') diff --git a/tests/storage/test_chewing_table.cpp b/tests/storage/test_chewing_table.cpp index 028cda9..a826f46 100644 --- a/tests/storage/test_chewing_table.cpp +++ b/tests/storage/test_chewing_table.cpp @@ -22,7 +22,7 @@ #include "timer.h" #include #include "pinyin_internal.h" - +#include "tests_helper.h" size_t bench_times = 1000; @@ -31,25 +31,8 @@ int main(int argc, char * argv[]) { ChewingLargeTable largetable(options); FacadePhraseIndex phrase_index; - for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) { - const char * tablename = pinyin_table_files[i]; - if ( NULL == tablename ) - continue; - - gchar * filename = g_build_filename("..", "..", "data", - tablename, NULL); - FILE * tablefile = fopen(filename, "r"); - if (NULL == tablefile) { - fprintf(stderr, "open %s failed!\n", tablename); - exit(ENOENT); - } - - largetable.load_text(tablefile); - fseek(tablefile, 0L, SEEK_SET); - phrase_index.load_text(i, tablefile); - fclose(tablefile); - g_free(filename); - } + if (!load_phrase_table(&largetable, NULL, &phrase_index)) + exit(ENOENT); MemoryChunk * new_chunk = new MemoryChunk; largetable.store(new_chunk); diff --git a/tests/storage/test_phrase_index.cpp b/tests/storage/test_phrase_index.cpp index f9cb8d2..807b29c 100644 --- a/tests/storage/test_phrase_index.cpp +++ b/tests/storage/test_phrase_index.cpp @@ -2,6 +2,7 @@ #include #include #include "pinyin_internal.h" +#include "tests_helper.h" size_t bench_times = 100000; @@ -37,17 +38,17 @@ int main(int argc, char * argv[]){ phrase_item.get_phrase_string(&string2); assert(string1 == string2); - FacadePhraseIndex phrase_index; - assert(!phrase_index.add_phrase_item(1, &phrase_item)); + FacadePhraseIndex phrase_index_test; + assert(!phrase_index_test.add_phrase_item(1, &phrase_item)); MemoryChunk* chunk = new MemoryChunk; - assert(phrase_index.store(0, chunk)); - assert(phrase_index.load(0, chunk)); + assert(phrase_index_test.store(0, chunk)); + assert(phrase_index_test.load(0, chunk)); PhraseItem item2; guint32 time = record_time(); for ( size_t i = 0; i < bench_times; ++i){ - phrase_index.get_phrase_item(1, item2); + phrase_index_test.get_phrase_item(1, item2); assert(item2.get_unigram_frequency() == 0); assert(item2.get_n_pronunciation() == 2); assert(item2.get_phrase_length() == 1); @@ -57,51 +58,36 @@ int main(int argc, char * argv[]){ { PhraseItem item3; - phrase_index.get_phrase_item(1, item3); + phrase_index_test.get_phrase_item(1, item3); item3.increase_pronunciation_possibility(options, &key1, 200); assert(item3.get_pronunciation_possibility(options, &key1) == 0.5) ; } { PhraseItem item5; - phrase_index.get_phrase_item(1, item5); + phrase_index_test.get_phrase_item(1, item5); gfloat poss = item5.get_pronunciation_possibility(options, &key1); printf("pinyin poss:%f\n", poss); assert(poss == 0.5); } - FacadePhraseIndex phrase_index_load; - for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) { - const char * tablename = pinyin_table_files[i]; - if (NULL == tablename) - continue; - - gchar * filename = g_build_filename("..", "..", "data", - tablename, NULL); - FILE* tablefile = fopen(filename, "r"); - if ( NULL == tablefile ){ - fprintf(stderr, "open %s failed!\n", tablename); - exit(ENOENT); - } - - phrase_index_load.load_text(i, tablefile); - fclose(tablefile); - g_free(filename); - } + FacadePhraseIndex phrase_index; + if (!load_phrase_table(NULL, NULL, &phrase_index)) + exit(ENOENT); phrase_index.compact(); MemoryChunk* store1 = new MemoryChunk; - phrase_index_load.store(1, store1); - phrase_index_load.load(1, store1); + phrase_index.store(1, store1); + phrase_index.load(1, store1); MemoryChunk* store2 = new MemoryChunk; - phrase_index_load.store(2, store2); - phrase_index_load.load(2, store2); + phrase_index.store(2, store2); + phrase_index.load(2, store2); phrase_index.compact(); - phrase_index_load.get_phrase_item(16870553, item2); + phrase_index.get_phrase_item(16870553, item2); assert( item2.get_phrase_length() == 14); assert( item2.get_n_pronunciation() == 1); @@ -112,11 +98,11 @@ int main(int argc, char * argv[]){ g_free(string); guint32 delta = 3; - phrase_index_load.add_unigram_frequency(16870553, delta); - phrase_index_load.get_phrase_item(16870553, item2); + phrase_index.add_unigram_frequency(16870553, delta); + phrase_index.get_phrase_item(16870553, item2); assert( item2.get_unigram_frequency() == 3); - phrase_index_load.get_phrase_item(16777222, item2); + phrase_index.get_phrase_item(16777222, item2); assert(item2.get_phrase_length() == 1); assert(item2.get_n_pronunciation() == 6); diff --git a/tests/storage/test_phrase_table.cpp b/tests/storage/test_phrase_table.cpp index a7043a8..58d634e 100644 --- a/tests/storage/test_phrase_table.cpp +++ b/tests/storage/test_phrase_table.cpp @@ -1,29 +1,15 @@ #include "timer.h" #include #include "pinyin_internal.h" +#include "tests_helper.h" size_t bench_times = 1000; int main(int argc, char * argv[]){ PhraseLargeTable largetable; - for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) { - const char * tablename = pinyin_table_files[i]; - if (NULL == tablename) - continue; - - gchar * filename = g_build_filename("..", "..", "data", - tablename, NULL); - FILE * tablefile = fopen(filename, "r"); - if ( tablefile == NULL ) { - fprintf(stderr, "open %s failed!\n", tablename); - return 1; - } - - largetable.load_text(tablefile); - fclose(tablefile); - g_free(filename); - } + if (!load_phrase_table(NULL, &largetable, NULL)) + exit(ENOENT); MemoryChunk * chunk = new MemoryChunk; largetable.store(chunk); diff --git a/tests/tests_helper.h b/tests/tests_helper.h index af1b38a..5694148 100644 --- a/tests/tests_helper.h +++ b/tests/tests_helper.h @@ -44,4 +44,34 @@ static bool load_phrase_index(FacadePhraseIndex * phrase_index){ return true; } +static bool load_phrase_table(ChewingLargeTable * chewing_table, + PhraseLargeTable * phrase_table, + FacadePhraseIndex * phrase_index){ + for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) { + const char * tablename = pinyin_table_files[i]; + if ( NULL == tablename ) + continue; + + gchar * filename = g_build_filename("..", "..", "data", + tablename, NULL); + FILE * tablefile = fopen(filename, "r"); + if (NULL == tablefile) { + fprintf(stderr, "open %s failed!\n", tablename); + return false; + } + g_free(filename); + + if (chewing_table) + chewing_table->load_text(tablefile); + fseek(tablefile, 0L, SEEK_SET); + if (phrase_table) + phrase_table->load_text(tablefile); + fseek(tablefile, 0L, SEEK_SET); + if (phrase_index) + phrase_index->load_text(i, tablefile); + fclose(tablefile); + } + return true; +} + #endif -- cgit