1 : /*
2 : * Merge different vocabularies together and create the tag and facet indexes
3 : *
4 : * Copyright (C) 2003-2007 Enrico Zini <enrico@debian.org>
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 2 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program; if not, write to the Free Software
18 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 : */
20 :
21 : #include <wibble/test.h>
22 : #include <ept/debtags/maint/vocabularymerger.h>
23 : #include <tagcoll/input/string.h>
24 :
25 : using namespace std;
26 : using namespace tagcoll;
27 :
28 : struct TestVocabularyMerger {
29 :
30 14 : inline static const char* indexref(const char* index, int id)
31 : {
32 14 : return index + ((int*)index)[id];
33 : }
34 :
35 :
36 1 : Test _1()
37 : {
38 : string voc1 =
39 : "Facet: taste\n"
40 : "Description: Taste\n\n"
41 : "Tag: taste::sweet\n"
42 : "Description: Sweet\n\n"
43 : "Tag: taste::salty\n"
44 1 : "Description: Salty\n\n";
45 : string voc2 =
46 : "Facet: smell\n"
47 : "Description: Smell\n\n"
48 : "Tag: smell::fresh\n"
49 : "Description: Fresh\n\n"
50 : "Tag: smell::mold\n"
51 2 : "Description: Mold\n\n";
52 1 : tagcoll::input::String in1(voc1);
53 1 : tagcoll::input::String in2(voc2);
54 :
55 1 : ept::debtags::VocabularyMerger vm;
56 :
57 : // Read and merge the two vocabulary samples
58 1 : vm.read(in1);
59 1 : vm.read(in2);
60 :
61 : // Write the merged vocabulary to /dev/null (but generate offsets and indexes in the meantime)
62 1 : vm.write("/dev/null");
63 :
64 : // Create the facet index
65 2 : char facetIndex[vm.facetIndexer().encodedSize()];
66 1 : vm.facetIndexer().encode(facetIndex);
67 :
68 : // Create the tag index
69 1 : char tagIndex[vm.tagIndexer().encodedSize()];
70 1 : vm.tagIndexer().encode(tagIndex);
71 :
72 : // Check that the facet names have been encoded correctly and in order
73 1 : assert_eq(string(indexref(facetIndex, 0) + 4*sizeof(int)), "smell");
74 2 : assert_eq(string(indexref(facetIndex, 1) + 4*sizeof(int)), "taste");
75 :
76 : // Check the first and last tag indexes for the facets
77 2 : assert_eq(((int*)indexref(facetIndex, 0))[2], 0);
78 2 : assert_eq(((int*)indexref(facetIndex, 0))[3], 1);
79 2 : assert_eq(((int*)indexref(facetIndex, 1))[2], 2);
80 2 : assert_eq(((int*)indexref(facetIndex, 1))[3], 3);
81 :
82 : // Check that the tag names have been encoded correctly and in order
83 2 : assert_eq(string(indexref(tagIndex, 0) + 3*sizeof(int)), "smell::fresh");
84 2 : assert_eq(string(indexref(tagIndex, 1) + 3*sizeof(int)), "smell::mold");
85 2 : assert_eq(string(indexref(tagIndex, 2) + 3*sizeof(int)), "taste::salty");
86 2 : assert_eq(string(indexref(tagIndex, 3) + 3*sizeof(int)), "taste::sweet");
87 :
88 : // Check the facet indexes for the tags
89 2 : assert_eq(((int*)indexref(tagIndex, 0))[2], 0);
90 2 : assert_eq(((int*)indexref(tagIndex, 1))[2], 0);
91 2 : assert_eq(((int*)indexref(tagIndex, 2))[2], 1);
92 2 : assert_eq(((int*)indexref(tagIndex, 3))[2], 1);
93 1 : }
94 :
95 : // Test parsing a vocabulary with a tag without a defined facet
96 1 : Test _2()
97 : {
98 : string voc =
99 : "Tag: foo::bar\n"
100 : "Description: Tag without facet\n"
101 1 : " VocabularyMerged should behave fine in this case.\n\n";
102 1 : tagcoll::input::String in(voc);
103 :
104 1 : ept::debtags::VocabularyMerger vm;
105 1 : vm.read(in);
106 :
107 : // Write the merged vocabulary to /dev/null (but generate offsets and indexes in the meantime)
108 1 : vm.write("/dev/null");
109 :
110 : // Create the facet index
111 2 : char facetIndex[vm.facetIndexer().encodedSize()];
112 1 : vm.facetIndexer().encode(facetIndex);
113 :
114 : // Create the tag index
115 1 : char tagIndex[vm.tagIndexer().encodedSize()];
116 1 : vm.tagIndexer().encode(tagIndex);
117 1 : }
118 :
119 : // Test parsing a vocabulary with a facet without tags
120 1 : Test _3()
121 : {
122 : string voc =
123 : "Facet: empty\n"
124 : "Description: Facet without tags\n"
125 1 : " VocabularyMerged used to segfault in this case.\n\n";
126 1 : tagcoll::input::String in(voc);
127 :
128 1 : ept::debtags::VocabularyMerger vm;
129 1 : vm.read(in);
130 :
131 : // Write the merged vocabulary to /dev/null (but generate offsets and indexes in the meantime)
132 1 : vm.write("/dev/null");
133 :
134 : // Create the facet index
135 2 : char facetIndex[vm.facetIndexer().encodedSize()];
136 1 : vm.facetIndexer().encode(facetIndex);
137 :
138 : // Create the tag index
139 1 : char tagIndex[vm.tagIndexer().encodedSize()];
140 1 : vm.tagIndexer().encode(tagIndex);
141 1 : }
142 :
143 : };
144 : // vim:set ts=4 sw=4:
|