public
Description: A lean-and-mean Ruby/ObjC bridge
Homepage: http://rubyobjc.com
Clone URL: git://github.com/timburks/rubyobjc.git
rubyobjc / test / testobject.m
100644 195 lines (168 sloc) 4.144 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
/*
* testobject.m
*
* Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
* For more information about this file, visit http://www.rubyobjc.com.
*/
 
#import <Foundation/Foundation.h>
 
@interface TestObject : NSObject
{
#ifndef __OBJC2__
  BOOL _boolValue;
  int _intValue;
  long _longValue;
  float _floatValue;
  double _doubleValue;
#endif
}
 
#ifdef __OBJC2__
@property(ivar) BOOL boolValue;
@property(ivar) int intValue;
@property(ivar) long longValue;
@property(ivar) float floatValue;
@property(ivar) double doubleValue;
#else
- (BOOL) boolValue;
- (int) intValue;
- (long) longValue;
- (float) floatValue;
- (double) doubleValue;
- (void) setBoolValue:(BOOL) b;
- (void) setIntValue:(int) i;
- (void) setLongValue:(long) l;
- (void) setFloatValue:(float) f;
- (void) setDoubleValue:(double) d;
#endif
@end
 
@protocol Hello
- (NSString *) hello;
- (int) two;
- (int) add:(int)x plus:(int)y;
- (float) fadd:(float)x plus:(float)y;
- (double) dadd:(double)x plus:(double)y;
@end
 
@interface NSObject(RubyClassMethods)
+ (int) passInt:(int)x;
@end
 
@implementation TestObject
#ifndef __OBJC2__
- (BOOL) boolValue {return _boolValue;}
- (int) intValue {return _intValue;}
- (long) longValue {return _longValue;}
- (float) floatValue {return _floatValue;}
- (double) doubleValue {return _doubleValue;}
- (void) setBoolValue:(BOOL) b {_boolValue = b;}
- (void) setIntValue:(int) i {_intValue = i;}
- (void) setLongValue:(long) l {_longValue = l;}
- (void) setFloatValue:(float) f {_floatValue = f;}
- (void) setDoubleValue:(double) d {_doubleValue = d;}
#endif
- (void) logBoolValue {NSLog(@"%d", _boolValue);}
- (void) logIntValue {NSLog(@"%d", _intValue);}
- (void) logLongValue {NSLog(@"%ld", _longValue);}
- (void) logFloatValue {NSLog(@"%f", _floatValue);}
- (void) logDoubleValue {NSLog(@"%lf", _doubleValue);}
 
- (int) testStack
{
    int errors = 0;
 
    Class stackClass = NSClassFromString(@"Stack");
    id stack = [[stackClass alloc] init];
    NSString *hello = [stack hello];
    if (![hello isEqual:@"hello Brad, this is Matz"]) {
        NSLog(@"string mismatch: %@", hello);
        errors++;
    }
 
    int two = [stack two];
    if (two != 2) {
        NSLog(@"two = %d", two);
        errors++;
    }
 
    int four = [stack add:two plus:two];
    if (four != 4) {
        NSLog(@"int: two + two = %d", four);
        errors++;
    }
 
    float f_four = [stack fadd:(float)2.1 plus:(float)2.2];
    if (fabs(f_four - 4.3) > 0.00001) {
        NSLog(@"float: two + two = %f", f_four);
        errors++;
    }
 
    double d_four = [stack dadd:2.3 plus:2.4];
    if (fabs(d_four - 4.7) > 0.00001) {
        NSLog(@"double: two + two = %lf", d_four);
        errors++;
    }
    return errors;
}
 
- (int) testClassMethod
{
  int errors = 0;
  int x = [NSObject passInt:99];
  if (x != 99) {
    NSLog(@"passInt failed with return value %d", x);
    errors++;
  }
  return errors;
}
 
@end
 
int itwo()
{
    return 2;
}
 
char cadd(char a, char b)
{
  //NSLog(@"adding %d to %d", a, b);  
  return a+b;
}
 
unsigned char ucadd(unsigned char a, unsigned char b)
{
  //NSLog(@"adding %d to %d", a, b);  
  return a+b;
}
 
int iadd(int a, int b)
{
  //NSLog(@"adding %d to %d", a, b);  
    return a+b;
}
 
unsigned int uiadd(unsigned int a, unsigned int b)
{
  //NSLog(@"adding %lld to %lld", (long long) a, (long long) b);  
    return a+b;
}
 
long ladd(long a, long b)
{
  //NSLog(@"adding %ld to %ld", a, b);
    return a+b;
}
 
unsigned long uladd(unsigned long a, unsigned long b)
{
  //NSLog(@"adding %lld to %lld", (long long) a, (long long) b);
    return a+b;
}
 
long long qadd(long long a, long long b)
{
  //NSLog(@"adding %lld to %lld", a, b);
    return a+b;
}
 
unsigned long long uqadd(unsigned long long a, unsigned long long b)
{
  //NSLog(@"adding %lld to %lld", a, b);
    return a+b;
}
 
double dadd(double a, double b)
{
    return a+b;
}
 
char *testFunction(int argc, char *argv[])
{
  char *leakyBuffer = (char *) malloc (1024 * sizeof(char));
    int i;
  char *p = leakyBuffer;
  strcpy(p, "");
    for (i = 0; i < argc; i++) {
    strcpy(p, argv[i]);
    p += strlen(argv[i]);
    }
    return leakyBuffer;
}
 
void Init_testobject() {}