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

  1. /*
  2.     File:        TestLinkedList.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 __TESTLINKEDLIST__
  11. #include "TestLinkedList.h"
  12. #endif
  13.  
  14. /**********************************************************************
  15. ** PUBLIC Constructor/Destructor
  16. ***********************************************************************/
  17.  
  18. Constructor(LinkedList)
  19. Destructor(LinkedList)
  20.  
  21. /**********************************************************************
  22. ** Test methods 
  23. ***********************************************************************/
  24.  
  25. void TTestLinkedList::TestIsEmpty(Boolean correct) const
  26. {
  27.     if (correct)
  28.     {
  29.         if (!fTest->IsEmpty())
  30.             Printf("### ERROR: IsEmpty returned false when the list was not empty\n");
  31.     }
  32.     else
  33.     {
  34.         if (fTest->IsEmpty())
  35.             Printf("### ERROR: IsEmpty returned true when the list was empty\n");
  36.     }
  37. }
  38.  
  39. void TTestLinkedList::TestCount(size_t shouldBe) const
  40. {
  41.     size_t is = fTest->Count();
  42.     if (is != shouldBe)
  43.         Printf("### ERROR: Count returned %u when it should have returned %u\n",
  44.                 is, shouldBe);
  45. }
  46.  
  47. void TTestLinkedList::TestFirstLast(short first, short last) const
  48. {
  49.     TNumber*    ptr;
  50.     TLink*        link;
  51.     
  52.     if ((ptr = (TNumber*)fTest->First()) != NULL)
  53.     {
  54.         if (ptr->fNumber != first)
  55.             if (first < 0)
  56.                 Printf("### ERROR: First returned #%hu when it should have returned NULL\n",
  57.                         ptr->fNumber);
  58.             else
  59.                 Printf("### ERROR: First returned #%hu when it should have returned #%hu\n",
  60.                         ptr->fNumber, first);
  61.     }
  62.     else
  63.     {
  64.         if (first >= 0)
  65.             Printf("### ERROR: First return NULL when it should have returned #%u\n",
  66.                     first);
  67.     }
  68.     if ((ptr = (TNumber*)fTest->Last()) != NULL)
  69.     {
  70.         if (ptr->fNumber != last)
  71.             if (last < 0)
  72.                 Printf("### ERROR: Last returned #%hu when it should have returned NULL\n",
  73.                         ptr->fNumber);
  74.             else
  75.                 Printf("### ERROR: Last returned #%hu when it should have returned #%hu\n",
  76.                         ptr->fNumber, last);
  77.     }
  78.     else
  79.     {
  80.         if (last >= 0)
  81.             Printf("### ERROR: Last return NULL when it should have returned #%u\n",
  82.                     last);
  83.     }
  84.     if ((link = fTest->FirstLink()) != NULL)
  85.     {
  86.         ptr = (TNumber*)link->GetValue();
  87.         if (ptr->fNumber != first)
  88.             if (first < 0)
  89.                 Printf("### ERROR: FirstLink returned #%hu when it should have returned NULL\n",
  90.                         ptr->fNumber);
  91.             else
  92.                 Printf("### ERROR: FirstLink returned #%hu when it should have returned #%hu\n",
  93.                         ptr->fNumber, first);
  94.     }
  95.     else
  96.     {
  97.         if (first >= 0)
  98.             Printf("### ERROR: FirstLink return NULL when it should have returned #%u\n",
  99.                     first);
  100.     }
  101.     if ((link = fTest->LastLink()) != NULL)
  102.     {
  103.         ptr = (TNumber*)link->GetValue();
  104.         if (ptr->fNumber != last)
  105.             if (last < 0)
  106.                 Printf("### ERROR: LastLink returned #%hu when it should have returned NULL\n",
  107.                         ptr->fNumber);
  108.             else
  109.                 Printf("### ERROR: LastLink returned #%hu when it should have returned #%hu\n",
  110.                         ptr->fNumber, last);
  111.     }
  112.     else
  113.     {
  114.         if (last >= 0)
  115.             Printf("### ERROR: LastLink return NULL when it should have returned #%u\n",
  116.                     last);
  117.     }
  118. }    
  119.  
  120. void TTestLinkedList::TestMember(short number, Boolean ifGone) const
  121. {
  122.     TNumber     obj(number);
  123.     TNumber*    ptr;
  124.     TLink*        link;
  125.     
  126.     if ((ptr = (TNumber*)fTest->Member(obj)) != NULL)
  127.     {
  128.         if (ifGone)
  129.             Printf("### ERROR: Member returned #%hu when it should not be present\n",
  130.                     ptr->fNumber);
  131.         else
  132.             if (number != ptr->fNumber)
  133.                 Printf("### ERROR: Member returned #%hu when it should have returned #%hu\n",
  134.                         ptr->fNumber, number);
  135.     }
  136.     else
  137.         if (!ifGone)
  138.             Printf("### ERROR: Member returned no object when #%hu should have been present\n",
  139.                     number);
  140.     if ((link = fTest->MemberLink(obj)) != NULL)
  141.     {
  142.         ptr = (TNumber*)link->GetValue();
  143.         if (ifGone)
  144.             Printf("### ERROR: MemberLink returned #%hu when it should not be present\n",
  145.                     ptr->fNumber);
  146.         else
  147.             if (number != ptr->fNumber)
  148.                 Printf("### ERROR: MemberLink returned #%hu when it should have returned #%hu\n",
  149.                         ptr->fNumber, number);
  150.     }
  151.     else
  152.         if (!ifGone)
  153.             Printf("### ERROR: MemberLink returned no object when #%hu should have been present\n",
  154.                     number);
  155. }    
  156.  
  157. /**********************************************************************
  158. ** PUBLIC InitTest
  159. ***********************************************************************/
  160.  
  161. void TTestLinkedList::InitTest(BooleanParm verbose, BooleanParm, int, char**)
  162. {
  163.     short    idx;
  164.     
  165.     TStandardPool* pool = GetPool();
  166.  
  167.     for (idx = 0; idx < 100; ++idx)
  168.         fArray[idx] = NULL;
  169.         
  170.     if (pool == NULL)
  171.     {
  172.         Printf("### ERROR: There is no global pool\n");
  173.         return;
  174.     }
  175.         
  176.     fTest = new (pool) TLinkedList;
  177.     
  178.     for (idx = 0; idx < 100; ++idx)
  179.     {
  180.         TLink*        link;
  181.         TNumber*    num;
  182.         if ((num = new (pool) TNumber(idx)) == NULL)
  183.         {
  184.             Printf("### ERROR: Out of Memory at #%d\n", idx);
  185.             break;
  186.         }
  187.         if ((link = new (pool) TLink(num)) == NULL)
  188.         {
  189.             Printf("### ERROR: Out of Memory at #%d\n", idx);
  190.             break;
  191.         }
  192.         fArray[idx] = link;
  193.     }
  194.     if (idx == 100 && verbose)
  195.         Printf("INFO: Allocated 100 links and objects\n");
  196. }
  197.  
  198. /**********************************************************************
  199. ** PUBLIC RunTestIteration
  200. ***********************************************************************/
  201.  
  202. void TTestLinkedList::RunTestIteration(BooleanParm verbose, BooleanParm)
  203. {
  204.     short        matchFirst;
  205.     short        matchLast;
  206.     short        toMatch;
  207.     short        idx, jdx;
  208.     TLink*        link;
  209.     TNumber*    ptr;
  210.     char*        str;
  211.     
  212.     if (verbose)
  213.         Printf("INFO: Testing EmptyList\n");
  214.         
  215.     TestIsEmpty(true);
  216.     TestFirstLast(-1, -1);
  217.     TestCount(0);
  218.     for (jdx = 0; jdx < 100; ++jdx)
  219.         TestMember(jdx, true);
  220.         
  221.     if (verbose)
  222.         Printf("INFO: Testing AddLinkFirst, Member, Count, First, Last and IsEmpty\n");
  223.     
  224.     for (idx = 0; idx < 100; ++idx)
  225.     {
  226.         link = fArray[idx];
  227.         fTest->AddLinkFirst(link);
  228.         
  229.         TestIsEmpty(false);
  230.         TestCount(idx + 1);
  231.         TestFirstLast(idx, 0);
  232.  
  233.         for (jdx = 0; jdx < 100; ++jdx)
  234.             TestMember(jdx, jdx > idx);
  235.     }
  236.     
  237.     TestIsEmpty(false);
  238.     TestCount(100);
  239.     TestFirstLast(99, 0);
  240.  
  241.     matchFirst = 0;
  242.     matchLast  = 99;
  243.     if (verbose)
  244.         Printf("INFO: Testing First, Last, RemoveFirst, RemoveLast,  and Member methods\n");
  245.  
  246.     for (idx = 0; idx < 100; ++idx)
  247.     {
  248.         TestIsEmpty(false);
  249.         TestCount(100 - idx);
  250.         TestFirstLast(matchLast, matchFirst);
  251.  
  252.         for (jdx = 0; jdx < 100; ++jdx)
  253.             TestMember(jdx, jdx < matchFirst || jdx > matchLast);
  254.         
  255.         if (idx & 1)
  256.         {
  257.             link = fTest->RemoveFirstLink();
  258.             toMatch = matchLast;
  259.             matchLast -= 1;
  260.             str = "RemoveFirstLink";
  261.         }
  262.         else
  263.         {
  264.             link = fTest->RemoveLastLink();
  265.             toMatch = matchFirst;
  266.             matchFirst += 1;
  267.             str = "RemoveLastLink";
  268.         }
  269.         if (link == NULL)
  270.             Printf("### ERROR: %s return no object, instead of #%hu\n", 
  271.                    str, toMatch);
  272.         else
  273.         {
  274.             ptr = (TNumber*)link->GetValue();
  275.             if (ptr->fNumber != toMatch)
  276.                 Printf("### ERROR: %s return object #%hu, instead of #%hu\n",
  277.                        str, ptr->fNumber, toMatch);
  278.         }
  279.     }
  280.     
  281.     TestIsEmpty(true);
  282.     TestFirstLast(-1, -1);
  283.     TestCount(0);
  284.     for (jdx = 0; jdx < 100; ++jdx)
  285.         TestMember(jdx, true);
  286.         
  287.     if (verbose)
  288.         Printf("INFO: Testing AddLinkLast, Member, Count, First, Last and IsEmpty\n");
  289.     
  290.     for (idx = 0; idx < 100; ++idx)
  291.     {
  292.         link = fArray[idx];
  293.         fTest->AddLinkLast(link);
  294.         
  295.         TestIsEmpty(false);
  296.         TestCount(idx + 1);
  297.         TestFirstLast(0, idx);
  298.  
  299.         for (jdx = 0; jdx < 100; ++jdx)
  300.             TestMember(jdx, jdx > idx);
  301.     }
  302.     
  303.     if (verbose)
  304.         Printf("INFO: Testing Remove by pointer\n");
  305.         
  306.     matchFirst = 49;
  307.     matchLast  = 50;
  308.     
  309.     for (idx = 0; idx < 100; ++idx)
  310.     {
  311.         if (idx & 1)
  312.             jdx = matchFirst;
  313.         else
  314.             jdx = matchLast;
  315.         link = fArray[jdx];
  316.         
  317.         TestIsEmpty(false);
  318.         TestCount(100 - idx);
  319.         if (idx != 99)
  320.             TestFirstLast(0, 99);
  321.         else
  322.             TestFirstLast(0, 0);
  323.         
  324.         if (!fTest->Remove(link->GetValue()))
  325.             Printf("ERROR: Remove Link #%u Failed\n", jdx);
  326.  
  327.         TestCount(99-idx);
  328.         if (idx < 98)
  329.             TestFirstLast(0, 99);
  330.         else
  331.             if (idx == 98)
  332.                 TestFirstLast(0, 0);
  333.                 
  334.         if (idx & 1)
  335.             matchFirst -= 1;
  336.         else
  337.             matchLast += 1;
  338.             
  339.     }
  340.     if (verbose)
  341.         Printf("INFO: Testing Remove by reference\n");
  342.         
  343.     fTest->RemoveAll();            // Just in case
  344.     
  345.     for (idx = 0; idx < 100; ++idx)
  346.     {
  347.         link = fArray[idx];
  348.         fTest->AddLinkLast(link);
  349.     }
  350.     TestIsEmpty(false);
  351.     TestCount(100);
  352.     TestFirstLast(0, 99);
  353.     for (jdx = 0; jdx < 100; ++jdx)
  354.         TestMember(jdx, false);
  355.  
  356.     matchFirst = 49;
  357.     matchLast  = 50;
  358.  
  359.     for (idx = 0; idx < 100; ++idx)
  360.     {
  361.         if (idx & 1)
  362.             jdx = matchFirst;
  363.         else
  364.             jdx = matchLast;
  365.         link = fArray[jdx];
  366.         
  367.         TestIsEmpty(false);
  368.         TestCount(100 - idx);
  369.         if (idx != 99)
  370.             TestFirstLast(0, 99);
  371.         else
  372.             TestFirstLast(0, 0);
  373.         
  374.         if (!fTest->Remove(*(TNumber*)link->GetValue()))
  375.             Printf("ERROR: Remove Link #%u Failed\n", jdx);
  376.  
  377.         TestCount(99-idx);
  378.         if (idx < 98)
  379.             TestFirstLast(0, 99);
  380.         else
  381.             if (idx == 98)
  382.                 TestFirstLast(0, 0);
  383.                 
  384.         if (idx & 1)
  385.             matchFirst -= 1;
  386.         else
  387.             matchLast += 1;
  388.             
  389.     }
  390.     
  391.     if (verbose)
  392.         Printf("INFO: Testing Iterator\n");
  393.         
  394.     fTest->RemoveAll();            // Just in case
  395.     
  396.     for (idx = 0; idx < 100; ++idx)
  397.     {
  398.         link = fArray[idx];
  399.         fTest->AddLinkLast(link);
  400.     }
  401.  
  402.     {
  403.         TListIterator    iter(fTest);
  404.         TNumber*        num;
  405.         idx = 0;
  406.         while ((num = (TNumber*)(&iter)->Next()) != NULL)
  407.         {
  408.             if (num->fNumber != idx)
  409.                 Printf("ERROR: Iterator returned #%u when #%u was expected\n",
  410.                        num->fNumber, idx);
  411.             idx += 1;
  412.         }
  413.         if (idx != 100)
  414.             Printf("ERROR: Iterator stopped after #%u\n", idx - 1);
  415.             
  416.         if (verbose)
  417.             Printf("INFO: Testing RemoveCurrentObject at front\n");
  418.         idx = 0;
  419.         (&iter)->Reset();
  420.         while ((num = (TNumber*)(&iter)->Next()) != NULL)
  421.         {
  422.             if (num->fNumber != idx)
  423.                 Printf("ERROR: Iterator returned #%u when #%u was expected\n",
  424.                        num->fNumber, idx);
  425.             if (!(&iter)->RemoveCurrentObject())
  426.                 Printf("ERROR: RemoveCurrentObject returned false for #%u\n",
  427.                        idx);
  428.             idx += 1;
  429.         }
  430.         if (idx != 100)
  431.             Printf("ERROR: Iterator stopped after #%u\n", idx - 1);
  432.         (&iter)->Reset();
  433.         if ((num = (TNumber*)(&iter)->Next()) != NULL)
  434.             Printf("ERROR: Iterator return #%u when list should be empty\n",
  435.                    num->fNumber);
  436.         TestIsEmpty(true);
  437.         TestCount(0);
  438.         TestFirstLast(-1, -1);
  439.         for (jdx = 0; jdx < 100; ++jdx)
  440.             TestMember(jdx, true);
  441.         fTest->RemoveAll();            // Just in case
  442.     
  443.         for (idx = 0; idx < 100; ++idx)
  444.         {
  445.             link = fArray[idx];
  446.             fTest->AddLinkLast(link);
  447.         }
  448.         if (verbose)
  449.             Printf("INFO: Testing RemoveCurrentObject at end\n");
  450.         for (jdx = 0; jdx < 100; ++jdx)
  451.         {
  452.             (&iter)->Reset();
  453.             idx = 0;
  454.             while ((num = (TNumber*)(&iter)->Next()) != NULL)
  455.             {
  456.                 if (num->fNumber > 99-jdx)
  457.                     Printf("ERROR: Iterator returned #%u when it should be gone\n",
  458.                            num->fNumber);
  459.                 if (num->fNumber != idx)
  460.                     Printf("ERROR: Iterator returned #%u when #%u was expected\n",
  461.                            num->fNumber, idx);
  462.                 if (num->fNumber == 99-jdx && !(&iter)->RemoveCurrentObject())
  463.                     Printf("ERROR: RemoveCurrentObject returned false for #%u\n",
  464.                            idx);
  465.                 idx += 1;
  466.             }
  467.             if (idx != 100-jdx)
  468.                 Printf("ERROR: Iterator stopped after #%u instead of #%u\n",
  469.                        idx - 1, 99 - jdx);
  470.         }
  471.         (&iter)->Reset();
  472.         if ((num = (TNumber*)(&iter)->Next()) != NULL)
  473.             Printf("ERROR: Iterator return #%u when list should be empty\n",
  474.                    num->fNumber);
  475.         TestIsEmpty(true);
  476.         TestCount(0);
  477.         TestFirstLast(-1, -1);
  478.         for (jdx = 0; jdx < 100; ++jdx)
  479.             TestMember(jdx, true);
  480.             
  481.         fTest->RemoveAll();            // Just in case
  482.     
  483.         for (idx = 0; idx < 100; ++idx)
  484.         {
  485.             link = fArray[idx];
  486.             fTest->AddLinkLast(link);
  487.         }
  488.         if (verbose)
  489.             Printf("INFO: Testing RemoveCurrentObject in the middle\n");
  490.         matchFirst = 49;
  491.         matchLast  = 50;
  492.         for (jdx = 0; jdx < 100; ++jdx)
  493.         {
  494.             (&iter)->Reset();
  495.             if (jdx & 1)
  496.                 idx = matchFirst;
  497.             else
  498.                 idx = matchLast;
  499.             while ((num = (TNumber*)(&iter)->Next()) != NULL)
  500.             {
  501.                 if (num->fNumber > matchFirst && num->fNumber < matchLast)
  502.                     Printf("ERROR: Iterator returned #%u when it should be gone\n",
  503.                            num->fNumber);
  504.                 if (idx == num->fNumber && !(&iter)->RemoveCurrentObject())
  505.                     Printf("ERROR: RemoveCurrentObject returned false for #%u\n",
  506.                            idx);
  507.             }
  508.             if (jdx & 1)
  509.                 matchFirst -= 1;
  510.             else
  511.                 matchLast += 1;
  512.         }
  513.         (&iter)->Reset();
  514.         if ((num = (TNumber*)(&iter)->Next()) != NULL)
  515.             Printf("ERROR: Iterator return #%u when list should be empty\n",
  516.                    num->fNumber);
  517.                    
  518.         TestIsEmpty(true);
  519.         TestCount(0);
  520.         TestFirstLast(-1, -1);
  521.         for (jdx = 0; jdx < 100; ++jdx)
  522.             TestMember(jdx, true);
  523.     }
  524.     
  525.     if (verbose)
  526.         Printf("INFO: Testing DeleteAll\n");
  527.     
  528.     fTest->RemoveAll();            // Just in case
  529.     
  530.     for (idx = 0; idx < 100; ++idx)
  531.     {
  532.         link = fArray[idx];
  533.         fTest->AddLinkLast(link);
  534.     }
  535.     
  536.         
  537.     fTest->DeleteAll(kTDynamicPointer);
  538.     
  539.     TestIsEmpty(true);
  540.     TestCount(0);
  541.     TestFirstLast(-1, -1);
  542.     for (jdx = 0; jdx < 100; ++jdx)
  543.         TestMember(jdx, true);
  544. }
  545.  
  546. /**********************************************************************
  547. ** PUBLIC EndTest
  548. ***********************************************************************/
  549.  
  550. void TTestLinkedList::EndTest(BooleanParm verbose, BooleanParm)
  551. {
  552.     if (verbose)
  553.         Printf("INFO: End of Linked List test\n");
  554.     for (short idx = 0; idx < 100; ++idx)
  555.     {
  556.         TLink* link = fArray[idx];
  557.         delete link;
  558.     }
  559.     if (verbose)
  560.         Printf("INFO: Deleted 100 links\n");
  561. }
  562.