Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
Sources/CollectionClassesTest.cp
/* |
File: CollectionClassesTest.cp |
Contains: The following collection classes are implemented: TLinkedList, TStack, (TQueue, TDeQueue), |
THashTable. |
CollectionClasses.cp contains the collection class member functions. |
Written by: Kent Sandvik |
Copyright: Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved. |
You may incorporate this Apple sample source code into your program(s) without |
restriction. This Apple sample source code has been provided "AS IS" and the |
responsibility for its operation is yours. You are not permitted to redistribute |
this Apple sample source code as "Apple sample source code" after having made |
changes. If you're going to re-distribute the source, we require that you make |
it clear in the source that the code was descended from Apple sample source |
code, but that you've made changes. |
Change History (most recent first): |
8/18/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1 |
*/ |
#ifndef _COLLECTION |
#include "CollectionClasses.h" |
#endif |
// FLAGS that will enable/disable tests |
//#define TLINKEDLIST |
//#define TSTACK |
//#define TQUEUE |
#define TDEQUE |
//#define THASHTABLE |
void DoSomething(THashEntryPtr p); // our MapCar test function |
void main(void) |
{ |
cout << "Start of collection class testsÉ\n"; |
// _________________________________________________________________________________________________________ // |
// TLINKEDLIST TEST |
#ifdef TLINKEDLIST |
cout << "\nTest1: TLinkedList testÉ\n"; |
// Create a TLinkedList |
TLinkedList myList; |
// Append elements to the list |
myList.Append(1); |
myList.Append(2); |
myList.Append(3); |
myList.Append(4); |
// Show values in list |
myList.Reset(); |
long val; |
while ((val = myList.Next()) != NULL) |
cout << "TLinkedList Entry = " << val << "\n"; |
// Show first entry |
cout << "First entry = " << myList.First() << "\n"; |
// Show last entry |
cout << "Last entry = " << myList.Last() << "\n"; |
// Try to find entry |
cout << "Trying to find entry 3É\n"; |
Boolean OK = myList.Find(3); |
if (OK) |
cout << "We found 3, OK!\n"; |
else |
cout << "Ouch, problems, time to debugÉ"; |
// Remove two entries, 2 and 4 |
myList.Remove(2); |
myList.Remove(4); |
cout << "Removed 2 and 4, list looks now like:\n"; |
myList.Reset(); |
while ((val = myList.Next()) != NULL) |
cout << "TLinkedList Entry = " << val << "\n"; |
#endif |
// _________________________________________________________________________________________________________ // |
// TSTACK TEST |
#ifdef TSTACK |
// TSTACK TEST PHASE |
cout << "Test2: test TStack:\n"; |
TStack myStack; |
myStack.Push(1 L); |
myStack.Push(2 L); |
myStack.Push(3 L); |
myStack.Push(4 L); |
myStack.Push(5 L); |
// test our linked list properties |
cout << "First entry = " << myStack.First() << "\n"; |
cout << "Last entry = " << myStack.Last() << "\n"; |
// test our next loop |
myStack.Reset(); |
long val; |
while ((val = myStack.Next()) != NULL) |
cout << "Entry = " << val << "\n"; |
// test Find |
long item1 = 4 L; |
long item2 = 44 L; |
Boolean outcome; |
outcome = myStack.Find(item1); |
if (outcome) |
cout << "We have item1 in queue\n"; // should trigger |
else |
cout << "We don't have item1 in queue\n"; |
outcome = myStack.Find(item2); |
if (outcome) |
cout << "We have item2 in queue\n"; |
else |
cout << "We don't have item2 in queue\n"; // should trigger |
// test out stack properties |
while (!myStack.IsEmpty()) |
cout << "Popping from the stack, value = " << myStack.Pop() << "\n"; |
cout << "End of TStack test!\n"; |
#endif |
// _________________________________________________________________________________________________________ // |
// TQUEUE TEST |
#ifdef TQUEUE |
cout << "\nTest3: TQueue testÉ\n"; |
// Create TQueue |
TQueue myQueue; |
// Place stuff in the queue |
myQueue.Put(10); |
myQueue.Put(20); |
myQueue.Put(30); |
myQueue.Put(40); |
// Test out .Last() |
cout << "Last, or actually the first pushed entry is = " << myQueue.Last() << "\n"; |
// get out 2 more values |
cout << " Get = " << myQueue.Get(); |
cout << " Get = " << myQueue.Get(); |
cout << "\nI will get out two more values, and the next last value is 30 = " << myQueue.Last() << "\n"; |
// Get Stuff from queue |
while (!myQueue.IsEmpty()) |
cout << "Get values from the queue, value = " << myQueue.Get() << "\n"; |
#endif |
// _________________________________________________________________________________________________________ // |
// TDEQUE TEST |
#ifdef TDEQUE |
cout << "\nTest4: TDeque testÉ\n"; |
// Create TDeque |
TDeque myDeque; |
// Place stuff at end of deque |
myDeque.Push(11); |
myDeque.Push(12); |
myDeque.Push(13); |
myDeque.Push(14); |
myDeque.PutAtEnd(21); |
myDeque.PutAtEnd(22); |
cout << "Pop (14) = " << myDeque.Pop() << "\n"; |
cout << "Pop (13) = " << myDeque.Pop() << "\n"; |
cout << "Get (22) = " << myDeque.Get() << "\n"; |
cout << "The list should now look like: 21 11 12\n"; |
// empty the deque |
while (!myDeque.IsEmpty()) |
cout << "Get values from the dequeue, value = " << myDeque.Get() << "\n"; |
#endif |
// _________________________________________________________________________________________________________ // |
// THASHTABLE TEST |
#ifdef THASHTABLE |
// THASHTABLE TEST PHASE |
cout << "\nTest6: THashTable testÉ\n"; |
// Create Hashtable. |
THashTable myHashTable; |
// Add entries to the Hashtable. |
myHashTable.Add(4, 100); |
myHashTable.Add(5, 400); |
myHashTable.Add(6, 500); |
// Find entry/value in HashTable |
long tableValue = myHashTable.Find(4); |
cout << "The value is " << tableValue << ", and should be 100\n"; |
// remove this entry |
myHashTable.Remove(4); |
// now try to find it, and signal we had problems |
TItemtype entry = myHashTable.Find(4); |
if (entry == 0) |
cout << "THashTable.Remove() seems to work fine!\n"; |
else |
cout << "Eh, a value, and we just deleted the entry, start debugging THashTable.Remove()!\n"; |
// MapCar test |
myHashTable.MapCar((MapFun)DoSomething); |
cout << "End of THashTable testÉ\n"; |
#endif |
cout << "End of collection class tests!\n"; |
} |
// This function is needed for the THashTable.MapCar test |
void DoSomething(THashEntryPtr p) |
{ |
// print out the value inside each THashEntryPtrÉ |
cout << "Hash Table entry = " << p->fValue << "\n"; |
} |
// _________________________________________________________________________________________________________ // |
/* Change History (most recent last): |
No Init. Date Comment |
1 khs 11/7/92 New file |
2 khs 11/28/92 Added TLinkedList |
3 khs 11/29/92 Added TQueue and TDeque |
4 khs 1/14/93 Cleanup |
*/ |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14