Skip to content

Instantly share code, notes, and snippets.

@alexbasson
Last active August 29, 2015 14:05
Show Gist options
  • Save alexbasson/7f4f7af1ff95ef3fdb80 to your computer and use it in GitHub Desktop.
Save alexbasson/7f4f7af1ff95ef3fdb80 to your computer and use it in GitHub Desktop.
Simple 1-1 mapping between `NSIndexPath`s and `NSInteger`s.
#import <Foundation/Foundation.h>
@interface Cantor : NSObject
- (NSInteger)tagForIndexPath:(NSIndexPath *)indexPath;
- (NSIndexPath *)indexPathForTag:(NSInteger)tag;
@end
NSInteger kSectionPrime = 3;
NSInteger kRowPrime = 2;
@implementation Cantor
- (NSInteger)tagForIndexPath:(NSIndexPath *)indexPath {
return powl(kSectionPrime, indexPath.section) * powl(kRowPrime, indexPath.row);
}
- (NSIndexPath *)indexPathForTag:(NSInteger)tag {
NSInteger section = 0;
NSInteger row = 0;
while (tag % kSectionPrime == 0) {
section += 1;
tag /= kSectionPrime;
}
while (tag % kRowPrime == 0) {
row += 1;
tag /= kRowPrime;
}
return [NSIndexPath indexPathForRow:row inSection:section];
}
@end
#import "Cantor.h"
using namespace Cedar::Matchers;
using namespace Cedar::Doubles;
SPEC_BEGIN(CantorSpec)
NSInteger sectionMax = 10;
NSInteger rowMax = 60;
describe(@"Cantor", ^{
__block Cantor *cantor;
__block NSIndexPath *originalIndexPath;
__block NSInteger tag;
__block NSInteger section;
__block NSInteger row;
NSInteger(^randomIntUnder)(NSInteger range) = ^NSInteger(NSInteger range) {
return abs(rand()%range);
};
beforeEach(^{
srand(time(NULL));
section = randomIntUnder(sectionMax);
row = randomIntUnder(rowMax);
originalIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
cantor = [[Cantor alloc] init];
});
it(@"should generate the same tag for the same index path", ^{
NSIndexPath *identicalIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
[cantor tagForIndexPath:identicalIndexPath] should equal([cantor tagForIndexPath:originalIndexPath]);
});
it(@"should generate different tags for different index paths", ^{
context(@"section is different", ^{
NSInteger differentSection = randomIntUnder(sectionMax);
while (differentSection == section) { differentSection = randomIntUnder(sectionMax); }
NSIndexPath *differentIndexPath = [NSIndexPath indexPathForRow:row inSection:differentSection];
[cantor tagForIndexPath:differentIndexPath] should_not equal([cantor tagForIndexPath:originalIndexPath]);
});
context(@"row is different", ^{
NSInteger differentRow = randomIntUnder(rowMax);
while (differentRow == row) { differentRow = randomIntUnder(rowMax); }
NSIndexPath *differentIndexPath = [NSIndexPath indexPathForRow:differentRow inSection:section];
[cantor tagForIndexPath:differentIndexPath] should_not equal([cantor tagForIndexPath:originalIndexPath]);
});
});
it(@"should restore the index path", ^{
tag = [cantor tagForIndexPath:originalIndexPath];
NSIndexPath *restoredIndexPath = [cantor indexPathForTag:tag];
restoredIndexPath should equal(originalIndexPath);
});
});
SPEC_END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment