Split ExceptionUtils out of Test.
5 // Created by Jens Alfke on 1/5/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import "CollectionUtils.h"
13 NSDictionary* _dictof(const struct _dictpair* pairs, size_t count)
16 id objects[count], keys[count];
18 for( size_t i=0; i<count; i++,pairs++ ) {
20 objects[n] = pairs->value;
25 return [NSDictionary dictionaryWithObjects: objects forKeys: keys count: n];
29 NSMutableDictionary* _mdictof(const struct _dictpair* pairs, size_t count)
32 id objects[count], keys[count];
34 for( size_t i=0; i<count; i++,pairs++ ) {
36 objects[n] = pairs->value;
41 return [NSMutableDictionary dictionaryWithObjects: objects forKeys: keys count: n];
45 NSArray* $apply( NSArray *src, SEL selector, id defaultValue )
47 NSMutableArray *dst = [NSMutableArray arrayWithCapacity: src.count];
48 for( id obj in src ) {
49 id result = [obj performSelector: selector] ?: defaultValue;
50 [dst addObject: result];
55 NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue )
57 NSMutableArray *dst = [NSMutableArray arrayWithCapacity: src.count];
58 for( id obj in src ) {
59 id result = [obj valueForKeyPath: keyPath] ?: defaultValue;
60 [dst addObject: result];
66 BOOL $equal(id obj1, id obj2) // Like -isEqual: but works even if either/both are nil
69 return obj2 && [obj1 isEqual: obj2];
75 NSValue* _box(const void *value, const char *encoding)
77 // file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.DeveloperTools.docset/Contents/Resources/Documents/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html
79 if( e=='r' ) // ignore 'const' modifier
82 case 'c': return [NSNumber numberWithChar: *(char*)value];
83 case 'C': return [NSNumber numberWithUnsignedChar: *(char*)value];
84 case 's': return [NSNumber numberWithShort: *(short*)value];
85 case 'S': return [NSNumber numberWithUnsignedShort: *(unsigned short*)value];
86 case 'i': return [NSNumber numberWithInt: *(int*)value];
87 case 'I': return [NSNumber numberWithUnsignedInt: *(unsigned int*)value];
88 case 'l': return [NSNumber numberWithLong: *(long*)value];
89 case 'L': return [NSNumber numberWithUnsignedLong: *(unsigned long*)value];
90 case 'q': return [NSNumber numberWithLongLong: *(long long*)value];
91 case 'Q': return [NSNumber numberWithUnsignedLongLong: *(unsigned long long*)value];
92 case 'f': return [NSNumber numberWithFloat: *(float*)value];
93 case 'd': return [NSNumber numberWithDouble: *(double*)value];
94 case '*': return [NSString stringWithUTF8String: *(char**)value];
95 case '@': return *(id*)value;
96 default: return [NSValue value: value withObjCType: encoding];
101 id _cast( Class requiredClass, id object )
103 if( object && ! [object isKindOfClass: requiredClass] )
104 [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p",
105 requiredClass,[object class],object];
109 id _castNotNil( Class requiredClass, id object )
111 if( ! [object isKindOfClass: requiredClass] )
112 [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p",
113 requiredClass,[object class],object];
117 id _castIf( Class requiredClass, id object )
119 if( object && ! [object isKindOfClass: requiredClass] )
124 NSArray* _castArrayOf(Class itemClass, NSArray *a)
127 foreach( item, $cast(NSArray,a) )
128 _cast(itemClass,item);
133 void setObj( id *var, id value )
135 if( value != *var ) {
137 *var = [value retain];
141 BOOL ifSetObj( id *var, id value )
143 if( value != *var && ![value isEqual: *var] ) {
145 *var = [value retain];
153 void setString( NSString **var, NSString *value )
155 if( value != *var ) {
162 BOOL ifSetString( NSString **var, NSString *value )
164 if( value != *var && ![value isEqualToString: *var] ) {
174 NSString* $string( const char *utf8Str )
177 return [NSString stringWithCString: utf8Str encoding: NSUTF8StringEncoding];
183 @implementation NSArray (MYUtils)
185 - (BOOL) my_containsObjectIdenticalTo: (id)object
187 return [self indexOfObjectIdenticalTo: object] != NSNotFound;
195 @implementation NSSet (MYUtils)
197 + (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2
199 if( set1 == set2 || set2.count==0 )
201 else if( set1.count==0 )
204 NSMutableSet *result = [set1 mutableCopy];
205 [result unionSet: set2];
206 return [result autorelease];
210 + (NSSet*) my_intersectionOfSet: (NSSet*)set1 andSet: (NSSet*)set2
212 if( set1 == set2 || set1.count==0 )
214 else if( set2.count==0 )
217 NSMutableSet *result = [set1 mutableCopy];
218 [result intersectSet: set2];
219 return [result autorelease];
223 + (NSSet*) my_differenceOfSet: (NSSet*)set1 andSet: (NSSet*)set2
225 if( set1.count==0 || set2.count==0 )
227 else if( set1==set2 )
230 NSMutableSet *result = [set1 mutableCopy];
231 [result minusSet: set2];
232 return [result autorelease];
242 TestCase(CollectionUtils) {
243 NSArray *a = $array(@"foo",@"bar",@"baz");
245 NSArray *aa = [NSArray arrayWithObjects: @"foo",@"bar",@"baz",nil];
248 const char *cstr = "a C string";
249 id o = $object(cstr);
251 CAssertEqual(o,@"a C string");
253 NSDictionary *d = $dict({@"int", $object(1)},
254 {@"double", $object(-1.1)},
255 {@"char", $object('x')},
256 {@"ulong", $object(1234567UL)},
257 {@"longlong",$object(987654321LL)},
258 {@"cstr", $object(cstr)});
260 NSDictionary *dd = [NSDictionary dictionaryWithObjectsAndKeys:
261 [NSNumber numberWithInt: 1], @"int",
262 [NSNumber numberWithDouble: -1.1], @"double",
263 [NSNumber numberWithChar: 'x'], @"char",
264 [NSNumber numberWithUnsignedLong: 1234567UL], @"ulong",
265 [NSNumber numberWithDouble: 987654321LL], @"longlong",
266 @"a C string", @"cstr",