Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

CLTest.cc

Go to the documentation of this file.
00001 #include "cl/FileName.h"
00002 #include "cl/List.h"
00003 #include <stdlib.h>
00004 #include "cl/Hash.h"
00005 #include "cl/Heap.h"
00006 
00007 #define CL_INST_RW
00008 #include "cl/InstArray.h"
00009 
00010 class StringNode : public Node, public String
00011 {
00012 public:
00013     StringNode() : Node(), String() {};
00014     StringNode(const char *s) : Node(), String() { (String&) SELF = s; };
00015     
00016     static Void *New() { return new StringNode; };
00017 };
00018 
00019 Void TestList()
00020 {
00021     TypedList<StringNode>   list;
00022     StringNode tt, *temp;
00023 
00024     cout << "\nList Test" << endl;
00025     cout << "---------\n" << endl;
00026 
00027     (String&) tt = "red";
00028     cout << list << endl;
00029     list.Append(&tt);
00030     list.Append(new StringNode("orange"));
00031     temp = (StringNode*) StringNode::New();
00032     *temp = "green";
00033     list.Append(temp);
00034     cout << list << endl;
00035 }
00036 
00037 Void MyPrint(StrConst bob)
00038 {
00039     cout << "print(StrConst): " << bob << endl;
00040 }
00041 
00042 Void TestString()
00043 {
00044     String      string;
00045     StrConst    strconst = "strconst";
00046     Int         i;
00047     StrConstArray   a;
00048     
00049     string = "string";
00050     
00051     cout << "\nString Test" << endl;
00052     cout << "-----------\n" << endl;
00053 
00054     cout << "sprintf test: " 
00055          << String().Printf("number %d, strings '%s' '%s' '%s'", 
00056             20, "cstring", strconst, string.CString()) << endl;
00057 
00058     cout << "StrConst + CStr: " << strconst + "cstr" << endl;
00059     cout << "String + CStr: " << string + "cstr" << endl;
00060     cout << "CStr + String: " << ("cstr" + string) << endl;
00061     cout << "String + StrConst + CStr: " << string + strconst + "cstr" << endl;
00062 
00063     MyPrint(string);
00064     MyPrint("blah-" + string + "-blah");
00065 
00066     cout << "iso for string/strconst == ";
00067     if (string == string && strconst == strconst)
00068         cout << "good" << endl;
00069     else
00070         cout << "bad" << endl;
00071 
00072     cout << "string/cstr == ";
00073     if (string == "string" && "string" == string)
00074         cout << "good" << endl;
00075     else
00076         cout << "bad" << endl;
00077 
00078     cout << "strconst/cstr == ";
00079     if (strconst == "strconst" && "strconst" == strconst)
00080         cout << "good" << endl;
00081     else
00082         cout << "bad" << endl;
00083 
00084     string = strconst;
00085     cout << "string/strconst == ";
00086     if (strconst == string && string == strconst)
00087         cout << "good" << endl;
00088     else
00089         cout << "bad" << endl;
00090 
00091     string = "abcdefgh";
00092 
00093     cout << "Suffix/Prefix test: " << string.Suffix(2) << ':' << string.Suffix(-2) << ':'
00094          << string.Prefix(2) << ':' << string.Prefix(-2) << '<' << endl;
00095 
00096     cout << "Suffix/Prefix bounds test: " << string.Suffix(10) << ':' << string.Suffix(-10) << ':'
00097          << string.Prefix(10) << ':' << string.Prefix(-10) << '<' << endl;
00098 
00099     cout << "SubString test: " << string.SubString(2, 4) << ':' << string.SubString(-6, 4) << '<' << endl;
00100 
00101     cout << "SubString bounds test: " << string.SubString(10, 4) << ':' << string.SubString(-10, 4)
00102          << ':' << string.SubString(2, 10) << '<' << endl;
00103 
00104     cout << "Find char test: " << string.FindChar('c')
00105          << ", " << string.FindCharLast('x')
00106          << ", " << strconst.FindChar('s')
00107          << ", " << strconst.FindCharLast('s') << endl;
00108 
00109     cout << "Find tokens test: " << endl;
00110     string = "   there is a disease   that is tearing apart the  lives of thousands";
00111     cout << "string = " << string << endl;
00112     Split(string, a);
00113     for (i = 0; i < a.NumItems(); i++)
00114         cout << "token " << i << " = '" << a[i] << "'" << endl;
00115 }
00116 
00117 static Void TestLoad(const FileName &fname)
00118 {
00119     cout << "exists: " << fname.GetPath() << ": " << fname.IsReadable() << endl;
00120 }
00121 
00122 StrConst kExt[] =
00123 {
00124     "a",
00125     "b",
00126     0
00127 };
00128 
00129 Void TestFileName()
00130 {
00131     FileName    t;
00132 
00133     cout << "\nFileName Test" << endl;
00134     cout << "-------------\n" << endl;
00135 
00136     t.SetPath("/one/two/three/and/wibble.sl.gz");   
00137 
00138     cout << "the path is " << t.GetPath() << endl;
00139     cout << "parent: " << t.GetDir() << endl;
00140     cout << "name: " << t.GetFile() << endl;
00141     cout << "extensions: " << t.GetExtension() << endl;
00142     t.RemoveExtension();
00143     cout << "removed extension" << endl;
00144     cout << "new name: " << t.GetFile() << endl;
00145     cout << "new ext: " << t.GetExtension() << endl;
00146 
00147     TestLoad(t);
00148     TestLoad(FileName().SetPath("Makefile"));
00149     TestLoad(FileName().SetPath("Makefile.depend"));
00150 
00151     FileName fname;
00152 
00153     Int x;
00154 
00155     x = fname.SetPath("a").FindFileExtension(kExt);
00156     cout << String().Printf("a: file %s %d [%d]\n", fname.GetPath().CString(), fname.flags, x);
00157 
00158     x = fname.SetPath("a.b").FindFileExtension(kExt);
00159     cout << String().Printf("a.b.gz: file %s %d [%d]\n", fname.GetPath().CString(), fname.flags, x);
00160 
00161     x = fname.SetPath("a.e").FindFileExtension(kExt);
00162     cout << String().Printf("a.e: file %s %d [%d]\n", fname.GetPath().CString(), fname.flags, x);
00163 
00164     x = fname.SetPath("b").FindFileExtension(kExt);
00165     cout << String().Printf("b: file %s %d [%d]\n", fname.GetPath().CString(), fname.flags, x);
00166 }
00167 
00168 Void TestEnviron()
00169 {
00170     cout << "\nEnviron Test" << endl;
00171     cout << "------------\n" << endl;
00172 
00173     cout << SubstituteEnvVars("shell = $SHELL, path = $HOME/bob") << endl;
00174 }
00175 
00176 Void TestInstArray()
00177 {
00178     InstArray<Int>  a;
00179     Int             i, sum;
00180     
00181     cout << "\nInstArray Test" << endl;
00182     cout << "--------------\n" << endl;
00183 
00184     a.stats.StartTrace(FileName().SetPath("trace.data"));
00185     a.SetSize(20);
00186     a.stats.accesses = new Int[a.NumItems()];
00187     for (i = 0; i < a.NumItems(); i++)
00188         a.stats.accesses[i] = 0;
00189         
00190     for (i = 0; i < a.NumItems(); i++)
00191         a[i] = i;
00192 
00193     for (i = 0; i < a.NumItems(); i++)
00194         a[i] += 3;
00195 
00196     for (i = 0; i < a.NumItems(); i++)
00197         sum += a[i];
00198 
00199     for (i = 0; i < 100; i++)
00200         a[rand() % 20] += 21;
00201         
00202     a.stats.StopTrace();
00203     a.stats.Dump();
00204     for (i = 0; i < a.NumItems(); i++)
00205         cout << i << " = " << a.stats.accesses[i] << endl;
00206 }
00207 
00208 class Printer : public IntHashIter
00209 {
00210 public:
00211     Void ProcessItem(StrConst s, Int a)
00212     { cout << s << " -> " << a << endl; };
00213 };
00214 
00215 Void TestHash()
00216 {
00217     cout << "\nHash Test" << endl;
00218     cout << "---------\n" << endl;
00219 
00220     IntHash     hash;
00221     Printer     printer;
00222     
00223     hash.SetItem("bob", 20);
00224     hash.SetItem("mary", 33);
00225     hash.SetItem("peter", 21);
00226     hash.SetItem("jane", 10);
00227 
00228     hash.SetItem("mary", hash.GetItem("mary") + 1);
00229     
00230     hash.Iterate(printer);
00231 
00232     cout << "bob exists: " << hash.ItemExists("bob") << endl;
00233     cout << "robert exists: " << hash.ItemExists("robert") << endl;
00234 }
00235 
00236 Void TestHeap()
00237 {
00238     Heap        heap;
00239     HeapEntry   *he;
00240     Int         i, n = 100;
00241     
00242     srand48(20);
00243     
00244     for (i = 0; i < n; i++)
00245     {
00246         he = new HeapEntry;
00247 
00248         he->cost = drand48();
00249         heap.Insert(he);
00250     }
00251 
00252     for (i = 0; i < n / 5; i++)
00253     {
00254         he = heap.heap[rand() % n];
00255         cout << "updating " << he->heapIdx << endl;
00256         he->cost = drand48();
00257         heap.Update(he);
00258     }
00259     
00260     for (i = 0; i < n / 5; i++)
00261     {
00262         he = heap.heap[rand() % heap.NumItems()];
00263         cout << "removing " << he->heapIdx << endl;
00264         heap.Delete(he);
00265     }
00266     
00267     for (i = 0; i < heap.NumItems(); i++)
00268         if (heap.heap[i]->heapIdx != i)
00269             cout << "bad heapIdx at " << i << " : " << heap.heap[i]->heapIdx << endl;
00270 
00271     cout << heap.NumItems() << " items in heap:" << endl;
00272     i = 0;
00273         
00274     while (he = heap.RemoveMax())
00275     {
00276         cout << i++ << " > " << he->cost << endl;
00277         delete he;
00278     }
00279 
00280 }
00281 
00282 Void TestObjArray()
00283 {
00284 
00285 
00286 }
00287 
00288 #ifdef UNFINISHED
00289 Void TestSplayTree()
00290 {
00291     /* A sample use of these functions.  Start with the empty tree,         */
00292     /* insert some stuff into it, and then delete it                        */
00293     Tree * root;
00294     int i;
00295     root = NULL;               /* the empty tree */
00296     size = 0;
00297     for (i = 0; i < 1024; i++)
00298     {
00299         root = insert((541 * i) & (1023), root);
00300     }
00301     for (i = 0; i < 1024; i++)
00302     {
00303         root = delete((541 * i) & (1023), root);
00304     }
00305     printf("size = %d\n", size);
00306 }
00307 #endif
00308 
00309 Int main(Int argc, Char *argv[])
00310 {
00311     FileName test;
00312     
00313     TestEnviron();
00314     TestList(); 
00315     TestString();
00316     TestInstArray();
00317     TestHash();
00318     TestFileName();
00319     TestHeap();
00320         
00321     return(0);
00322 }

Generated at Sat Aug 5 00:16:31 2000 for Class Library by doxygen 1.1.0 written by Dimitri van Heesch, © 1997-2000