1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/**
* @file test-utils.h
* @author Peter Rockai (mornfall) <mornfall@danill.sk>
* @brief Utility functions for the unit tests
*/
#include <tut.h>
#define TESTGRP(name) \
typedef test_group<name ## _shar> tg; \
typedef tg::object to; \
tg name ## _tg (#name);
namespace tut_guessnet {
inline static std::string __ensure_errmsg(std::string f, int l, std::string msg)
{
char buf[64];
snprintf(buf, 63, "%d", l);
buf[63] = 0;
std::string ln = buf;
f.append(":");
f.append(ln);
f.append(": '");
f.append(msg);
f.append("'");
return f;
}
#define gen_ensure(x) ensure (__ensure_errmsg(__FILE__, __LINE__, #x).c_str(), (x))
}
#if 0
#include <string>
#include <tagcoll/stringf.h>
#define TEST_TAGCOLL
#ifdef TEST_TAGCOLL
#include <tagcoll/Collection.h>
#include <tagcoll/Patches.h>
#endif
/*
#include <apt-front/cache.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/pkgsystem.h>
#include <apt-pkg/init.h>
#include <iostream>
*/
namespace tut_tagcoll {
using namespace std;
using namespace stringf;
using namespace Tagcoll;
using namespace tut;
template<class ITEM, class TAG>
class TestConsumer : public Tagcoll::Consumer<ITEM, TAG>
{
protected:
virtual void consumeItemUntagged(const ITEM& item) { items++; }
virtual void consumeItem(const ITEM& item, const OpSet<TAG>& tags)
{
items++;
this->tags += tags.size();
}
public:
int items;
int tags;
TestConsumer() : items(0), tags(0) {}
};
void outputCollection(const std::string& str, Tagcoll::Consumer<string, string>& cons);
void __tc_ensure_coll_equal(std::string f, int l, std::string s,
const Tagcoll::Collection<string, string>& c1,
const Tagcoll::Collection<string, string>& c2);
#define ensure_coll_equal(a, b) \
__tc_ensure_coll_equal(__FILE__, __LINE__, #a " == " #b, a, b)
inline static std::string __tc_ensure_errmsg(std::string f, int l, std::string f1, int l1, std::string msg)
{
return f + ":" + fmt(l) + ": '" + f1 + ":" + fmt(l1) + ": " + msg + "'";
}
#ifdef TEST_TAGCOLL
inline static void __test_tagged_collection(std::string f, int l, Collection<string, string>& tc)
{
#define ttc_ensure(x) ensure (__tc_ensure_errmsg(f, l, __FILE__, __LINE__, #x).c_str(), (x))
// Test handling of untagged items (they are not stored)
tc.consume("untagged");
ttc_ensure(tc.getTags("untagged").empty());
// Test handling of tagged items
OpSet<string> tagset;
tagset += "tag1"; tagset += "tag2";
tc.consume("tagged", tagset);
ttc_ensure(tc.getTaggedItems().contains("tagged"));
//ttc_ensure(tc.hasTag("tag1"));
//ttc_ensure(tc.hasTag("tag2"));
tagset = tc.getTags("tagged");
ttc_ensure(tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
OpSet<string> itemset = tc.getItems("tag1");
ttc_ensure(itemset.contains("tagged"));
itemset = tc.getItems("tag2");
ttc_ensure(itemset.contains("tagged"));
tagset = tc.getAllTags();
ttc_ensure(tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
tagset.clear();
tagset += "tag1";
tagset = tc.getCompanionTags(tagset);
ttc_ensure(!tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
// Test handling of changes
PatchList<string, string> change;
Patch<string, string> p("tagged");
tagset.clear();
p.remove("tag1");
p.remove("tag2");
change.addPatch(p);
tc.applyChange(change);
// "tagged" should now be untagged
ttc_ensure(tc.getTags("tagged").empty());
tc.applyChange(change.getReverse());
// "tagged" should now be as before
//ttc_ensure(tc.hasTag("tag1"));
//ttc_ensure(tc.hasTag("tag2"));
tagset = tc.getTags("tagged");
ttc_ensure(tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
itemset = tc.getItems("tag1");
ttc_ensure(itemset.contains("tagged"));
itemset = tc.getItems("tag2");
ttc_ensure(itemset.contains("tagged"));
tagset = tc.getAllTags();
ttc_ensure(tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
tagset.clear();
tagset += "tag1";
tagset = tc.getCompanionTags(tagset);
ttc_ensure(!tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
// Try a patch that adds a tag
change = PatchList<string, string>();
p = Patch<string, string>("tagged");
p.add("tag3");
change.addPatch(p);
tc.applyChange(change);
//ttc_ensure(tc.hasTag("tag1"));
//ttc_ensure(tc.hasTag("tag2"));
//ttc_ensure(tc.hasTag("tag3"));
tagset = tc.getTags("tagged");
ttc_ensure(tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
ttc_ensure(tagset.contains("tag3"));
itemset = tc.getItems("tag1");
ttc_ensure(itemset.contains("tagged"));
itemset = tc.getItems("tag2");
ttc_ensure(itemset.contains("tagged"));
itemset = tc.getItems("tag3");
ttc_ensure(itemset.contains("tagged"));
tagset = tc.getAllTags();
ttc_ensure(tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
ttc_ensure(tagset.contains("tag3"));
tagset.clear();
tagset += "tag1";
tagset = tc.getCompanionTags(tagset);
ttc_ensure(!tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
ttc_ensure(tagset.contains("tag3"));
// Try a patch that adds some items
change = PatchList<string, string>();
p = Patch<string, string>("tagged1");
p.add("tag1");
p.add("tag2");
p.add("tag4");
change.addPatch(p);
tc.applyChange(change);
tagset = tc.getTags("tagged1");
ttc_ensure(tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
ttc_ensure(!tagset.contains("tag3"));
ttc_ensure(tagset.contains("tag4"));
itemset = tc.getItems("tag1");
ttc_ensure(itemset.contains("tagged1"));
itemset = tc.getItems("tag2");
ttc_ensure(itemset.contains("tagged1"));
itemset = tc.getItems("tag3");
ttc_ensure(!itemset.contains("tagged1"));
itemset = tc.getItems("tag4");
ttc_ensure(!itemset.contains("tagged"));
ttc_ensure(itemset.contains("tagged1"));
tagset = tc.getAllTags();
ttc_ensure(tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
ttc_ensure(tagset.contains("tag3"));
ttc_ensure(tagset.contains("tag4"));
tagset.clear();
tagset += "tag1";
tagset = tc.getCompanionTags(tagset);
ttc_ensure(!tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
ttc_ensure(tagset.contains("tag3"));
ttc_ensure(tagset.contains("tag4"));
// And reverse it
tc.applyChange(change.getReverse());
itemset = tc.getItems("tag1");
ttc_ensure(!itemset.contains("tagged1"));
itemset = tc.getItems("tag2");
ttc_ensure(!itemset.contains("tagged1"));
itemset = tc.getItems("tag3");
ttc_ensure(!itemset.contains("tagged1"));
ttc_ensure(tc.getItems("tag4") == OpSet<string>());
tagset = tc.getAllTags();
ttc_ensure(tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
ttc_ensure(tagset.contains("tag3"));
ttc_ensure(!tagset.contains("tag4"));
tagset.clear();
tagset += "tag1";
tagset = tc.getCompanionTags(tagset);
ttc_ensure(!tagset.contains("tag1"));
ttc_ensure(tagset.contains("tag2"));
ttc_ensure(tagset.contains("tag3"));
ttc_ensure(!tagset.contains("tag4"));
#undef ttc_ensure
}
#endif
}
#define test_tagged_collection(x) (__test_tagged_collection(__FILE__, __LINE__, (x)))
/*
namespace tut {
static void aptInit () {
pkgInitConfig (*_config);
_config->Set("Dir", CACHE_DIR);
_config->Set("Dir::Cache", "cache");
_config->Set("Dir::State", "state");
_config->Set("Dir::Etc", "etc");
_config->Set("Dir::State::status", CACHE_DIR "dpkg-status");
pkgInitSystem (*_config, _system);
// _config -> Set ("Capture::Cache::UseExtState", extstate);
}
}
*/
#endif
|