~ubuntu-branches/ubuntu/trusty/gnustep-base/trusty

« back to all changes in this revision

Viewing changes to Testing/containers.m

Tags: upstream-1.20.0
ImportĀ upstreamĀ versionĀ 1.20.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Test/example program for the base library
2
 
 
3
 
   Copyright (C) 2005 Free Software Foundation, Inc.
4
 
   
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.
8
 
 
9
 
   This file is part of the GNUstep Base Library.
10
 
*/
11
 
#include <Foundation/Foundation.h>
12
 
#include <objc/Protocol.h>
13
 
 
14
 
@interface Foo: NSObject
15
 
{
16
 
}
17
 
 
18
 
+ foo;
19
 
- (BOOL) conformsToProtocol: (Protocol *) aProtocol;
20
 
- (BOOL) respondsToSelector: (SEL) aSelector;
21
 
- copyWithZone: (NSZone *) zone;
22
 
- mutableCopyWithZone: (NSZone *) zone;
23
 
@end
24
 
 
25
 
 
26
 
@implementation Foo
27
 
 
28
 
+ foo
29
 
{
30
 
  return [[[Foo alloc] init] autorelease];
31
 
}
32
 
 
33
 
- (BOOL) conformsToProtocol: (Protocol *) aProtocol
34
 
{
35
 
  BOOL ret = [super conformsToProtocol: aProtocol];
36
 
 
37
 
  NSLog(@"-[<%@:0x%x> %@<%s>] -> %@",
38
 
    NSStringFromClass([self class]), self, NSStringFromSelector(_cmd),
39
 
    [aProtocol name], ret ? @"YES" : @"NO");
40
 
  return ret;
41
 
}
42
 
 
43
 
        
44
 
- (BOOL) respondsToSelector: (SEL) aSelector
45
 
{
46
 
 BOOL ret = [super respondsToSelector: aSelector];
47
 
 
48
 
 if (![NSStringFromSelector(aSelector) hasPrefix:@"description"])
49
 
    NSLog(@"-[<%@:0x%x> %@%@] -> %@",
50
 
      NSStringFromClass([self class]), self, NSStringFromSelector(_cmd),
51
 
      NSStringFromSelector(aSelector), ret ? @"YES" : @"NO");
52
 
  return ret;
53
 
}
54
 
 
55
 
- copyWithZone: (NSZone *) zone
56
 
{
57
 
  id ret = [Foo foo];
58
 
 
59
 
  NSLog(@"-[<%@:0x%x> %@0x%x] -> <%@:0x%x>",
60
 
    NSStringFromClass([self class]), self,
61
 
    NSStringFromSelector(_cmd), zone, NSStringFromClass([ret class]), ret);
62
 
  return ret;
63
 
}
64
 
 
65
 
- mutableCopyWithZone: (NSZone *) zone
66
 
{
67
 
  id ret = [Foo foo];
68
 
 
69
 
  NSLog(@"-[<%@:0x%x> %@0x%x] -> <%@:0x%x>",
70
 
    NSStringFromClass([self class]), self,
71
 
    NSStringFromSelector(_cmd), zone, NSStringFromClass([ret class]), ret);
72
 
  return ret;
73
 
}
74
 
 
75
 
- retain
76
 
{
77
 
  id ret = [super retain];
78
 
 
79
 
  NSLog(@"-[<%@:0x%x> %@] -> retainCount = %d",
80
 
    NSStringFromClass([self class]), self,
81
 
    NSStringFromSelector(_cmd), [ret retainCount]);
82
 
  return ret;
83
 
}
84
 
 
85
 
@end
86
 
 
87
 
void
88
 
isDeepArrayCopy(NSArray  *obj1, NSArray  *obj2)
89
 
{
90
 
  id obj1FirstObject = [obj1 objectAtIndex:0];
91
 
  id obj2FirstObject = [obj2 objectAtIndex:0];
92
 
 
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)");
97
 
}
98
 
 
99
 
void
100
 
isDeepDictionaryCopy(NSDictionary  *obj1, NSDictionary  *obj2)
101
 
{
102
 
  id obj1FirstObject = [obj1 objectForKey: @"Key"];
103
 
  id obj2FirstObject = [obj2 objectForKey: @"Key"];
104
 
 
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)");
109
 
}
110
 
 
111
 
void
112
 
isDeepSetCopy(NSSet  *obj1, NSSet  *obj2)
113
 
{
114
 
  id obj1FirstObject = [obj1 anyObject];
115
 
  id obj2FirstObject = [obj2 anyObject];
116
 
 
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)");
121
 
}
122
 
 
123
 
 
124
 
int main(int argc, char **argv)
125
 
{
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];
142
 
  NSSet                 *aSetCopy;
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();
149
 
 
150
 
  while (zone != 0)
