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

Generated on Wed Oct 27 2010 12:43:31 for libept by  doxygen 1.7.1