Skip to content
Browse files

Work on mem cache, add unit test

  • Loading branch information...
1 parent 01cb2f1 commit 2856bfaf4f72ee033644373b61fc41eae832c757 @clawoo committed Mar 23, 2011
View
1 Classes/RCLMemoryCache.m
@@ -28,6 +28,7 @@ - (id)init {
}
- (void)storeData:(NSData *)object forKeyPath:(NSString *)keyPath {
+ [self storeData:object forKeyPath:keyPath expires:[NSDate distantFuture]];
}
- (void)storeData:(NSData *)object forKeyPath:(NSString *)keyPath expires:(NSDate *)expirationDate {
View
6 RCL.xcodeproj/project.pbxproj
@@ -27,6 +27,7 @@
3B0868431315B8E10050B886 /* RCLAsyncDownloaderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B0868421315B8E10050B886 /* RCLAsyncDownloaderTest.m */; };
3B0868541315BD460050B886 /* RCLAsyncDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B918D8313032E4200776B29 /* RCLAsyncDownloader.m */; };
3B0868561315BD460050B886 /* RCLAsyncImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B918D8913032EA600776B29 /* RCLAsyncImageView.m */; };
+ 3B502695133A986F00F2A350 /* RCLMemoryCacheTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B502694133A986F00F2A350 /* RCLMemoryCacheTest.m */; };
3B8E32371316FB1200206157 /* RCLMemoryCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B8E32361316FB1200206157 /* RCLMemoryCache.m */; };
3B918D8413032E4200776B29 /* RCLAsyncDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B918D8313032E4200776B29 /* RCLAsyncDownloader.m */; };
3B918D8713032E7500776B29 /* RCLCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B918D8613032E7500776B29 /* RCLCache.m */; };
@@ -66,6 +67,8 @@
3B07FE4A1313265100CE0476 /* GTMUnitTestDevLog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GTMUnitTestDevLog.m; path = "../Frameworks/google-toolbox-for-mac/UnitTesting/GTMUnitTestDevLog.m"; sourceTree = SOURCE_ROOT; };
3B0868411315B8E10050B886 /* RCLAsyncDownloaderTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCLAsyncDownloaderTest.h; sourceTree = "<group>"; };
3B0868421315B8E10050B886 /* RCLAsyncDownloaderTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCLAsyncDownloaderTest.m; sourceTree = "<group>"; };
+ 3B502693133A986F00F2A350 /* RCLMemoryCacheTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCLMemoryCacheTest.h; sourceTree = "<group>"; };
+ 3B502694133A986F00F2A350 /* RCLMemoryCacheTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCLMemoryCacheTest.m; sourceTree = "<group>"; };
3B8E32351316FB1200206157 /* RCLMemoryCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCLMemoryCache.h; sourceTree = "<group>"; };
3B8E32361316FB1200206157 /* RCLMemoryCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCLMemoryCache.m; sourceTree = "<group>"; };
3B918D8213032E4200776B29 /* RCLAsyncDownloader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCLAsyncDownloader.h; sourceTree = "<group>"; };
@@ -205,6 +208,8 @@
3B07FE011313231900CE0476 /* RCLCacheTest.m */,
3B0868411315B8E10050B886 /* RCLAsyncDownloaderTest.h */,
3B0868421315B8E10050B886 /* RCLAsyncDownloaderTest.m */,
+ 3B502693133A986F00F2A350 /* RCLMemoryCacheTest.h */,
+ 3B502694133A986F00F2A350 /* RCLMemoryCacheTest.m */,
);
name = Tests;
sourceTree = "<group>";
@@ -351,6 +356,7 @@
3B07FE4B1313265100CE0476 /* GTMUnitTestDevLog.m in Sources */,
3B0868431315B8E10050B886 /* RCLAsyncDownloaderTest.m in Sources */,
3B8E32371316FB1200206157 /* RCLMemoryCache.m in Sources */,
+ 3B502695133A986F00F2A350 /* RCLMemoryCacheTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
6,044 RCL.xcodeproj/project.xcworkspace/xcuserdata/clw.xcuserdatad/UserInterfaceState.xcuserstate
3,397 additions, 2,647 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
15 RCLMemoryCacheTest.h
@@ -0,0 +1,15 @@
+//
+// RCLMemoryCacheTest.h
+// RCL
+//
+// Created by Clawoo on 3/23/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "GTMSenTestCase.h"
+
+@interface RCLMemoryCacheTest : GTMTestCase {
+
+}
+
+@end
View
64 RCLMemoryCacheTest.m
@@ -0,0 +1,64 @@
+//
+// RCLMemoryCacheTest.m
+// RCL
+//
+// Created by Clawoo on 3/23/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "RCLMemoryCacheTest.h"
+#import "RCLMemoryCache.h"
+
+@implementation RCLMemoryCacheTest
+
+- (void)testStorageAndRetrieval {
+ NSString *storedString = @"Hello world!";
+ NSString *keyPath = @"&Hd2h378734&$%Y*(&98";
+
+ [[RCLMemoryCache instance] storeData:[storedString dataUsingEncoding:NSUTF8StringEncoding]
+ forKeyPath:keyPath];
+
+ STAssertTrue([[RCLMemoryCache instance] objectAvailableForKeyPath:keyPath], @"Stored object cannot be found in cache");
+
+ NSData *retrievedObject = [[RCLMemoryCache instance] objectForKeyPath:keyPath];
+ STAssertNotNil(retrievedObject, @"Retrieved object is nil");
+
+ NSString *retrievedString = [[NSString alloc] initWithData:retrievedObject
+ encoding:NSUTF8StringEncoding];
+ STAssertEqualStrings(storedString, retrievedString, @"Stored string is different than the retrieved string");
+ [retrievedString release];
+}
+
+- (void)testStorageAndRemoval {
+ NSString *storedString = @"Hello world!";
+ NSString *keyPath = @"&Hd2h378734&$%Y*(&98";
+
+ [[RCLMemoryCache instance] storeData:[storedString dataUsingEncoding:NSUTF8StringEncoding]
+ forKeyPath:keyPath];
+
+ STAssertTrue([[RCLMemoryCache instance] objectAvailableForKeyPath:keyPath], @"Stored object cannot be found in cache");
+
+ [[RCLMemoryCache instance] removeObjectForKeyPath:keyPath];
+ STAssertFalse([[RCLMemoryCache instance] objectAvailableForKeyPath:keyPath], @"Stored object was not removed from cache");
+}
+
+- (void)testExpiredCache {
+ NSString *storedString = @"Hello world!";
+ NSString *keyPath = @"&Hd2h378734&$%Y*(&98";
+
+ [[RCLMemoryCache instance] storeData:[storedString dataUsingEncoding:NSUTF8StringEncoding]
+ forKeyPath:keyPath
+ expires:[NSDate dateWithTimeIntervalSinceNow:-1]];
+
+ STAssertFalse([[RCLMemoryCache instance] objectAvailableForKeyPath:keyPath], @"Stored object is still available in cache");
+
+ [[RCLMemoryCache instance] storeData:[storedString dataUsingEncoding:NSUTF8StringEncoding]
+ forKeyPath:keyPath
+ expires:[NSDate dateWithTimeIntervalSinceNow:1]];
+ STAssertTrue([[RCLMemoryCache instance] objectAvailableForKeyPath:keyPath], @"Stored object is not available in cache although not expired yet");
+ [NSThread sleepForTimeInterval:1];
+ // the object should have expired by now
+ STAssertFalse([[RCLMemoryCache instance] objectAvailableForKeyPath:keyPath], @"Stored object is still available in cache after expiration date");
+}
+
+@end

0 comments on commit 2856bfa

Please sign in to comment.
Something went wrong with that request. Please try again.