diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index 0886eab2d..80cf064e8 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -1159,7 +1159,7 @@ static SEL rlSel; - (BOOL) isSubsetOfOrderedSet: (NSOrderedSet *)otherSet { - id so = nil, oo = nil; + id so, oo; NSEnumerator *selfEnum = [self objectEnumerator]; NSEnumerator *otherEnum = [otherSet objectEnumerator]; NSUInteger l = [self count]; @@ -1177,9 +1177,9 @@ static SEL rlSel; } so = [selfEnum nextObject]; // get first object in enum... - while((oo = [otherEnum nextObject]) != nil) + while ((oo = [otherEnum nextObject]) != nil) { - if([oo isEqual: so]) // if object is equal advance + if ([oo isEqual: so]) // if object is equal advance { so = [selfEnum nextObject]; if(so == nil) @@ -1194,18 +1194,30 @@ static SEL rlSel; - (BOOL) isSubsetOfSet: (NSSet *)otherSet { - id o = nil, e = nil; + id o, e; + NSUInteger l = [self count]; - // -1. If this set is empty, this method should return NO. - if ([self count] == 0) - return NO; - - // 0. Loop for all members in otherSet - e = [otherSet objectEnumerator]; - while ((o = [e nextObject])) // 1. pick a member from otherSet. + // -1. If this set is empty, this method should return YES. + if (l == 0) { - if ([self containsObject: o] == NO) // 2. check the member is in this set(self). - return NO; + return YES; + } + + // If count of set is more than otherSet it's not a subset + if (l > [otherSet count]) + { + return NO; + } + + // 0. Loop for all members in self + e = [self objectEnumerator]; + while ((o = [e nextObject])) // 1. pick a member from self. + { + if ([otherSet containsObject: o] == NO) // 2. check the member is in otherset. + { + NSLog(@"Object not contained %@", o); + return NO; + } } return YES; // if all members are in set. } diff --git a/Tests/base/NSOrderedSet/basic.m b/Tests/base/NSOrderedSet/basic.m index 532e93e99..28289cf57 100644 --- a/Tests/base/NSOrderedSet/basic.m +++ b/Tests/base/NSOrderedSet/basic.m @@ -122,6 +122,7 @@ int main() NSMutableOrderedSet *mutableTest1, *mutableTest2; NSMutableArray *testObjs = [NSMutableArray new]; NSData *data = [stringData dataUsingEncoding: NSUTF8StringEncoding]; + NSMutableSet *testSet; testObj = [NSOrderedSet new]; [testObjs addObject: testObj]; @@ -228,6 +229,13 @@ int main() PASS(isSubset, "mutableTest2 is subset of mutableTest1"); + testSet = [NSMutableSet set]; + [testSet addObject: o7]; + [testSet addObject: o8]; + isSubset = [mutableTest2 isSubsetOfSet: testSet]; + PASS(isSubset, + "mutableTest2 is subset of testSet"); + id o9 = @"Hello"; id o10 = @"World"; id o11 = @"Ready"; @@ -242,6 +250,12 @@ int main() PASS(isSubset == NO, "mutableTest2 is not subset of mutableTest1"); + testSet = [NSMutableSet set]; + [testSet addObject: o9]; + isSubset = [mutableTest2 isSubsetOfSet: testSet]; + PASS(isSubset == NO, + "mutableTest2 is not subset of testSet"); + o9 = @"Hello"; o10 = @"World"; o11 = @"Ready"; @@ -260,6 +274,15 @@ int main() PASS(isSubset, "mutableTest2 is subset of mutableTest1"); + testSet = [NSMutableSet set]; + [testSet addObject: o9]; + [testSet addObject: o10]; + [testSet addObject: o12]; + [testSet addObject: o11]; + isSubset = [mutableTest2 isSubsetOfSet: testSet]; + PASS(isSubset, + "mutableTest2 is subset of testSet"); + o9 = @"Hello"; o10 = @"World"; o11 = @"Ready";