home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 10.9 KB | 464 lines | [TEXT/MPS ] |
- /*
- File: TestPriorityList.cp
-
- Contains: Tester for the Priority list class
-
- Copyright: © 1991-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #ifndef __TESTPRIORITYLIST__
- #include "TestPriorityList.h"
- #endif
-
- /**********************************************************************
- ** PUBLIC Constructor/Destructor
- ***********************************************************************/
-
- Constructor(PriorityList)
- Destructor(PriorityList)
-
- /**********************************************************************
- ** Test methods
- ***********************************************************************/
-
- void TTestPriorityList :: TestIsEmpty(Boolean correct) const
- {
- if (correct)
- {
- if (!fTest->IsEmpty())
- Printf("### ERROR: IsEmpty returned false when the list was not empty\n");
- }
- else
- {
- if (fTest->IsEmpty())
- Printf("### ERROR: IsEmpty returned true when the list was empty\n");
- }
- }
-
- void TTestPriorityList :: TestCount(size_t shouldBe) const
- {
- size_t is = fTest->Count();
- if (is != shouldBe)
- Printf("### ERROR: Count returned %u when it should have returned %u\n",
- is, shouldBe);
- }
-
- void TTestPriorityList :: TestFirstLast(short iteration, short first, short last) const
- {
- TNumber* ptr;
- TPriorityLink* link;
-
- if ((ptr = (TNumber*)fTest->First()) != NULL)
- {
- if (ptr->fNumber != first)
- if (first < 0)
- Printf("### ERROR: It#%hu: First returned #%hu when it should have returned NULL\n",
- iteration, ptr->fNumber);
- else
- Printf("### ERROR: It#%hu: First returned #%hu when it should have returned #%hu\n",
- iteration, ptr->fNumber, first);
- }
- else
- {
- if (first >= 0)
- Printf("### ERROR: It#%hu: First return NULL when it should have returned #%u\n",
- iteration, first);
- }
- if ((ptr = (TNumber*)fTest->Last()) != NULL)
- {
- if (ptr->fNumber != last)
- if (last < 0)
- Printf("### ERROR: It#%hu: Last returned #%hu when it should have returned NULL\n",
- iteration, ptr->fNumber);
- else
- Printf("### ERROR: It#%hu: Last returned #%hu when it should have returned #%hu\n",
- iteration, ptr->fNumber, last);
- }
- else
- {
- if (last >= 0)
- Printf("### ERROR: It#%hu: Last return NULL when it should have returned #%u\n",
- iteration, last);
- }
- if ((link = (TPriorityLink*)fTest->FirstLink()) != NULL)
- {
- ptr = (TNumber*)link->GetValue();
- if (ptr->fNumber != first)
- if (first < 0)
- Printf("### ERROR: It#%hu: FirstLink returned #%hu when it should have returned NULL\n",
- iteration, ptr->fNumber);
- else
- Printf("### ERROR: It#%hu: FirstLink returned #%hu when it should have returned #%hu\n",
- iteration, ptr->fNumber, first);
- if (first >= 0 && link->GetPriority() != first)
- Printf("### ERROR: It#%hu: FirstLink returned priority #%hu when it should have returned #%hu\n",
- iteration, link->GetPriority(), first);
- }
- else
- {
- if (first >= 0)
- Printf("### ERROR: It#%hu: FirstLink return NULL when it should have returned #%u\n",
- iteration, first);
- }
- if ((link = (TPriorityLink*)fTest->LastLink()) != NULL)
- {
- ptr = (TNumber*)link->GetValue();
- if (ptr->fNumber != last)
- if (last < 0)
- Printf("### ERROR: It#%hu: LastLink returned #%hu when it should have returned NULL\n",
- iteration, ptr->fNumber);
- else
- Printf("### ERROR: It#%hu: LastLink returned #%hu when it should have returned #%hu\n",
- iteration, ptr->fNumber, last);
- if (last >= 0 && link->GetPriority() != last)
- Printf("### ERROR: It#%hu: LastLink returned priority #%hu when it should have returned #%hu\n",
- iteration, link->GetPriority(), first);
- }
- else
- {
- if (last >= 0)
- Printf("### ERROR: It#%hu: LastLink return NULL when it should have returned #%u\n",
- iteration, last);
- }
- }
-
- void TTestPriorityList :: TestMember(short number, Boolean ifGone) const
- {
- TNumber obj(number);
- TNumber* ptr;
- TPriorityLink* link;
-
- if ((ptr = (TNumber*)fTest->Member(obj)) != NULL)
- {
- if (ifGone)
- Printf("### ERROR: Member returned #%hu when it should not be present\n",
- ptr->fNumber);
- else
- if (number != ptr->fNumber)
- Printf("### ERROR: Member returned #%hu when it should have returned #%hu\n",
- ptr->fNumber, number);
- }
- else
- if (!ifGone)
- Printf("### ERROR: Member returned no object when #%hu should have been present\n",
- number);
- if ((link = (TPriorityLink*)fTest->MemberLink(obj)) != NULL)
- {
- ptr = (TNumber*)link->GetValue();
- if (ifGone)
- Printf("### ERROR: MemberLink returned #%hu when it should not be present\n",
- ptr->fNumber);
- else
- {
- if (number != ptr->fNumber)
- Printf("### ERROR: MemberLink returned #%hu when it should have returned #%hu\n",
- ptr->fNumber, number);
- if (link->GetPriority() != ptr->fNumber)
- Printf("### ERROR: MemberLink returned priority #%hu when it should have returned #%hu\n",
- link->GetPriority(), number);
- }
- }
- else
- if (!ifGone)
- Printf("### ERROR: MemberLink returned no object when #%hu should have been present\n",
- number);
- }
-
- /**********************************************************************
- ** PUBLIC InitTest
- ***********************************************************************/
-
- void TTestPriorityList :: InitTest(BooleanParm verbose, BooleanParm, int, char**)
- {
- short idx;
-
- TStandardPool* pool = GetPool();
-
- for (idx = 0; idx < 100; ++idx)
- fArray[idx] = NULL;
-
- if (pool == NULL)
- {
- Printf("### ERROR: There is no global pool\n");
- return;
- }
-
- fTest = new (pool) TPriorityList;
-
- for (idx = 0; idx < 100; ++idx)
- {
- TPriorityLink* link;
- TNumber* num;
- if ((num = new (pool) TNumber(idx)) == NULL)
- {
- Printf("### ERROR: Out of Memory at #%d\n", idx);
- break;
- }
- if ((link = new (pool) TPriorityLink(num)) == NULL)
- {
- Printf("### ERROR: Out of Memory at #%d\n", idx);
- break;
- }
- link->SetPriority(idx);
- fArray[idx] = link;
- }
- if (idx == 100 && verbose)
- Printf("INFO: Allocated 100 links and objects\n");
- }
-
- /**********************************************************************
- ** PUBLIC RunTestIteration
- ***********************************************************************/
-
- void TTestPriorityList :: RunTestIteration(BooleanParm verbose, BooleanParm)
- {
- short matchFirst;
- short matchLast;
- short toMatch;
- short idx, jdx;
- TPriorityLink* link;
- TNumber* ptr;
- char* str;
-
- if (verbose)
- Printf("INFO: Testing EmptyList\n");
-
- TestIsEmpty(true);
- TestFirstLast(-1, -1, -1);
- TestCount(0);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, true);
-
- if (verbose)
- Printf("INFO: Testing AddLink, Member, Count, First, Last and IsEmpty\n");
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLink(link);
-
- TestIsEmpty(false);
- TestCount(idx + 1);
- TestFirstLast(idx, 0, idx);
-
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, jdx > idx);
- }
-
- TestIsEmpty(false);
- TestCount(100);
- TestFirstLast(-1, 0, 99);
-
- matchFirst = 0;
- matchLast = 99;
- if (verbose)
- Printf("INFO: Testing First, Last, RemoveFirst, RemoveLast, and Member methods\n");
-
- for (idx = 0; idx < 100; ++idx)
- {
- TestIsEmpty(false);
- TestCount(100 - idx);
- TestFirstLast(idx, matchFirst, matchLast);
-
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, jdx < matchFirst || jdx > matchLast);
-
- if (idx & 1)
- {
- link = (TPriorityLink*)fTest->RemoveFirstLink();
- toMatch = matchFirst;
- matchFirst += 1;
- str = "RemoveFirstLink";
- }
- else
- {
- link = (TPriorityLink*)fTest->RemoveLastLink();
- toMatch = matchLast;
- matchLast -= 1;
- str = "RemoveLastLink";
- }
- if (link == NULL)
- Printf("### ERROR: %s return no object, instead of #%hu\n",
- str, toMatch);
- else
- {
- ptr = (TNumber*)link->GetValue();
- if (ptr->fNumber != toMatch)
- Printf("### ERROR: %s return object #%hu, instead of #%hu\n",
- str, ptr->fNumber, toMatch);
- if (link->GetPriority() != toMatch)
- Printf("### ERROR: %s return priority #%hu, instead of #%hu\n",
- str, link->GetPriority(), toMatch);
- }
- }
-
- TestIsEmpty(true);
- TestFirstLast(-1, -1, -1);
- TestCount(0);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, true);
-
- if (verbose)
- Printf("INFO: Testing AddLink in Random order\n");
-
- fTest->RemoveAll(); // Just in case
-
- matchFirst = 0;
- matchLast = 99;
- for (idx = 0; idx < 100; ++idx)
- {
- if (idx & 1)
- link = fArray[matchFirst];
- else
- link = fArray[matchLast];
- fTest->AddLink(link);
-
- TestIsEmpty(false);
- TestCount(idx + 1);
- if (idx == 0)
- TestFirstLast(idx, 99, 99);
- else
- TestFirstLast(idx, 0, 99);
-
- for (jdx = 0; jdx < 100; ++jdx)
- if (idx & 1)
- TestMember(jdx, jdx > matchFirst && jdx <= matchLast);
- else
- TestMember(jdx, jdx >= matchFirst && jdx < matchLast);
- if (idx & 1)
- matchFirst += 1;
- else
- matchLast -= 1;
- }
-
- TestIsEmpty(false);
- TestCount(100);
- TestFirstLast(-1, 0, 99);
-
- if (verbose)
- Printf("INFO: Testing Remove by pointer\n");
-
- matchFirst = 49;
- matchLast = 50;
-
- for (idx = 0; idx < 100; ++idx)
- {
- if (idx & 1)
- jdx = matchFirst;
- else
- jdx = matchLast;
- link = fArray[jdx];
-
- TestIsEmpty(false);
- TestCount(100 - idx);
- if (idx != 99)
- TestFirstLast(idx, 0, 99);
- else
- TestFirstLast(idx, 0, 0);
-
- if (!fTest->Remove(link->GetValue()))
- Printf("ERROR: Remove Link #%u Failed\n", jdx);
-
- TestCount(99-idx);
- if (idx < 98)
- TestFirstLast(idx, 0, 99);
- else
- if (idx == 98)
- TestFirstLast(idx, 0, 0);
-
- if (idx & 1)
- matchFirst -= 1;
- else
- matchLast += 1;
-
- }
- if (verbose)
- Printf("INFO: Testing Remove by reference\n");
-
- fTest->RemoveAll(); // Just in case
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLink(link);
- }
-
- TestIsEmpty(false);
- TestCount(100);
- TestFirstLast(-1, 0, 99);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, false);
-
- matchFirst = 49;
- matchLast = 50;
-
- for (idx = 0; idx < 100; ++idx)
- {
- if (idx & 1)
- jdx = matchFirst;
- else
- jdx = matchLast;
- link = fArray[jdx];
-
- TestIsEmpty(false);
- TestCount(100 - idx);
- if (idx != 99)
- TestFirstLast(idx, 0, 99);
- else
- TestFirstLast(idx, 0, 0);
-
- if (!fTest->Remove(*(TNumber*)link->GetValue()))
- Printf("ERROR: Remove Link #%u Failed\n", jdx);
-
- TestCount(99-idx);
- if (idx < 98)
- TestFirstLast(idx, 0, 99);
- else
- if (idx == 98)
- TestFirstLast(idx, 0, 0);
-
- if (idx & 1)
- matchFirst -= 1;
- else
- matchLast += 1;
-
- }
-
- if (verbose)
- Printf("INFO: Testing DeleteAll\n");
-
- fTest->RemoveAll(); // Just in case
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLink(link);
- }
-
- fTest->DeleteAll(kTDynamicPointer);
-
- TestIsEmpty(true);
- TestCount(0);
- TestFirstLast(-1, -1, -1);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, true);
- }
-
- /**********************************************************************
- ** PUBLIC EndTest
- ***********************************************************************/
-
- void TTestPriorityList :: EndTest(BooleanParm verbose, BooleanParm)
- {
- if (verbose)
- Printf("INFO: End of Priority List test\n");
- for (short idx = 0; idx < 100; ++idx)
- {
- TPriorityLink* link = fArray[idx];
- delete link;
- }
- if (verbose)
- Printf("INFO: Deleted 100 links\n");
- }
-