<commit_before>@import <Foundation/CPArray.j>
@import <Foundation/CPString.j>
@import <Foundation/CPNumber.j>
@import <Foundation/CPSortDescriptor.j>

@implementation CPArrayTest : OJTestCase

- (void)testComponentsJoinedByString
{
    var testStrings = [
        [[], "", ""],
        [[], "-", ""],
        [[1,2], "-", "1-2"],
        [[1,2,3], "-", "1-2-3"],
        [["123", 456], "-", "123-456"]
    ];
    
    for (var i = 0; i < testStrings.length; i++)
        [self assert:[testStrings[i][0] componentsJoinedByString:testStrings[i][1]] equals:testStrings[i][2]];
}

- (void)testsInsertObjectsAtIndexes
{
    var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects:@"a", @"b", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:1];

    [indexes addIndex:3];

    [array insertObjects:newAdditions atIndexes:indexes];

    [self assert:array equals:[@"one", @"a", @"two", @"b", @"three", @"four"]];

    var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects:@"a", @"b", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:5];
    
    [indexes addIndex:4];
    
    [array insertObjects:newAdditions atIndexes:indexes];

    [self assert:array equals:[@"one", @"two", @"three", @"four", @"a", @"b"]];
    
    var array = [CPMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects: @"a", @"b", @"c", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:1];

    [indexes addIndex:2];
    [indexes addIndex:4];
    
    [array insertObjects:newAdditions atIndexes:indexes];

    [self assert:array equals:[@"one", @"a", @"b", @"two", @"c", @"three", @"four"]];


    var array = [CPMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects: @"a", @"b", @"c", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:1];

    [indexes addIndex:2];
    [indexes addIndex:6];

    [array insertObjects:newAdditions atIndexes:indexes];
    
    [self assert:array equals:[@"one", @"a", @"b", @"two", @"three", @"four", @"c"]];

    //
    
    var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects:@"a", @"b", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:5];
    
    [indexes addIndex:6];
    
    try
    {
        [array insertObjects:newAdditions atIndexes:indexes];
        [self fail];
    }
    catch (e)
    {
        if ((e.isa) && [e name] == AssertionFailedError)
            throw e;
    }
}

- (void)testRemoveObjectsAtIndexes
{
	var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
			indexes = [CPMutableIndexSet indexSetWithIndex: 2];
			
	[array removeObjectsAtIndexes: indexes];
	
	[self assert:array equals:[@"one", @"two", @"four"]];
}

- (void)testIndexOfObjectSortedByFunction
{
    var array = [0, 1, 2, 3, 4, 7];

    [self assert:[array indexOfObject:3 sortedByFunction:function(a, b){ return a - b; }] equals:3];
    [self assert:[[array arrayByReversingArray] indexOfObject:3 sortedByFunction:function(a, b){ return b - a; }] equals:2];
}

- (void)testIndexOfObjectSortedByDescriptors
{
    var array = [0, 1, 2, 3, 4, 7];

    [self assert:[array indexOfObject:3
                  sortedByDescriptors:[[[CPSortDescriptor alloc] initWithKey:@"intValue" ascending:YES]]]
          equals:3];

    [self assert:[[array arrayByReversingArray] indexOfObject:3
                  sortedByDescriptors:[[[CPSortDescriptor alloc] initWithKey:@"intValue" ascending:NO]]]
          equals:2];
}

- (void)testIndexOutOfBounds
{
    try
    {
        [[] objectAtIndex:0];
        [self assert:false];
    }
    catch (anException)
    {
        [self assert:[anException name] equals:CPRangeException];
        [self assert:[anException reason] equals:@"index (0) beyond bounds (0)"];
    }

    [[0, 1, 2] objectAtIndex:0];
    [[0, 1, 2] objectAtIndex:1];
    [[0, 1, 2] objectAtIndex:2];

    try
    {
        [[0, 1, 2] objectAtIndex:3];
        [self assert:false];
    }
    catch (anException)
    {
        [self assert:[anException name] equals:CPRangeException];
        [self assert:[anException reason] equals:@"index (3) beyond bounds (3)"];
    }

    try
    {
        [[0, 1, 2] objectAtIndex:4];
        [self assert:false];
    }
    catch (anException)
    {
        [self assert:[anException name] equals:CPRangeException];
        [self assert:[anException reason] equals:@"index (4) beyond bounds (3)"];
    }
}

@end

@implementation CPArray (reverse)

- (CPArray)arrayByReversingArray
{
    var a = [];
    for (i = length - 1; i>0; --i)
        a.push(self[i]);

    return a;
}

@end<commit_msg>Add a test for the initWithItems:copy: fix.<commit_after>@import <Foundation/CPArray.j>
@import <Foundation/CPString.j>
@import <Foundation/CPNumber.j>
@import <Foundation/CPSortDescriptor.j>

@implementation CPArrayTest : OJTestCase

