|
|
@ -40,8 +40,16 @@ NSArray* Permutations(NSUInteger count) {
|
|
|
|
@implementation PriorityQueueTest
|
|
|
|
@implementation PriorityQueueTest
|
|
|
|
|
|
|
|
|
|
|
|
-(void) testTrivialPrioritizing {
|
|
|
|
-(void) testTrivialPrioritizing {
|
|
|
|
PriorityQueue* q = [PriorityQueue priorityQueueAscendingWithComparator:^(id obj1, id obj2){ return
|
|
|
|
PriorityQueue* q = [PriorityQueue priorityQueueAscendingWithComparator:^(id obj1, id obj2){
|
|
|
|
[obj1 intValue] - [obj2 intValue]; }];
|
|
|
|
int diff =[obj2 intValue] - [obj1 intValue];
|
|
|
|
|
|
|
|
if (diff > 0 ) {
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedAscending;
|
|
|
|
|
|
|
|
} else if (diff < 0){
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedDescending;
|
|
|
|
|
|
|
|
} else{
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedSame;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
test([q count] == 0);
|
|
|
|
test([q count] == 0);
|
|
|
|
testThrows([q peek]);
|
|
|
|
testThrows([q peek]);
|
|
|
|
testThrows([q dequeue]);
|
|
|
|
testThrows([q dequeue]);
|
|
|
@ -60,8 +68,17 @@ NSArray* Permutations(NSUInteger count) {
|
|
|
|
testThrows([q dequeue]);
|
|
|
|
testThrows([q dequeue]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
-(void) testOrdersByComparator {
|
|
|
|
-(void) testOrdersByComparator {
|
|
|
|
PriorityQueue* q = [PriorityQueue priorityQueueAscendingWithComparator:^(id obj1, id obj2){ return
|
|
|
|
PriorityQueue* q = [PriorityQueue priorityQueueAscendingWithComparator:^(id obj1, id obj2){
|
|
|
|
[obj2 intValue] - [obj1 intValue]; }];
|
|
|
|
int diff =[obj2 intValue] - [obj1 intValue];
|
|
|
|
|
|
|
|
if (diff > 0 ) {
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedAscending;
|
|
|
|
|
|
|
|
} else if (diff < 0){
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedDescending;
|
|
|
|
|
|
|
|
} else{
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedSame;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
[q enqueue:@1];
|
|
|
|
[q enqueue:@1];
|
|
|
|
[q enqueue:@2];
|
|
|
|
[q enqueue:@2];
|
|
|
|
[q enqueue:@3];
|
|
|
|
[q enqueue:@3];
|
|
|
@ -72,8 +89,16 @@ NSArray* Permutations(NSUInteger count) {
|
|
|
|
-(void) testSortsAllSmallPermutations {
|
|
|
|
-(void) testSortsAllSmallPermutations {
|
|
|
|
const NSUInteger N = 7;
|
|
|
|
const NSUInteger N = 7;
|
|
|
|
for (NSArray* permutation in Permutations(N)) {
|
|
|
|
for (NSArray* permutation in Permutations(N)) {
|
|
|
|
PriorityQueue* q = [PriorityQueue priorityQueueAscendingWithComparator:^(id obj1, id obj2){ return
|
|
|
|
PriorityQueue* q = [PriorityQueue priorityQueueAscendingWithComparator:^(id obj1, id obj2){
|
|
|
|
[obj1 intValue] - [obj2 intValue]; }];
|
|
|
|
int diff =[obj2 intValue] - [obj1 intValue];
|
|
|
|
|
|
|
|
if (diff > 0 ) {
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedAscending;
|
|
|
|
|
|
|
|
} else if (diff < 0){
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedDescending;
|
|
|
|
|
|
|
|
} else{
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedSame;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
for (NSNumber* e in permutation) {
|
|
|
|
for (NSNumber* e in permutation) {
|
|
|
|
[q enqueue:e];
|
|
|
|
[q enqueue:e];
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -90,8 +115,16 @@ NSArray* Permutations(NSUInteger count) {
|
|
|
|
const NSUInteger Size = 500;
|
|
|
|
const NSUInteger Size = 500;
|
|
|
|
const NSUInteger Repetitions = 50;
|
|
|
|
const NSUInteger Repetitions = 50;
|
|
|
|
for (NSUInteger repeat = 0; repeat < Repetitions; repeat++) {
|
|
|
|
for (NSUInteger repeat = 0; repeat < Repetitions; repeat++) {
|
|
|
|
PriorityQueue* q = [PriorityQueue priorityQueueAscendingWithComparator:^(id obj1, id obj2){ return
|
|
|
|
PriorityQueue* q = [PriorityQueue priorityQueueAscendingWithComparator:^(id obj1, id obj2){
|
|
|
|
[obj1 intValue] - [obj2 intValue]; }];
|
|
|
|
int diff =[obj2 intValue] - [obj1 intValue];
|
|
|
|
|
|
|
|
if (diff > 0 ) {
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedAscending;
|
|
|
|
|
|
|
|
} else if (diff < 0){
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedDescending;
|
|
|
|
|
|
|
|
} else{
|
|
|
|
|
|
|
|
return (NSComparisonResult)NSOrderedSame;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
NSArray* permutation = RandomPermutation(Size);
|
|
|
|
NSArray* permutation = RandomPermutation(Size);
|
|
|
|
for (NSNumber* e in permutation) {
|
|
|
|
for (NSNumber* e in permutation) {
|
|
|
|
[q enqueue:e];
|
|
|
|
[q enqueue:e];
|
|
|
|