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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSExpression.h>
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSPredicate.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#import "Foundation/NSException.h"
#import "Foundation/NSDateFormatter.h"
void
testKVC(NSDictionary *dict)
{
PASS([@"A Title" isEqual: [dict valueForKey: @"title"]], "valueForKeyPath: with string");
PASS([@"A Title" isEqual: [dict valueForKeyPath: @"title"]], "valueForKeyPath: with string");
PASS([@"John" isEqual: [dict valueForKeyPath: @"Record1.Name"]], "valueForKeyPath: with string");
PASS(30 == [[dict valueForKeyPath: @"Record2.Age"] intValue], "valueForKeyPath: with int");
}
void
testContains(NSDictionary *dict)
{
NSPredicate *p;
p = [NSPredicate predicateWithFormat: @"%@ CONTAINS %@", @"AABBBAA", @"BBB"];
PASS([p evaluateWithObject: dict], "%%@ CONTAINS %%@");
p = [NSPredicate predicateWithFormat: @"%@ IN %@", @"BBB", @"AABBBAA"];
PASS([p evaluateWithObject: dict], "%%@ IN %%@");
}
void
testString(NSDictionary *dict)
{
NSPredicate *p;
p = [NSPredicate predicateWithFormat: @"%K == %@", @"Record1.Name", @"John"];
PASS([p evaluateWithObject: dict], "%%K == %%@");
p = [NSPredicate predicateWithFormat: @"%K MATCHES[c] %@", @"Record1.Name", @"john"];
PASS([p evaluateWithObject: dict], "%%K MATCHES[c] %%@");
p = [NSPredicate predicateWithFormat: @"%K BEGINSWITH %@", @"Record1.Name", @"Jo"];
PASS([p evaluateWithObject: dict], "%%K BEGINSWITH %%@");
p = [NSPredicate predicateWithFormat: @"(%K == %@) AND (%K == %@)", @"Record1.Name", @"John", @"Record2.Name", @"Mary"];
PASS([p evaluateWithObject: dict], "(%%K == %%@) AND (%%K == %%@)");
NSMutableArray *strings = [NSMutableArray arrayWithObjects: @"a", @"aa",
@"aaa", @"aaaa", nil];
NSArray *expect = [NSMutableArray arrayWithObjects: @"aaa", @"aaaa", nil];
p = [NSPredicate predicateWithFormat: @"self beginswith 'aaa'"];
[strings filterUsingPredicate: p];
PASS_EQUAL(strings, expect, "filter using BEGINSWITH")
}
void
testInteger(NSDictionary *dict)
{
NSPredicate *p;
p = [NSPredicate predicateWithFormat: @"%K == %d", @"Record1.Age", 34];
PASS([p evaluateWithObject: dict], "%%K == %%d");
p = [NSPredicate predicateWithFormat: @"%K = %@", @"Record1.Age", [NSNumber numberWithInt: 34]];
PASS([p evaluateWithObject: dict], "%%K = %%@");
p = [NSPredicate predicateWithFormat: @"%K == %@", @"Record1.Age", [NSNumber numberWithInt: 34]];
PASS([p evaluateWithObject: dict], "%%K == %%@");
p = [NSPredicate predicateWithFormat: @"%K < %d", @"Record1.Age", 40];
PASS([p evaluateWithObject: dict], "%%K < %%d");
p = [NSPredicate predicateWithFormat: @"%K < %@", @"Record1.Age", [NSNumber numberWithInt: 40]];
PASS([p evaluateWithObject: dict], "%%K < %%@");
p = [NSPredicate predicateWithFormat: @"%K <= %@", @"Record1.Age", [NSNumber numberWithInt: 40]];
PASS([p evaluateWithObject: dict], "%%K <= %%@");
p = [NSPredicate predicateWithFormat: @"%K <= %@", @"Record1.Age", [NSNumber numberWithInt: 34]];
PASS([p evaluateWithObject: dict], "%%K <= %%@");
p = [NSPredicate predicateWithFormat: @"%K > %@", @"Record1.Age", [NSNumber numberWithInt: 20]];
PASS([p evaluateWithObject: dict], "%%K > %%@");
p = [NSPredicate predicateWithFormat: @"%K >= %@", @"Record1.Age", [NSNumber numberWithInt: 34]];
PASS([p evaluateWithObject: dict], "%%K >= %%@");
p = [NSPredicate predicateWithFormat: @"%K >= %@", @"Record1.Age", [NSNumber numberWithInt: 20]];
PASS([p evaluateWithObject: dict], "%%K >= %%@");
p = [NSPredicate predicateWithFormat: @"%K != %@", @"Record1.Age", [NSNumber numberWithInt: 20]];
PASS([p evaluateWithObject: dict], "%%K != %%@");
p = [NSPredicate predicateWithFormat: @"%K <> %@", @"Record1.Age", [NSNumber numberWithInt: 20]];
PASS([p evaluateWithObject: dict], "%%K <> %%@");
p = [NSPredicate predicateWithFormat: @"%K BETWEEN %@", @"Record1.Age", [NSArray arrayWithObjects: [NSNumber numberWithInt: 20], [NSNumber numberWithInt: 40], nil]];
PASS([p evaluateWithObject: dict], "%%K BETWEEN %%@");
p = [NSPredicate predicateWithFormat: @"(%K == %d) OR (%K == %d)", @"Record1.Age", 34, @"Record2.Age", 34];
PASS([p evaluateWithObject: dict], "(%%K == %%d) OR (%%K == %%d)");
}
void
testFloat(NSDictionary *dict)
{
NSPredicate *p;
p = [NSPredicate predicateWithFormat: @"%K < %f", @"Record1.Age", 40.5];
PASS([p evaluateWithObject: dict], "%%K < %%f");
p = [NSPredicate predicateWithFormat: @"%f > %K", 40.5, @"Record1.Age"];
PASS([p evaluateWithObject: dict], "%%f > %%K");
}
void
testAttregate(NSDictionary *dict)
{
NSPredicate *p;
p = [NSPredicate predicateWithFormat: @"%@ IN %K", @"Kid1", @"Record1.Children"];
PASS([p evaluateWithObject: dict], "%%@ IN %%K");
p = [NSPredicate predicateWithFormat: @"Any %K == %@", @"Record2.Children", @"Girl1"];
PASS([p evaluateWithObject: dict], "Any %%K == %%@");
}
void
testBlock(NSDictionary* dict)
{
START_SET("Block predicates");
# if __has_feature(blocks)
NSPredicate *p = nil;
NSPredicate *p2 = nil;
NSDictionary *v =
[NSDictionary dictionaryWithObjectsAndKeys: @"Record2", @"Key", nil];
p = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bindings)
{
NSString *key = [bindings objectForKey: @"Key"];
if (nil == key)
{
key = @"Record1";
}
NSString *value = [[obj objectForKey: key] objectForKey: @"Name"];
return [value isEqualToString: @"John"];
}];
PASS([p evaluateWithObject: dict], "BLOCKPREDICATE() without bindings");
PASS(![p evaluateWithObject: dict
substitutionVariables: v], "BLOCKPREDICATE() with bound variables");
p2 = [p predicateWithSubstitutionVariables:
[NSDictionary dictionaryWithObjectsAndKeys: @"Record2", @"Key", nil]];
PASS(p2 != nil, "BLOCKPREDICATE() instantiated from template");
# ifdef APPLE
/* The next test is known to be fail on OS X, so mark it as hopeful there.
* cf. rdar://25059737
*/
testHopeful = YES;
# endif
PASS(![p2 evaluateWithObject: dict],
"BLOCKPREDICATE() with bound variables in separate object");
# ifdef APPLE
testHopeful = NO;
# endif
# else
SKIP("No blocks support in the compiler.");
# endif
END_SET("Block predicates");
}
void testArray(void)
{
NSArray *array;
NSPredicate *predicate;
array = [NSArray arrayWithObjects:
[NSNumber numberWithInteger: 1],
[NSNumber numberWithInteger: 2],
[NSNumber numberWithInteger: 0],
nil];
predicate = [NSPredicate predicateWithFormat: @"SELF[FIRST] = 1"];
PASS([predicate evaluateWithObject: array], "first is one")
predicate = [NSPredicate predicateWithFormat: @"SELF[LAST] = 0"];
PASS([predicate evaluateWithObject: array], "last is zero")
predicate = [NSPredicate predicateWithFormat: @"SELF[SIZE] = 3"];
PASS([predicate evaluateWithObject: array], "size is three")
}
void testExpressions(void)
{
NSExpression *expression = [NSExpression expressionWithFormat: @"%d*%f",3,3.5];
PASS(expression != nil, "expressionWithFormat: returns an initialized expression");
id value = [expression expressionValueWithObject: nil context: nil];
PASS(value != nil, "Expression evaluation returns a value");
NSExpression *expression2 = [NSExpression expressionWithFormat: @"%f*%f"
argumentArray: [NSArray arrayWithObjects:
[NSNumber numberWithFloat: 3.4], [NSNumber numberWithFloat: 3.1], nil]];
PASS(expression2 != nil, "expressionWithFormat:argumentArray: returns an initialized expression");
id value2 = [expression2 expressionValueWithObject: nil context: nil];
PASS(value2 != nil, "Expression evaluation returns a value");
NSExpression *expression3 = [NSExpression expressionForAggregate:[NSArray arrayWithObjects: expression, expression2, nil]];
PASS(expression3 != nil, "expressionForAggregate: returns an initialized expression");
id value3 = [expression3 expressionValueWithObject: nil context: nil];
PASS(value3 != nil, "Expression evaluation returns a value");
PASS([value3 isKindOfClass: [NSArray class]], "value is an NSArray");
NSExpression *set1 = [NSExpression expressionForAggregate: [NSArray arrayWithObjects:
[NSExpression expressionForConstantValue: @"A"],
[NSExpression expressionForConstantValue: @"B"],
[NSExpression expressionForConstantValue: @"C"], nil]];
NSExpression *set2 = [NSExpression expressionForAggregate: [NSArray arrayWithObjects:
[NSExpression expressionForConstantValue: @"C"],
[NSExpression expressionForConstantValue: @"D"],
[NSExpression expressionForConstantValue: @"E"], nil]];
NSExpression *expression4 = [NSExpression expressionForIntersectSet:set1 with:set2];
id value4 = [expression4 expressionValueWithObject:nil context:nil];
BOOL flag4 = [value4 isEqualToSet: [NSSet setWithObjects: @"C", nil]];
PASS(value4 != nil, "Expression evaluation returns a value");
PASS([value4 isKindOfClass: [NSSet class]], "value is an NSSet");
PASS(flag4 == YES, "returns correct value");
NSExpression *expression5 = [NSExpression expressionForUnionSet:set1 with:set2];
id value5 = [expression5 expressionValueWithObject:nil context:nil];
// BOOL flag5 = [value5 isEqualToSet: [NSSet setWithObjects: @"A", @"B", @"C" @"E", @"E", nil]];
PASS(value5 != nil, "Expression evaluation returns a value");
PASS([value5 isKindOfClass: [NSSet class]], "value is an NSSet");
// PASS(flag5 == YES, "returns correct value");
NSExpression *expression6 = [NSExpression expressionForMinusSet:set1 with:set2];
id value6 = [expression6 expressionValueWithObject:nil context:nil];
BOOL flag6 = [value6 isEqualToSet: [NSSet setWithObjects: @"A", @"B", nil]];
PASS(value6 != nil, "Expression evaluation returns a value");
PASS([value6 isKindOfClass: [NSSet class]], "value is an NSSet");
PASS(flag6 == YES, "returns correct value");
// This should error out...
BOOL raised = NO;
NS_DURING
{
NSExpression *expression7 = [NSExpression expressionForMinusSet:set1 with:expression2];
NSLog(@"%@",[expression7 expressionValueWithObject:nil context:nil]);
}
NS_HANDLER
{
raised = YES;
NSLog(@"exception = %@", localException);
}
NS_ENDHANDLER;
PASS(raised, "Raise an exception when a set based NSExpression tries to process a non-set");
}
int main()
{
NSArray *filtered;
NSArray *pitches;
NSArray *expect;
NSArray *a;
NSMutableDictionary *dict;
NSPredicate *p;
NSDictionary *d;
START_SET("basic")
dict = [[NSMutableDictionary alloc] init];
[dict setObject: @"A Title" forKey: @"title"];
d = [NSDictionary dictionaryWithObjectsAndKeys:
@"John", @"Name",
[NSNumber numberWithInt: 34], @"Age",
[NSArray arrayWithObjects: @"Kid1", @"Kid2", nil], @"Children",
nil];
[dict setObject: d forKey: @"Record1"];
d = [NSDictionary dictionaryWithObjectsAndKeys:
@"Mary", @"Name",
[NSNumber numberWithInt: 30], @"Age",
[NSArray arrayWithObjects: @"Kid1", @"Girl1", nil], @"Children",
nil];
[dict setObject: d forKey: @"Record2"];
testKVC(dict);
testContains(dict);
testString(dict);
testInteger(dict);
testFloat(dict);
testAttregate(dict);
testBlock(dict);
[dict release];
pitches = [NSArray arrayWithObjects:
@"Do", @"Re", @"Mi", @"Fa", @"So", @"La", nil];
expect = [NSArray arrayWithObjects: @"Do", nil];
filtered = [pitches filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat: @"SELF == 'Do'"]];
PASS([filtered isEqual: expect], "filter with SELF");
filtered = [pitches filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat: @"description == 'Do'"]];
PASS([filtered isEqual: expect], "filter with description");
filtered = [pitches filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat: @"SELF == '%@'", @"Do"]];
PASS([filtered isEqual: [NSArray array]], "filter with format");
PASS([NSExpression expressionForEvaluatedObject]
== [NSExpression expressionForEvaluatedObject],
"expressionForEvaluatedObject is unique");
p = [NSPredicate predicateWithFormat: @"SELF == 'aaa'"];
PASS([p evaluateWithObject: @"aaa"], "SELF equality works");
d = [NSDictionary dictionaryWithObjectsAndKeys:
@"2", @"foo", nil];
p = [NSPredicate predicateWithFormat: @"SELF.foo <= 2"];
PASS_EXCEPTION([p evaluateWithObject: d], NSInvalidArgumentException, "SELF.foo <= 2 throws an exception");
a = [NSArray arrayWithObjects: @"a", @"b", @"c", @"d", nil];
expect = [NSArray arrayWithObjects: @"b", @"c", nil];
p = [NSPredicate predicateWithFormat:@"SELF BETWEEN {%@, %@}", @"b", @"c"];
PASS_EQUAL([a filteredArrayUsingPredicate: p], expect, "BETWEEN on string array works");
NSNumber *num1 = [NSNumber numberWithInt: 1];
NSNumber *num2 = [NSNumber numberWithInt: 2];
NSNumber *num3 = [NSNumber numberWithInt: 3];
a = [NSArray arrayWithObjects: num1, num2, num3, nil];
expect = [NSArray arrayWithObjects: num2, num3, nil];
p = [NSPredicate predicateWithFormat:@"SELF BETWEEN {%d, %d}", 2, 3];
PASS_EQUAL([a filteredArrayUsingPredicate: p], expect, "BETWEEN on number array works");
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *first = [dateFormatter dateFromString:@"2024-01-01"];
NSDate *second = [dateFormatter dateFromString:@"2024-02-01"];
NSDate *third = [dateFormatter dateFromString:@"2024-03-01"];
NSDate *fourth = [dateFormatter dateFromString:@"2024-04-01"];
a = [NSArray arrayWithObjects: first, second, third, fourth, nil];
expect = [NSArray arrayWithObjects: second, third, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN {%@, %@}", second, third];
PASS_EQUAL([a filteredArrayUsingPredicate:predicate], expect, "BETWEEN on date array works");
[dateFormatter release];
#if 0
if ([p respondsToSelector: @selector(subpredicates)])
NSLog(@"subpredicates=%@", [(NSCompoundPredicate *)p subpredicates]);
if ([p respondsToSelector: @selector(leftExpression)])
NSLog(@"left=%@", [(NSComparisonPredicate *)p leftExpression]);
if ([p respondsToSelector: @selector(rightExpression)])
NSLog(@"right=%@", [(NSComparisonPredicate *)p rightExpression]);
#endif
p = [NSPredicate predicateWithFormat:
@"%K like %@+$b+$c", @"$single", @"b\""];
PASS_EQUAL([p predicateFormat], @"$single LIKE (\"b\\\"\" + $b) + $c",
"predicate created with format has the format is preserved");
p = [p predicateWithSubstitutionVariables:
[NSDictionary dictionaryWithObjectsAndKeys:
@"val_for_single_string", @"single", // why %K does not make a variable
@"val_for_$b", @"b",
@"val_for_$c", @"c",
nil]];
PASS_EQUAL([p predicateFormat],
@"$single LIKE (\"b\\\"\" + \"val_for_$b\") + \"val_for_$c\"",
"Predicate created by substitution has the expected format");
a = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: 1], @"a", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: 2], @"a", nil],
nil];
p = [NSPredicate predicateWithFormat: @"sum(a) == 3"];
PASS([p evaluateWithObject: a], "aggregate sum works");
p = [NSPredicate predicateWithFormat: @"self IN %@",
[NSArray arrayWithObject:@"yes"]];
a = [[NSArray arrayWithObjects:@"yes", @"no", nil]
filteredArrayUsingPredicate: p];
PASS_EQUAL([a description], @"(yes)",
"predicate created with format can filter an array")
testArray();
testExpressions();
END_SET("basic")
return 0;
}
|