File: PriorityQueueTest.m

package info (click to toggle)
oolite 1.77.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,264 kB
  • ctags: 5,362
  • sloc: objc: 132,090; ansic: 10,457; python: 2,225; sh: 1,325; makefile: 332; perl: 259; xml: 125; php: 5
file content (126 lines) | stat: -rw-r--r-- 2,777 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#import <Foundation/Foundation.h>
#import "OOPriorityQueue.h"


#if 0
#define DumpQueueState(queue)  NSLog(@"%@", [queue debugDescription])
#else
#define DumpQueueState(queue) do {} while (0)
#endif


#ifdef DEBUG_GRAPHVIZ
@interface OOPriorityQueue (DebugGraphViz)
- (void) writeGraphVizToPath:(NSString *)path;
@end
#endif


static void PutNumbersInQueue(unsigned count, OOPriorityQueue *queue);
static void PutStringsInQueue(OOPriorityQueue *queue);
static void DumpQueue(OOPriorityQueue *queue);
static void TestEquality(void);


int main (int argc, const char * argv[])
{
    NSAutoreleasePool	*pool = [[NSAutoreleasePool alloc] init];
	OOPriorityQueue		*queue = nil;
	unsigned			i;
	
	srandomdev();
	queue = [[OOPriorityQueue alloc] init];
	
	for (i = 0; i != 25; ++i)
	{
		NSLog(@"%u numbers:", i);
		PutNumbersInQueue(i, queue);
#ifdef DEBUG_GRAPHVIZ
		[queue writeGraphVizToPath:[NSString stringWithFormat:@"pqtest_int_%u.dot", i]];
#endif
		DumpQueue(queue);
		NSLog(@"\n");
	}
	
	PutStringsInQueue(queue);
#ifdef DEBUG_GRAPHVIZ
	[queue writeGraphVizToPath:@"pqtest_string.dot"];
#endif
	DumpQueue(queue);
	
	TestEquality();
	
    [pool release];
    return 0;
}


static void DumpQueue(OOPriorityQueue *queue)
{
#if 0
	id					value = nil;
	
	while ((value = [queue nextObject]))
	{
		DumpQueueState(queue);
		NSLog(@"%@", value);
	}
#else
	NSArray				*values = nil;
	NSArray				*resorted = nil;
	
	values = [queue sortedObjects];
	NSLog(@"%@", values);
	
	resorted = [values sortedArrayUsingSelector:@selector(compare:)];
	if (![values isEqual:resorted])
	{
		NSLog(@"FAILED - out of order. Correct order is: %@", resorted);
	}
#endif
}


static void PutNumbersInQueue(unsigned count, OOPriorityQueue *queue)
{
	while (count--)
	{
		[queue addObject:[NSNumber numberWithLong:random() % 100]];
		DumpQueueState(queue);
	}
}


static void PutStringsInQueue(OOPriorityQueue *queue)
{
	NSArray *array = [NSArray arrayWithObjects: @"dog", @"cat", @"apple", @"zebra", @"spanner", @"cat", nil];
	[queue addObjects:array];
	DumpQueueState(queue);
}


static void TestEquality(void)
{
	BOOL		OK = YES;
	
	// Note: permuations of the same objects.
	NSArray *array1 = [NSArray arrayWithObjects: @"dog", @"cat", @"apple", @"zebra", @"spanner", @"cat", nil];
	NSArray *array2 = [NSArray arrayWithObjects: @"apple", @"cat", @"dog", @"zebra", @"cat", @"spanner", nil];
	OOPriorityQueue	*q1 = [[OOPriorityQueue alloc] init];
	OOPriorityQueue	*q2 = [[OOPriorityQueue alloc] init];
	
	[q1 addObjects:array1];
	[q2 addObjects:array2];
	if (![q1 isEqual:q2])  OK = NO;
	
	[q2 addObject:@"snake"];
	if ([q1 isEqual:q2])  OK = NO;
	
	[q2 removeObject:@"snake"];
	if (![q1 isEqual:q2])  OK = NO;
	
	NSLog(@"Equality test %@", OK ? @"passed" : @"FAILED");
	
	[q1 release];
	[q2 release];
}