1
/* Test/example program for the base library
3
Copyright (C) 2005 Free Software Foundation, Inc.
5
Copying and distribution of this file, with or without modification,
6
are permitted in any medium without royalty provided the copyright
7
notice and this notice are preserved.
9
This file is part of the GNUstep Base Library.
11
#include <Foundation/Foundation.h>
12
#include <objc/Protocol.h>
14
@interface Foo: NSObject
19
- (BOOL) conformsToProtocol: (Protocol *) aProtocol;
20
- (BOOL) respondsToSelector: (SEL) aSelector;
21
- copyWithZone: (NSZone *) zone;
22
- mutableCopyWithZone: (NSZone *) zone;
30
return [[[Foo alloc] init] autorelease];
33
- (BOOL) conformsToProtocol: (Protocol *) aProtocol
35
BOOL ret = [super conformsToProtocol: aProtocol];
37
NSLog(@"-[<%@:0x%x> %@<%s>] -> %@",
38
NSStringFromClass([self class]), self, NSStringFromSelector(_cmd),
39
[aProtocol name], ret ? @"YES" : @"NO");
44
- (BOOL) respondsToSelector: (SEL) aSelector
46
BOOL ret = [super respondsToSelector: aSelector];
48
if (![NSStringFromSelector(aSelector) hasPrefix:@"description"])
49
NSLog(@"-[<%@:0x%x> %@%@] -> %@",
50
NSStringFromClass([self class]), self, NSStringFromSelector(_cmd),
51
NSStringFromSelector(aSelector), ret ? @"YES" : @"NO");
55
- copyWithZone: (NSZone *) zone
59
NSLog(@"-[<%@:0x%x> %@0x%x] -> <%@:0x%x>",
60
NSStringFromClass([self class]), self,
61
NSStringFromSelector(_cmd), zone, NSStringFromClass([ret class]), ret);
65
- mutableCopyWithZone: (NSZone *) zone
69
NSLog(@"-[<%@:0x%x> %@0x%x] -> <%@:0x%x>",
70
NSStringFromClass([self class]), self,
71
NSStringFromSelector(_cmd), zone, NSStringFromClass([ret class]), ret);
77
id ret = [super retain];
79
NSLog(@"-[<%@:0x%x> %@] -> retainCount = %d",
80
NSStringFromClass([self class]), self,
81
NSStringFromSelector(_cmd), [ret retainCount]);
88
isDeepArrayCopy(NSArray *obj1, NSArray *obj2)
90
id obj1FirstObject = [obj1 objectAtIndex:0];
91
id obj2FirstObject = [obj2 objectAtIndex:0];
93
NSLog(@"<%@:0x%x> -> <%@:0x%x>, %@",
94
NSStringFromClass([obj1 class]), obj1,
95
NSStringFromClass([obj2 class]), obj2,
96
(obj1FirstObject == obj2FirstObject) ? ((obj1 == obj2) ? @"retained" : @"shallow") : @"deep(ish)");
100
isDeepDictionaryCopy(NSDictionary *obj1, NSDictionary *obj2)
102
id obj1FirstObject = [obj1 objectForKey: @"Key"];
103
id obj2FirstObject = [obj2 objectForKey: @"Key"];
105
NSLog(@"<%@:0x%x> -> <%@:0x%x>, %@",
106
NSStringFromClass([obj1 class]), obj1,
107
NSStringFromClass([obj2 class]), obj2,
108
(obj1FirstObject == obj2FirstObject) ? ((obj1 == obj2) ? @"retained" : @"shallow") : @"deep(ish)");
112
isDeepSetCopy(NSSet *obj1, NSSet *obj2)
114
id obj1FirstObject = [obj1 anyObject];
115
id obj2FirstObject = [obj2 anyObject];
117
NSLog(@"<%@:0x%x> -> <%@:0x%x>, %@",
118
NSStringFromClass([obj1 class]), obj1,
119
NSStringFromClass([obj2 class]), obj2,
120
(obj1FirstObject == obj2FirstObject) ? ((obj1 == obj2) ? @"retained" : @"shallow") : @"deep(ish)");
124
int main(int argc, char **argv)
126
NSAutoreleasePool *thePool = [[NSAutoreleasePool alloc] init];
127
NSArray *anArrayCopy;
128
NSArray *aMutableArrayCopy;
129
NSArray *anArrayMutableCopy;
130
NSArray *aMutableArrayMutableCopy;
131
NSMutableArray *aMutableArray
132
= [NSMutableArray arrayWithObject: [Foo foo]];
133
NSArray *anArray = [NSArray arrayWithObject: [Foo foo]];
134
NSDictionary *aDictionaryCopy;
135
NSDictionary *aMutableDictionaryCopy;
136
NSDictionary *aDictionaryMutableCopy;
137
NSDictionary *aMutableDictionaryMutableCopy;
138
NSMutableDictionary *aMutableDictionary
139
= [NSMutableDictionary dictionaryWithObjectsAndKeys: [Foo foo], @"Key", nil];
140
NSDictionary *aDictionary
141
= [NSDictionary dictionaryWithObjectsAndKeys: [Foo foo], @"Key", nil];
143
NSSet *aMutableSetCopy;
144
NSSet *aSetMutableCopy;
145
NSSet *aMutableSetMutableCopy;
146
NSMutableSet *aMutableSet = [NSMutableSet setWithObject: [Foo foo]];
147
NSSet *aSet = [NSSet setWithObject: [Foo foo]];
148
NSZone *zone = NSDefaultMallocZone();
152
NSLog(@"Copying from zone 0x%x -> 0x%x", NSDefaultMallocZone(), zone);
154
NSLog(@"MutableArray -copy");
155
aMutableArrayCopy = [aMutableArray copyWithZone:zone];
156
NSLog(@"MutableArray -mutableCopy");
157
aMutableArrayMutableCopy = [aMutableArray mutableCopyWithZone:zone];
158
NSLog(@"Array -copy");
159
anArrayCopy = [anArray copyWithZone:zone];
160
NSLog(@"Array -mutableCopy");
161
anArrayMutableCopy = [anArray mutableCopyWithZone:zone];
163
NSLog(@"MutableArray: %@", aMutableArray);
164
NSLog(@"MutableArrayCopy: %@", aMutableArrayCopy);
165
NSLog(@"MutableArrayMutableCopy: %@", aMutableArrayMutableCopy);
166
NSLog(@"anArray: %@", anArray);
167
NSLog(@"anArrayCopy: %@", anArrayCopy);
168
NSLog(@"anArrayCopy: %@", anArrayMutableCopy);
170
NSLog(@"Test MutableArray against Copy");
171
isDeepArrayCopy(aMutableArray, aMutableArrayCopy);
173
NSLog(@"Test MutableArray against MutableCopy");
174
isDeepArrayCopy(aMutableArray, aMutableArrayMutableCopy);
176
NSLog(@"Test Array against Copy");
177
isDeepArrayCopy(anArray, anArrayCopy);
179
NSLog(@"Test Array against MutableCopy");
180
isDeepArrayCopy(anArray, anArrayMutableCopy);
182
NSLog(@"MutableDictionary -copy");
183
aMutableDictionaryCopy = [aMutableDictionary copyWithZone:zone];
184
NSLog(@"MutableDictionary -mutableCopy");
185
aMutableDictionaryMutableCopy = [aMutableDictionary mutableCopyWithZone:zone];
186
NSLog(@"Dictionary -copy");
187
aDictionaryCopy = [aDictionary copyWithZone:zone];
188
NSLog(@"Dictionary -mutableCopy");
189
aDictionaryMutableCopy = [aDictionary mutableCopyWithZone:zone];
191
NSLog(@"MutableDictionary: %@", aMutableDictionary);
192
NSLog(@"MutableDictionaryCopy: %@", aMutableDictionaryCopy);
193
NSLog(@"MutableDictionaryMutableCopy: %@", aMutableDictionaryMutableCopy);
194
NSLog(@"aDictionary: %@", aDictionary);
195
NSLog(@"aDictionaryCopy: %@", aDictionaryCopy);
196
NSLog(@"aDictionaryCopy: %@", aDictionaryMutableCopy);
198
NSLog(@"Test MutableDictionary against Copy");
199
isDeepDictionaryCopy(aMutableDictionary, aMutableDictionaryCopy);
201
NSLog(@"Test MutableDictionary against MutableCopy");
202
isDeepDictionaryCopy(aMutableDictionary, aMutableDictionaryMutableCopy);
204
NSLog(@"Test Dictionary against Copy");
205
isDeepDictionaryCopy(aDictionary, aDictionaryCopy);
207
NSLog(@"Test Dictionary against MutableCopy");
208
isDeepDictionaryCopy(aDictionary, aDictionaryMutableCopy);
210
NSLog(@"MutableSet -copy");
211
aMutableSetCopy = [aMutableSet copyWithZone:zone];
212
NSLog(@"MutableSet -mutableCopy");
213
aMutableSetMutableCopy = [aMutableSet mutableCopyWithZone:zone];
215
aSetCopy = [aSet copyWithZone:zone];
216
NSLog(@"Set -mutableCopy");
217
aSetMutableCopy = [aSet mutableCopyWithZone:zone];
219
NSLog(@"MutableSet: %@", aMutableSet);
220
NSLog(@"MutableSetCopy: %@", aMutableSetCopy);
221
NSLog(@"MutableSetMutableCopy: %@", aMutableSetMutableCopy);
222
NSLog(@"aSet: %@", aSet);
223
NSLog(@"aSetCopy: %@", aSetCopy);
224
NSLog(@"aSetCopy: %@", aSetMutableCopy);
226
NSLog(@"Test MutableSet against Copy");
227
isDeepSetCopy(aMutableSet, aMutableSetCopy);
229
NSLog(@"Test MutableSet against MutableCopy");
230
isDeepSetCopy(aMutableSet, aMutableSetMutableCopy);
232
NSLog(@"Test Set against Copy");
233
isDeepSetCopy(aSet, aSetCopy);
235
NSLog(@"Test Set against MutableCopy");
236
isDeepSetCopy(aSet, aSetMutableCopy);
238
if (zone == NSDefaultMallocZone())
239
zone = NSCreateZone(NSPageSize(), NSPageSize(), YES);