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

TestAllocator.C

Go to the documentation of this file.
00001 // Copyright (C) 2003, Kenneth C. Schalk
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 May 27 14:58:26 EDT 2004 by ken@xorian.net        
00020 
00021 #include <list>
00022 #include <vector>
00023 #include <algorithm>
00024 
00025 #include "BasicsAllocator.H"
00026 
00027 // Text used in the test.
00028 static const Text testTexts[] = {
00029   "The quick brown fox jumped over the lazy dog.",
00030   "Now is the Time for All Good Men to Come to the Aid of their Country.",
00031   "It was the best of times, it was the worst of times"
00032   };
00033 
00034 using std::list;
00035 using std::vector;
00036 using std::cout;
00037 using std::endl;
00038 using std::reverse;
00039 
00040 // Typedefs for the STL types we'll be using to test
00041 // Basics::Allocator.
00042 typedef list<char, Basics::Allocator<char> > charList;
00043 typedef vector<char, Basics::Allocator<char> > charVector;
00044 
00045 // These functions convert a Text to either a charList or a
00046 // charVector.
00047 
00048 charList listFromText(const Text &in)
00049 {
00050   const char *s = in.cchars();
00051   charList result;
00052   while(*s)
00053     result.push_back(*s++);
00054   return result;
00055 }
00056 
00057 charVector vectorFromText(const Text &in)
00058 {
00059   const char *s = in.cchars();
00060   charVector result;
00061   while(*s)
00062     result.push_back(*s++);
00063   return result;
00064 }
00065 
00066 // Main body of the test.
00067 
00068 int main(void)
00069 {
00070   unsigned int nTexts = sizeof(testTexts) / sizeof(testTexts[0]);
00071 
00072   for(unsigned int i = 0; i < nTexts; i++)
00073     {
00074       const Text &testText = testTexts[i];
00075       // cout << "text[" << i << "] = \"" << testText << "\"" << endl;
00076 
00077       // Convert the text to both a list and a vector.
00078       charList l = listFromText(testText);
00079       charVector v = vectorFromText(testText);
00080 
00081       // Reverse both of them.
00082       reverse(l.begin(), l.end());
00083       reverse(v.begin(), v.end());
00084 
00085       // Loop forward over the text string.
00086       for(unsigned int j = 0; j < testText.Length(); j++)
00087         {
00088           // The lengths of both containers should be equal to the number
00089           // of characters remaining in the text string.
00090           assert(v.size() == l.size());
00091           assert(v.size() == (testText.Length() - j));
00092 
00093           // Get the next character from the back of both of the
00094           // containers and the next character in sequence from the text.
00095           char cv = v.back(), cl = l.back(), ct = testText[j];
00096 
00097           // All three of those should be the same.
00098           assert(cv == ct);
00099           assert(cl == ct);
00100 
00101           v.pop_back();
00102           l.pop_back();
00103         }
00104     }
00105 
00106   cout << "All tests passed!" << endl;
00107   return 0;
00108 }

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