libept
|
00001 /* 00002 * Merge different vocabularies together and create the tag and facet indexes 00003 * 00004 * Copyright (C) 2003-2007 Enrico Zini <enrico@debian.org> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 #include <wibble/test.h> 00022 #include <ept/debtags/maint/vocabularymerger.h> 00023 #include <tagcoll/input/string.h> 00024 00025 using namespace std; 00026 using namespace tagcoll; 00027 00028 struct TestVocabularyMerger { 00029 00030 inline static const char* indexref(const char* index, int id) 00031 { 00032 return index + ((int*)index)[id]; 00033 } 00034 00035 00036 Test _1() 00037 { 00038 string voc1 = 00039 "Facet: taste\n" 00040 "Description: Taste\n\n" 00041 "Tag: taste::sweet\n" 00042 "Description: Sweet\n\n" 00043 "Tag: taste::salty\n" 00044 "Description: Salty\n\n"; 00045 string voc2 = 00046 "Facet: smell\n" 00047 "Description: Smell\n\n" 00048 "Tag: smell::fresh\n" 00049 "Description: Fresh\n\n" 00050 "Tag: smell::mold\n" 00051 "Description: Mold\n\n"; 00052 tagcoll::input::String in1(voc1); 00053 tagcoll::input::String in2(voc2); 00054 00055 ept::debtags::VocabularyMerger vm; 00056 00057 // Read and merge the two vocabulary samples 00058 vm.read(in1); 00059 vm.read(in2); 00060 00061 // Write the merged vocabulary to /dev/null (but generate offsets and indexes in the meantime) 00062 vm.write("/dev/null"); 00063 00064 // Create the facet index 00065 char facetIndex[vm.facetIndexer().encodedSize()]; 00066 vm.facetIndexer().encode(facetIndex); 00067 00068 // Create the tag index 00069 char tagIndex[vm.tagIndexer().encodedSize()]; 00070 vm.tagIndexer().encode(tagIndex); 00071 00072 // Check that the facet names have been encoded correctly and in order 00073 assert_eq(string(indexref(facetIndex, 0) + 4*sizeof(int)), "smell"); 00074 assert_eq(string(indexref(facetIndex, 1) + 4*sizeof(int)), "taste"); 00075 00076 // Check the first and last tag indexes for the facets 00077 assert_eq(((int*)indexref(facetIndex, 0))[2], 0); 00078 assert_eq(((int*)indexref(facetIndex, 0))[3], 1); 00079 assert_eq(((int*)indexref(facetIndex, 1))[2], 2); 00080 assert_eq(((int*)indexref(facetIndex, 1))[3], 3); 00081 00082 // Check that the tag names have been encoded correctly and in order 00083 assert_eq(string(indexref(tagIndex, 0) + 3*sizeof(int)), "smell::fresh"); 00084 assert_eq(string(indexref(tagIndex, 1) + 3*sizeof(int)), "smell::mold"); 00085 assert_eq(string(indexref(tagIndex, 2) + 3*sizeof(int)), "taste::salty"); 00086 assert_eq(string(indexref(tagIndex, 3) + 3*sizeof(int)), "taste::sweet"); 00087 00088 // Check the facet indexes for the tags 00089 assert_eq(((int*)indexref(tagIndex, 0))[2], 0); 00090 assert_eq(((int*)indexref(tagIndex, 1))[2], 0); 00091 assert_eq(((int*)indexref(tagIndex, 2))[2], 1); 00092 assert_eq(((int*)indexref(tagIndex, 3))[2], 1); 00093 } 00094 00095 // Test parsing a vocabulary with a tag without a defined facet 00096 Test _2() 00097 { 00098 string voc = 00099 "Tag: foo::bar\n" 00100 "Description: Tag without facet\n" 00101 " VocabularyMerged should behave fine in this case.\n\n"; 00102 tagcoll::input::String in(voc); 00103 00104 ept::debtags::VocabularyMerger vm; 00105 vm.read(in); 00106 00107 // Write the merged vocabulary to /dev/null (but generate offsets and indexes in the meantime) 00108 vm.write("/dev/null"); 00109 00110 // Create the facet index 00111 char facetIndex[vm.facetIndexer().encodedSize()]; 00112 vm.facetIndexer().encode(facetIndex); 00113 00114 // Create the tag index 00115 char tagIndex[vm.tagIndexer().encodedSize()]; 00116 vm.tagIndexer().encode(tagIndex); 00117 } 00118 00119 // Test parsing a vocabulary with a facet without tags 00120 Test _3() 00121 { 00122 string voc = 00123 "Facet: empty\n" 00124 "Description: Facet without tags\n" 00125 " VocabularyMerged used to segfault in this case.\n\n"; 00126 tagcoll::input::String in(voc); 00127 00128 ept::debtags::VocabularyMerger vm; 00129 vm.read(in); 00130 00131 // Write the merged vocabulary to /dev/null (but generate offsets and indexes in the meantime) 00132 vm.write("/dev/null"); 00133 00134 // Create the facet index 00135 char facetIndex[vm.facetIndexer().encodedSize()]; 00136 vm.facetIndexer().encode(facetIndex); 00137 00138 // Create the tag index 00139 char tagIndex[vm.tagIndexer().encodedSize()]; 00140 vm.tagIndexer().encode(tagIndex); 00141 } 00142 00143 }; 00144 // vim:set ts=4 sw=4: