write test cases for table info
authorPeng Wu <alexepico@gmail.com>
Mon, 8 Apr 2013 07:10:27 +0000 (15:10 +0800)
committerPeng Wu <alexepico@gmail.com>
Mon, 8 Apr 2013 07:10:27 +0000 (15:10 +0800)
data/Makefile.am
src/storage/table_info.cpp
tests/storage/Makefile.am
tests/storage/test_table_info.cpp [new file with mode: 0644]

index 81d3463..da34394 100644 (file)
@@ -38,9 +38,10 @@ binary_model_data    = phrase_index.bin pinyin_index.bin \
 MAINTAINERCLEANFILES   = Makefile.in
 
 EXTRA_DIST             = $(textual_model_data) \
-                         table.conf
+                          table.conf
 
-libpinyin_db_DATA      = $(binary_model_data)
+libpinyin_db_DATA      = $(binary_model_data) \
+                          table.conf
 
 libpinyin_dbdir                = $(libdir)/libpinyin/data
 
index fd2d2f4..5cd0ca4 100644 (file)
@@ -130,15 +130,15 @@ bool SystemTableInfo::load(const char * filename) {
     int binver = 0, modelver = 0;
     gfloat lambda = 0.;
 
-    int num = fscanf(input, "binary format version:%d", &binver);
+    int num = fscanf(input, "binary format version:%d\n", &binver);
     if (1 != num)
         return false;
 
-    num = fscanf(input, "model data version:%d", &modelver);
+    num = fscanf(input, "model data version:%d\n", &modelver);
     if (1 != num)
         return false;
 
-    num = fscanf(input, "lambda parameter:%f", &lambda);
+    num = fscanf(input, "lambda parameter:%f\n", &lambda);
     if (1 != num)
         return false;
 
@@ -153,7 +153,7 @@ bool SystemTableInfo::load(const char * filename) {
     int index = 0;
     char tablefile[256], sysfile[256], userfile[256], filetype[256];
     while (!feof(input)) {
-        num = fscanf(input, "%d %s %s %s %s",
+        num = fscanf(input, "%d %s %s %s %s\n",
                      &index, tablefile, sysfile, userfile, filetype);
 
         if (5 != num)
@@ -210,11 +210,11 @@ bool UserTableInfo::load(const char * filename) {
 
     int binver = 0, modelver = 0;
 
-    int num = fscanf(input, "binary format version:%d", &binver);
+    int num = fscanf(input, "binary format version:%d\n", &binver);
     if (1 != num)
         return false;
 
-    num = fscanf(input, "model data version:%d", &modelver);
+    num = fscanf(input, "model data version:%d\n", &modelver);
     if (1 != num)
         return false;
 
index 7b03120..b7ed8b6 100644 (file)
@@ -32,7 +32,8 @@ noinst_PROGRAMS          = test_phrase_index \
                           test_ngram \
                           test_flexible_ngram \
                           test_parser2 \
-                          test_chewing_table
+                          test_chewing_table \
+                          test_table_info
 
 
 test_phrase_index_SOURCES = test_phrase_index.cpp
@@ -64,3 +65,7 @@ test_parser2_LDADD      = ../../src/libpinyin_internal.la @GLIB2_LIBS@
 test_chewing_table_SOURCES    = test_chewing_table.cpp
 
 test_chewing_table_LDADD      = ../../src/libpinyin_internal.la @GLIB2_LIBS@
+
+test_table_info_SOURCES    = test_table_info.cpp
+
+test_table_info_LDADD      = ../../src/libpinyin_internal.la @GLIB2_LIBS@
diff --git a/tests/storage/test_table_info.cpp b/tests/storage/test_table_info.cpp
new file mode 100644 (file)
index 0000000..30f6a0d
--- /dev/null
@@ -0,0 +1,83 @@
+/* 
+ *  libpinyin
+ *  Library to deal with pinyin.
+ *  
+ *  Copyright (C) 2013 Peng Wu <alexepico@gmail.com>
+ *  
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ * 
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <stdio.h>
+#include "pinyin_internal.h"
+
+
+int main(int argc, char * argv[]) {
+    SystemTableInfo system_table_info;
+
+    bool retval = system_table_info.load("../../data/table.conf");
+    if (!retval) {
+        exit(ENOENT);
+    }
+
+    printf("lambda:%f\n", system_table_info.get_lambda());
+
+    size_t i;
+    for (i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
+        const pinyin_table_info_t * tableinfo =
+            system_table_info.get_table_info() + i;
+
+        assert(i == tableinfo->m_dict_index);
+        printf("table index:%d\n", tableinfo->m_dict_index);
+
+        switch(tableinfo->m_file_type) {
+        case NOT_USED:
+            printf("not used.\n");
+            break;
+
+        case SYSTEM_FILE:
+            printf("system file:%s %s %s.\n", tableinfo->m_table_filename,
+                   tableinfo->m_system_filename, tableinfo->m_user_filename);
+            break;
+
+        case DICTIONARY:
+            printf("dictionary:%s %s %s.\n", tableinfo->m_table_filename,
+                   tableinfo->m_system_filename, tableinfo->m_user_filename);
+            break;
+
+        case USER_FILE:
+            printf("user file:%s.\n", tableinfo->m_user_filename);
+            break;
+
+        default:
+            assert(false);
+        }
+    }
+
+    UserTableInfo user_table_info;
+    retval = user_table_info.is_conform(&system_table_info);
+    assert(!retval);
+
+    user_table_info.make_conform(&system_table_info);
+    retval = user_table_info.is_conform(&system_table_info);
+    assert(retval);
+
+    assert(user_table_info.save("/tmp/user.conf"));
+    assert(user_table_info.load("/tmp/user.conf"));
+
+    retval = user_table_info.is_conform(&system_table_info);
+    assert(retval);
+
+    return 0;
+}