• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

debtags.test.h

Go to the documentation of this file.
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 #ifndef EPT_DEBTAGS_TESTH
00039 #define EPT_DEBTAGS_TESTH
00040 
00041 using namespace tagcoll;
00042 using namespace std;
00043 using namespace ept;
00044 using namespace ept::debtags;
00045 using namespace wibble::operators;
00046 
00047 struct TestDebtags : DebtagsTestEnvironment
00048 {
00049     Debtags debtags;
00050 
00051     TestDebtags() {}
00052 
00053     Vocabulary& voc() { return debtags.vocabulary(); }
00054 
00055     Test _1() {
00056         for (Debtags::const_iterator i = debtags.begin(); i != debtags.end(); ++i)
00057         {
00058             *i;
00059             i->first;
00060             i->second;
00061         }
00062         int items = 0, tags = 0;
00063         debtags.outputSystem(stream::countingSink(items, tags));
00064         
00065         int pitems = 0, ptags = 0;
00066     debtags.outputPatched(stream::countingSink(pitems, ptags));
00067 
00068     assert(items > 10);
00069     assert(tags > 10);
00070     assert(items <= pitems);
00071     assert(tags <= ptags);
00072 }
00073 
00074     Test _2()
00075 {
00076     string p("debtags");
00077     std::set<Tag> tags = debtags.getTagsOfItem(p);
00078     assert( !tags.empty() );
00079 
00080 #if 0
00081     for ( std::set< Tag >::iterator i = tags.begin(); i != tags.end(); ++ i ) {
00082         std::cerr << i->id() << ": " << i->fullname() << std::endl;
00083     }
00084     std::cerr << "---" << std::endl;
00085     Tag t = voc().tagByName( "interface::commandline" );
00086     std::cerr << t.id() << ": " << t.fullname() << std::endl;
00087 #endif
00088 
00089     assert_eq( tags.size(), 8u );
00090     assert( voc().tagByName( "devel::buildtools" ) <= tags );
00091     assert( voc().tagByName( "implemented-in::c++" ) <= tags );
00092     assert( voc().tagByName( "interface::commandline" ) <= tags );
00093     assert( voc().tagByName( "role::program" ) <= tags );
00094     assert( voc().tagByName( "scope::application" ) <= tags );
00095     assert( voc().tagByName( "suite::debian" ) <= tags );
00096     assert( voc().tagByName( "use::searching" ) <= tags );
00097     assert( voc().tagByName( "works-with::software:package" ) <= tags );
00098 }
00099 
00100     Test _3()
00101 {
00102     using namespace std;
00103 
00104     /* Get the 'debtags' package */
00105     string p("debtags");
00106 
00107     /* Get its tags */
00108     std::set<Tag> tags = debtags.getTagsOfItem(p);
00109     assert(!tags.empty());
00110 
00111     /*
00112     cerr << "Intersection size: " << endl;
00113     using namespace wibble::operators;
00114     std::set<Tag>::const_iterator dbgi = tags.begin();
00115     cerr << "* " << dbgi->fullname() << ": " << dbgi->id() << endl;
00116     std::set<int> dbgres = debtags.tagdb().getItemsHavingTag(dbgi->id());
00117     std::set<Package> dbgpres = debtags.getItemsHavingTag(*dbgi);
00118     cerr << " #pkgs " << dbgres.size() << " == " << dbgpres.size() << endl;
00119     cerr << " #isec " << dbgres.size() << " == " << dbgpres.size() << endl;
00120     cerr << "  "; ppset(dbgpres); cerr << endl;
00121     cerr << "  "; piset(dbgres); cerr << endl;
00122     for (++dbgi ; dbgi != tags.end(); ++dbgi)
00123     {
00124         cerr << "* " << dbgi->fullname() << ": " << dbgi->id() << endl;
00125         std::set<Package> dbgpkgs = debtags.getItemsHavingTag(*dbgi);
00126         std::set<int> dbgids = debtags.tagdb().getItemsHavingTag(dbgi->id());
00127         cerr << "  "; ppset(dbgpkgs); cerr << endl;
00128         cerr << "  "; piset(dbgids); cerr << endl;
00129         cerr << " #pkgs " << dbgpkgs.size() << " == " << dbgids.size() << endl;
00130         dbgres &= dbgids;
00131         dbgpres &= dbgpkgs;
00132         cerr << " #isec " << dbgres.size() << " == " << dbgpres.size() << endl;
00133     }
00134     cerr << " " << dbgres.size() << endl << "Results: " << endl;
00135     for (std::set<int>::const_iterator i = dbgres.begin(); i != dbgres.end(); ++i)
00136         cerr << "   " << *i << endl;
00137     */
00138 
00139 
00140 //  cerr << "Tags of debtags: ";
00141 //  for (std::set<Tag>::const_iterator i = tags.begin(); i != tags.end(); ++i)
00142 //  {
00143 //      cerr << " " + i->fullname() << endl;
00144 //      std::set<Package> packages = debtags.getItemsHavingTag(*i);
00145 //      for (std::set<Package>::const_iterator p = packages.begin();
00146 //              p != packages.end(); ++p)
00147 //          cerr << "   PKG " << p->name() << endl;
00148 //  }
00149 //  cerr << endl;
00150 
00151     /* Get the items for the tagset of 'debtags' */
00152     std::set<string> packages = debtags.getItemsHavingTags(tags);
00153     //cerr << packages.size() << endl;
00154     assert(!packages.empty());
00155     /*
00156     for ( std::set< Package >::iterator i = packages.begin(); i != packages.end(); ++ i )
00157         std::cerr << i->name() << std::endl;
00158     std::cerr << "---" << std::endl;
00159     std::cerr << p.name() << std::endl;
00160     */
00161     /* They should at least contain 'debtags' */
00162     assert( p <= packages );
00163 
00164     /* Get one of the tags of 'debtags' */
00165     Tag tag = *tags.begin();
00166     assert(tag);
00167 
00168     /* Get its items */
00169     {
00170         /* Need this workaround until I figure out how to tell the new GCC
00171          * that TagDB is a TDBReadonlyDiskIndex and should behave as such
00172          */
00173         std::set<Tag> ts;
00174         ts.insert(tag);
00175         packages = debtags.getItemsHavingTags(ts);
00176     }
00177     //packages = c.debtags().tagdb().getItems(tag);
00178     assert(!packages.empty());
00179     /* They should at least contain 'debtags' */
00180     assert( p <= packages );
00181 
00182     //c.debtags().getTags(""); // XXX HACK AWW!
00183 }
00184 
00185     Test _4()
00186 {
00187     std::string patchfile = Path::debtagsUserSourceDir() + "patch";
00188     unlink(patchfile.c_str());
00189 
00190     string p("debtags");
00191 
00192     /* Get its tags */
00193     std::set<Tag> tags = debtags.getTagsOfItem(p);
00194     assert(!tags.empty());
00195 
00196     // Ensure that it's not tagged with gameplaying
00197     Tag t = voc().tagByName("use::gameplaying");
00198     assert(! (t <= tags) );
00199 
00200     // Add the gameplaying tag
00201     PatchList<string, Tag> change;
00202     change.addPatch(Patch<string, Tag>(p, wibble::singleton(t), wibble::Empty<Tag>()));
00203     debtags.applyChange(change);
00204 
00205     // See that the patch is non empty
00206     PatchList<string, Tag> tmp = debtags.changes();
00207     assert(tmp.size() > 0);
00208     assert_eq(tmp.size(), 1u);
00209 
00210     // Ensure that the tag has been added
00211     tags = debtags.getTagsOfItem(p);
00212     assert(!tags.empty());
00213 
00214     t = voc().tagByName("use::gameplaying");
00215     assert( t <= tags );
00216 
00217     // Save the patch
00218     debtags.savePatch();
00219 
00220     // Check that the saved patch is correct
00221     FILE* in = fopen(patchfile.c_str(), "r");
00222     string writtenPatch;
00223     int c;
00224     while ((c = getc(in)) != EOF)
00225         writtenPatch += c;
00226     fclose(in);
00227 
00228     assert_eq(writtenPatch, string("debtags: +use::gameplaying\n"));
00229 
00230     unlink(patchfile.c_str());
00231 
00232     // Reapply the patch and see that it doesn't disrept things
00233     debtags.applyChange(change);
00234 
00235     // The patch should not have changed
00236     tmp = debtags.changes();
00237     assert_eq(tmp.size(), 1u);
00238     assert_eq(tmp.begin()->first, p);
00239     assert_eq(tmp.begin()->second.item, p);
00240 }
00241 
00242 // If there is no data, Debtags should work as an empty collection
00243     Test _5()
00244 {
00245     Path::OverrideDebtagsSourceDir odsd("./empty");
00246     Path::OverrideDebtagsIndexDir odid("./empty");
00247     Path::OverrideDebtagsUserSourceDir odusd("./empty");
00248     Path::OverrideDebtagsUserIndexDir oduid("./empty");
00249     Debtags empty;
00250 
00251     assert(empty.begin() == empty.end());
00252     assert_eq(empty.timestamp(), 0);
00253     assert(!empty.hasData());
00254 
00255     tagcoll::PatchList<std::string, Tag> patches = empty.changes();
00256     assert(patches.empty());
00257 
00258     set<Tag> res = empty.getTagsOfItem("apt");
00259     assert(res.empty());
00260     res = empty.getTagsOfItems(wibble::singleton(string("apt")));
00261     assert(res.empty());
00262 
00263     res = empty.getAllTags();
00264     assert(res.empty());
00265 
00266     tagcoll::coll::Simple<string, Tag> coll;
00267     empty.outputSystem(tagcoll::coll::inserter(coll));
00268     assert_eq(coll.itemCount(), 0u);
00269 
00270     coll.clear();
00271 
00272     empty.outputPatched(tagcoll::coll::inserter(coll));
00273     assert_eq(coll.itemCount(), 0u);
00274 }
00275 
00276 };
00277 
00278 #include <ept/debtags/debtags.tcc>
00279 #include <tagcoll/coll/simple.tcc>
00280 
00281 #endif
00282 
00283 // vim:set ts=4 sw=4:

Generated on Fri Oct 14 2011 17:41:02 for libept by  doxygen 1.7.1