Skip to content

Commit 7e66428

Browse files
committed
Added CCCache test
1 parent d734ea8 commit 7e66428

File tree

3 files changed

+159
-12
lines changed

3 files changed

+159
-12
lines changed

cocos2d-tests-ios.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0E28FE1A197FCE4500F78989 /* CCCacheTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E28FE19197FCE4500F78989 /* CCCacheTest.m */; };
1011
75556A04185636F100ED1B0F /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 75556A03185636F100ED1B0F /* XCTest.framework */; };
1112
75556A05185636F100ED1B0F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B7E2605717E7D278007067F0 /* Foundation.framework */; };
1213
75556A06185636F100ED1B0F /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B7E2605517E7D278007067F0 /* UIKit.framework */; };
@@ -107,6 +108,7 @@
107108
/* End PBXContainerItemProxy section */
108109

109110
/* Begin PBXFileReference section */
111+
0E28FE19197FCE4500F78989 /* CCCacheTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCCacheTest.m; path = "cocos2d-ui-tests/tests/CCCacheTest.m"; sourceTree = SOURCE_ROOT; };
110112
755569E31856361100ED1B0F /* CCFileUtilTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCFileUtilTests.m; sourceTree = "<group>"; };
111113
755569E41856361100ED1B0F /* CCNodeTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCNodeTests.m; sourceTree = "<group>"; };
112114
755569E51856361100ED1B0F /* CCPhysicsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCPhysicsTests.m; sourceTree = "<group>"; };
@@ -354,6 +356,7 @@
354356
B71B087F17EA5B500082EBC0 /* Tests */ = {
355357
isa = PBXGroup;
356358
children = (
359+
0E28FE19197FCE4500F78989 /* CCCacheTest.m */,
357360
D2B4894B1917EE6700C3443A /* CCEffectsTest.m */,
358361
D3D6CF5E18BD5F0500A51531 /* CCRendererTest.m */,
359362
D3870C5F18B440150033D885 /* SpritePerformanceTest.m */,
@@ -656,6 +659,7 @@
656659
B7EE697918186D5200B983FE /* CCTextFieldTest.m in Sources */,
657660
D29698121926DF7500BBB097 /* CCEffectPongTest.m in Sources */,
658661
B7E2620017E7D321007067F0 /* AppDelegate.m in Sources */,
662+
0E28FE1A197FCE4500F78989 /* CCCacheTest.m in Sources */,
659663
B77060AB1832E3310043CC67 /* CCTextureCacheTest.m in Sources */,
660664
A664A4EF18A3D9B8006184B8 /* PositioningTest.m in Sources */,
661665
B7E2621C17E7D370007067F0 /* main.m in Sources */,

cocos2d-ui-tests/tests/CCCacheTest.m

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#import "TestBase.h"
2+
#import "CCCache.h"
3+
4+
// -----------------------------------------------------------------
5+
// Simple demo cache, holding an allocated array
6+
// -----------------------------------------------------------------
7+
8+
@interface DemoCache : CCCache @end
9+
10+
@implementation DemoCache
11+
12+
- (id)createSharedDataForKey:(id<NSCopying>)key
13+
{
14+
unsigned char* rawData = malloc(255);
15+
CCLOG(@"Data created for key: %@", key);
16+
return([NSValue valueWithPointer:rawData]);
17+
}
18+
19+
- (id)createPublicObjectForSharedData:(id)data
20+
{
21+
return([NSValue valueWithNonretainedObject:data]);
22+
}
23+
24+
- (void)disposeOfSharedData:(id)data
25+
{
26+
free([((NSValue *)data) pointerValue]);
27+
}
28+
29+
- (void)logCache
30+
{
31+
for (unsigned char letter = 'A'; letter < 'X'; letter ++)
32+
{
33+
NSString *key = [NSString stringWithFormat:@"object%c", letter];
34+
if ([self keyExists:key])
35+
{
36+
NSValue *value = [self rawObjectForKey:key];
37+
if (value) CCLOG(@"%@", key); else CCLOG(@"%@ (unused)", key);
38+
}
39+
}
40+
}
41+
42+
@end
43+
44+
// -----------------------------------------------------------------
45+
// Cache test
46+
// -----------------------------------------------------------------
47+
48+
@interface CCCacheTest : TestBase @end
49+
50+
// -----------------------------------------------------------------
51+
52+
@implementation CCCacheTest
53+
{
54+
DemoCache *_cache;
55+
int _testStep;
56+
NSValue *_A;
57+
NSValue *_B;
58+
}
59+
60+
- (NSArray*) testConstructors
61+
{
62+
return [NSArray arrayWithObjects:
63+
@"cacheTest",
64+
nil];
65+
}
66+
67+
- (void)cacheTest
68+
{
69+
self.subTitle = @"Cache Test";
70+
71+
CCButton *button = [CCButton buttonWithTitle:@"[ Step test]" fontName:@"Arial" fontSize:18];
72+
button.positionType = CCPositionTypeNormalized;
73+
button.position = ccp(0.5, 0.5);
74+
[button setTarget:self selector:@selector(stepClicked:)];
75+
[self.contentNode addChild:button];
76+
77+
_testStep = 0;
78+
79+
_cache = [DemoCache cache];
80+
81+
[_cache preload:@"objectA"];
82+
[_cache preload:@"objectA"]; // same object as above
83+
[_cache preload:@"objectB"];
84+
[_cache preload:@"objectC"];
85+
}
86+
87+
- (void)stepClicked:(id)sender
88+
{
89+
CCLOG(@"---------------------------------------------------");
90+
switch (_testStep) {
91+
case 0:
92+
CCLOG(@"-- All cached objects should be unused");
93+
break;
94+
95+
case 1:
96+
CCLOG(@"-- objectA in use");
97+
_A = [_cache objectForKey:@"objectA"];
98+
break;
99+
100+
case 2:
101+
CCLOG(@"-- objectB in use");
102+
_B = [_cache objectForKey:@"objectB"];
103+
break;
104+
105+
case 3:
106+
CCLOG(@"-- Flush, and thus remove objectC");
107+
[_cache flush];
108+
break;
109+
110+
case 4:
111+
CCLOG(@"-- objectA not in use anymore");
112+
_A = nil;
113+
break;
114+
115+
case 5:
116+
CCLOG(@"-- Flush, and thus only leaving objectB");
117+
[_cache flush];
118+
break;
119+
120+
case 6:
121+
CCLOG(@"-- New objectA should be created");
122+
_A = [_cache objectForKey:@"objectA"];
123+
break;
124+
125+
case 7:
126+
CCLOG(@"-- No new objectB should be created");
127+
[_cache flush];
128+
_B = [_cache objectForKey:@"objectB"];
129+
break;
130+
131+
default:
132+
CCLOG(@"End of test!");
133+
_testStep = -1;
134+
break;
135+
}
136+
[_cache logCache];
137+
_testStep ++;
138+
}
139+
140+
141+
// -----------------------------------------------------------------
142+
143+
@end

cocos2d/CCCache.m

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,28 @@ - (CCCacheEntry *)entryForKey:(id<NSCopying>)key
8484
CCCacheEntry *entry = [_cacheList objectForKey:key];
8585

8686
if (entry == nil)
87-
{
88-
// Create the cached entry with the shared data.
87+
{
88+
// Create the cached entry with the shared data.
8989
entry = [[CCCacheEntry alloc] init];
90-
entry.sharedData = [self createSharedDataForKey:key];
91-
90+
entry.sharedData = [self createSharedDataForKey:key];
91+
9292
[_cacheList setObject:entry forKey:key];
9393
}
94-
95-
return entry;
94+
95+
return entry;
9696
}
9797

9898
- (id)objectForKey:(id<NSCopying>)key
9999
{
100100
CCCacheEntry *entry = [self entryForKey:key];
101101

102-
id object = entry.publicObject;
103-
if (object == nil)
104-
{
102+
id object = entry.publicObject;
103+
if (object == nil)
104+
{
105105
// Create the public object from the shared data.
106-
object = entry.publicObject = [self createPublicObjectForSharedData:entry.sharedData];
107-
}
108-
106+
object = entry.publicObject = [self createPublicObjectForSharedData:entry.sharedData];
107+
}
108+
109109
return object;
110110
}
111111

0 commit comments

Comments
 (0)