Merged changes from other author

This commit is contained in:
Gregory John Casamento 2019-06-28 01:55:33 -04:00
commit 80843a9e4b
3 changed files with 424 additions and 501 deletions

View file

@ -1,22 +1,22 @@
/** Concrete implementation of GSOrderedSet and GSMutableOrderedSet
based on GNU NSOrderedSet and NSMutableOrderedSet classes
Copyright (C) 2019 Free Software Foundation, Inc.
Written by: Gregory Casamento <greg.casamento@gmail.com>
Created: May 17 2019
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
_version 2 of the License, or (at your option) any later _version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
@ -40,14 +40,8 @@
#import "GSDispatch.h"
#import "GSSorting.h"
#define GSI_ARRAY_TYPE NSRange
#define GSI_ARRAY_NO_RELEASE 0
#define GSI_ARRAY_NO_RETAIN 0
#define GSI_ARRAY_TYPES GSUNION_OBJ
#define GSI_ARRAY_RELEASE(A, X) [(X).obj release]
#define GSI_ARRAY_RETAIN(A, X) [(X).obj retain]
#import "GNUstepBase/GSIArray.h"
@interface GSOrderedSet : NSOrderedSet
@ -92,7 +86,7 @@
- (id) nextObject
{
if(current < count)
if (current < count)
{
GSIArrayItem item = GSIArrayItemAtIndex(&set->array, current);
current++;
@ -113,7 +107,7 @@
- (id) initWithOrderedSet: (GSOrderedSet*)d
{
self = [super initWithOrderedSet: d];
if(self != nil)
if (self != nil)
{
current = GSIArrayCount(&set->array);
}
@ -125,8 +119,10 @@
GSIArrayItem item;
if (current == 0)
return nil;
{
return nil;
}
item = GSIArrayItemAtIndex(&set->array, --current);
return (id)(item.obj);
}
@ -186,7 +182,7 @@ static Class mutableSetClass;
NSUInteger count = [self count];
NSUInteger i = 0;
for(i = 0; i < count; i++)
for (i = 0; i < count; i++)
{
GSIArrayItem item = GSIArrayItemAtIndex(&array, i);
size += [item.obj sizeInBytesExcluding: exclude];
@ -218,7 +214,7 @@ static Class mutableSetClass;
{
id obj = objs[i];
GSIArrayItem item;
if (objs[i] == nil)
{
DESTROY(self);
@ -227,10 +223,9 @@ static Class mutableSetClass;
}
item.obj = obj;
if(![self containsObject: obj])
if (![self containsObject: obj])
{
GSIArrayAddItem(&array, item);
RETAIN(obj);
}
}
return self;
@ -248,26 +243,20 @@ static Class mutableSetClass;
}
}
- (void) addObject: (id)anObject
{
[self insertObject: anObject atIndex: [self count]];
}
- (void) insertObject: (id)object atIndex: (NSUInteger)index
{
GSIArrayItem item;
if(object == nil)
if (object == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Tried to add nil to set"];
}
else
{
if([self containsObject: object] == NO)
if ([self containsObject: object] == NO)
{
item.obj = object;
GSIArrayInsertItem(&array, item, index);
RETAIN(object);
_version++;
}
}
@ -279,13 +268,6 @@ static Class mutableSetClass;
GSIArrayRemoveItemAtIndex(&array, index);
}
- (void) replaceObjectAtIndex: (NSUInteger)index
withObject: (id)obj
{
[self removeObjectAtIndex: index];
[self insertObject: obj atIndex: index];
}
- (id) init
{
return [self initWithCapacity: 0];
@ -305,11 +287,11 @@ static Class mutableSetClass;
// Init and fill set
self = [self initWithCapacity: count];
if(self != nil)
if (self != nil)
{
for(i = 0; i < count; i++)
for (i = 0; i < count; i++)
{
id anObject = objects[i];
id anObject = objects[i];
[self addObject: anObject];
}
}
@ -329,4 +311,3 @@ static Class mutableSetClass;
}
@end

File diff suppressed because it is too large Load diff

View file

@ -117,40 +117,35 @@ static NSString *stringData = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
int main()
{
START_SET("NSOrderedSet base")
NSOrderedSet *testObj, *testObj2;
NSMutableOrderedSet *mutableTest1, *mutableTest2;
NSMutableArray *testObjs = [NSMutableArray new];
NSData *data = [stringData dataUsingEncoding: NSUTF8StringEncoding];
NSMutableSet *testSet;
testObj = [NSOrderedSet new];
[testObjs addObject: testObj];
PASS(testObj != nil && [testObj count] == 0,
"can create an empty ordered set");
testObj = [NSOrderedSet orderedSetWithObject: @"Hello"];
[testObjs addObject: testObj];
PASS(testObj != nil && [testObj count] == 1,
"can create an ordered set with one element");
id objs[] = {@"Hello", @"Hello1"};
testObj = [NSOrderedSet orderedSetWithObjects: objs count: 2];
[testObjs addObject: testObj];
PASS(testObj != nil && [testObj count] == 2,
"can create an ordered set with multi element");
id objs1[] = {@"Hello", @"Hello"};
testObj = [NSOrderedSet orderedSetWithObjects: objs1 count: 2];
[testObjs addObject: testObj];
PASS(testObj != nil && [testObj count] == 1,
"cannot create an ordered set with multiple like elements");
id objs2[] = {@"Hello"};
testObj = [NSOrderedSet orderedSetWithObjects: objs2 count: 2];
[testObjs addObject: testObj];
PASS(testObj != nil && [testObj count] == 1,
"Does not throw exception when count != to number of elements");
NSMutableArray *arr = [NSMutableArray array];
[arr addObject: @"Hello"];
[arr addObject: @"World"];
@ -158,7 +153,7 @@ int main()
[testObjs addObject: testObj];
PASS(testObj != nil && [testObj count] == 2,
"Is able to initialize with array");
id objs3[] = {@"Hello"};
id objc4[] = {@"World"};
testObj = [NSOrderedSet orderedSetWithObjects: objs3 count: 1];
@ -168,7 +163,7 @@ int main()
BOOL result = [testObj intersectsOrderedSet: testObj2];
PASS(result == NO,
"Sets do not intersect!");
id objs5[] = {@"Hello"};
id objc6[] = {@"Hello"};
testObj = [NSOrderedSet orderedSetWithObjects: objs5 count: 1];
@ -178,99 +173,121 @@ int main()
BOOL result1 = [testObj intersectsOrderedSet: testObj2];
PASS(result1 == YES,
"Sets do intersect!");
id o1 = @"Hello";
id o2 = @"World";
mutableTest1 = [NSMutableOrderedSet orderedSet];
[mutableTest1 addObject:o1];
[mutableTest1 addObject: o1];
[testObjs addObject: mutableTest1];
mutableTest2 = [NSMutableOrderedSet orderedSet];
[mutableTest2 addObject:o2];
[mutableTest2 addObject: o2];
[testObjs addObject: mutableTest2];
[mutableTest1 unionOrderedSet:mutableTest2];
[mutableTest1 unionOrderedSet: mutableTest2];
PASS(mutableTest1 != nil && mutableTest2 != nil && [mutableTest1 count] == 2,
"mutableSets union properly");
id o3 = @"Hello";
id o4 = @"World";
mutableTest1 = [NSMutableOrderedSet orderedSet];
[mutableTest1 addObject:o3];
[mutableTest1 addObject: o3];
[testObjs addObject: mutableTest1];
mutableTest2 = [NSMutableOrderedSet orderedSet];
[mutableTest2 addObject:o4];
[mutableTest2 addObject: o4];
[testObjs addObject: mutableTest2];
[mutableTest1 intersectOrderedSet:mutableTest2];
[mutableTest1 intersectOrderedSet: mutableTest2];
PASS(mutableTest1 != nil && mutableTest2 != nil && [mutableTest1 count] == 0,
"mutableSets do not intersect");
id o5 = @"Hello";
id o6 = @"Hello";
mutableTest1 = [NSMutableOrderedSet orderedSet];
[mutableTest1 addObject:o5];
[mutableTest1 addObject: o5];
[testObjs addObject: mutableTest1];
mutableTest2 = [NSMutableOrderedSet orderedSet];
[mutableTest2 addObject:o6];
[mutableTest2 addObject: o6];
[testObjs addObject: mutableTest2];
[mutableTest1 intersectOrderedSet:mutableTest2];
[mutableTest1 intersectOrderedSet: mutableTest2];
PASS(mutableTest1 != nil && mutableTest2 != nil && [mutableTest1 count] == 1,
"mutableSets do intersect");
id o7 = @"Hello";
id o8 = @"World";
mutableTest1 = [NSMutableOrderedSet orderedSet];
[mutableTest1 addObject:o7];
[mutableTest1 addObject:o8];
[mutableTest1 addObject: o7];
[mutableTest1 addObject: o8];
[testObjs addObject: mutableTest1];
mutableTest2 = [NSMutableOrderedSet orderedSet];
[mutableTest2 addObject:o7];
[mutableTest2 addObject: o7];
[testObjs addObject: mutableTest2];
BOOL isSubset = [mutableTest2 isSubsetOfOrderedSet:mutableTest1];
BOOL isSubset = [mutableTest2 isSubsetOfOrderedSet: mutableTest1];
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";
mutableTest1 = [NSMutableOrderedSet orderedSet];
[mutableTest1 addObject:o9];
[mutableTest1 addObject: o9];
[testObjs addObject: mutableTest1];
mutableTest2 = [NSMutableOrderedSet orderedSet];
[mutableTest2 addObject:o10];
[mutableTest2 addObject:o9];
[mutableTest2 addObject: o10];
[mutableTest2 addObject: o9];
[testObjs addObject: mutableTest2];
isSubset = [mutableTest2 isSubsetOfOrderedSet:mutableTest1];
isSubset = [mutableTest2 isSubsetOfOrderedSet: mutableTest1];
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";
id o12 = @"ToGo";
mutableTest1 = [NSMutableOrderedSet orderedSet];
[mutableTest1 addObject:o9];
[mutableTest1 addObject:o10];
[mutableTest1 addObject:o12];
[mutableTest1 addObject:o11];
[mutableTest1 addObject: o9];
[mutableTest1 addObject: o10];
[mutableTest1 addObject: o12];
[mutableTest1 addObject: o11];
[testObjs addObject: mutableTest1];
mutableTest2 = [NSMutableOrderedSet orderedSet];
[mutableTest2 addObject:o9];
[mutableTest2 addObject:o10];
[mutableTest2 addObject: o9];
[mutableTest2 addObject: o10];
[testObjs addObject: mutableTest2];
isSubset = [mutableTest2 isSubsetOfOrderedSet:mutableTest1];
isSubset = [mutableTest2 isSubsetOfOrderedSet: mutableTest1];
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";
o12 = @"ToGo";
mutableTest1 = [NSMutableOrderedSet orderedSet];
[mutableTest1 addObject:o9];
[mutableTest1 addObject:o10];
[mutableTest1 addObject:o12];
[mutableTest1 addObject:o11];
[mutableTest1 addObject: o9];
[mutableTest1 addObject: o10];
[mutableTest1 addObject: o12];
[mutableTest1 addObject: o11];
[testObjs addObject: mutableTest1];
PASS([mutableTest1 isEqual:mutableTest1],
PASS([mutableTest1 isEqual: mutableTest1],
"mutableTest1 is equal to itself");
o9 = @"Hello";
@ -278,64 +295,64 @@ int main()
o11 = @"Ready";
o12 = @"ToGo";
NSMutableOrderedSet *mutableTest3 = [NSMutableOrderedSet orderedSet];
[mutableTest3 addObject:o9];
[mutableTest3 addObject:o10];
[mutableTest3 addObject:o12];
[mutableTest3 addObject:o11];
[mutableTest3 insertObject:@"Hello" atIndex:2];
[mutableTest3 addObject: o9];
[mutableTest3 addObject: o10];
[mutableTest3 addObject: o12];
[mutableTest3 addObject: o11];
[mutableTest3 insertObject: @"Hello" atIndex: 2];
[testObjs addObject: mutableTest3];
PASS([mutableTest3 isEqual:mutableTest1] == YES,
PASS([mutableTest3 isEqual: mutableTest1] == YES,
"Insert at index does not replace existing object");
NSMutableOrderedSet *mutableTest4 = [NSMutableOrderedSet orderedSet];
[mutableTest4 addObject:@"Now"];
[mutableTest4 addObject:@"is"];
[mutableTest4 addObject:@"the"];
[mutableTest4 addObject:@"time"];
[mutableTest4 addObject:@"for"];
[mutableTest4 addObject:@"all"];
[mutableTest4 addObject:@"Good"];
[mutableTest4 addObject:@"men"];
[mutableTest4 addObject:@"to"];
[mutableTest4 addObject:@"come"];
[mutableTest4 addObject:@"to the aid"];
[mutableTest4 addObject:@"of their country"];
[mutableTest4 moveObjectsAtIndexes:[NSIndexSet indexSetWithIndex:3] toIndex:10];
[mutableTest4 addObject: @"Now"];
[mutableTest4 addObject: @"is"];
[mutableTest4 addObject: @"the"];
[mutableTest4 addObject: @"time"];
[mutableTest4 addObject: @"for"];
[mutableTest4 addObject: @"all"];
[mutableTest4 addObject: @"Good"];
[mutableTest4 addObject: @"men"];
[mutableTest4 addObject: @"to"];
[mutableTest4 addObject: @"come"];
[mutableTest4 addObject: @"to the aid"];
[mutableTest4 addObject: @"of their country"];
[mutableTest4 moveObjectsAtIndexes: [NSIndexSet indexSetWithIndex: 3] toIndex: 10];
[testObjs addObject: mutableTest4];
PASS([[mutableTest4 objectAtIndex: 10] isEqual:@"time"] == YES,
PASS([[mutableTest4 objectAtIndex: 10] isEqual: @"time"] == YES,
"Move to index moves to correct index");
NSMutableOrderedSet *mutableTest5 = [NSMutableOrderedSet orderedSet];
[mutableTest5 addObject:@"Now"];
[mutableTest5 addObject:@"is"];
[mutableTest5 addObject:@"the"];
[mutableTest5 exchangeObjectAtIndex:0 withObjectAtIndex:2];
[mutableTest5 addObject: @"Now"];
[mutableTest5 addObject: @"is"];
[mutableTest5 addObject: @"the"];
[mutableTest5 exchangeObjectAtIndex: 0 withObjectAtIndex: 2];
[testObjs addObject: mutableTest5];
PASS([[mutableTest5 objectAtIndex: 0] isEqual:@"the"] == YES &&
[[mutableTest5 objectAtIndex: 2] isEqual:@"Now"] == YES,
PASS([[mutableTest5 objectAtIndex: 0] isEqual: @"the"] == YES &&
[[mutableTest5 objectAtIndex: 2] isEqual: @"Now"] == YES,
"Exchanges indexes properly");
//NSLog(@"RESULT: %@",mutableTest4);
mutableTest4 = [NSMutableOrderedSet orderedSet];
[mutableTest4 addObject:@"Now"];
[mutableTest4 addObject:@"is"];
[mutableTest4 addObject:@"the"];
[mutableTest4 addObject:@"time"];
[mutableTest4 addObject:@"for"];
[mutableTest4 addObject:@"all"];
[mutableTest4 addObject:@"Good"];
[mutableTest4 addObject:@"men"];
[mutableTest4 addObject:@"to"];
[mutableTest4 addObject:@"come to"];
[mutableTest4 addObject:@"the aid"];
[mutableTest4 addObject:@"of their country"];
NSMutableIndexSet *is = [NSMutableIndexSet indexSetWithIndex:6];
[mutableTest4 addObject: @"Now"];
[mutableTest4 addObject: @"is"];
[mutableTest4 addObject: @"the"];
[mutableTest4 addObject: @"time"];
[mutableTest4 addObject: @"for"];
[mutableTest4 addObject: @"all"];
[mutableTest4 addObject: @"Good"];
[mutableTest4 addObject: @"men"];
[mutableTest4 addObject: @"to"];
[mutableTest4 addObject: @"come to"];
[mutableTest4 addObject: @"the aid"];
[mutableTest4 addObject: @"of their country"];
NSMutableIndexSet *is = [NSMutableIndexSet indexSetWithIndex: 6];
[is addIndex: 9];
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"Horrible", @"Flee From", nil];
NSMutableArray *array = [NSMutableArray arrayWithObjects: @"Horrible", @"Flee From", nil];
[mutableTest4 replaceObjectsAtIndexes: is
withObjects: array];
[testObjs addObject: mutableTest4];
PASS([[mutableTest4 objectAtIndex: 9] isEqual:@"Flee From"] == YES,
PASS([[mutableTest4 objectAtIndex: 9] isEqual: @"Flee From"] == YES,
"replaceObjectsAtIndexes: adds to correct indexes");
id uobj = [NSKeyedUnarchiver unarchiveObjectWithData: data];
@ -343,7 +360,7 @@ int main()
[uobj isKindOfClass: [NSMutableOrderedSet class]] &&
[uobj containsObject: @"Now"]),
"Object unarchives correctly from macOS archive")
test_NSObject(@"NSOrderedSet", testObjs);
test_NSCoding(testObjs);
test_NSCopying(@"NSOrderedSet", @"NSMutableOrderedSet", testObjs, YES, NO);