2011-02-16 08:21:17 +00:00
# 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 >
void
testKVC ( NSDictionary * dict )
{
2013-02-15 09:59:26 +00:00
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" ) ;
2011-02-16 08:21:17 +00:00
}
void
testContains ( NSDictionary * dict )
{
NSPredicate * p ;
2013-02-15 09:59:26 +00:00
p = [ NSPredicate predicateWithFormat : @ "%@ CONTAINS %@" , @ "AABBBAA" , @ "BBB" ] ;
PASS ( [ p evaluateWithObject : dict ] , "%%@ CONTAINS %%@" ) ;
p = [ NSPredicate predicateWithFormat : @ "%@ IN %@" , @ "BBB" , @ "AABBBAA" ] ;
PASS ( [ p evaluateWithObject : dict ] , "%%@ IN %%@" ) ;
2011-02-16 08:21:17 +00:00
}
void
testString ( NSDictionary * dict )
{
NSPredicate * p ;
2013-02-15 09:59:26 +00:00
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 == %%@)" ) ;
2011-02-16 08:21:17 +00:00
}
void
testInteger ( NSDictionary * dict )
{
NSPredicate * p ;
2013-02-15 09:59:26 +00:00
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)" ) ;
2011-02-16 08:21:17 +00:00
}
void
testFloat ( NSDictionary * dict )
{
NSPredicate * p ;
2013-02-15 09:59:26 +00:00
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" ) ;
2011-02-16 08:21:17 +00:00
}
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 == %%@" ) ;
}
2016-03-09 13:19:35 +00:00
void
testBlock ( NSDictionary * dict )
{
START_SET ( "Block predicates" ) ;
2016-03-09 15:38:32 +00:00
# if __has _feature ( blocks )
2016-03-09 13:19:35 +00:00
NSPredicate * p = nil ;
NSPredicate * p2 = nil ;
2016-03-09 15:38:32 +00:00
NSDictionary * v =
[ NSDictionary dictionaryWithObjectsAndKeys : @ "Record2" , @ "Key" , nil ] ;
2016-03-09 13:35:20 +00:00
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" ] ;
} ] ;
2016-03-09 13:19:35 +00:00
PASS ( [ p evaluateWithObject : dict ] , "BLOCKPREDICATE() without bindings" ) ;
2016-03-09 15:38:32 +00:00
PASS ( ! [ p evaluateWithObject : dict
substitutionVariables : v ] , "BLOCKPREDICATE() with bound variables" ) ;
2016-03-09 13:19:35 +00:00
p2 = [ p predicateWithSubstitutionVariables :
[ NSDictionary dictionaryWithObjectsAndKeys : @ "Record2" , @ "Key" , nil ] ] ;
2016-03-09 15:38:32 +00:00
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
2016-03-09 13:19:35 +00:00
# else
SKIP ( "No blocks support in the compiler." ) ;
# endif
END_SET ( "Block predicates" ) ;
}
2011-02-16 08:21:17 +00:00
int main ( )
{
NSArray * filtered ;
NSArray * pitches ;
NSArray * expect ;
2016-05-19 07:21:36 +00:00
NSArray * a ;
2011-02-16 08:21:17 +00:00
NSMutableDictionary * dict ;
NSPredicate * p ;
NSDictionary * d ;
2016-05-19 07:21:36 +00:00
START_SET ( "basic" )
2011-02-16 08:21:17 +00:00
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 ) ;
2016-03-09 13:19:35 +00:00
testBlock ( dict ) ;
2011-02-16 08:21:17 +00:00
[ 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" ) ;
2017-04-09 12:45:18 +00:00
d = [ NSDictionary dictionaryWithObjectsAndKeys :
@ "2" , @ "foo" , nil ] ;
p = [ NSPredicate predicateWithFormat : @ "SELF.foo <= 2" ] ;
PASS ( [ p evaluateWithObject : d ] , "SELF.foo <= 2" ) ;
2013-02-15 09:59:26 +00:00
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" ) ;
# 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 = [ 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" ) ;
2016-05-19 07:21:36 +00:00
a = [ NSArray arrayWithObjects :
[ NSDictionary dictionaryWithObjectsAndKeys :
2016-06-08 07:45:56 +00:00
[ NSNumber numberWithInt : 1 ] , @ "a" , nil ] ,
2016-05-19 07:21:36 +00:00
[ NSDictionary dictionaryWithObjectsAndKeys :
2016-06-08 07:45:56 +00:00
[ NSNumber numberWithInt : 2 ] , @ "a" , nil ] ,
2016-05-19 07:21:36 +00:00
nil ] ;
2016-06-08 07:45:56 +00:00
p = [ NSPredicate predicateWithFormat : @ "sum(a) == 3" ] ;
2016-05-19 07:21:36 +00:00
PASS ( [ p evaluateWithObject : a ] , "aggregate sum works" ) ;
END_SET ( "basic" )
2011-02-16 08:21:17 +00:00
return 0 ;
}