kpilot/lib

testdatabase.cc

00001 /* testdatabase         KPilot
00002 **
00003 ** Copyright (C) 2005 by Adriaan de Groot <groot@kde.org)
00004 **
00005 ** Test the functions related to local databases.
00006 */
00007 
00008 /*
00009 ** This program is free software; you can redistribute it and/or modify
00010 ** it under the terms of the GNU Lesser General Public License as published by
00011 ** the Free Software Foundation; either version 2.1 of the License, or
00012 ** (at your option) any later version.
00013 **
00014 ** This program is distributed in the hope that it will be useful,
00015 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017 ** GNU Lesser General Public License for more details.
00018 **
00019 ** You should have received a copy of the GNU Lesser General Public License
00020 ** along with this program in a file called COPYING; if not, write to
00021 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022 ** MA 02110-1301, USA.
00023 */
00024 
00025 /*
00026 ** Bug reports and questions can be sent to kde-pim@kde.org
00027 */
00028 
00029 #include "options.h"
00030 
00031 #include <kaboutdata.h>
00032 #include <kapplication.h>
00033 #include <kdebug.h>
00034 #include <klocale.h>
00035 #include <kcmdlineargs.h>
00036 
00037 #include "pilotLocalDatabase.h"
00038 #include "pilotRecord.h"
00039 #include "pilotMemo.h"
00040 
00041 
00042 /* Return values for the various check* functions. They
00043    return OK if all is OK; ERR is for generic errors.
00044    ERR_NO_EXIST is returned if something (usually a database
00045    or other file) doesn't exist that should. The latter
00046    error might be ignored.
00047 */
00048 #define OK           (0)
00049 #define ERR          (1)
00050 #define ERR_NO_EXIST (2)
00051 
00052 
00053 /* Data about the records in a database. The id field is
00054    interpreted specially for the first and last entries.
00055 */
00056 typedef struct { int id,size; } recordInfo;
00057 
00058 /* Use END in the last recordInfo struct describing a database
00059    to indicate you expect the database to end there. Use NO_EXIST
00060    as the ID in the first struct to indicate that the database
00061    is expected _not_ to exist.
00062 */
00063 #define NO_EXIST   (-2)
00064 #define END        (-1)
00065 
00066 /* These tables of data are taken from various databases I have
00067    (but which I cannot commit to SVN due to license issues).
00068    The aesop listing is from an eBook of Aesop's fables.
00069    The way to create these tables is to use a third-party
00070    tool such as par to read the database:
00071 
00072    ./par l /tmp/Aesop.pdb | awk '{print "{",$3,",",$4,"},";}'
00073 
00074 */
00075 recordInfo nonexistent[] = {
00076     { NO_EXIST, 0 }
00077 } ;
00078 
00079 recordInfo aesop[] = {
00080 { 7307264 , 214 },
00081 { 7307265 , 1564 },
00082 { 7307266 , 1575 },
00083 { 7307267 , 2214 },
00084 { 7307268 , 2276 },
00085 { 7307269 , 2148 },
00086 { 7307270 , 2194 },
00087 { 7307271 , 2178 },
00088 { 7307272 , 2220 },
00089 { 7307273 , 2216 },
00090 { 7307274 , 2181 },
00091 { 7307275 , 2183 },
00092 { 7307276 , 2197 },
00093 { 7307277 , 2010 },
00094 { 7307278 , 2198 },
00095 { 7307279 , 2196 },
00096 { 7307280 , 2243 },
00097 { 7307281 , 2211 },
00098 { 7307282 , 2274 },
00099 { 7307283 , 364 },
00100 { 7307284 , 49124 },
00101     { END, 0 }
00102 } ;
00103 
00104 int checkDatabase(const char *path, recordInfo *info)
00105 {
00106     FUNCTIONSETUP;
00107 
00108     PilotLocalDatabase db(QString::fromLatin1(path));
00109     if (!db.isDBOpen())
00110     {
00111         kdDebug() << "No database " << path << endl;
00112         if ( info[0].id == NO_EXIST )
00113         {
00114             kdDebug() << "This was expected" << endl;
00115             return OK;
00116         }
00117         else
00118         {
00119             return ERR_NO_EXIST;
00120         }
00121     }
00122 
00123     if ( info[0].id == NO_EXIST )
00124     {
00125         kdDebug() << "Database not expected" << endl;
00126         return ERR;
00127     }
00128 
00129     int fail = 0;
00130     int index = 0;
00131     PilotRecord *r;
00132     while( (r = db.readRecordByIndex(index) ) )
00133     {
00134         kdDebug() << "[" << index << "] id=" << r->id() << " size=" << r->size() << endl;
00135         if ( ((recordid_t)info[index].id) != r->id() )
00136         {
00137             kdDebug() << "* Bad ID (expected" << r->id() << ")" << endl;
00138             fail++;
00139         }
00140         else if ( info[index].size != r->size() )
00141         {
00142             kdDebug() << "* Bad size (expected " << info[index].size << ")" << endl;
00143             fail++;
00144         }
00145         index++;
00146     }
00147     if ( info[index].id != END )
00148     {
00149         kdDebug() << "* End wasn't expected yet." << endl;
00150         r++;
00151     }
00152 
00153     if (fail)
00154     {
00155         kdDebug() << "* " << fail << " failures." << endl;
00156         return ERR;
00157     }
00158     return OK;
00159 }
00160 
00161 const char *categoryNames[4] =
00162 {
00163     "aardvarks",
00164     "toolongToBeaCategoryName",
00165     "personal",
00166     "impersonal"
00167 } ;
00168 
00169 QStringList listCategories()
00170 {
00171     QStringList cats;
00172     PilotLocalDatabase *l = new PilotLocalDatabase(SOURCE "/data/MemoDB");
00173     PilotMemoInfo *m = new PilotMemoInfo(l);
00174 
00175     if (!l->isDBOpen()) return cats;
00176 
00177     cats.append(CSL1("Unfiled"));
00178     m->dump();
00179 
00180     for (int i=0; i<20; i++)
00181     {
00182         PilotRecord *r = l->readRecordByIndex(i);
00183         kdDebug() << "Read record " << (void *)r << " with id=" << r->id() << endl;
00184         if (!r) break;
00185     }
00186 
00187     for (int i=0; i<4; i++)
00188     {
00189         QString s = m->category(i);
00190         kdDebug() << "Category " << i << ": " << (s.isEmpty() ? CSL1("<empty>") : s) << endl;
00191         cats.append(s);
00192 /*
00193         if (i<((sizeof(categoryNames) / sizeof(categoryNames[0]))))
00194             m->setCategoryName(i,QString::fromLatin1(categoryNames[i]));
00195 */
00196     }
00197 
00198     m->write(l);
00199 
00200     delete m;
00201     delete l;
00202 
00203     return cats;
00204 }
00205 
00206 int checkCategories()
00207 {
00208     QStringList l = listCategories();
00209     QStringList m = listCategories();
00210 
00211     if (l.isEmpty() || m.isEmpty()) return ERR;
00212     if (l!=m) return ERR;
00213     return OK;
00214 }
00215 
00216 int checkMemo()
00217 {
00218     PilotLocalDatabase *l = new PilotLocalDatabase(SOURCE "/data/MemoDB");
00219     if (!l->isDBOpen()) return ERR_NO_EXIST;
00220 
00221     PilotMemoInfo *m = new PilotMemoInfo(l);
00222     m->dump();
00223 
00224     QString c = m->category(1);
00225     if (c != CSL1("Business"))
00226     {
00227         kdDebug() << "* Category 1 is not 'Business' but " << c << endl;
00228         return ERR;
00229     }
00230 
00231     m->setCategoryName(2,CSL1("Aardvark"));
00232     m->write(l);
00233 
00234     c = m->category(2);
00235     if (c != CSL1("Aardvark"))
00236     {
00237         kdDebug() << "* Category 2 is not 'Aardvark' but " << c << endl;
00238         return ERR;
00239     }
00240 
00241 
00242     delete m;
00243     delete l;
00244     return OK;
00245 }
00246 
00247 static const KCmdLineOptions options[] =
00248 {
00249   {"verbose", "Verbose output", 0},
00250   KCmdLineLastOption
00251 };
00252 
00253 
00254 int main(int argc, char **argv)
00255 {
00256     KAboutData aboutData("testdatabase","Test Databases","0.1");
00257     KCmdLineArgs::init(argc,argv,&aboutData);
00258     KCmdLineArgs::addCmdLineOptions( options );
00259 
00260     //  KApplication app( false, false );
00261     KApplication app;
00262 
00263     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00264 
00265     Q_UNUSED(args)
00266 
00267     int r = 0;
00268     int i = 0;
00269 #ifdef DEBUG
00270     debug_level=4;
00271 #endif
00272 
00273     Q_UNUSED(argc);
00274     Q_UNUSED(argv);
00275 
00276 #define ALLOW_NO_EXIST (1)
00277     static struct { const char *path; recordInfo *info; int flags; } 
00278         tests[] =
00279     {
00280         { "/tmp/nonexistant/nonexistent", nonexistent,0 },
00281         { "/tmp/Aesop", aesop, ALLOW_NO_EXIST },
00282         { 0L, 0L, 0 }
00283     } ;
00284 
00285     while ( tests[i].path )
00286     {
00287         kdDebug() << "*** Test " << i << endl;
00288         int ret = checkDatabase( tests[i].path, tests[i].info );
00289         if ( ret )
00290         {
00291             if ( (ret==ERR_NO_EXIST) && 
00292                 (tests[i].flags & ALLOW_NO_EXIST) )
00293             {
00294                 kdDebug() << "* Test database doesn't exist, ignored." << endl;
00295             }
00296             else
00297             {
00298                 r++;
00299             }
00300         }
00301         i++;
00302     }
00303 
00304     kdDebug() << "*** Test " << i << endl;
00305     if (checkMemo()) r++;
00306     i++;
00307 
00308     if (r)
00309     {
00310         kdDebug() << "***\n*** Failed " << r << " tests." << endl;
00311         return 1;
00312     }
00313     return 0;
00314 }
00315 
KDE Home | KDE Accessibility Home | Description of Access Keys