File: test-utils.h

package info (click to toggle)
tagcoll 1.6.3-2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 2,500 kB
  • ctags: 2,062
  • sloc: cpp: 10,930; sh: 8,892; makefile: 201; lex: 54; yacc: 27
file content (191 lines) | stat: -rw-r--r-- 5,546 bytes parent folder | download | duplicates (2)
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
/**
 * @file test-utils.h
 * @author Peter Rockai (mornfall) <mornfall@danill.sk>
 * @brief Utility functions for the unit tests
 */
#include <tut.h>

#include <string>
#include <tagcoll/stringf.h>

#define TEST_TAGCOLL

#ifdef TEST_TAGCOLL
#include <tagcoll/Collection.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>
*/

#define TESTGRP(name) \
typedef test_group<name ## _shar> tg; \
typedef tg::object to; \
tg name ## _tg (#name);


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) {}
};

class Location
{
	string file;
	int line;
	string str;
	string testfile;
	int testline;
	string teststr;
public:
	Location(const std::string& file, int line, const std::string& str)
		: file(file), line(line), str(str) {}
	Location(const Location& loc,
		 const std::string& testfile, int testline, const std::string& str) :
		file(loc.file), line(loc.line), str(loc.str),
		testfile(testfile), testline(testline), teststr(str) {}
	string locstr() const
	{
		std::stringstream ss;
		ss << file << ":" << line << "(" << str << ")";
		if (!testfile.empty())
			ss << "[" << testfile << ":" << testline << "(" << teststr << ")]";
		ss << ": ";
		return ss.str();
	}
	string msg(const std::string m) const
	{
		return locstr() + ": " + m;
	}
};

void outputCollection(const std::string& str, Tagcoll::Consumer<string, string>& cons);

#define gen_ensure(x) _gen_ensure(Location(__FILE__, __LINE__, #x), (x))
#define inner_ensure(x) _gen_ensure(Location(loc, __FILE__, __LINE__, #x), (x))
void _gen_ensure(const Location& loc, bool res);

#define gen_ensure_equals(x, y) my_ensure_equals(Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
#define inner_ensure_equals(x, y) my_ensure_equals(Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
template <class T,class Q>
void my_ensure_equals(const Location& loc, const Q& actual, const T& expected)
{
	if( expected != actual )
	{
		std::stringstream ss;
		ss << "expected " << expected << " actual " << actual;
		throw failure(loc.msg(ss.str()));
	}
}

#define gen_ensure_coll_equals(a, b) \
	__tc_ensure_coll_equals(Location(__FILE__, __LINE__, #a " == " #b), a, b)
#define inner_ensure_coll_equals(a, b) \
	__tc_ensure_coll_equals(Location(loc, __FILE__, __LINE__, #a " == " #b), a, b)
void __tc_ensure_coll_equals(const Location& loc,
		const Tagcoll::ReadonlyCollection<string, string>& c1,
		const Tagcoll::ReadonlyCollection<string, string>& c2);

#define gen_ensure_contains(a, b) \
	_ensure_contains(Location(__FILE__, __LINE__, #a " contains " #b), a, b)
#define inner_ensure_contains(a, b) \
	_ensure_contains(Location(loc, __FILE__, __LINE__, #a " contains " #b), a, b)
template<class T>
void _ensure_contains(const Location& loc, const OpSet<T>& s, const T& item)
{
	if (!s.contains(item))
	{
		std::stringstream ss;
		ss << "tagset ";
		for (typename OpSet<T>::const_iterator i = s.begin();
				i != s.end(); i++)
			if (i == s.begin())
				ss << *i;
			else
				ss << ", " << *i;
		ss << " does not contain " << item;
		
		throw failure(loc.msg(ss.str()));
	}
}

#define gen_ensure_not_contains(a, b) \
	_ensure_not_contains(Location(__FILE__, __LINE__, #a " contains " #b), a, b)
#define inner_ensure_not_contains(a, b) \
	_ensure_not_contains(Location(loc, __FILE__, __LINE__, #a " contains " #b), a, b)
template<class T>
void _ensure_not_contains(const Location& loc, const OpSet<T>& s, const T& item)
{
	if (s.contains(item))
	{
		std::stringstream ss;
		ss << "tagset ";
		for (typename OpSet<T>::const_iterator i = s.begin();
				i != s.end(); i++)
			if (i == s.begin())
				ss << *i;
			else
				ss << ", " << *i;
		ss << " does not contain " << item;
		
		throw failure(loc.msg(ss.str()));
	}
}

#ifdef TEST_TAGCOLL

// Output a test collection for later testing with test_tagged_collection_ro

#define output_test_collection(x) (__output_test_collection(Location(__FILE__, __LINE__, #x), (x)))
#define inner_output_test_collection(x) (__output_test_collection(Location(loc, __FILE__, __LINE__, $x), (x)))
void __output_test_collection(const Location& loc, Consumer<string, string>& tc);

#define test_readonly_collection(x) (__test_readonly_collection(Location(__FILE__, __LINE__, #x), (x)))
#define inner_test_readonly_collection(x) (__test_readonly_collection(Location(loc, __FILE__, __LINE__, #x), (x)))
void __test_readonly_collection(const Location& loc, ReadonlyCollection<string, string>& tc);


#define test_collection(x) (__test_collection(Location(__FILE__, __LINE__, #x), (x)))
void __test_collection(const Location& loc, Collection<string, string>& tc);

#endif
}

/*
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);
    }
}
*/