From 7b657f4777af234702833efa4e7ade0fa50e8841 Mon Sep 17 00:00:00 2001 From: hmelder Date: Sat, 9 Dec 2023 20:43:04 +0100 Subject: [PATCH] NSPredicate: Check if predicates with NSNull work as expected --- Tests/base/NSPredicate/nsnull.m | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Tests/base/NSPredicate/nsnull.m diff --git a/Tests/base/NSPredicate/nsnull.m b/Tests/base/NSPredicate/nsnull.m new file mode 100644 index 000000000..d443fed88 --- /dev/null +++ b/Tests/base/NSPredicate/nsnull.m @@ -0,0 +1,34 @@ +#import "ObjectTesting.h" +#import "Foundation/NSAutoreleasePool.h" +#import "Foundation/NSPredicate.h" +#import "Foundation/NSArray.h" +#import "Foundation/NSNull.h" + +int main(void) { + NSAutoreleasePool *arp = [NSAutoreleasePool new]; + NSArray *array, *filtered; + NSPredicate *predicate; + + // Basic filtering with NSPredicate + array = @[@{@"key": @"value1"}, @{@"key": @"value2"}, [NSNull null]]; + predicate = [NSPredicate predicateWithFormat:@"key == %@", @"value2"]; + filtered = [array filteredArrayUsingPredicate: predicate]; + + PASS(filtered.count == 1 && [filtered[0][@"key"] isEqualToString:@"value2"], + "NSPredicate should correctly filter array including NSNull"); + + // Filtering with NSPredicate where no match is found + predicate = [NSPredicate predicateWithFormat:@"key == %@", @"nonexistent"]; + filtered = [array filteredArrayUsingPredicate: predicate]; + PASS(filtered.count == 0, + "NSPredicate should return an empty array when no match is found"); + + // Filtering with NSPredicate with a different key + predicate = [NSPredicate predicateWithFormat:@"anotherKey == %@", @"value1"]; + filtered = [array filteredArrayUsingPredicate: predicate]; + PASS(filtered.count == 0, + "NSPredicate should return an empty array when filtering with a non-existent key"); + + [arp release]; + return 0; +}