forked from PrimaryFeather/Sparrow-Framework
/
SPUtilsTest.m
149 lines (115 loc) · 5.14 KB
/
SPUtilsTest.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// SPUtilsTest.m
// Sparrow
//
// Created by Daniel Sperl on 04.01.11.
// Copyright 2011 Gamua. All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the Simplified BSD License.
//
#import <Availability.h>
#ifdef __IPHONE_3_0
#import <SenTestingKit/SenTestingKit.h>
#import "SPUtils.h"
#import "SPNSExtensions.h"
// -------------------------------------------------------------------------------------------------
@interface SPUtilsTest : SenTestCase
@end
// -------------------------------------------------------------------------------------------------
@implementation SPUtilsTest
- (void)testGetNextPowerOfTwo
{
STAssertEquals(1, [SPUtils nextPowerOfTwo:0], @"wrong power of two");
STAssertEquals(1, [SPUtils nextPowerOfTwo:1], @"wrong power of two");
STAssertEquals(2, [SPUtils nextPowerOfTwo:2], @"wrong power of two");
STAssertEquals(4, [SPUtils nextPowerOfTwo:3], @"wrong power of two");
STAssertEquals(4, [SPUtils nextPowerOfTwo:4], @"wrong power of two");
STAssertEquals(8, [SPUtils nextPowerOfTwo:5], @"wrong power of two");
STAssertEquals(8, [SPUtils nextPowerOfTwo:6], @"wrong power of two");
STAssertEquals(256, [SPUtils nextPowerOfTwo:129], @"wrong power of two");
STAssertEquals(256, [SPUtils nextPowerOfTwo:255], @"wrong power of two");
STAssertEquals(256, [SPUtils nextPowerOfTwo:256], @"wrong power of two");
}
- (void)testGetRandomFloat
{
for (int i=0; i<20; ++i)
{
float rnd = [SPUtils randomFloat];
STAssertTrue(rnd >= 0.0f, @"random number too small");
STAssertTrue(rnd < 1.0f, @"random number too big");
}
}
- (void)testGetRandomInt
{
for (int i=0; i<20; ++i)
{
int rnd = [SPUtils randomIntBetweenMin:5 andMax:10];
STAssertTrue(rnd >= 5, @"random number too small");
STAssertTrue(rnd < 10, @"random number too big");
}
}
- (void)testFileExistsAtPath
{
NSString *absolutePath = [[NSBundle appBundle] pathForResource:@"pvrtc_image.pvr"];
BOOL fileExists = [SPUtils fileExistsAtPath:absolutePath];
STAssertTrue(fileExists, @"resource file not found");
fileExists = [SPUtils fileExistsAtPath:@"/tmp/some_non_existing_file.foo"];
STAssertFalse(fileExists, @"found non-existing file");
NSString *folder = [absolutePath stringByDeletingLastPathComponent];
BOOL folderExists = [SPUtils fileExistsAtPath:folder];
STAssertTrue(folderExists, @"folder not found");
}
- (void)testAbsolutePathToFile
{
NSString *absolutePath1x = [SPUtils absolutePathToFile:@"pvrtc_image.pvr"];
NSString *absolutePath2x = [SPUtils absolutePathToFile:@"pvrtc_image.pvr" withScaleFactor:2.0f];
STAssertNotNil(absolutePath1x, @"resource not found (1x)");
STAssertNotNil(absolutePath2x, @"resource not found (2x)");
uint suffixLoc = [absolutePath2x rangeOfString:@"@2x.pvr"].location;
STAssertEquals(suffixLoc, absolutePath2x.length - 7, @"did not find correct resource (2x)");
NSString *nonexistingPath = [SPUtils absolutePathToFile:@"does_not_exist.foo"];
STAssertNil(nonexistingPath, @"found non-existing file");
nonexistingPath = [SPUtils absolutePathToFile:@"does_not_exist@2x.foo"];
STAssertNil(nonexistingPath, @"found non-existing file");
NSString *nilPath = [SPUtils absolutePathToFile:nil];
STAssertNil(nilPath, @"found nil-path");
nilPath = [SPUtils absolutePathToFile:nil withScaleFactor:2.0f];
STAssertNil(nilPath, @"found nil-path (2x)");
}
- (void)testIdiom
{
NSString *filename = @"image_idiom.png";
NSString *absolutePath = [SPUtils absolutePathToFile:filename withScaleFactor:1.0f
idiom:UIUserInterfaceIdiomPhone];
STAssertTrue([absolutePath hasSuffix:@"image_idiom~iphone.png"], @"idiom image not found");
}
- (void)testScaledIdiom
{
NSString *filename = @"image_idiom.png";
NSString *absolutePath = [SPUtils absolutePathToFile:filename withScaleFactor:2.0f
idiom:UIUserInterfaceIdiomPhone];
STAssertTrue([absolutePath hasSuffix:@"image_idiom@2x~iphone.png"], @"idiom image not found");
}
- (void)testGetSdTextureFallback
{
NSString *filename = @"image_only_sd.png";
NSString *absolutePath = [SPUtils absolutePathToFile:filename withScaleFactor:2.0f];
STAssertTrue([absolutePath hasSuffix:filename], @"1x fallback resource not found");
}
- (void)testGetHdTextureFallback
{
NSString *filename = @"image_only_hd.png";
// @4x is not available -> @2x should be returned as a fallback
NSString *absolutePath = [SPUtils absolutePathToFile:filename withScaleFactor:4.0f];
STAssertEquals(2.0f, [absolutePath contentScaleFactor], @"2x fallback not found");
}
- (void)testOnlyHdTextureAvailable
{
NSString *filename = @"image_only_hd.png";
NSString *fullFilename = [filename stringByAppendingSuffixToFilename:@"@2x"];
NSString *absolutePath = [SPUtils absolutePathToFile:filename withScaleFactor:2.0f];
STAssertTrue([absolutePath hasSuffix:fullFilename], @"2x resource not found");
}
@end
#endif