This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ObjectiveHaskellTests.m
118 lines (90 loc) · 3.59 KB
/
ObjectiveHaskellTests.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
//
// ObjectiveHaskellTests.m
// ObjectiveHaskellTests
//
// Created by Justin Spahr-Summers on 2012-07-13.
// Released into the public domain.
//
#import "ObjectiveHaskellTests.h"
#import "BridgedObjectTest_stub.h"
#import "FibTest_stub.h"
#import "MsgSendTest_stub.h"
@implementation ObjectiveHaskellTests
- (void)testNSArrayBridging {
NSArray *array = addFoobarToArray(@[ @5, @{} ]);
NSArray *expectedArray = @[ @5, @{}, @"foobar" ];
STAssertEqualObjects(array, expectedArray, @"");
NSArray *passthroughArray = [NSArray objectWithHaskellPointer:array.haskellPointer];
STAssertEqualObjects(array, passthroughArray, @"");
}
- (void)testNSDataBridging {
unsigned char bytes[] = { 1, 2, 3 };
NSData *data = appendByte([NSData dataWithBytes:bytes length:sizeof(bytes)], 5);
unsigned char expectedBytes[] = { 1, 2, 3, 5 };
STAssertEquals([data length], sizeof(expectedBytes), @"");
STAssertTrue(memcmp([data bytes], expectedBytes, sizeof(expectedBytes)) == 0, @"");
NSData *passthroughData = [NSData objectWithHaskellPointer:data.haskellPointer];
STAssertEqualObjects(data, passthroughData, @"");
}
- (void)testNSDictionaryBridging {
NSDictionary *dict = setFooToBar(@{ @"fuzz": @5, @"foo": @"buzz" });
NSDictionary *expectedDict = @{ @"fuzz": @5, @"foo": @"bar" };
STAssertEqualObjects(dict, expectedDict, @"");
NSDictionary *passthroughDict = [NSDictionary objectWithHaskellPointer:dict.haskellPointer];
STAssertEqualObjects(dict, passthroughDict, @"");
}
- (void)testNSNumberBridging {
NSNumber *inum = @5;
STAssertEqualObjects(@6, plusInt(inum, 1), @"");
STAssertEqualsWithAccuracy(6.5, [plusDouble(inum, 1.5) doubleValue], 0.001, @"");
NSNumber *dnum = @5.25;
STAssertEqualsWithAccuracy(6.25, [plusInt(dnum, 1) doubleValue], 0.001, @"");
STAssertEqualsWithAccuracy(6.75, [plusDouble(dnum, 1.5) doubleValue], 0.001, @"");
NSNumber *passthroughNum = [NSNumber objectWithHaskellPointer:dnum.haskellPointer];
STAssertEqualObjects(dnum, passthroughNum, @"");
}
- (void)testNSStringBridging {
NSString *str = appendFoobar(@"fuzzbuzz");
STAssertEqualObjects(str, @"fuzzbuzzfoobar", @"");
NSString *passthroughStr = [NSString objectWithHaskellPointer:str.haskellPointer];
STAssertEqualObjects(str, passthroughStr, @"");
}
- (void)testNSValueBridging {
NSValue *value = nullNSValue();
STAssertNotNil(value, @"");
STAssertNil(value.pointerValue, @"");
uintptr_t addr = ptrAddress(value);
STAssertEquals(addr, (uintptr_t)0, @"");
}
- (void)testNSURLConversion {
NSURL *URL = httpsURLFromHost(@"google.com");
STAssertEqualObjects(URL, [NSURL URLWithString:@"https://google.com"], @"");
}
- (void)testFibonacci {
STAssertEquals(3, fibonacci_hs(4), @"");
STAssertEquals(5, fibonacci_hs(5), @"");
}
- (void)testMsgSend {
NSString *str = @"foo";
NSMutableString *mutableStr = msgSendTest(str);
STAssertEqualObjects(str, mutableStr, @"");
STAssertFalse(str == mutableStr, @"");
}
- (void)testMsgSendMemoryManagement {
// used to test the memory management of the string returned from Haskell
__weak id weakStr = nil;
@autoreleasepool {
{
__attribute__((objc_precise_lifetime)) NSMutableString *str = msgSendTest(@"foo");
// not necessary for normal code -- just for memory management testing
hs_perform_gc();
weakStr = str;
STAssertNotNil(weakStr, @"");
STAssertEqualObjects(str, @"foo", @"");
}
// the string shouldn't be released until the autorelease pool is popped
STAssertNotNil(weakStr, @"");
}
STAssertNil(weakStr, @"");
}
@end