-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
addressing warnings and adding unit test for Chunkify Util
Signed-off-by: Alfredo Gutierrez <[email protected]>
- Loading branch information
1 parent
0092362
commit 5369431
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
common/src/test/java/com/hedera/block/common/utils/ChunkUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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 or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.block.common.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ChunkUtilsTest { | ||
|
||
@Test | ||
public void testEmptyCollection() { | ||
List<Integer> emptyList = Collections.emptyList(); | ||
List<List<Integer>> chunks = ChunkUtils.chunkify(emptyList, 3); | ||
assertTrue(chunks.isEmpty(), "Chunks of empty collection should be empty"); | ||
} | ||
|
||
@Test | ||
public void testChunkSizeZero() { | ||
List<Integer> list = Arrays.asList(1, 2, 3); | ||
Exception exception = | ||
assertThrows(IllegalArgumentException.class, () -> ChunkUtils.chunkify(list, 0)); | ||
assertEquals("Chunk size must be greater than 0", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testChunkSizeNegative() { | ||
List<Integer> list = Arrays.asList(1, 2, 3); | ||
Exception exception = | ||
assertThrows(IllegalArgumentException.class, () -> ChunkUtils.chunkify(list, -1)); | ||
assertEquals("Chunk size must be greater than 0", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testChunkifyCollectionSmallerThanChunkSize() { | ||
List<Integer> list = Arrays.asList(1, 2); | ||
List<List<Integer>> chunks = ChunkUtils.chunkify(list, 5); | ||
assertEquals(1, chunks.size(), "Should return one chunk"); | ||
assertEquals(list, chunks.get(0), "Chunk should contain the entire collection"); | ||
} | ||
|
||
@Test | ||
public void testChunkifyCollectionExactlyDivisible() { | ||
List<Integer> list = Arrays.asList(1, 2, 3, 4); | ||
List<List<Integer>> chunks = ChunkUtils.chunkify(list, 2); | ||
assertEquals(2, chunks.size(), "Should return two chunks"); | ||
assertEquals(Arrays.asList(1, 2), chunks.get(0), "First chunk mismatch"); | ||
assertEquals(Arrays.asList(3, 4), chunks.get(1), "Second chunk mismatch"); | ||
} | ||
|
||
@Test | ||
public void testChunkifyCollectionNotExactlyDivisible() { | ||
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); | ||
List<List<Integer>> chunks = ChunkUtils.chunkify(list, 2); | ||
assertEquals(3, chunks.size(), "Should return three chunks"); | ||
assertEquals(Arrays.asList(1, 2), chunks.get(0), "First chunk mismatch"); | ||
assertEquals(Arrays.asList(3, 4), chunks.get(1), "Second chunk mismatch"); | ||
assertEquals(Arrays.asList(5), chunks.get(2), "Third chunk mismatch"); | ||
} | ||
|
||
@Test | ||
public void testNullCollection() { | ||
assertThrows(NullPointerException.class, () -> ChunkUtils.chunkify(null, 3)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters