test: unit test for RangeUtil

This commit is contained in:
siujamo
2025-06-04 11:05:02 +08:00
parent 56dbf1a6f2
commit 410cb0d6d1
@@ -19,98 +19,113 @@ package com.onixbyte.devkit.utils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class RangeUtilTest { class RangeUtilTest {
// Test range(end) with normal positive end value /**
* Tests generating ascending range from 0 up to end (exclusive).
*/
@Test @Test
void testRangeEndValid() { void testRangeEndValid() {
IntStream stream = RangeUtil.range(5);
int[] expected = {0, 1, 2, 3, 4}; int[] expected = {0, 1, 2, 3, 4};
assertArrayEquals(expected, stream.toArray()); assertArrayEquals(expected, RangeUtil.range(5).toArray());
} }
// Test range(end) with end less than or equal to zero should throw IllegalArgumentException /**
* Tests that range(end) throws IllegalArgumentException for end less than or equal to zero.
*/
@Test @Test
void testRangeEndInvalidThrows() { void testRangeEndInvalidThrows() {
IllegalArgumentException exception1 = assertThrows(IllegalArgumentException.class, IllegalArgumentException ex1 = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(0)); () -> RangeUtil.range(0));
assertTrue(exception1.getMessage().contains("Parameter [end] should not less than 0")); assertTrue(ex1.getMessage().contains("should not be less than or equal to 0"));
IllegalArgumentException exception2 = assertThrows(IllegalArgumentException.class, IllegalArgumentException ex2 = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(-5)); () -> RangeUtil.range(-3));
assertTrue(exception2.getMessage().contains("Parameter [end] should not less than 0")); assertTrue(ex2.getMessage().contains("should not be less than or equal to 0"));
} }
// Test range(start, end) with valid input where start < end /**
* Tests ascending range where start is less than end.
*/
@Test @Test
void testRangeStartEndValid() { void testRangeStartEndAscending() {
IntStream stream = RangeUtil.range(3, 8);
int[] expected = {3, 4, 5, 6, 7}; int[] expected = {3, 4, 5, 6, 7};
assertArrayEquals(expected, stream.toArray()); assertArrayEquals(expected, RangeUtil.range(3, 8).toArray());
} }
// Test range(start, end) where start >= end should throw IllegalStateException /**
* Tests descending range where start is greater than end.
*/
@Test @Test
void testRangeStartEndInvalidThrows() { void testRangeStartEndDescending() {
IllegalStateException exception = assertThrows(IllegalStateException.class, int[] expected = {8, 7, 6, 5, 4};
() -> RangeUtil.range(8, 3)); assertArrayEquals(expected, RangeUtil.range(8, 3).toArray());
assertTrue(exception.getMessage().contains("Parameter [start] should less than parameter [end]"));
// Also test equal values
IllegalStateException exceptionEqual = assertThrows(IllegalStateException.class,
() -> RangeUtil.range(5, 5));
assertTrue(exceptionEqual.getMessage().contains("Parameter [start] should less than parameter [end]"));
} }
// Test rangeClosed(start, end) generates inclusive ranges correctly /**
* Tests empty stream when start equals end.
*/
@Test @Test
void testRangeClosed() { void testRangeStartEqualsEndReturnsEmpty() {
IntStream stream = RangeUtil.rangeClosed(3, 8); assertEquals(0, RangeUtil.range(5, 5).count());
}
/**
* Tests that rangeClosed generates inclusive range in ascending order.
*/
@Test
void testRangeClosedAscending() {
int[] expected = {3, 4, 5, 6, 7, 8}; int[] expected = {3, 4, 5, 6, 7, 8};
assertArrayEquals(expected, stream.toArray()); assertArrayEquals(expected, RangeUtil.rangeClosed(3, 8).toArray());
} }
// Test range(start, end, step) with positive step and valid parameters /**
* Tests range method with positive step generating ascending sequence.
*/
@Test @Test
void testRangeWithStepPositive() { void testRangeWithPositiveStep() {
IntStream stream = RangeUtil.range(3, 10, 2); int[] expected = {2, 4, 6, 8};
int[] expected = {3, 5, 7, 9}; assertArrayEquals(expected, RangeUtil.range(2, 10, 2).toArray());
assertArrayEquals(expected, stream.toArray());
} }
// Test range(start, end, step) with negative step and valid parameters (descending range) /**
* Tests range method with negative step generating descending sequence.
*/
@Test @Test
void testRangeWithStepNegative() { void testRangeWithNegativeStep() {
IntStream stream = RangeUtil.range(10, 3, -2); int[] expected = {10, 7, 4, 1};
int[] expected = {10, 8, 6, 4}; assertArrayEquals(expected, RangeUtil.range(10, 0, -3).toArray());
assertArrayEquals(expected, stream.toArray());
} }
// Test range(start, end, step) throws IllegalArgumentException if step is zero /**
* Tests that passing zero step throws IllegalArgumentException.
*/
@Test @Test
void testRangeWithStepZeroThrows() { void testRangeStepZeroThrows() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(0, 10, 0)); () -> RangeUtil.range(0, 10, 0));
assertEquals("Step value must not be zero.", exception.getMessage()); assertEquals("Step value must not be zero.", ex.getMessage());
} }
// Test range(start, end, step) throws if parameters inconsistent with step positive /**
* Tests that range with positive step but invalid start/end throws IllegalArgumentException.
*/
@Test @Test
void testRangeWithStepPositiveInvalidRangeThrows() { void testRangePositiveStepInvalidRangeThrows() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(10, 5, 1)); () -> RangeUtil.range(10, 5, 1));
assertEquals("Range parameters are inconsistent with the step value.", exception.getMessage()); assertEquals("Range parameters are inconsistent with the step value.", ex.getMessage());
} }
// Test range(start, end, step) throws if parameters inconsistent with step negative /**
* Tests that range with negative step but invalid start/end throws IllegalArgumentException.
*/
@Test @Test
void testRangeWithStepNegativeInvalidRangeThrows() { void testRangeNegativeStepInvalidRangeThrows() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(5, 10, -1)); () -> RangeUtil.range(5, 10, -1));
assertEquals("Range parameters are inconsistent with the step value.", exception.getMessage()); assertEquals("Range parameters are inconsistent with the step value.", ex.getMessage());
} }
} }