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
|
/*
* Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
* Alternatively, it may be distributed under the terms of the GNU General
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <string.h>
#import "TestsAppDelegate.h"
#define FMT @"%@ %@ %@ %@ %@ %@ %@ %@ %@ %g %g %g %g %g %g %g %g %g"
#define ARGS @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", \
1.5, 2.25, 3.125, 4.0625, 5.03125, 6.5, 7.25, 8.0, 9.0
#define RESULT @"a b c d e f g h i 1.5 2.25 3.125 4.0625 5.03125 6.5 7.25 8 9"
static OFString *module = @"Forwarding";
static size_t forwardings = 0;
static bool success = false;
static id target = nil;
struct stret_test {
char s[1024];
};
@interface ForwardingTest: OFObject
@end
@interface ForwardingTest (Test)
+ (void)test;
- (void)test;
- (uint32_t)forwardingTargetTest: (intptr_t)a0
: (intptr_t)a1
: (double)a2
: (double)a3;
- (OFString *)forwardingTargetVarArgTest: (OFConstantString *)fmt, ...;
- (long double)forwardingTargetFPRetTest;
- (struct stret_test)forwardingTargetStRetTest;
- (void)forwardingTargetNilTest;
- (void)forwardingTargetSelfTest;
- (struct stret_test)forwardingTargetNilStRetTest;
- (struct stret_test)forwardingTargetSelfStRetTest;
@end
@interface ForwardingTarget: OFObject
@end
static void
test(id self, SEL _cmd)
{
success = true;
}
@implementation ForwardingTest
+ (bool)resolveClassMethod: (SEL)selector
{
forwardings++;
if (sel_isEqual(selector, @selector(test))) {
class_replaceMethod(object_getClass(self), @selector(test),
(IMP)test, "v#:");
return YES;
}
return NO;
}
+ (bool)resolveInstanceMethod: (SEL)selector
{
forwardings++;
if (sel_isEqual(selector, @selector(test))) {
class_replaceMethod(self, @selector(test), (IMP)test, "v@:");
return YES;
}
return NO;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
/*
* Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
* Alternatively, it may be distributed under the terms of the GNU General
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <string.h>
#import "TestsAppDelegate.h"
#define FORMAT @"%@ %@ %@ %@ %@ %@ %@ %@ %@ %g %g %g %g %g %g %g %g %g"
#define ARGS @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", \
1.5, 2.25, 3.125, 4.0625, 5.03125, 6.5, 7.25, 8.0, 9.0
#define RESULT @"a b c d e f g h i 1.5 2.25 3.125 4.0625 5.03125 6.5 7.25 8 9"
static OFString *const module = @"Forwarding";
static size_t forwardingsCount = 0;
static bool success = false;
static id target = nil;
struct StretTest {
char buffer[1024];
};
@interface ForwardingTest: OFObject
@end
@interface ForwardingTest (Test)
+ (void)test;
- (void)test;
- (uint32_t)forwardingTargetTest: (intptr_t)a0
: (intptr_t)a1
: (double)a2
: (double)a3;
- (OFString *)forwardingTargetVarArgTest: (OFConstantString *)format, ...;
- (long double)forwardingTargetFPRetTest;
- (struct StretTest)forwardingTargetStRetTest;
- (void)forwardingTargetNilTest;
- (void)forwardingTargetSelfTest;
- (struct StretTest)forwardingTargetNilStRetTest;
- (struct StretTest)forwardingTargetSelfStRetTest;
@end
@interface ForwardingTarget: OFObject
@end
static void
test(id self, SEL _cmd)
{
success = true;
}
@implementation ForwardingTest
+ (bool)resolveClassMethod: (SEL)selector
{
forwardingsCount++;
if (sel_isEqual(selector, @selector(test))) {
class_replaceMethod(object_getClass(self), @selector(test),
(IMP)test, "v#:");
return YES;
}
return NO;
}
+ (bool)resolveInstanceMethod: (SEL)selector
{
forwardingsCount++;
if (sel_isEqual(selector, @selector(test))) {
class_replaceMethod(self, @selector(test), (IMP)test, "v@:");
return YES;
}
return NO;
|
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
return 0;
if (a3 != 2.75)
return 0;
return 0x12345678;
}
- (OFString *)forwardingTargetVarArgTest: (OFConstantString *)fmt, ...
{
va_list args;
OFString *ret;
OFEnsure(self == target);
va_start(args, fmt);
ret = [[[OFString alloc] initWithFormat: fmt
arguments: args] autorelease];
va_end(args);
return ret;
}
- (long double)forwardingTargetFPRetTest
{
OFEnsure(self == target);
return 12345678.00006103515625;
}
- (struct stret_test)forwardingTargetStRetTest
{
struct stret_test ret = { { 0 } };
OFEnsure(self == target);
memcpy(ret.s, "abcdefghijklmnopqrstuvwxyz", 27);
return ret;
}
@end
@implementation TestsAppDelegate (ForwardingTests)
- (void)forwardingTests
{
void *pool = objc_autoreleasePoolPush();
TEST(@"Forwarding a message and adding a class method",
R([ForwardingTest test]) && success &&
R([ForwardingTest test]) && forwardings == 1);
ForwardingTest *t = [[[ForwardingTest alloc] init] autorelease];
success = false;
forwardings = 0;
TEST(@"Forwarding a message and adding an instance method",
R([t test]) && success && R([t test]) && forwardings == 1);
#ifdef OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
target = [[[ForwardingTarget alloc] init] autorelease];
TEST(@"-[forwardingTargetForSelector:]",
[t forwardingTargetTest: 0xDEADBEEF
: -1
: 1.25
: 2.75] == 0x12345678)
TEST(@"-[forwardingTargetForSelector:] variable arguments",
[[t forwardingTargetVarArgTest: FMT, ARGS] isEqual: RESULT])
/*
* Don't try fpret on Win64 if we don't have stret forwarding, as
* long double is handled as a struct there.
*/
# if !defined(OF_WINDOWS) || !defined(OF_X86_64) || \
defined(OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET)
TEST(@"-[forwardingTargetForSelector:] fp return",
[t forwardingTargetFPRetTest] == 12345678.00006103515625)
# endif
# ifdef OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
TEST(@"-[forwardingTargetForSelector:] struct return",
!memcmp([t forwardingTargetStRetTest].s,
"abcdefghijklmnopqrstuvwxyz", 27))
# endif
EXPECT_EXCEPTION(@"-[forwardingTargetForSelector:] nil target",
OFNotImplementedException, [t forwardingTargetNilTest])
EXPECT_EXCEPTION(@"-[forwardingTargetForSelector:] self target",
OFNotImplementedException, [t forwardingTargetSelfTest])
# ifdef OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
EXPECT_EXCEPTION(@"-[forwardingTargetForSelector:] nil target + "
@"stret", OFNotImplementedException,
[t forwardingTargetNilStRetTest])
EXPECT_EXCEPTION(@"-[forwardingTargetForSelector:] self target + "
@"stret", OFNotImplementedException,
[t forwardingTargetSelfStRetTest])
# endif
#endif
objc_autoreleasePoolPop(pool);
}
@end
|
|
|
|
|
|
|
|
>
|
|
|
>
|
|
|
|
|
>
|
|
|
|
|
|
|
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
return 0;
if (a3 != 2.75)
return 0;
return 0x12345678;
}
- (OFString *)forwardingTargetVarArgTest: (OFConstantString *)format, ...
{
va_list args;
OFString *ret;
OFEnsure(self == target);
va_start(args, format);
ret = [[[OFString alloc] initWithFormat: format
arguments: args] autorelease];
va_end(args);
return ret;
}
- (long double)forwardingTargetFPRetTest
{
OFEnsure(self == target);
return 12345678.00006103515625;
}
- (struct StretTest)forwardingTargetStRetTest
{
struct StretTest ret = { { 0 } };
OFEnsure(self == target);
memcpy(ret.buffer, "abcdefghijklmnopqrstuvwxyz", 27);
return ret;
}
@end
@implementation TestsAppDelegate (ForwardingTests)
- (void)forwardingTests
{
void *pool = objc_autoreleasePoolPush();
TEST(@"Forwarding a message and adding a class method",
R([ForwardingTest test]) && success &&
R([ForwardingTest test]) && forwardingsCount == 1);
ForwardingTest *testObject =
[[[ForwardingTest alloc] init] autorelease];
success = false;
forwardingsCount = 0;
TEST(@"Forwarding a message and adding an instance method",
R([testObject test]) && success && R([testObject test]) &&
forwardingsCount == 1);
#ifdef OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
target = [[[ForwardingTarget alloc] init] autorelease];
TEST(@"-[forwardingTargetForSelector:]",
[testObject forwardingTargetTest: 0xDEADBEEF
: -1
: 1.25
: 2.75] == 0x12345678)
TEST(@"-[forwardingTargetForSelector:] variable arguments",
[[testObject forwardingTargetVarArgTest: FORMAT, ARGS]
isEqual: RESULT])
/*
* Don't try fpret on Win64 if we don't have stret forwarding, as
* long double is handled as a struct there.
*/
# if !defined(OF_WINDOWS) || !defined(OF_X86_64) || \
defined(OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET)
TEST(@"-[forwardingTargetForSelector:] fp return",
[testObject forwardingTargetFPRetTest] == 12345678.00006103515625)
# endif
# ifdef OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
TEST(@"-[forwardingTargetForSelector:] struct return",
!memcmp([testObject forwardingTargetStRetTest].buffer,
"abcdefghijklmnopqrstuvwxyz", 27))
# endif
EXPECT_EXCEPTION(@"-[forwardingTargetForSelector:] nil target",
OFNotImplementedException, [testObject forwardingTargetNilTest])
EXPECT_EXCEPTION(@"-[forwardingTargetForSelector:] self target",
OFNotImplementedException, [testObject forwardingTargetSelfTest])
# ifdef OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR_STRET
EXPECT_EXCEPTION(@"-[forwardingTargetForSelector:] nil target + "
@"stret", OFNotImplementedException,
[testObject forwardingTargetNilStRetTest])
EXPECT_EXCEPTION(@"-[forwardingTargetForSelector:] self target + "
@"stret", OFNotImplementedException,
[testObject forwardingTargetSelfStRetTest])
# endif
#endif
objc_autoreleasePoolPop(pool);
}
@end
|