File: basic.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (275 lines) | stat: -rw-r--r-- 5,929 bytes parent folder | download | duplicates (2)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSDate.h>
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSDecimalNumber.h>
#import <Foundation/NSGarbageCollector.h>

typedef struct {
  int	i;
  char	c;
} myStruct;

@interface TestClass : NSObject
{
  NSString *name;
  NSDate *date;
  int num1;
  double num2;
  int num3;
  int num4;
  TestClass *child;
  myStruct s;
  bool b;
}

- (void) setNum3:(int) num;
- (int) num3;
- (void) _setNum4:(int) num;
- (int) _num4;
- (char) sc;
- (int) si;
- (myStruct) sv;
- (void) setSv: (myStruct)v;

@end

@implementation TestClass

- (void) dealloc
{
  DESTROY(name);
  DESTROY(date);
  DESTROY(child);
  DEALLOC
}

- (id) init
{
  s.i = 1;
  s.c = 2;
  return self;
}

- (void) setNum3:(int) num
{
  num3 = num;
  if (num3 == 8) num3 = 7;
}

- (int) num3
{
  return num3;
}

- (void) _setNum4:(int) num
{
  num4 = num;
  if (num4 == 8) num4 = 7;
}

- (int) _num4
{
  return num4;
}

- (char) sc
{
  return s.c;
}

- (int) si
{
  return s.i;
}

- (myStruct) sv
{
  return s;
}

- (void) setSv: (myStruct)v
{
  s = v;
}
@end

@interface UndefinedKey : NSObject
{
  int num1;
  NSString *string;
}
@end

@implementation UndefinedKey
- (void) dealloc
{
  [string release];
  [super dealloc];
}

- (void) setValue:(id) value forUndefinedKey:(NSString *) key
{
  if ([key isEqualToString: @"Lxcke"]) {
    [string release];
    string = [value copy];
  }
}

- (id) valueForUndefinedKey:(NSString *) key
{
  if ([key isEqualToString: @"Lxcke"]) {
    return string;
  }
  return nil;
}

@end

@interface UndefinedKey2 : NSObject
{
  int num1;
  NSString *string;
}
@end

@implementation UndefinedKey2
- (void) dealloc
{
  [string release];
  [super dealloc];
}

- (void) handleTakeValue:(id) value forUnboundKey:(NSString *) key
{
  if ([key isEqualToString: @"Lxcke"]) {
    [string release];
    string = [value copy];
  }
}

- (id) handleQueryWithUnboundKey:(NSString *) key
{
  if ([key isEqualToString: @"Lxcke"]) {
    return string;
  }
  return nil;
}

@end

int main() 
{
  NSAutoreleasePool *arp = [NSAutoreleasePool new];
  NSMutableString *m = [NSMutableString string];
  TestClass *tester = [[[TestClass alloc] init] autorelease];
  NSUInteger rc;
  NSValue *v;
  myStruct s;

  [m appendString: @"testing"];

  [tester setValue:[[[TestClass alloc] init] autorelease] forKey: @"child"];
  UndefinedKey *undefinedKey = [[[UndefinedKey alloc] init] autorelease];
  UndefinedKey2 *undefinedKey2 = [[[UndefinedKey2 alloc] init] autorelease];

  NSNumber *n = [NSNumber numberWithInt:8];
  NSNumber *nb = [NSNumber numberWithBool:1];
  NSNumber *adjustedN = [NSNumber numberWithInt:7];
  NSNumber *n2 = [NSNumber numberWithDouble:87.999];

  [tester setValue: @"tester" forKey: @"name"];
  PASS([[tester valueForKey: @"name"] isEqualToString: @"tester"],
      "KVC works with strings");

  rc = [m retainCount];
  [tester setValue: m forKey: @"name"];
  PASS([tester valueForKey: @"name"] == m,
      "KVC works with mutable string");
  if (nil == [NSGarbageCollector defaultCollector])
    {
      PASS(rc + 1 == [m retainCount], "KVC retains object values");
    }

  [tester setValue:nb forKey: @"b"];
  PASS([[tester valueForKey: @"b"] isEqualToNumber:nb],
      "KVC works with bool");

  [tester setValue:n forKey: @"num1"];
  PASS([[tester valueForKey: @"num1"] isEqualToNumber:n],
      "KVC works with ints");

  [tester setValue:n2 forKey: @"num2"];
  PASS([[tester valueForKey: @"num2"] isEqualToNumber:n2],
      "KVC works with doubles");

  [tester setValue:n forKey: @"num3"];
  PASS([[tester valueForKey: @"num3"] isEqualToNumber:adjustedN],
      "KVC works with setKey:");

  [tester setValue:n forKey: @"num4"];
  PASS([[tester valueForKey: @"num4"] isEqualToNumber:adjustedN],
      "KVC works with _setKey:");

  v = [tester valueForKey: @"s"];
  s.i = 0;
  s.c = 0;
  [v getValue: &s];
  PASS(s.i == 1 && s.c == 2, "KVC valueForKey: works for a struct (direct)");

  v = [tester valueForKey: @"sv"];
  s.i = 0;
  s.c = 0;
  [v getValue: &s];
  PASS(s.i == 1 && s.c == 2, "KVC valueForKey: works for a struct (getter)");

  s.i = 3;
  s.c = 4;
  v = [NSValue valueWithBytes: &s objCType: @encode(myStruct)];
  [tester setValue: v forKey: @"s"];
  PASS([tester si] == s.i && [tester sc] == s.c,
    "KVC setValue:forKey: works for a struct (direct)");

  s.i = 5;
  s.c = 6;
  v = [NSValue valueWithBytes: &s objCType: @encode(myStruct)];
  [tester setValue: v forKey: @"sv"];
  PASS([tester si] == s.i && [tester sc] == s.c,
    "KVC setValue:forKey: works for a struct (setter)");

  [undefinedKey setValue: @"GNUstep" forKey: @"Lxcke"];
  PASS([[undefinedKey valueForKey: @"Lxcke"] isEqualToString: @"GNUstep"],
      "KVC works with undefined keys");

  [undefinedKey2 setValue: @"GNUstep" forKey: @"Lxcke"];
  PASS([[undefinedKey2 valueForKey: @"Lxcke"] isEqualToString: @"GNUstep"],
      "KVC works with undefined keys (using deprecated methods) ");

  PASS_EXCEPTION(
    [tester setValue: @"" forKey: @"nonexistent"],
    NSUndefinedKeyException,
    "KVC properly throws @\"NSUnknownKeyException\"");

  PASS_EXCEPTION(
    [tester setValue: @"" forKey: @"nonexistent"],
    NSUndefinedKeyException,
    "KVC properly throws NSUndefinedKeyException");

  PASS_EXCEPTION(
    [tester setValue: @"" forKeyPath: @"child.nonexistent"],
    @"NSUnknownKeyException",
    "KVC properly throws @\"NSUnknownKeyException\" with key paths");

  PASS_EXCEPTION(
    [tester setValue: @"" forKeyPath: @"child.nonexistent"],
    NSUndefinedKeyException,
    "KVC properly throws NSUndefinedKeyException with key paths");

  PASS(sel_getUid(0) == 0, "null string gives null selector");
  PASS(sel_registerName(0) == 0, "register null string gives null selector");
  PASS(strcmp(sel_getName(0) , "<null selector>") == 0, "null selector name");

  [arp release]; arp = nil;
  return 0;
}