libept  0.5.25
vocabularymerger.test.h
Go to the documentation of this file.
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>
23 #include <tagcoll/input/string.h>
24 
25 using namespace std;
26 using namespace tagcoll;
27 
29 
30  inline static const char* indexref(const char* index, int id)
31  {
32  return index + ((int*)index)[id];
33  }
34 
35 
36  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  "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  "Description: Mold\n\n";
52  tagcoll::input::String in1(voc1);
53  tagcoll::input::String in2(voc2);
54 
56 
57  // Read and merge the two vocabulary samples
58  vm.read(in1);
59  vm.read(in2);
60 
61  // Write the merged vocabulary to /dev/null (but generate offsets and indexes in the meantime)
62  vm.write("/dev/null");
63 
64  // Create the facet index
65  char facetIndex[vm.facetIndexer().encodedSize()];
66  vm.facetIndexer().encode(facetIndex);
67 
68  // Create the tag index
69  char tagIndex[vm.tagIndexer().encodedSize()];
70  vm.tagIndexer().encode(tagIndex);
71 
72  // Check that the facet names have been encoded correctly and in order
73  assert_eq(string(indexref(facetIndex, 0) + 4*sizeof(int)), "smell");
74  assert_eq(string(indexref(facetIndex, 1) + 4*sizeof(int)), "taste");
75 
76  // Check the first and last tag indexes for the facets
77  assert_eq(((int*)indexref(facetIndex, 0))[2], 0);
78  assert_eq(((int*)indexref(facetIndex, 0))[3], 1);
79  assert_eq(((int*)indexref(facetIndex, 1))[2], 2);
80  assert_eq(((int*)indexref(facetIndex, 1))[3], 3);
81 
82  // Check that the tag names have been encoded correctly and in order
83  assert_eq(string(indexref(tagIndex, 0) + 3*sizeof(int)), "smell::fresh");
84  assert_eq(string(indexref(tagIndex, 1) + 3*sizeof(int)), "smell::mold");
85  assert_eq(string(indexref(tagIndex, 2) + 3*sizeof(int)), "taste::salty");
86  assert_eq(string(indexref(tagIndex, 3) + 3*sizeof(int)), "taste::sweet");
87 
88  // Check the facet indexes for the tags
89  assert_eq(((int*)indexref(tagIndex, 0))[2], 0);
90  assert_eq(((int*)indexref(tagIndex, 1))[2], 0);
91  assert_eq(((int*)indexref(tagIndex, 2))[2], 1);
92  assert_eq(((int*)indexref(tagIndex, 3))[2], 1);
93 }
94 
95 // Test parsing a vocabulary with a tag without a defined facet
96  Test _2()
97 {
98  string voc =
99  "Tag: foo::bar\n"
100  "Description: Tag without facet\n"
101  " VocabularyMerged should behave fine in this case.\n\n";
102  tagcoll::input::String in(voc);
103 
105  vm.read(in);
106 
107  // Write the merged vocabulary to /dev/null (but generate offsets and indexes in the meantime)
108  vm.write("/dev/null");
109 
110  // Create the facet index
111  char facetIndex[vm.facetIndexer().encodedSize()];
112  vm.facetIndexer().encode(facetIndex);
113 
114  // Create the tag index
115  char tagIndex[vm.tagIndexer().encodedSize()];
116  vm.tagIndexer().encode(tagIndex);
117 }
118 
119 // Test parsing a vocabulary with a facet without tags
120  Test _3()
121 {
122  string voc =
123  "Facet: empty\n"
124  "Description: Facet without tags\n"
125  " VocabularyMerged used to segfault in this case.\n\n";
126  tagcoll::input::String in(voc);
127 
129  vm.read(in);
130 
131  // Write the merged vocabulary to /dev/null (but generate offsets and indexes in the meantime)
132  vm.write("/dev/null");
133 
134  // Create the facet index
135  char facetIndex[vm.facetIndexer().encodedSize()];
136  vm.facetIndexer().encode(facetIndex);
137 
138  // Create the tag index
139  char tagIndex[vm.tagIndexer().encodedSize()];
140  vm.tagIndexer().encode(tagIndex);
141 }
142 
143 };
144 // vim:set ts=4 sw=4: