/
RCLMemoryCacheTest.m
73 lines (56 loc) · 3.23 KB
/
RCLMemoryCacheTest.m
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
//
// 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");
}
- (void)testEmptyCache {
NSString *storedString = @"Hello world!";
NSString *keyPath = @"&Hd2h378734&$%Y*(&98";
[[RCLMemoryCache instance] storeData:[storedString dataUsingEncoding:NSUTF8StringEncoding]
forKeyPath:keyPath];
[[RCLMemoryCache instance] removeAllObjects];
STAssertFalse([[RCLMemoryCache instance] objectAvailableForKeyPath:keyPath], @"Stored object is still available in cache after cache was emptied");
}
@end