151
 
    {
152
 
      NSLog(@"Copying from zone 0x%x -> 0x%x", NSDefaultMallocZone(), zone);
153
 
 
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];
162
 
 
163
 
      NSLog(@"MutableArray: %@", aMutableArray);
164
 
      NSLog(@"MutableArrayCopy: %@", aMutableArrayCopy);
165
 
      NSLog(@"MutableArrayMutableCopy: %@", aMutableArrayMutableCopy);
166
 
      NSLog(@"anArray: %@", anArray);
167
 
      NSLog(@"anArrayCopy: %@", anArrayCopy);
168
 
      NSLog(@"anArrayCopy: %@", anArrayMutableCopy);
169
 
 
170
 
      NSLog(@"Test MutableArray against Copy");
171
 
      isDeepArrayCopy(aMutableArray, aMutableArrayCopy);
172
 
 
173
 
      NSLog(@"Test MutableArray against MutableCopy");
174
 
      isDeepArrayCopy(aMutableArray, aMutableArrayMutableCopy);
175
 
 
176
 
      NSLog(@"Test Array against Copy");
177
 
      isDeepArrayCopy(anArray, anArrayCopy);
178
 
 
179
 
      NSLog(@"Test Array against MutableCopy");
180
 
      isDeepArrayCopy(anArray, anArrayMutableCopy);
181
 
 
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];
190
 
 
191
 
      NSLog(@"MutableDictionary: %@", aMutableDictionary);
192
 
      NSLog(@"MutableDictionaryCopy: %@", aMutableDictionaryCopy);
193
 
      NSLog(@"MutableDictionaryMutableCopy: %@", aMutableDictionaryMutableCopy);
194
 
      NSLog(@"aDictionary: %@", aDictionary);
195
 
      NSLog(@"aDictionaryCopy: %@", aDictionaryCopy);
196
 
      NSLog(@"aDictionaryCopy: %@", aDictionaryMutableCopy);
197
 
 
198
 
      NSLog(@"Test MutableDictionary against Copy");
199
 
      isDeepDictionaryCopy(aMutableDictionary, aMutableDictionaryCopy);
200
 
 
201
 
      NSLog(@"Test MutableDictionary against MutableCopy");
202
 
      isDeepDictionaryCopy(aMutableDictionary, aMutableDictionaryMutableCopy);
203
 
 
204
 
      NSLog(@"Test Dictionary against Copy");
205
 
      isDeepDictionaryCopy(aDictionary, aDictionaryCopy);
206
 
 
207
 
      NSLog(@"Test Dictionary against MutableCopy");
208
 
      isDeepDictionaryCopy(aDictionary, aDictionaryMutableCopy);
209
 
 
210
 
      NSLog(@"MutableSet -copy");
211
 
      aMutableSetCopy = [aMutableSet copyWithZone:zone];
212
 
      NSLog(@"MutableSet -mutableCopy");
213
 
      aMutableSetMutableCopy = [aMutableSet mutableCopyWithZone:zone];
214
 
      NSLog(@"Set -copy");
215
 
      aSetCopy = [aSet copyWithZone:zone];
216
 
      NSLog(@"Set -mutableCopy");
217
 
      aSetMutableCopy = [aSet mutableCopyWithZone:zone];
218
 
 
219
 
      NSLog(@"MutableSet: %@", aMutableSet);
220
 
      NSLog(@"MutableSetCopy: %@", aMutableSetCopy);
221
 
      NSLog(@"MutableSetMutableCopy: %@", aMutableSetMutableCopy);
222
 
      NSLog(@"aSet: %@", aSet);
223
 
      NSLog(@"aSetCopy: %@", aSetCopy);
224
 
      NSLog(@"aSetCopy: %@", aSetMutableCopy);
225
 
 
226
 
      NSLog(@"Test MutableSet against Copy");
227
 
      isDeepSetCopy(aMutableSet, aMutableSetCopy);
228
 
 
229
 
      NSLog(@"Test MutableSet against MutableCopy");
230
 
      isDeepSetCopy(aMutableSet, aMutableSetMutableCopy);
231
 
 
232
 
      NSLog(@"Test Set against Copy");
233
 
      isDeepSetCopy(aSet, aSetCopy);
234
 
 
235
 
      NSLog(@"Test Set against MutableCopy");
236
 
      isDeepSetCopy(aSet, aSetMutableCopy);
237
 
 
238
 
      if (zone == NSDefaultMallocZone())
239
 
        zone = NSCreateZone(NSPageSize(), NSPageSize(), YES);
240
 
      else
241
 
        zone = 0;
242
 
    }
243
 
  [thePool release];
244
 
 
245
 
  return 0;
246
 
}
247