Skip to content

Commit

Permalink
Fixing a bug in IntArrayContainer.merge and adding tests for that met…
Browse files Browse the repository at this point in the history
…hod and the others.
  • Loading branch information
Craigacp committed Nov 27, 2024
1 parent 81605a2 commit 2b271e6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -141,7 +141,6 @@ public static void removeOther(IntArrayContainer input, int[] otherArray, IntArr
}
}
output.size = k;
assert(k == newSize);
//logger.info("output = " + Arrays.toString(outputArray));
}

Expand All @@ -154,9 +153,9 @@ public static void removeOther(IntArrayContainer input, int[] otherArray, IntArr
* @return A sorted array containing all the elements from the input.
*/
public static int[] merge(List<int[]> input, IntArrayContainer firstBuffer, IntArrayContainer secondBuffer) {
if (input.size() > 0) {
if (!input.isEmpty()) {
firstBuffer.fill(input.get(0));
for (int i = 0; i < input.size(); i++) {
for (int i = 1; i < input.size(); i++) {
merge(firstBuffer,input.get(i),secondBuffer);
IntArrayContainer tmp = secondBuffer;
secondBuffer = firstBuffer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.tribuo.common.tree.impl;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class IntArrayContainerTest {

@Test
public void mergeListTest() {
IntArrayContainer first = new IntArrayContainer(10);
IntArrayContainer second = new IntArrayContainer(10);
List<int[]> list = new ArrayList<>();
list.add(new int[]{5, 7, 9});
list.add(new int[]{1, 2, 4, 6});
list.add(new int[]{3, 8, 10});

int[] output = IntArrayContainer.merge(list, first, second);

int[] expectedOutput = new int[]{1,2,3,4,5,6,7,8,9,10};

Assertions.assertArrayEquals(expectedOutput, output);
}

@Test
public void mergeTest() {
IntArrayContainer first = new IntArrayContainer(10);
IntArrayContainer second = new IntArrayContainer(10);
first.fill(new int[]{1,2,3,5,7,9});
int[] other = new int[]{0,4,6,8,10};

IntArrayContainer.merge(first, other, second);

int[] expectedOutput = new int[]{0,1,2,3,4,5,6,7,8,9,10};

int[] output = second.copy();

Assertions.assertArrayEquals(expectedOutput, output);
}

@Test
public void fillTest() {
IntArrayContainer first = new IntArrayContainer(10);
IntArrayContainer second = new IntArrayContainer(10);
int[] expected = new int[]{1,2,3,5,7,9};
first.fill(expected);
Assertions.assertArrayEquals(expected, first.copy());

second.fill(first);
Assertions.assertArrayEquals(expected, second.copy());
}

@Test
public void growTest() {
IntArrayContainer first = new IntArrayContainer(10);
int[] expected = new int[]{1,2,3,5,7,9};
first.fill(expected);
Assertions.assertArrayEquals(expected, first.copy());

first.grow(20);
Assertions.assertEquals(20,first.array.length);
Assertions.assertArrayEquals(expected, first.copy());
}

@Test
public void removeOtherTest() {
IntArrayContainer first = new IntArrayContainer(10);
IntArrayContainer second = new IntArrayContainer(10);
first.fill(new int[]{1,2,3,5,7,9});

int[] removed = new int[]{2,6,8,9};

IntArrayContainer.removeOther(first, removed, second);

int[] expected = new int[]{1,3,5,7};

Assertions.assertArrayEquals(expected, second.copy());
}

}

0 comments on commit 2b271e6

Please sign in to comment.