summaryrefslogtreecommitdiffstats
path: root/tests/storage/test_flexible_ngram.cpp
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2011-04-08 12:23:25 +0800
committerPeng Wu <alexepico@gmail.com>2011-04-08 12:23:25 +0800
commit8c7b4f779a5a9bc8b01f1580a44fa02ebc1948f1 (patch)
tree677f13231a35d1171ed53269ebb3f5e0bb00fb73 /tests/storage/test_flexible_ngram.cpp
parent0d839f2ef6d4f70dbad007384ca998eb0ae51732 (diff)
downloadlibpinyin-8c7b4f779a5a9bc8b01f1580a44fa02ebc1948f1.tar.gz
libpinyin-8c7b4f779a5a9bc8b01f1580a44fa02ebc1948f1.tar.xz
libpinyin-8c7b4f779a5a9bc8b01f1580a44fa02ebc1948f1.zip
add test case
Diffstat (limited to 'tests/storage/test_flexible_ngram.cpp')
-rw-r--r--tests/storage/test_flexible_ngram.cpp69
1 files changed, 68 insertions, 1 deletions
diff --git a/tests/storage/test_flexible_ngram.cpp b/tests/storage/test_flexible_ngram.cpp
index 744187f..f915a1f 100644
--- a/tests/storage/test_flexible_ngram.cpp
+++ b/tests/storage/test_flexible_ngram.cpp
@@ -1,5 +1,72 @@
#include "pinyin.h"
int main(int argc, char * argv[]) {
- FlexibleBigram<guint64, guint32, guint32> test_bigram;
+ FlexibleSingleGram<guint32, guint32> single_gram;
+ typedef FlexibleSingleGram<guint32, guint32>::ArrayItemWithToken array_item_t;
+
+ const guint32 total_freq = 16;
+ assert(single_gram.set_array_header(total_freq));
+
+ phrase_token_t tokens[6] = { 2, 6, 4, 3, 1, 3 };
+ guint32 freqs[6] = { 1, 2, 4, 8, 16, 32};
+
+ for ( size_t i = 0; i < 6; ++i ){
+ single_gram.set_array_item(tokens[i], freqs[i]);
+ }
+
+ guint32 freq;
+ single_gram.get_array_item(3, freq);
+ assert(freq == 32);
+
+ printf("--------------------------------------------------------\n");
+ PhraseIndexRange range;
+ FlexibleBigramPhraseArray array = g_array_new(FALSE, FALSE, sizeof(array_item_t));
+ range.m_range_begin = 0; range.m_range_end = 8;
+ single_gram.search(&range, array);
+ for ( size_t i = 0; i < array->len; ++i ){
+ array_item_t * item = &g_array_index(array, array_item_t, i);
+ printf("item:%d:%d\n", item->m_token, item->m_item);
+ }
+
+ assert(single_gram.get_array_header(freq));
+ assert(freq == total_freq);
+
+ FlexibleBigram<guint32, guint32, guint32> bigram;
+ assert(bigram.attach("/tmp/training.db"));
+ bigram.store(1, &single_gram);
+ single_gram.set_array_item(5, 8);
+ single_gram.set_array_header(32);
+ single_gram.get_array_header(freq);
+ printf("new array header:%d\n", freq);
+
+ bigram.store(2, &single_gram);
+
+ for (int m = 1; m <= 2; ++m ){
+ printf("--------------------------------------------------------\n");
+ FlexibleSingleGram<guint32, guint32> * train_gram;
+ bigram.load(m, train_gram);
+ g_array_set_size(array, 0);
+ range.m_range_begin = 0; range.m_range_end = 8;
+ train_gram->search(&range, array);
+ for ( size_t i = 0; i < array->len; ++i ){
+ array_item_t * item = &g_array_index(array, array_item_t, i);
+ printf("item:%d:%d\n", item->m_token, item->m_item);
+ }
+ delete train_gram;
+ }
+ g_array_free(array, TRUE);
+
+ GArray * items = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
+ bigram.get_all_items(items);
+ printf("-----------------------train----------------------------\n");
+ for ( size_t i = 0; i < items->len; ++i ){
+ phrase_token_t * token = &g_array_index(items, phrase_token_t, i);
+ printf("item:%d\n", *token);
+ }
+
+ printf("-----------------------magic header---------------------\n");
+ bigram.set_magic_header(total_freq);
+ bigram.get_magic_header(freq);
+ assert(total_freq == freq);
+ printf("magic header:%d\n", freq);
}