libept
0.5.25
|
00001 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t -*- 00007 /* 00008 * Test for the Debtags data provider 00009 * 00010 * Copyright (C) 2003-2007 Enrico Zini <enrico@debian.org> 00011 * 00012 * This library is free software; you can redistribute it and/or 00013 * modify it under the terms of the GNU Lesser General Public 00014 * License as published by the Free Software Foundation; either 00015 * version 2.1 of the License, or (at your option) any later version. 00016 * 00017 * This library is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 * Lesser General Public License for more details. 00021 * 00022 * You should have received a copy of the GNU Lesser General Public 00023 * License along with this library; if not, write to the Free Software 00024 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00025 */ 00026 00027 00028 #include <ept/debtags/debtags.h> 00029 #include <ept/core/apt.h> 00030 00031 #include <tagcoll/coll/simple.h> 00032 #include <tagcoll/stream/sink.h> 00033 00034 #include <wibble/operators.h> 00035 00036 #include <ept/test.h> 00037 00038 #include <stdio.h> 00039 00040 #ifndef EPT_DEBTAGS_TESTH 00041 #define EPT_DEBTAGS_TESTH 00042 00043 using namespace tagcoll; 00044 using namespace std; 00045 using namespace ept; 00046 using namespace ept::debtags; 00047 using namespace wibble::operators; 00048 00049 struct TestDebtags : DebtagsTestEnvironment 00050 { 00051 Debtags debtags; 00052 00053 TestDebtags() {} 00054 00055 Vocabulary& voc() { return debtags.vocabulary(); } 00056 00057 Test _1() { 00058 for (Debtags::const_iterator i = debtags.begin(); i != debtags.end(); ++i) 00059 { 00060 *i; 00061 i->first; 00062 i->second; 00063 } 00064 int items = 0, tags = 0; 00065 debtags.outputSystem(stream::countingSink(items, tags)); 00066 00067 int pitems = 0, ptags = 0; 00068 debtags.outputPatched(stream::countingSink(pitems, ptags)); 00069 00070 assert(items > 10); 00071 assert(tags > 10); 00072 assert(items <= pitems); 00073 assert(tags <= ptags); 00074 } 00075 00076 Test _2() 00077 { 00078 string p("debtags"); 00079 std::set<Tag> tags = debtags.getTagsOfItem(p); 00080 assert( !tags.empty() ); 00081 00082 #if 0 00083 for ( std::set< Tag >::iterator i = tags.begin(); i != tags.end(); ++ i ) { 00084 std::cerr << i->id() << ": " << i->fullname() << std::endl; 00085 } 00086 std::cerr << "---" << std::endl; 00087 Tag t = voc().tagByName( "interface::commandline" ); 00088 std::cerr << t.id() << ": " << t.fullname() << std::endl; 00089 #endif 00090 00091 assert_eq( tags.size(), 8u ); 00092 assert( voc().tagByName( "devel::buildtools" ) <= tags ); 00093 assert( voc().tagByName( "implemented-in::c++" ) <= tags ); 00094 assert( voc().tagByName( "interface::commandline" ) <= tags ); 00095 assert( voc().tagByName( "role::program" ) <= tags ); 00096 assert( voc().tagByName( "scope::application" ) <= tags ); 00097 assert( voc().tagByName( "suite::debian" ) <= tags ); 00098 assert( voc().tagByName( "use::searching" ) <= tags ); 00099 assert( voc().tagByName( "works-with::software:package" ) <= tags ); 00100 } 00101 00102 Test _3() 00103 { 00104 using namespace std; 00105 00106 /* Get the 'debtags' package */ 00107 string p("debtags"); 00108 00109 /* Get its tags */ 00110 std::set<Tag> tags = debtags.getTagsOfItem(p); 00111 assert(!tags.empty()); 00112 00113 /* 00114 cerr << "Intersection size: " << endl; 00115 using namespace wibble::operators; 00116 std::set<Tag>::const_iterator dbgi = tags.begin(); 00117 cerr << "* " << dbgi->fullname() << ": " << dbgi->id() << endl; 00118 std::set<int> dbgres = debtags.tagdb().getItemsHavingTag(dbgi->id()); 00119 std::set<Package> dbgpres = debtags.getItemsHavingTag(*dbgi); 00120 cerr << " #pkgs " << dbgres.size() << " == " << dbgpres.size() << endl; 00121 cerr << " #isec " << dbgres.size() << " == " << dbgpres.size() << endl; 00122 cerr << " "; ppset(dbgpres); cerr << endl; 00123 cerr << " "; piset(dbgres); cerr << endl; 00124 for (++dbgi ; dbgi != tags.end(); ++dbgi) 00125 { 00126 cerr << "* " << dbgi->fullname() << ": " << dbgi->id() << endl; 00127 std::set<Package> dbgpkgs = debtags.getItemsHavingTag(*dbgi); 00128 std::set<int> dbgids = debtags.tagdb().getItemsHavingTag(dbgi->id()); 00129 cerr << " "; ppset(dbgpkgs); cerr << endl; 00130 cerr << " "; piset(dbgids); cerr << endl; 00131 cerr << " #pkgs " << dbgpkgs.size() << " == " << dbgids.size() << endl; 00132 dbgres &= dbgids; 00133 dbgpres &= dbgpkgs; 00134 cerr << " #isec " << dbgres.size() << " == " << dbgpres.size() << endl; 00135 } 00136 cerr << " " << dbgres.size() << endl << "Results: " << endl; 00137 for (std::set<int>::const_iterator i = dbgres.begin(); i != dbgres.end(); ++i) 00138 cerr << " " << *i << endl; 00139 */ 00140 00141 00142 // cerr << "Tags of debtags: "; 00143 // for (std::set<Tag>::const_iterator i = tags.begin(); i != tags.end(); ++i) 00144 // { 00145 // cerr << " " + i->fullname() << endl; 00146 // std::set<Package> packages = debtags.getItemsHavingTag(*i); 00147 // for (std::set<Package>::const_iterator p = packages.begin(); 00148 // p != packages.end(); ++p) 00149 // cerr << " PKG " << p->name() << endl; 00150 // } 00151 // cerr << endl; 00152 00153 /* Get the items for the tagset of 'debtags' */ 00154 std::set<string> packages = debtags.getItemsHavingTags(tags); 00155 //cerr << packages.size() << endl; 00156 assert(!packages.empty()); 00157 /* 00158 for ( std::set< Package >::iterator i = packages.begin(); i != packages.end(); ++ i ) 00159 std::cerr << i->name() << std::endl; 00160 std::cerr << "---" << std::endl; 00161 std::cerr << p.name() << std::endl; 00162 */ 00163 /* They should at least contain 'debtags' */ 00164 assert( p <= packages ); 00165 00166 /* Get one of the tags of 'debtags' */ 00167 Tag tag = *tags.begin(); 00168 assert(tag); 00169 00170 /* Get its items */ 00171 { 00172 /* Need this workaround until I figure out how to tell the new GCC 00173 * that TagDB is a TDBReadonlyDiskIndex and should behave as such 00174 */ 00175 std::set<Tag> ts; 00176 ts.insert(tag); 00177 packages = debtags.getItemsHavingTags(ts); 00178 } 00179 //packages = c.debtags().tagdb().getItems(tag); 00180 assert(!packages.empty()); 00181 /* They should at least contain 'debtags' */ 00182 assert( p <= packages ); 00183 00184 //c.debtags().getTags(""); // XXX HACK AWW! 00185 } 00186 00187 Test _4() 00188 { 00189 std::string patchfile = Path::debtagsUserSourceDir() + "patch"; 00190 unlink(patchfile.c_str()); 00191 00192 string p("debtags"); 00193 00194 /* Get its tags */ 00195 std::set<Tag> tags = debtags.getTagsOfItem(p); 00196 assert(!tags.empty()); 00197 00198 // Ensure that it's not tagged with gameplaying 00199 Tag t = voc().tagByName("use::gameplaying"); 00200 assert(! (t <= tags) ); 00201 00202 // Add the gameplaying tag 00203 PatchList<string, Tag> change; 00204 change.addPatch(Patch<string, Tag>(p, wibble::singleton(t), wibble::Empty<Tag>())); 00205 debtags.applyChange(change); 00206 00207 // See that the patch is non empty 00208 PatchList<string, Tag> tmp = debtags.changes(); 00209 assert(tmp.size() > 0); 00210 assert_eq(tmp.size(), 1u); 00211 00212 // Ensure that the tag has been added 00213 tags = debtags.getTagsOfItem(p); 00214 assert(!tags.empty()); 00215 00216 t = voc().tagByName("use::gameplaying"); 00217 assert( t <= tags ); 00218 00219 // Save the patch 00220 debtags.savePatch(); 00221 00222 // Check that the saved patch is correct 00223 FILE* in = fopen(patchfile.c_str(), "r"); 00224 string writtenPatch; 00225 int c; 00226 while ((c = getc(in)) != EOF) 00227 writtenPatch += c; 00228 fclose(in); 00229 00230 assert_eq(writtenPatch, string("debtags: +use::gameplaying\n")); 00231 00232 unlink(patchfile.c_str()); 00233 00234 // Reapply the patch and see that it doesn't disrept things 00235 debtags.applyChange(change); 00236 00237 // The patch should not have changed 00238 tmp = debtags.changes(); 00239 assert_eq(tmp.size(), 1u); 00240 assert_eq(tmp.begin()->first, p); 00241 assert_eq(tmp.begin()->second.item, p); 00242 } 00243 00244 // If there is no data, Debtags should work as an empty collection 00245 Test _5() 00246 { 00247 Path::OverrideDebtagsSourceDir odsd("./empty"); 00248 Path::OverrideDebtagsIndexDir odid("./empty"); 00249 Path::OverrideDebtagsUserSourceDir odusd("./empty"); 00250 Path::OverrideDebtagsUserIndexDir oduid("./empty"); 00251 Debtags empty; 00252 00253 assert(empty.begin() == empty.end()); 00254 assert_eq(empty.timestamp(), 0); 00255 assert(!empty.hasData()); 00256 00257 tagcoll::PatchList<std::string, Tag> patches = empty.changes(); 00258 assert(patches.empty()); 00259 00260 set<Tag> res = empty.getTagsOfItem("apt"); 00261 assert(res.empty()); 00262 res = empty.getTagsOfItems(wibble::singleton(string("apt"))); 00263 assert(res.empty()); 00264 00265 res = empty.getAllTags(); 00266 assert(res.empty()); 00267 00268 tagcoll::coll::Simple<string, Tag> coll; 00269 empty.outputSystem(tagcoll::coll::inserter(coll)); 00270 assert_eq(coll.itemCount(), 0u); 00271 00272 coll.clear(); 00273 00274 empty.outputPatched(tagcoll::coll::inserter(coll)); 00275 assert_eq(coll.itemCount(), 0u); 00276 } 00277 00278 }; 00279 00280 #include <ept/debtags/debtags.tcc> 00281 #include <tagcoll/coll/simple.tcc> 00282 00283 #endif 00284 00285 // vim:set ts=4 sw=4: