home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / TestTools / Sources / TestAllocLinkedList.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  4.3 KB  |  204 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TestAllocLinkedList.cp
  3.  
  4.     Contains:    Tester for the linked list class
  5.  
  6.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #ifndef __TESTALLOCLINKEDLIST__
  11. #include "TestAllocLinkedList.h"
  12. #endif
  13.  
  14. /**********************************************************************
  15. ** PUBLIC Constructor/Destructor
  16. ***********************************************************************/
  17.  
  18. Constructor(AllocLinkedList)
  19. Destructor(AllocLinkedList)
  20.  
  21. /**********************************************************************
  22. ** PUBLIC InitTest
  23. ***********************************************************************/
  24.  
  25. void TTestAllocLinkedList :: InitTest(BooleanParm verbose, BooleanParm, int, char**)
  26. {
  27.     short    idx;
  28.     
  29.     TStandardPool* pool = GetPool();
  30.  
  31.     for (idx = 0; idx < 100; ++idx)
  32.         fArray[idx] = NULL;
  33.         
  34.     if (pool == NULL)
  35.     {
  36.         Printf("### ERROR: There is no global pool\n");
  37.         return;
  38.     }
  39.         
  40.     fTest = new (pool) TLinkedList(pool);
  41.     
  42.     for (idx = 0; idx < 100; ++idx)
  43.     {
  44.         TNumber*    num;
  45.         if ((num = new (pool) TNumber(idx)) == NULL)
  46.         {
  47.             Printf("### ERROR: Out of Memory at #%d\n", idx);
  48.             break;
  49.         }
  50.         fArray[idx] = (TLink*)num;
  51.     }
  52.     if (idx == 100 && verbose)
  53.         Printf("INFO: Allocated 100 objects\n");
  54. }
  55.  
  56. /**********************************************************************
  57. ** PUBLIC RunTestIteration
  58. ***********************************************************************/
  59.  
  60. void TTestAllocLinkedList :: RunTestIteration(BooleanParm verbose, BooleanParm)
  61. {
  62.     short        matchFirst;
  63.     short        matchLast;
  64.     short        toMatch;
  65.     short        idx, jdx;
  66.     TNumber*    ptr;
  67.     char*        str;
  68.     
  69.     if (verbose)
  70.         Printf("INFO: Testing EmptyList\n");
  71.         
  72.     TestIsEmpty(true);
  73.     TestFirstLast(-1, -1);
  74.     TestCount(0);
  75.     for (jdx = 0; jdx < 100; ++jdx)
  76.         TestMember(jdx, true);
  77.         
  78.     if (verbose)
  79.         Printf("INFO: Testing AddFirst, Member, Count, First, Last and IsEmpty\n");
  80.     
  81.     for (idx = 0; idx < 100; ++idx)
  82.     {
  83.         ptr = (TNumber*)fArray[idx];
  84.         fTest->AddFirst(ptr);
  85.         
  86.         TestIsEmpty(false);
  87.         TestCount(idx + 1);
  88.         TestFirstLast(idx, 0);
  89.  
  90.         for (jdx = 0; jdx < 100; ++jdx)
  91.             TestMember(jdx, jdx > idx);
  92.     }
  93.     
  94.     TestIsEmpty(false);
  95.     TestCount(100);
  96.     TestFirstLast(99, 0);
  97.  
  98.     matchFirst = 0;
  99.     matchLast  = 99;
  100.     if (verbose)
  101.         Printf("INFO: Testing First, Last, RemoveFirst, RemoveLast,  and Member methods\n");
  102.  
  103.     for (idx = 0; idx < 100; ++idx)
  104.     {
  105.         TestIsEmpty(false);
  106.         TestCount(100 - idx);
  107.         TestFirstLast(matchLast, matchFirst);
  108.  
  109.         for (jdx = 0; jdx < 100; ++jdx)
  110.             TestMember(jdx, jdx < matchFirst || jdx > matchLast);
  111.         
  112.         if (idx & 1)
  113.         {
  114.             ptr = (TNumber*)fTest->RemoveFirst();
  115.             toMatch = matchLast;
  116.             matchLast -= 1;
  117.             str = "RemoveFirst";
  118.         }
  119.         else
  120.         {
  121.             ptr = (TNumber*)fTest->RemoveLast();
  122.             toMatch = matchFirst;
  123.             matchFirst += 1;
  124.             str = "RemoveLast";
  125.         }
  126.         if (ptr == NULL)
  127.             Printf("### ERROR: %s return no object, instead of #%hu\n", 
  128.                    str, toMatch);
  129.         else
  130.         {
  131.             if (ptr->fNumber != toMatch)
  132.                 Printf("### ERROR: %s return object #%hu, instead of #%hu\n",
  133.                        str, ptr->fNumber, toMatch);
  134.         }
  135.     }
  136.     
  137.     TestIsEmpty(true);
  138.     TestFirstLast(-1, -1);
  139.     TestCount(0);
  140.     for (jdx = 0; jdx < 100; ++jdx)
  141.         TestMember(jdx, true);
  142.         
  143.     if (verbose)
  144.         Printf("INFO: Testing AddLast, Member, Count, First, Last and IsEmpty\n");
  145.     
  146.     for (idx = 0; idx < 100; ++idx)
  147.     {
  148.         ptr = (TNumber*)fArray[idx];
  149.         fTest->AddLast(ptr);
  150.         
  151.         TestIsEmpty(false);
  152.         TestCount(idx + 1);
  153.         TestFirstLast(0, idx);
  154.  
  155.         for (jdx = 0; jdx < 100; ++jdx)
  156.             TestMember(jdx, jdx > idx);
  157.     }
  158.     
  159.     if (verbose)
  160.         Printf("INFO: Testing RemoveAll\n");
  161.         
  162.     fTest->RemoveAll();
  163.     
  164.     TestIsEmpty(true);
  165.     TestCount(0);
  166.     TestFirstLast(-1, -1);
  167.     for (jdx = 0; jdx < 100; ++jdx)
  168.         TestMember(jdx, true);
  169.  
  170.     if (verbose)
  171.         Printf("INFO: Testing Add, Member, Count, First, Last and IsEmpty\n");
  172.     
  173.     for (idx = 0; idx < 100; ++idx)
  174.     {
  175.         fTest->Add((TNumber*)fArray[idx]);
  176.         
  177.         TestIsEmpty(false);
  178.         TestCount(idx + 1);
  179.         TestFirstLast(0, idx);
  180.  
  181.         for (jdx = 0; jdx < 100; ++jdx)
  182.             TestMember(jdx, jdx > idx);
  183.     }
  184.     
  185.     fTest->RemoveAll();
  186. }
  187.  
  188. /**********************************************************************
  189. ** PUBLIC EndTest
  190. ***********************************************************************/
  191.  
  192. void TTestAllocLinkedList :: EndTest(BooleanParm verbose, BooleanParm)
  193. {
  194.     if (verbose)
  195.         Printf("INFO: End of Linked List test\n");
  196.     for (short idx = 0; idx < 100; ++idx)
  197.     {
  198.         TNumber* ptr = (TNumber*)fArray[idx];
  199.         delete ptr;
  200.     }
  201.     if (verbose)
  202.         Printf("INFO: Deleted 100 objects\n");
  203. }
  204.