refine tests
[platform/upstream/libpinyin.git] / tests / storage / test_phrase_index.cpp
1 #include "timer.h"
2 #include <stdio.h>
3 #include <errno.h>
4 #include "pinyin_internal.h"
5 #include "tests_helper.h"
6
7 size_t bench_times = 100000;
8
9 int main(int argc, char * argv[]){
10     PhraseItem phrase_item;
11     ucs4_t string1 = 2;
12     ChewingKey key1 = ChewingKey(CHEWING_CH, CHEWING_ZERO_MIDDLE, CHEWING_ENG);
13     ChewingKey key2 = ChewingKey(CHEWING_SH, CHEWING_ZERO_MIDDLE, CHEWING_ANG);
14
15
16     phrase_item.set_phrase_string(1, &string1);
17     phrase_item.append_pronunciation(&key1, 100);
18     phrase_item.append_pronunciation(&key2, 300);
19
20     assert(phrase_item.get_phrase_length() == 1);
21
22     ChewingKey key3;
23     guint32 freq;
24     phrase_item.get_nth_pronunciation(0, &key3, freq);
25     assert(key3 == key1);
26     assert(freq == 100);
27     phrase_item.get_nth_pronunciation(1, &key3, freq);
28     assert(key3 == key2);
29     assert(freq == 300);
30
31     pinyin_option_t options = 0;
32     gfloat poss = phrase_item.get_pronunciation_possibility(options, &key1);
33     printf("pinyin possiblitiy:%f\n", poss);
34
35     assert(phrase_item.get_unigram_frequency() == 0);
36
37     ucs4_t string2;
38     phrase_item.get_phrase_string(&string2);
39     assert(string1 == string2);
40
41     FacadePhraseIndex phrase_index_test;
42     assert(!phrase_index_test.add_phrase_item(1, &phrase_item));
43
44     MemoryChunk* chunk = new MemoryChunk;
45     assert(phrase_index_test.store(0, chunk));
46     assert(phrase_index_test.load(0, chunk));
47
48     PhraseItem item2;
49     guint32 time = record_time();
50     for ( size_t i = 0; i < bench_times; ++i){
51         phrase_index_test.get_phrase_item(1, item2);
52         assert(item2.get_unigram_frequency() == 0);
53         assert(item2.get_n_pronunciation() == 2);
54         assert(item2.get_phrase_length() == 1);
55         assert(item2.get_pronunciation_possibility(options, &key2) == 0.75);
56     }
57     print_time(time, bench_times);
58
59     {
60         PhraseItem item3;
61         phrase_index_test.get_phrase_item(1, item3);
62         item3.increase_pronunciation_possibility(options, &key1, 200);
63         assert(item3.get_pronunciation_possibility(options, &key1) == 0.5) ;
64     }
65
66     {
67         PhraseItem item5;
68         phrase_index_test.get_phrase_item(1, item5);
69         gfloat poss = item5.get_pronunciation_possibility(options, &key1);
70         printf("pinyin poss:%f\n", poss);
71         assert(poss == 0.5);
72     }
73
74     FacadePhraseIndex phrase_index;
75     if (!load_phrase_table(NULL, NULL, &phrase_index))
76         exit(ENOENT);
77
78     phrase_index.compact();
79
80     MemoryChunk* store1 = new MemoryChunk;
81     phrase_index.store(1, store1);
82     phrase_index.load(1, store1);
83
84     MemoryChunk* store2 = new MemoryChunk;
85     phrase_index.store(2, store2);
86     phrase_index.load(2, store2);
87
88     phrase_index.compact();
89
90     phrase_index.get_phrase_item(16870553, item2);
91     assert( item2.get_phrase_length() == 14);
92     assert( item2.get_n_pronunciation() == 1);
93
94     ucs4_t buf[1024];
95     item2.get_phrase_string(buf);
96     char * string = g_ucs4_to_utf8( buf, 14, NULL, NULL, NULL);
97     printf("%s\n", string);
98     g_free(string);
99
100     guint32 delta = 3;
101     phrase_index.add_unigram_frequency(16870553, delta);
102     phrase_index.get_phrase_item(16870553, item2);
103     assert( item2.get_unigram_frequency() == 3);
104
105     phrase_index.get_phrase_item(16777222, item2);
106     assert(item2.get_phrase_length() == 1);
107     assert(item2.get_n_pronunciation() == 6);
108
109     return 0;
110 }