-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathGarbageCollectionTests.m
More file actions
217 lines (166 loc) · 7.68 KB
/
GarbageCollectionTests.m
File metadata and controls
217 lines (166 loc) · 7.68 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// GarbageCollectionTests.m
// Cocoa Script
//
// Created by Adam Fedor on 7/6/15.
//
//
#import <Cocoa/Cocoa.h>
#import <XCTest/XCTest.h>
#import "COScript.h"
@interface GarbageCollectionTests : XCTestCase
@end
@implementation GarbageCollectionTests
{
COScript *jsContext;
NSURL *testScriptURL;
}
- (void)setUp {
[super setUp];
testScriptURL = [[NSBundle bundleForClass: [self class]] resourceURL];
jsContext = [[COScript alloc] init];
}
- (void)tearDown {
[super tearDown];
}
#pragma mark Simple Scripts
- (void)testCallFunctionWithName_WithSimpleAdd
{
[jsContext executeString: @"function add() { return 1 + 1}"];
id result = [jsContext executeString: @"add()"];
XCTAssertTrue([result isEqualTo: @(2)], @"Adding doesnt work");
}
- (void)testCallFunctionWithName_WithSimpleAddMultipleTimes
{
[jsContext executeString: @"function add() { return 1 + 1}"];
id result;
for (NSInteger i = 0; i < 1000; i++) {
result = [jsContext executeString: @"add()"];
}
XCTAssertTrue([result isEqualTo: @(2)], @"Adding multiple doesnt work");
}
- (void)testCallFunctionWithName_WithObjectFunction
{
[jsContext executeString: @"function myfunc() { var dict = NSMutableDictionary.dictionary(); \
dict.setObject_forKey_(\"foobar\", \"string\"); return 2;}"];
id result = [jsContext executeString: @"myfunc()"];
XCTAssertTrue([result isEqualTo: @(2)], @"Myfunc doesnt work");
}
- (void)testCallFunctionWithName_WithObjectFunctionMultipleTimes
{
[jsContext executeString: @"function myfunc() { var dict = NSMutableDictionary.dictionary(); \
dict.setObject_forKey_(\"foobar\", \"string\"); return 2;}"];
id result;
for (NSInteger i = 0; i < 1000; i++) {
result = [jsContext executeString: @"myfunc()"];
}
XCTAssertTrue([result isEqualTo: @(2)], @"Myfunc multiple doesnt work");
}
- (void)testCallFunctionWithName_WithIdenticalProperties_CanChangeEachPropertySeperately
{
[jsContext executeString: @"function myfunc() { \
var dict1 = NSMutableDictionary.dictionary(); \
var dict2 = NSMutableDictionary.dictionary(); \
dict1.setObject_forKey_(\"foobar\", \"string\"); \
print(\"dict1 is \" + dict1); \
print(\"dict2 is \" + dict2); \
return dict2.count();}"];
id result = [jsContext executeString: @"myfunc()"];
XCTAssertTrue([result isEqualTo: @(0)], @"Setting mutable dictionary doesnt work");
}
#pragma mark Stored Scripts
- (void)testUsingCoreGraphicsScript_ShouldRun {
NSURL *URL = [testScriptURL URLByAppendingPathComponent: @"CoreGraphics.js"];
NSError *error = nil;
NSString *testScript = [NSString stringWithContentsOfURL:URL usedEncoding:NULL error:&error];
XCTAssertNotNil(testScript, @"Error loading test script: %@", error);
[jsContext executeString:testScript];
NSArray *result = [jsContext executeString:@"main()"];
XCTAssertTrue([result[0] boolValue], @"%@", result[1]);
result = nil;
}
#pragma mark Memory Allocation
- (void)test_10_UsingMemoryAllocationScript_ShouldRun {
NSURL *URL = [testScriptURL URLByAppendingPathComponent: @"JSMemoryAllocation.js"];
NSError *error = nil;
NSString *testScript = [NSString stringWithContentsOfURL:URL usedEncoding:NULL error:&error];
XCTAssertNotNil(testScript, @"Error loading test script: %@", error);
[jsContext executeString:testScript];
NSArray *result = [jsContext callFunctionNamed: @"main" withArguments: @[@(10)]];
XCTAssertTrue([result[0] boolValue], @"%@", result[1]);
}
- (void)test_11_UsingMemoryAllocationScript_ShouldRun {
NSURL *URL = [testScriptURL URLByAppendingPathComponent: @"MemoryAllocation.js"];
NSError *error = nil;
NSString *testScript = [NSString stringWithContentsOfURL:URL usedEncoding:NULL error:&error];
XCTAssertNotNil(testScript, @"Error loading test script: %@", error);
[jsContext executeString:testScript];
NSArray *result = [jsContext callFunctionNamed:@"main" withArguments: @[@(10)]];
XCTAssertTrue([result[0] boolValue], @"%@", result[1]);
}
- (void)test_12_UsingJSMemoryAllocationScript_WithMoreIterations_ShouldRunFast {
NSURL *URL = [testScriptURL URLByAppendingPathComponent: @"JSMemoryAllocation.js"];
NSError *error = nil;
NSString *testScript = [NSString stringWithContentsOfURL:URL usedEncoding:NULL error:&error];
XCTAssertNotNil(testScript, @"Error loading test script: %@", error);
[jsContext executeString:testScript];
__block NSArray *result;
[self measureBlock:^{
result = [jsContext callFunctionNamed:@"main" withArguments: @[@(5000)]];
}];
XCTAssertTrue([result[0] boolValue], @"%@", result[1]);
}
- (void)test_13_UsingMemoryAllocationScript_WithMoreIterations_ShouldRunFast {
NSURL *URL = [testScriptURL URLByAppendingPathComponent: @"MemoryAllocation.js"];
NSError *error = nil;
NSString *testScript = [NSString stringWithContentsOfURL:URL usedEncoding:NULL error:&error];
XCTAssertNotNil(testScript, @"Error loading test script: %@", error);
[jsContext executeString:testScript];
__block NSArray *result;
[self measureBlock:^{
result = [jsContext callFunctionNamed:@"main" withArguments: @[@(500)]];
}];
XCTAssertTrue([result[0] boolValue], @"%@", result[1]);
}
#pragma mark Garbage Collection
/* Takes ~.7 sec on a 2011 Mini */
- (void)test_14_UsingJSMemoryAllocationScript_WithHugeNumberOfIterations_ShouldNotCrash {
NSURL *URL = [testScriptURL URLByAppendingPathComponent: @"JSMemoryAllocation.js"];
NSError *error = nil;
NSString *testScript = [NSString stringWithContentsOfURL:URL usedEncoding:NULL error:&error];
XCTAssertNotNil(testScript, @"Error loading test script: %@", error);
[jsContext executeString:testScript];
NSArray *result;
result = [jsContext callFunctionNamed:@"main" withArguments: @[@(100000)]];
XCTAssertTrue([result[0] boolValue], @"%@", result[1]);
}
/* Unfortunately this will just crash if it doesn't work. Takes ~70 sec on a 2011 Mini */
- (void)test_15_UsingMemoryAllocationScript_WithHugeNumberOfIterations_ShouldNotCrash {
NSURL *URL = [testScriptURL URLByAppendingPathComponent: @"MemoryAllocation.js"];
NSError *error = nil;
NSString *testScript = [NSString stringWithContentsOfURL:URL usedEncoding:NULL error:&error];
XCTAssertNotNil(testScript, @"Error loading test script: %@", error);
[jsContext executeString:testScript];
NSArray *result;
result = [jsContext callFunctionNamed:@"main" withArguments: @[@(100000)]];
XCTAssertTrue([result[0] boolValue], @"%@", result[1]);
}
/* Unfortunately this will just crash if it doesn't work. Takes ~10 sec on a 2011 Mini */
- (void)test_16_UsingGarbageCollect2Script_WithHugeNumberOfIterations_ShouldNotCrash {
NSURL *URL = [testScriptURL URLByAppendingPathComponent: @"GarbageCollect2.js"];
NSError *error = nil;
NSString *testScript = [NSString stringWithContentsOfURL:URL usedEncoding:NULL error:&error];
XCTAssertNotNil(testScript, @"Error loading test script: %@", error);
[jsContext executeString: testScript];
XCTAssertTrue(1, @"GarbageCollect2 Test did not run");
}
/* Unfortunately this will just crash if it doesn't work. */
- (void)test_17_UsingLoadPlist_WithHugeNumberOfIterations_ShouldNotCrash {
NSURL *URL = [testScriptURL URLByAppendingPathComponent: @"loadPlist.js"];
NSError *error = nil;
NSString *testScript = [NSString stringWithContentsOfURL:URL usedEncoding:NULL error:&error];
XCTAssertNotNil(testScript, @"Error loading test script: %@", error);
[jsContext executeString: testScript];
XCTAssertTrue(1, @"loadPlist Test did not run");
}
@end