| # kjs Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# testavl.cpp

    00001 // -*- c-basic-offset: 2 -*-
    00002 /*
    00003  *  This file is part of the KDE libraries
    00004  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
    00005  *
    00006  *  This library is free software; you can redistribute it and/or
    00007  *  modify it under the terms of the GNU Lesser General Public
    00008  *  License as published by the Free Software Foundation; either
    00009  *  version 2 of the License, or (at your option) any later version.
    00010  *
    00011  *  This library is distributed in the hope that it will be useful,
    00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014  *  Lesser General Public License for more details.
    00015  *
    00016  *  You should have received a copy of the GNU Lesser General Public License
    00017  *  along with this library; see the file COPYING.LIB.  If not, write to
    00018  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019  *  Boston, MA 02111-1307, USA.
    00020  *
    00021  *  $Id: testavl.cpp,v 1.4 2002/06/19 08:21:00 domi Exp $
    00022  */
    00023
    00024 #include "value.h"
    00025 #include "property_map.h"
    00026 #include "internal.h"
    00027 #include "ustring.h"
    00028
    00029 #include <stdio.h>
    00030 #include <stdlib.h>
    00031 #include <time.h>
    00032 #include <assert.h>
    00033
    00034 using namespace KJS;
    00035
    00036 bool testInsertDelete(int numInsert, int numDelete, int delOffset, int randSeed) {
    00037
    00038   srand(randSeed);
    00039   char str[20];
    00040   bool result = true;
    00041
    00042   assert(numDelete >= 0 && numDelete < numInsert);
    00043   assert(delOffset >= 0 && delOffset+numDelete <= numInsert);
    00044   PropertyMap map;
    00045
    00046   // add some random numbers
    00047   int *nums = (int*)malloc(numInsert*sizeof(int));
    00048   int i;
    00049   for (i = 0; i < numInsert; i++) {
    00050     nums[i] = int(1000.0*rand()/RAND_MAX);
    00051
    00052     Value val = Number(nums[i]);
    00053     ValueImp *v = val.imp();
    00054     v->ref();
    00055
    00056     sprintf(str,"%05d-%05d",nums[i],i); // ensure uniqueness
    00057     map.put(str,v,0);
    00058     map.checkTree();
    00059   }
    00060
    00061   // check to ensure they're all there
    00062   for (i = 0; i < numInsert; i++) {
    00063     sprintf(str,"%05d-%05d",nums[i],i);
    00064     ValueImp *v = map.get(str);
    00065     if (v == 0 || v->type() != NumberType ||
    00066         static_cast<NumberImp*>(v)->value() != nums[i]) {
    00067       result = false;
    00068     }
    00069     map.checkTree();
    00070   }
    00071
    00072   // delete some
    00073   for (i = delOffset; i < delOffset+numDelete; i++) {
    00074     sprintf(str,"%05d-%05d",nums[i],i);
    00075     map.remove(str);
    00076     map.checkTree();
    00077   }
    00078
    00079   // make sure the deletes ones aren't there any more, and the others are
    00080   for (i = 0; i < numInsert; i++) {
    00081     sprintf(str,"%05d-%05d",nums[i],i);
    00082     ValueImp *v = map.get(str);
    00083
    00084     if (i >= delOffset && i < delOffset+numDelete) {
    00085       // should have been deleted
    00086       if (v)
    00087         result = false;
    00088     }
    00089     else {
    00090       // should not have been deleted
    00091       if (v == 0 || v->type() != NumberType ||
    00092           static_cast<NumberImp*>(v)->value() != nums[i]) {
    00093         result = false;
    00094       }
    00095     }
    00096     map.checkTree();
    00097   }
    00098
    00099   // check that first() and next() work
    00100   PropertyMapNode *it = map.first();
    00101   int itcount = 0;
    00102   while (it) {
    00103     itcount++;
    00104     PropertyMapNode *prev = it;
    00105     it = it->next();
    00106     if (it) {
    00107       if (uscompare(prev->name,it->name) >= 0)
    00108         result = false;
    00109     }
    00110   }
    00111   if (itcount != numInsert-numDelete)
    00112     result = false;
    00113
    00114   if (result)
    00115     printf("PASS: Insert %d, delete %d-%d, seed %d\n",numInsert,delOffset,
    00116            delOffset+numDelete-1,randSeed);
    00117   else
    00118     printf("FAIL: Insert %d, delete %d-%d, seed %d\n",numInsert,delOffset,
    00119            delOffset+numDelete-1,randSeed);
    00120
    00121   return result;
    00122 }
    00123
    00124 void testMisc() {
    00125   PropertyMap *map;
    00126
    00127   // test remove() doesn't crash with empty list
    00128   map = new PropertyMap();
    00129   map->remove("nonexistant");
    00130   delete map;
    00131   printf("PASS: remove() doesn't crash with empty list\n");
    00132
    00133   // test get() doesn't crash with empty list
    00134   map = new PropertyMap();
    00135   map->get("nonexistant");
    00136   delete map;
    00137   printf("PASS: get() doesn't crash with empty list\n");
    00138
    00139   // test get() returns 0 on an empty list
    00140   map = new PropertyMap();
    00141   if (map->get("nonexistant") == 0)
    00142     printf("PASS: get() returns 0 on an empty list\n");
    00143   else
    00144     printf("FAIL: get() returns 0 on an empty list\n");
    00145   delete map;
    00146 }
    00147
    00148 int main()
    00149 {
    00150   PropertyMap map;
    00151
    00152   testMisc();
    00153
    00154   int randomSeed = 4;
    00155   int numTests = 100;
    00156
    00157   srand(randomSeed);
    00158
    00159   int *numInserts = (int*)malloc(numTests*sizeof(int));
    00160   int *numDeletes = (int*)malloc(numTests*sizeof(int));
    00161   int *delOffsets = (int*)malloc(numTests*sizeof(int));
    00162   int i;
    00163   for (i = 0; i < numTests; i++) {
    00164     numInserts[i] = int(1000.0*rand()/RAND_MAX);
    00165     numDeletes[i] = int(float(numInserts[i])*rand()/RAND_MAX);
    00166     delOffsets[i] = int(float(numInserts[i]-numDeletes[i])*rand()/RAND_MAX);
    00167   }
    00168
    00169   for (i = 0; i < numTests; i++) {
    00170     testInsertDelete(numInserts[i],numDeletes[i],delOffsets[i],4);
    00171   }
    00172
    00173
    00174
    00175   return 0;
    00176 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:08:35 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001