- (void)testComponentsJoinedByString
{
    var testStrings = [
        [[], "", ""],
        [[], "-", ""],
        [[1,2], "-", "1-2"],
        [[1,2,3], "-", "1-2-3"],
        [["123", 456], "-", "123-456"]
    ];
    
    for (var i = 0; i < testStrings.length; i++)
        [self assert:[testStrings[i][0] componentsJoinedByString:testStrings[i][1]] equals:testStrings[i][2]];
}

- (void)testsInsertObjectsAtIndexes
{
    var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects:@"a", @"b", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:1];

    [indexes addIndex:3];

    [array insertObjects:newAdditions atIndexes:indexes];

    [self assert:array equals:[@"one", @"a", @"two", @"b", @"three", @"four"]];

    var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects:@"a", @"b", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:5];
    
    [indexes addIndex:4];
    
    [array insertObjects:newAdditions atIndexes:indexes];

    [self assert:array equals:[@"one", @"two", @"three", @"four", @"a", @"b"]];
    
    var array = [CPMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects: @"a", @"b", @"c", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:1];

    [indexes addIndex:2];
    [indexes addIndex:4];
    
    [array insertObjects:newAdditions atIndexes:indexes];

    [self assert:array equals:[@"one", @"a", @"b", @"two", @"c", @"three", @"four"]];


    var array = [CPMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects: @"a", @"b", @"c", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:1];

    [indexes addIndex:2];
    [indexes addIndex:6];

    [array insertObjects:newAdditions atIndexes:indexes];
    
    [self assert:array equals:[@"one", @"a", @"b", @"two", @"three", @"four", @"c"]];

    //
    
    var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
        newAdditions = [CPArray arrayWithObjects:@"a", @"b", nil],
        indexes = [CPMutableIndexSet indexSetWithIndex:5];
    
    [indexes addIndex:6];
    
    try
    {
        [array insertObjects:newAdditions atIndexes:indexes];
        [self fail];
    }
    catch (e)
    {
        if ((e.isa) && [e name] == AssertionFailedError)
            throw e;
    }
}

- (void)testRemoveObjectsAtIndexes
{
	var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
			indexes = [CPMutableIndexSet indexSetWithIndex: 2];
			
	[array removeObjectsAtIndexes: indexes];
	
	[self assert:array equals:[@"one", @"two", @"four"]];
}

- (void)testIndexOfObjectSortedByFunction
{
    var array = [0, 1, 2, 3, 4, 7];

    [self assert:[array indexOfObject:3 sortedByFunction:function(a, b){ return a - b; }] equals:3];
    [self assert:[[array arrayByReversingArray] indexOfObject:3 sortedByFunction:function(a, b){ return b - a; }] equals:2];
}

- (void)testIndexOfObjectSortedByDescriptors
{
    var array = [0, 1, 2, 3, 4, 7];

    [self assert:[array indexOfObject:3
                  sortedByDescriptors:[[[CPSortDescriptor alloc] initWithKey:@"intValue" ascending:YES]]]
          equals:3];

    [self assert:[[array arrayByReversingArray] indexOfObject:3
                  sortedByDescriptors:[[[CPSortDescriptor alloc] initWithKey:@"intValue" ascending:NO]]]
          equals:2];
}

- (void)testIndexOutOfBounds
{
    try
    {
        [[] objectAtIndex:0];
        [self assert:false];
    }
    catch (anException)
    {
        [self assert:[anException name] equals:CPRangeException];
        [self assert:[anException reason] equals:@"index (0) beyond bounds (0)"];
    }

    [[0, 1, 2] objectAtIndex:0];
    [[0, 1, 2] objectAtIndex:1];
    [[0, 1, 2] objectAtIndex:2];

    try
    {
        [[0, 1, 2] objectAtIndex:3];
        [self assert:false];
    }
    catch (anException)
    {
        [self assert:[anException name] equals:CPRangeException];
        [self assert:[anException reason] equals:@"index (3) beyond bounds (3)"];
    }

    try
    {
        [[0, 1, 2] objectAtIndex:4];
        [self assert:false];
    }
    catch (anException)
    {
        [self assert:[anException name] equals:CPRangeException];
        [self assert:[anException reason] equals:@"index (4) beyond bounds (3)"];
    }
}

- (void)testInitWithArrayCopyItems
{
    var a = [[CopyableObject new], 2, 3];
    var b = [[CPArray alloc] initWithArray:a copyItems:YES];

    [self assert:a notEqual:b];
}

@end

@implementation CPArray (reverse)

- (CPArray)arrayByReversingArray
{
    var a = [];
    for (i = length - 1; i>0; --i)
        a.push(self[i]);

    return a;
}

@end

@implementation CopyableObject : CPObject
{
}

- (id)copy
{
    return [[[self class] alloc] init];
}

@end
{'fraction_non_alphanumeric': 0.1776765375854214, 'fraction_numerical': 0.01762899871248886, 'mean_word_length': 3.356341673856773, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6693809', 'n_tokens_mistral': 3659, 'n_tokens_neox': 3323, 'n_words': 749}