home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / basedemo / sequence / seqtest.cxx < prev   
C/C++ Source or Header  |  1994-10-14  |  4KB  |  159 lines

  1.  
  2.  
  3.  
  4. // A test program to exercise some of the methods on the Sequence and
  5. // related classes
  6. //
  7. // M. A. Sridhar
  8. // January 1994
  9.  
  10. #include "base/objseq.h"
  11. #include "base/strgseq.h"
  12. #include "base/intset.h"
  13. #include "base/intseq.h"
  14. #include "io/binfile.h"
  15. #include <stdio.h>
  16.  
  17.  
  18. void PrintOut (const CL_StringSequence& a)
  19. {
  20.     for (short i = 0; i < a.Size(); i++)
  21.         printf ("'%s'\n", a[i].AsPtr());
  22.     printf ("-------------------------------------------\n");
  23. }
  24.  
  25.  
  26. main ()
  27. {
  28.     // CL_StringSequence tst (18000); // Testing big sequences
  29.     CL_StringSequence a;
  30.     a.Add ("world!");
  31.     a.Add ("Hello,");
  32.     a.Add ("this is");
  33.     a.Add ("a great day.");
  34.     PrintOut (a);
  35.  
  36.     a.Insert ("world! 2", 1);
  37.     PrintOut (a);
  38.     a.Insert ("Hello, 2", -1);
  39.     PrintOut (a);
  40.     a.Insert ("this is 2", 2);
  41.     PrintOut (a);
  42.     a.Insert ("a great day. 2", 3);
  43.     PrintOut (a);
  44.  
  45.  
  46.     a.Sort();
  47.     PrintOut (a);
  48.  
  49. #ifndef __GNUC__  // GCC Still has bugs!
  50.     CL_IntegerSet st1(2,4);
  51.     st1.Add (4);
  52.     st1.Add (7);
  53.     st1.Add (8);
  54.     st1.Add (9);
  55.     CL_StringSequence sq = a - st1;
  56.     PrintOut (sq);
  57. #endif
  58.     
  59.     a.ShiftRightAt (2);
  60.     PrintOut (a);
  61.  
  62.     a[2] = "Some string here";
  63.     PrintOut (a);
  64.     
  65.     a.ShiftLeftAt (2, 2);
  66.     PrintOut (a);
  67.  
  68.     CL_StringSequence* b = new CL_StringSequence(10);
  69.     *b = a;
  70.     PrintOut (*b);
  71.  
  72.     *b += a;
  73.     PrintOut (*b);
  74.     
  75.     b->Sort();
  76.     PrintOut (*b);
  77.     
  78.     CL_BinaryFile f ("seqtest.dat", TRUE);
  79.     f << *b; // Testing persistent sequences
  80.     b->MakeEmpty ();
  81.     
  82.  
  83.     // CL_Builder<CL_String> aBuilder;
  84.     // CL_ObjectSequence a2(0, &aBuilder);
  85.     CL_ObjectSequence a2;
  86.     CL_String* pStrg;
  87.     pStrg = new CL_String ("1"); a2.Add (pStrg);
  88.     pStrg = new CL_String ("2"); a2.Add (pStrg);
  89.     pStrg = new CL_String ("3"); a2.Add (pStrg);
  90.     pStrg = new CL_String ("10"); a2.Add (pStrg);
  91.     pStrg = new CL_String ("20"); a2.Add (pStrg);
  92.     pStrg = new CL_String ("30"); a2.Add (pStrg);
  93.     pStrg = new CL_String ("11"); a2.Add (pStrg);
  94.     pStrg = new CL_String ("22"); a2.Add (pStrg);
  95.     pStrg = new CL_String ("31"); a2.Add (pStrg);
  96.     pStrg = new CL_String ("10"); a2.Add (pStrg);
  97.     printf ("a2's size: %ld\n", a2.Size());
  98.  
  99.     f << a2;             // Testing persistent sequences
  100.     f.SeekToBegin();
  101.     f >> *b;
  102.     // b->ReadFrom (f);
  103.     
  104.     printf ("Restored b is: \n"); PrintOut (*b);
  105.     delete b;
  106.  
  107.     for (long i = 0; i < a2.Size(); i++) {
  108.         printf ("'%s'\n", ((CL_String*) a2[i])->AsPtr());
  109.     }
  110.     pStrg = (CL_String*) a2.ExtractLeftmost();
  111.     printf ("Leftmost: '%s'\n", pStrg->AsPtr());
  112.     for (i = 0; i < a2.Size(); i++) {
  113.         printf ("'%s'\n", ((CL_String*) a2[i])->AsPtr());
  114.     }
  115.     delete pStrg;
  116.  
  117.     pStrg = (CL_String*) a2[4];
  118.     a2.Remove (4);
  119.     delete pStrg;
  120.     for (i = 0; i < a2.Size(); i++) {
  121.         printf ("'%s'\n", ((CL_String*) a2[i])->AsPtr());
  122.     }
  123.     
  124.     a2.DestroyContents ();
  125.     f >> a2;
  126.     printf ("Restored a2 is:\n");
  127.     for (i = 0; i < a2.Size(); i++) {
  128.         printf ("'%s'\n", ((CL_String*) a2[i])->AsPtr());
  129.     }
  130.     printf ("------------------------------------\n");
  131.     a2.DestroyContents ();
  132.     
  133.     
  134.     
  135.  
  136.     CL_IntegerSequence iseq;
  137.     for (i = 0; i < 100; i++) {
  138.         iseq.Add (i);
  139.         iseq.Add (5*i);
  140.     }
  141.     for (i = 5; i < 80; i++)
  142.         iseq.Remove (2);
  143.     iseq.Add (253);
  144.     printf ("iseq size %ld\n", iseq.Size());
  145.     for (long j = 0; j < iseq.Size(); j++) {
  146.         printf ("iseq[%ld] = %ld\n", j, iseq[j]);
  147.     }
  148.     iseq.Sort();
  149.     printf ("==============================\niseq size %ld\n", iseq.Size());
  150.     for (j = 0; j < iseq.Size(); j++) {
  151.         printf ("iseq[%ld] = %ld\n", j, iseq[j]);
  152.     }
  153.     iseq.Sort();
  154.  
  155.     return 0;
  156. }
  157.  
  158.  
  159.