Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TestTextSeq.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Thu Apr 21 18:13:11 EDT 2005 by ken@xorian.net        
00020 //      modified on Thu Apr 21 09:34:29 EDT 2005 by irina.furman@intel.com
00021 
00022 // TestTextSeq.C - Test code for TextSeq.  (Mostly this tests
00023 // Sequence::Iterator and that the STL sort algorithm can be used on a
00024 // Sequence.)
00025 
00026 #include <Generics.H>
00027 
00028 #include <algorithm>
00029 
00030 #include <iostream>
00031 #include <stdlib.h>
00032 #include <assert.h>
00033 
00034 using std::cout;
00035 using std::endl;
00036 using std::sort;
00037 
00038 void init_random()
00039 {
00040   srandom(time(0));
00041 }
00042 
00043 // Generate a random string
00044 char *random_str()
00045 {
00046   static const char *chars=("abcdefghijklmnopqrstuvwxyz"
00047                             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00048                             "0123456789"
00049                             "!@#$%^&*-_=+,.<>;:/?`~\\|'\"");
00050   unsigned int nchars = strlen(chars);
00051 
00052   unsigned int len = (random() % (nchars/1)) + 1;
00053   char* result = NEW_PTRFREE_ARRAY(char, len+1);
00054   for(unsigned int i = 0; i < len; i++)
00055     {
00056       result[i] = chars[random() % nchars];
00057     }
00058   result[len] = 0;
00059   return result;
00060 }
00061 
00062 extern "C"
00063 {
00064   int qsort_strcmp(const void *va, const void *vb)
00065   {
00066     const char *sa = *((const char **) va);
00067     const char *sb = *((const char **) vb);
00068     
00069     return strcmp(sa, sb);
00070   }
00071 }
00072 
00073 void SortTest()
00074 {
00075   unsigned int nstrings = (random() % 30) + 10;
00076   char** strings = NEW_ARRAY(char*, nstrings);
00077   TextSeq seq1(nstrings), seq2(nstrings);
00078 
00079   for(unsigned int i = 0; i < nstrings; i++)
00080     {
00081       strings[i] = random_str();
00082       // Note: we add the in opposite orders.
00083       seq1.addhi(strings[i]);
00084       seq2.addlo(strings[i]);
00085     }
00086 
00087   // Sort the array with qsort and the TextSeqs with STL sort.
00088   qsort(strings, nstrings, sizeof(char *), qsort_strcmp);
00089   sort(seq1.begin(), seq1.end());
00090   sort(seq2.begin(), seq2.end());
00091 
00092   // The sorted order should be the same.
00093   for(unsigned int i = 0; i < nstrings; i++)
00094     {
00095       assert(seq1.get(i) == strings[i]);
00096       assert(seq2.get(i) == strings[i]);
00097     }
00098 }
00099 
00100 int main(int argc, char **argv)
00101 {
00102   init_random();
00103   SortTest();
00104 
00105   cout << "All tests passed!" << endl;
00106   return 0;
00107 }

Generated on Mon May 8 00:48:30 2006 for Vesta by  doxygen 1.4.2