home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / basedemo / btree / dkbtrtst.cxx < prev    next >
C/C++ Source or Header  |  1994-10-14  |  2KB  |  100 lines

  1.  
  2. // Testing the disk-based B-tree
  3. //
  4. // M. A. Sridhar 5/5/94
  5.  
  6. #include "io/dskbtree.h"
  7. #include <iostream.h>
  8. #include "base/memory.h"
  9.  
  10. class TestClass: public CL_Object {
  11.  
  12. public:
  13.     TestClass () {};
  14.     
  15.     TestClass (const CL_String& name, long salary)
  16.         :_name (name), _salary (salary) {};
  17.  
  18.     ~TestClass ();
  19.     
  20.     short Compare (const CL_Object&) const;
  21.  
  22.     // Override the CL_Object's methods:
  23.     virtual bool ReadFrom (const CL_Stream&);
  24.  
  25.     virtual bool WriteTo  (CL_Stream&) const;
  26.  
  27.     CL_String AsString () const {return _name + ": " + CL_String (_salary);};
  28.  
  29.     CL_String _name;
  30.     long      _salary;
  31. };
  32.  
  33.  
  34. TestClass::~TestClass ()
  35. {
  36. }
  37.  
  38. short TestClass::Compare (const CL_Object& o) const
  39. {
  40.     return _name.Compare (((const TestClass&) o)._name);
  41. }
  42.  
  43. bool TestClass::ReadFrom (const CL_Stream& s)
  44. {
  45.     return _name.ReadFrom (s) && s.Read (_salary);
  46. }
  47.  
  48.  
  49. bool TestClass::WriteTo (CL_Stream& s) const
  50. {
  51.     return _name.WriteTo (s) && s.Write (_salary);
  52. }
  53.  
  54.  
  55. typedef CL_Builder<TestClass> TestBuilder;
  56.  
  57. #define BTREE_DATA_FILE "btree.dat"
  58.  
  59.  
  60. struct {
  61.     char* name;
  62.     short value;
  63. } Table [] = {
  64.     "Michael Jordan", 300,
  65.     "Lee Iacocca",    150,
  66.     "Patty O'Furniture", 20,
  67.     "Polly C. Maker", 40,
  68.     "John Q. Public", 22,
  69.     "Fly Swatter",    23,
  70.     "Slim Pickens",     44,
  71.     "Curly Moe",      202,
  72.     "Meg O. Byte",    1001,
  73.     0, 0
  74. };
  75.  
  76. void main ()
  77. {
  78.     TestBuilder bld;
  79.     if (!CL_BinaryFile::Exists (BTREE_DATA_FILE)) {
  80.         CL_ByteStringStore store (BTREE_DATA_FILE, TRUE);
  81.         CL_DiskBTree diskTree (store, &bld, 3, TRUE);
  82.  
  83.         short i;
  84.     for (i = 0; Table[i].name != 0; i++) {
  85.             TestClass* p = new TestClass  (Table[i].name, Table[i].value);
  86.             diskTree.Add (p);
  87.         }
  88.         diskTree.IntoStream (cout);
  89.     }
  90.     else {
  91.         CL_ByteStringStore store (BTREE_DATA_FILE);
  92.         CL_DiskBTree diskTree (store, &bld, 3, FALSE);
  93.         diskTree.IntoStream (cout);
  94.         TestClass q ("Fly Swatter", 0);
  95.         TestClass* p = (TestClass*) diskTree.Find (&q);
  96.         cout << "\n\n" << p->AsString() << endl;
  97.     }
  98. }
  99.  
  100.