summaryrefslogtreecommitdiffstats
path: root/kpilot/tests/testdatabase.cc
blob: 02b43c6beb39fd2f13b779564ac806a1ab3015f2 (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* testdatabase			KPilot
**
** Copyright (C) 2005 by Adriaan de Groot <groot@kde.org)
**
** Test the functions related to local databases.
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to kde-pim@kde.org
*/

#include "options.h"

#include <kaboutdata.h>
#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <kcmdlineargs.h>

#include "pilotLocalDatabase.h"
#include "pilotRecord.h"
#include "pilotMemo.h"


/* Return values for the various check* functions. They
   return OK if all is OK; ERR is for generic errors.
   ERR_NO_EXIST is returned if something (usually a database
   or other file) doesn't exist that should. The latter
   error might be ignored.
*/
#define OK           (0)
#define ERR          (1)
#define ERR_NO_EXIST (2)


/* Data about the records in a database. The id field is
   interpreted specially for the first and last entries.
*/
typedef struct { int id,size; } recordInfo;

/* Use END in the last recordInfo struct describing a database
   to indicate you expect the database to end there. Use NO_EXIST
   as the ID in the first struct to indicate that the database
   is expected _not_ to exist.
*/
#define NO_EXIST   (-2)
#define END        (-1)

/* These tables of data are taken from various databases I have
   (but which I cannot commit to SVN due to license issues).
   The aesop listing is from an eBook of Aesop's fables.
   The way to create these tables is to use a third-party
   tool such as par to read the database:

   ./par l /tmp/Aesop.pdb | awk '{print "{",$3,",",$4,"},";}'

*/
recordInfo nonexistent[] = {
	{ NO_EXIST, 0 }
} ;

recordInfo aesop[] = {
{ 7307264 , 214 },
{ 7307265 , 1564 },
{ 7307266 , 1575 },
{ 7307267 , 2214 },
{ 7307268 , 2276 },
{ 7307269 , 2148 },
{ 7307270 , 2194 },
{ 7307271 , 2178 },
{ 7307272 , 2220 },
{ 7307273 , 2216 },
{ 7307274 , 2181 },
{ 7307275 , 2183 },
{ 7307276 , 2197 },
{ 7307277 , 2010 },
{ 7307278 , 2198 },
{ 7307279 , 2196 },
{ 7307280 , 2243 },
{ 7307281 , 2211 },
{ 7307282 , 2274 },
{ 7307283 , 364 },
{ 7307284 , 49124 },
	{ END, 0 }
} ;

int checkDatabase(const char *path, recordInfo *info)
{
	FUNCTIONSETUP;

	PilotLocalDatabase db(TQString::fromLatin1(path));
	if (!db.isOpen())
	{
		kdDebug() << "No database " << path << endl;
		if ( info[0].id == NO_EXIST )
		{
			kdDebug() << "This was expected" << endl;
			return OK;
		}
		else
		{
			return ERR_NO_EXIST;
		}
	}

	if ( info[0].id == NO_EXIST )
	{
		kdDebug() << "Database not expected" << endl;
		return ERR;
	}

	int fail = 0;
	int index = 0;
	PilotRecord *r;
	while( (r = db.readRecordByIndex(index) ) )
	{
		kdDebug() << "[" << index << "] id=" << r->id() << " size=" << r->size() << endl;
		if ( ((recordid_t)info[index].id) != r->id() )
		{
			kdDebug() << "* Bad ID (expected" << r->id() << ")" << endl;
			fail++;
		}
		else if ( info[index].size != r->size() )
		{
			kdDebug() << "* Bad size (expected " << info[index].size << ")" << endl;
			fail++;
		}
		index++;
	}
	if ( info[index].id != END )
	{
		kdDebug() << "* End wasn't expected yet." << endl;
		r++;
	}

	if (fail)
	{
		kdDebug() << "* " << fail << " failures." << endl;
		return ERR;
	}
	return OK;
}

