mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 09:04:13 +00:00
New tests and corrected code for NSOrderedSet/NSMutableOrderedSet. Submitting for review.
This commit is contained in:
parent
d59a90206a
commit
f8994e1049
3 changed files with 151 additions and 28 deletions
|
@ -210,8 +210,16 @@ static Class mutableSetClass;
|
|||
/* Designated initialiser */
|
||||
- (id) initWithObjects: (const id*)objs count: (NSUInteger)c
|
||||
{
|
||||
NSUInteger i;
|
||||
NSUInteger i = 0;
|
||||
|
||||
// Reduce C if it doesn't match. This is apparently how
|
||||
// macOS behaves.
|
||||
if(c > sizeof(objs))
|
||||
{
|
||||
c = sizeof(objs);
|
||||
}
|
||||
|
||||
// Initialize and fill the set.
|
||||
GSIArrayInitWithZoneAndCapacity(&array, [self zone], c);
|
||||
for (i = 0; i < c; i++)
|
||||
{
|
||||
|
@ -224,10 +232,13 @@ static Class mutableSetClass;
|
|||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"Tried to init set with nil value"];
|
||||
}
|
||||
|
||||
|
||||
item.obj = obj;
|
||||
RETAIN(obj);
|
||||
GSIArrayAddItem(&array, item);
|
||||
if(![self containsObject: obj])
|
||||
{
|
||||
GSIArrayAddItem(&array, item);
|
||||
RETAIN(obj);
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
@ -264,10 +275,14 @@ static Class mutableSetClass;
|
|||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"Tried to add nil to set"];
|
||||
}
|
||||
|
||||
item.obj = anObject;
|
||||
GSIArrayAddItem(&array, item);
|
||||
_version++;
|
||||
|
||||
if([self containsObject: anObject] == NO)
|
||||
{
|
||||
item.obj = anObject;
|
||||
GSIArrayAddItem(&array, item);
|
||||
RETAIN(anObject);
|
||||
_version++;
|
||||
}
|
||||
}
|
||||
|
||||
- (void) insertObject: (id)object atIndex: (NSUInteger)index
|
||||
|
@ -305,22 +320,36 @@ static Class mutableSetClass;
|
|||
count: (NSUInteger)count
|
||||
{
|
||||
NSUInteger i = 0;
|
||||
self = [self initWithCapacity: count];
|
||||
|
||||
for(i = 0; i < count; i++)
|
||||
// Reduce count if more then size of list of objects
|
||||
if(sizeof(objects) < count)
|
||||
{
|
||||
id anObject = objects[i];
|
||||
|
||||
if (anObject == nil)
|
||||
count = sizeof(objects);
|
||||
}
|
||||
|
||||
// Init and fill set
|
||||
self = [self initWithCapacity: count];
|
||||
if(self != nil)
|
||||
{
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
NSLog(@"Tried to init an orderedset with a nil object");
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
GSIArrayItem item;
|
||||
item.obj = anObject;
|
||||
GSIArrayAddItem(&array, item);
|
||||
id anObject = objects[i];
|
||||
|
||||
if (anObject == nil)
|
||||
{
|
||||
NSLog(@"Tried to init an orderedset with a nil object");
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
GSIArrayItem item;
|
||||
if(![self containsObject: anObject])
|
||||
{
|
||||
item.obj = anObject;
|
||||
GSIArrayAddItem(&array, item);
|
||||
RETAIN(anObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return self;
|
||||
|
|
|
@ -94,7 +94,6 @@ static SEL rlSel;
|
|||
NSOrderedSet_abstract_class = self;
|
||||
NSOrderedSet_concrete_class = [GSOrderedSet class];
|
||||
[NSMutableSet class];
|
||||
// [self registerAtExit];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1230,6 +1229,18 @@ static SEL rlSel;
|
|||
}
|
||||
}
|
||||
|
||||
+ (id) allocWithZone: (NSZone*)z
|
||||
{
|
||||
if (self == NSMutableOrderedSet_abstract_class)
|
||||
{
|
||||
return NSAllocateObject(NSMutableOrderedSet_concrete_class, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NSAllocateObject(self, 0, z);
|
||||
}
|
||||
}
|
||||
|
||||
+ (instancetype)orderedSetWithCapacity: (NSUInteger)capacity
|
||||
{
|
||||
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithCapacity: capacity]);
|
||||
|
|
|
@ -5,19 +5,102 @@
|
|||
int main()
|
||||
{
|
||||
START_SET("NSOrderedSet base")
|
||||
|
||||
NSOrderedSet *testObj;
|
||||
|
||||
NSOrderedSet *testObj, *testObj2;
|
||||
NSMutableOrderedSet *mutableTest1, *mutableTest2;
|
||||
NSMutableArray *testObjs = [NSMutableArray new];
|
||||
|
||||
|
||||
testObj = [NSOrderedSet new];
|
||||
[testObjs addObject: testObj];
|
||||
PASS(testObj != nil && [testObj count] == 0,
|
||||
"can create an empty ordered set");
|
||||
|
||||
"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");
|
||||
"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"];
|
||||
testObj = [NSOrderedSet orderedSetWithArray: arr];
|
||||
[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];
|
||||
[testObjs addObject: testObj];
|
||||
testObj2 = [NSOrderedSet orderedSetWithObjects: objc4 count: 1];
|
||||
[testObjs addObject: testObj2];
|
||||
BOOL result = [testObj intersectsOrderedSet: testObj2];
|
||||
PASS(result == NO,
|
||||
"Sets do not intersect!");
|
||||
|
||||
id objs5[] = {@"Hello"};
|
||||
id objc6[] = {@"Hello"};
|
||||
testObj = [NSOrderedSet orderedSetWithObjects: objs5 count: 1];
|
||||
[testObjs addObject: testObj];
|
||||
testObj2 = [NSOrderedSet orderedSetWithObjects: objc6 count: 1];
|
||||
[testObjs addObject: testObj2];
|
||||
BOOL result1 = [testObj intersectsOrderedSet: testObj2];
|
||||
PASS(result1 == YES,
|
||||
"Sets do intersect!");
|
||||
|
||||
id o1 = @"Hello";
|
||||
id o2 = @"World";
|
||||
mutableTest1 = [NSMutableOrderedSet orderedSet];
|
||||
[mutableTest1 addObject:o1];
|
||||
[testObjs addObject: mutableTest1];
|
||||
mutableTest2 = [NSMutableOrderedSet orderedSet];
|
||||
[mutableTest2 addObject:o2];
|
||||
[testObjs addObject: 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];
|
||||
[testObjs addObject: mutableTest1];
|
||||
mutableTest2 = [NSMutableOrderedSet orderedSet];
|
||||
[mutableTest2 addObject:o4];
|
||||
[testObjs addObject: 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];
|
||||
[testObjs addObject: mutableTest1];
|
||||
mutableTest2 = [NSMutableOrderedSet orderedSet];
|
||||
[mutableTest2 addObject:o6];
|
||||
[testObjs addObject: mutableTest2];
|
||||
[mutableTest1 intersectOrderedSet:mutableTest2];
|
||||
PASS(mutableTest1 != nil && mutableTest2 != nil && [mutableTest1 count] == 1,
|
||||
"mutableSets do intersect");
|
||||
|
||||
test_NSObject(@"NSOrderedSet", testObjs);
|
||||
test_NSCoding(testObjs);
|
||||
|
|
Loading…
Reference in a new issue