~ubuntu-branches/ubuntu/wily/oolite/wily-proposed

« back to all changes in this revision

Viewing changes to tests/priorityQueue/PriorityQueueTest.m

  • Committer: Package Import Robot
  • Author(s): Nicolas Boulenguez
  • Date: 2011-12-22 00:22:39 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20111222002239-pr3upeupp4jw1psp
Tags: 1.76-1
* New upstream.
* watch: scan upstream stable releases instead of dev snapshots.
* control: use default gobjc instead of explicit 4.6.
* rules: use dpkg-dev build flags.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#import <Foundation/Foundation.h>
 
2
#import "OOPriorityQueue.h"
 
3
 
 
4
 
 
5
#if 0
 
6
#define DumpQueueState(queue)  NSLog(@"%@", [queue debugDescription])
 
7
#else
 
8
#define DumpQueueState(queue) do {} while (0)
 
9
#endif
 
10
 
 
11
 
 
12
#ifdef DEBUG_GRAPHVIZ
 
13
@interface OOPriorityQueue (DebugGraphViz)
 
14
- (void) writeGraphVizToPath:(NSString *)path;
 
15
@end
 
16
#endif
 
17
 
 
18
 
 
19
static void PutNumbersInQueue(unsigned count, OOPriorityQueue *queue);
 
20
static void PutStringsInQueue(OOPriorityQueue *queue);
 
21
static void DumpQueue(OOPriorityQueue *queue);
 
22
static void TestEquality(void);
 
23
 
 
24
 
 
25
int main (int argc, const char * argv[])
 
26
{
 
27
    NSAutoreleasePool   *pool = [[NSAutoreleasePool alloc] init];
 
28
        OOPriorityQueue         *queue = nil;
 
29
        unsigned                        i;
 
30
        
 
31
        srandomdev();
 
32
        queue = [[OOPriorityQueue alloc] init];
 
33
        
 
34
        for (i = 0; i != 25; ++i)
 
35
        {
 
36
                NSLog(@"%u numbers:", i);
 
37
                PutNumbersInQueue(i, queue);
 
38
#ifdef DEBUG_GRAPHVIZ
 
39
                [queue writeGraphVizToPath:[NSString stringWithFormat:@"pqtest_int_%u.dot", i]];
 
40
#endif
 
41
                DumpQueue(queue);
 
42
                NSLog(@"\n");
 
43
        }
 
44
        
 
45
        PutStringsInQueue(queue);
 
46
#ifdef DEBUG_GRAPHVIZ
 
47
        [queue writeGraphVizToPath:@"pqtest_string.dot"];
 
48
#endif
 
49
        DumpQueue(queue);
 
50
        
 
51
        TestEquality();
 
52
        
 
53
    [pool release];
 
54
    return 0;
 
55
}
 
56
 
 
57
 
 
58
static void DumpQueue(OOPriorityQueue *queue)
 
59
{
 
60
#if 0
 
61
        id                                      value = nil;
 
62
        
 
63
        while ((value = [queue nextObject]))
 
64
        {
 
65
                DumpQueueState(queue);
 
66
                NSLog(@"%@", value);
 
67
        }
 
68
#else
 
69
        NSArray                         *values = nil;
 
70
        NSArray                         *resorted = nil;
 
71
        
 
72
        values = [queue sortedObjects];
 
73
        NSLog(@"%@", values);
 
74
        
 
75
        resorted = [values sortedArrayUsingSelector:@selector(compare:)];
 
76
        if (![values isEqual:resorted])
 
77
        {
 
78
                NSLog(@"FAILED - out of order. Correct order is: %@", resorted);
 
79
        }
 
80
#endif
 
81
}
 
82
 
 
83
 
 
84
static void PutNumbersInQueue(unsigned count, OOPriorityQueue *queue)
 
85
{
 
86
        while (count--)
 
87
        {
 
88
                [queue addObject:[NSNumber numberWithLong:random() % 100]];
 
89
                DumpQueueState(queue);
 
90
        }
 
91
}
 
92
 
 
93
 
 
94
static void PutStringsInQueue(OOPriorityQueue *queue)
 
95
{
 
96
        NSArray *array = [NSArray arrayWithObjects: @"dog", @"cat", @"apple", @"zebra", @"spanner", @"cat", nil];
 
97
        [queue addObjects:array];
 
98
        DumpQueueState(queue);
 
99
}
 
100
 
 
101
 
 
102
static void TestEquality(void)
 
103
{
 
104
        BOOL            OK = YES;
 
105
        
 
106
        // Note: permuations of the same objects.
 
107
        NSArray *array1 = [NSArray arrayWithObjects: @"dog", @"cat", @"apple", @"zebra", @"spanner", @"cat", nil];
 
108
        NSArray *array2 = [NSArray arrayWithObjects: @"apple", @"cat", @"dog", @"zebra", @"cat", @"spanner", nil];
 
109
        OOPriorityQueue *q1 = [[OOPriorityQueue alloc] init];
 
110
        OOPriorityQueue *q2 = [[OOPriorityQueue alloc] init];
 
111
        
 
112
        [q1 addObjects:array1];
 
113
        [q2 addObjects:array2];
 
114
        if (![q1 isEqual:q2])  OK = NO;
 
115
        
 
116
        [q2 addObject:@"snake"];
 
117
        if ([q1 isEqual:q2])  OK = NO;
 
118
        
 
119
        [q2 removeObject:@"snake"];
 
120
        if (![q1 isEqual:q2])  OK = NO;
 
121
        
 
122
        NSLog(@"Equality test %@", OK ? @"passed" : @"FAILED");
 
123
        
 
124
        [q1 release];
 
125
        [q2 release];
 
126
}