home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / PriorityQueue / queuetest.m < prev    next >
Text File  |  1992-11-10  |  3KB  |  138 lines

  1. /* NAME:
  2. **    QueueTest.m
  3. **
  4. **    COPYRIGHT 1992 BY ONYSCHUK AND ASSOCIATES
  5. **    ALL RIGHTS RESERVED.
  6. **
  7. ** REVISION HISTORY:
  8. **    Sun Aug 23 21:10:26 EDT 1992    Mark Onyschuk
  9. **                    Starting point
  10. **
  11. ** DESCRIPTION:
  12. **    Program to demonstrate and put the PriorityQueue class
  13. **    to the test.
  14. **
  15. ** DISCLAIMER:
  16. **    This is free software; you can redistribute it and/or modify
  17. **    it under the terms of the GNU General Public License as
  18. **    published by the Free Software Foundation; either version
  19. **    1, or (at your option) any later version.
  20. **
  21. **    This program is distributed in the hope that it will be
  22. **    useful, but WITHOUT ANY WARRANTY; without even the implied
  23. **    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  24. **    PURPOSE.  See the GNU General Public License for more
  25. **    details.
  26. **
  27. **    You should have received a copy of the GNU General Public
  28. **    License along with this program; if not, write to the Free
  29. **    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  30. **    02139, USA.
  31. */
  32.  
  33. #define NELEM    10
  34.  
  35. #import "PriorityQueue.h"
  36.  
  37. void prioritize(void)
  38. {
  39.     int            i;
  40.     
  41.     id            queue;
  42.  
  43.     id            obj;
  44.     unsigned int    priority;
  45.  
  46.     queue = [[PriorityQueue alloc] init];
  47.     
  48.     printf("Queueing and dequeueing Objects:\n\n");
  49.     
  50.     for (i = NELEM; i > 0; i--)
  51.     {
  52.          obj = [[Object alloc] init];
  53.      priority = random() % NELEM;
  54.      
  55.      printf("Queueing object %p with priority %d\n", obj, priority);
  56.      
  57.      [queue addObject:obj withPriority:priority];
  58.     }
  59.     
  60.     printf("\n");
  61.     
  62.     for (i = NELEM; i > 0; i--)
  63.     {
  64.         obj = [queue removeObject];
  65.     
  66.     printf("Dequeued object %p\n", obj);
  67.     }
  68. }
  69.  
  70.  
  71. void copyqueue(void)
  72. {
  73.     int            i;
  74.     
  75.     id            queue;
  76.     id            copyA;
  77.     id            copyB;
  78.     id            copyC;
  79.     id            copyD;
  80.     id            temp;
  81.  
  82.     id            obj;
  83.     unsigned int    priority;
  84.  
  85.     queue = [[PriorityQueue alloc] init];
  86.     
  87.     printf("Copying Objects:\n\n");
  88.     
  89.     for (i = NELEM; i > 0; i--)
  90.     {
  91.          obj = [[Object alloc] init];
  92.      priority = random() % NELEM;
  93.      
  94.      printf("Queueing object %p with priority %d\n", obj, priority);
  95.      
  96.      [queue addObject:obj withPriority:priority];
  97.     }
  98.     
  99.     printf("Copying queue to copyA, copyB, copyC, and copyD.\n");
  100.     copyA = [queue copy];
  101.     copyB = [queue copy];
  102.     copyC = [queue copy];
  103.     copyD = [queue copy];
  104.  
  105.     for (i = NELEM; i > 0; i--)
  106.     {
  107.     priority = [queue highestPriority];
  108.         obj      = [queue removeObject];
  109.     
  110.     printf("Dequeued object %p (orig.) with priority %d\n", obj, priority);
  111.     }
  112.  
  113.     for (i = NELEM; i > 0; i--)
  114.     {
  115.     priority = [copyA highestPriority];
  116.         obj      = [copyA removeObject];
  117.     
  118.     printf("Dequeued object %p (copyA) with priority %d\n", obj, priority);
  119.     }
  120.     
  121.     printf("copyB %s copyC\n", ([copyB isEqual:copyC]) ? "==" : "!=");
  122.     
  123.     
  124.     printf("removing last item from copyD\n");
  125.     [copyD removeObject];
  126.     
  127.     printf("copyC %s copyD\n", ([copyC isEqual:copyD]) ? "==" : "!=");
  128. }
  129.  
  130.  
  131. void main(void)
  132. {
  133.     prioritize();
  134.  
  135.     printf("\n");
  136.  
  137.     copyqueue();
  138. }