const char *categoryNames[4] =
{
	"aardvarks",
	"toolongToBeaCategoryName",
	"personal",
	"impersonal"
} ;

TQStringList listCategories()
{
	TQStringList cats;
	PilotLocalDatabase *l = new PilotLocalDatabase("./data/MemoDB");
	PilotMemoInfo *m = new PilotMemoInfo(l);

	if (!l->isOpen()) return cats;

	cats.append(CSL1("Unfiled"));
	m->dump();

	for (int i=0; i<20; i++)
	{
		PilotRecord *r = l->readRecordByIndex(i);
		kdDebug() << "Read record " << (void *)r << " with id=" << r->id() << endl;
		if (!r) break;
	}

	for (int i=0; i<4; i++)
	{
		TQString s = m->category(i);
		kdDebug() << "Category " << i << ": " << (s.isEmpty() ? CSL1("<empty>") : s) << endl;
		cats.append(s);
/*
		if (i<((sizeof(categoryNames) / sizeof(categoryNames[0]))))
			m->setCategoryName(i,TQString::fromLatin1(categoryNames[i]));
*/
	}

	m->write(l);

	delete m;
	delete l;

	return cats;
}

int checkCategories()
{
	TQStringList l = listCategories();
	TQStringList m = listCategories();

	if (l.isEmpty() || m.isEmpty()) return ERR;
	if (l!=m) return ERR;
	return OK;
}

int checkMemo()
{
	PilotLocalDatabase *l = new PilotLocalDatabase("./data/MemoDB");
	if (!l->isOpen()) return ERR_NO_EXIST;

	PilotMemoInfo *m = new PilotMemoInfo(l);
	m->dump();

	TQString c = m->category(1);
	if (c != CSL1("Business"))
	{
		kdDebug() << "* Category 1 is not 'Business' but " << c << endl;
		return ERR;
	}

	m->setCategoryName(2,CSL1("Aardvark"));
	m->dump();

	c = m->category(2);
	if (c != CSL1("Aardvark"))
	{
		kdDebug() << "* Category 2 is not 'Aardvark' but " << c << endl;
		return ERR;
	}


	delete m;
	delete l;
	return OK;
}

static const KCmdLineOptions options[] =
{
  {"verbose", "Verbose output", 0},
  KCmdLineLastOption
};


int main(int argc, char **argv)
{
	KApplication::disableAutoDcopRegistration();

	KAboutData aboutData("testdatabase","Test Databases","0.1");
	KCmdLineArgs::init(argc,argv,&aboutData);
	KCmdLineArgs::addCmdLineOptions( options );

	KApplication app( false, false );

	KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

	Q_UNUSED(args)

	int r = 0;
	int i = 0;
#ifdef DEBUG
	debug_level=4;
#endif

	Q_UNUSED(argc);
	Q_UNUSED(argv);

#define ALLOW_NO_EXIST (1)
	static struct { const char *path; recordInfo *info; int flags; } 
		tests[] =
	{
		{ "/tmp/nonexistant/nonexistent", nonexistent,0 },
		{ "/tmp/Aesop", aesop, ALLOW_NO_EXIST },
		{ 0L, 0L, 0 }
	} ;

	while ( tests[i].path )
	{
		kdDebug() << "*** Test " << i << endl;
		int ret = checkDatabase( tests[i].path, tests[i].info );
		if ( ret )
		{
			if ( (ret==ERR_NO_EXIST) && 
				(tests[i].flags & ALLOW_NO_EXIST) )
			{
				kdDebug() << "* Test database doesn't exist, ignored." << endl;
			}
			else
			{
				r++;
			}
		}
		i++;
	}

	kdDebug() << "*** Test " << i << endl;
	if (checkMemo()) r++;
	i++;

	if (r)
	{
		kdDebug() << "***\n*** Failed " << r << " tests." << endl;
		return 1;
	}
	return 0;
}