Skip to content

Commit 410cb0d

Browse files
committed
test: unit test for RangeUtil
1 parent 56dbf1a commit 410cb0d

File tree

1 file changed

+66
-51
lines changed

1 file changed

+66
-51
lines changed

devkit-utils/src/test/java/com/onixbyte/devkit/utils/RangeUtilTest.java

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,98 +19,113 @@
1919

2020
import org.junit.jupiter.api.Test;
2121

22-
import java.util.stream.IntStream;
23-
2422
import static org.junit.jupiter.api.Assertions.*;
2523

2624
class RangeUtilTest {
2725

28-
// Test range(end) with normal positive end value
26+
/**
27+
* Tests generating ascending range from 0 up to end (exclusive).
28+
*/
2929
@Test
3030
void testRangeEndValid() {
31-
IntStream stream = RangeUtil.range(5);
3231
int[] expected = {0, 1, 2, 3, 4};
33-
assertArrayEquals(expected, stream.toArray());
32+
assertArrayEquals(expected, RangeUtil.range(5).toArray());
3433
}
3534

36-
// Test range(end) with end less than or equal to zero should throw IllegalArgumentException
35+
/**
36+
* Tests that range(end) throws IllegalArgumentException for end less than or equal to zero.
37+
*/
3738
@Test
3839
void testRangeEndInvalidThrows() {
39-
IllegalArgumentException exception1 = assertThrows(IllegalArgumentException.class,
40+
IllegalArgumentException ex1 = assertThrows(IllegalArgumentException.class,
4041
() -> RangeUtil.range(0));
41-
assertTrue(exception1.getMessage().contains("Parameter [end] should not less than 0"));
42+
assertTrue(ex1.getMessage().contains("should not be less than or equal to 0"));
4243

43-
IllegalArgumentException exception2 = assertThrows(IllegalArgumentException.class,
44-
() -> RangeUtil.range(-5));
45-
assertTrue(exception2.getMessage().contains("Parameter [end] should not less than 0"));
44+
IllegalArgumentException ex2 = assertThrows(IllegalArgumentException.class,
45+
() -> RangeUtil.range(-3));
46+
assertTrue(ex2.getMessage().contains("should not be less than or equal to 0"));
4647
}
4748

48-
// Test range(start, end) with valid input where start < end
49+
/**
50+
* Tests ascending range where start is less than end.
51+
*/
4952
@Test
50-
void testRangeStartEndValid() {
51-
IntStream stream = RangeUtil.range(3, 8);
53+
void testRangeStartEndAscending() {
5254
int[] expected = {3, 4, 5, 6, 7};
53-
assertArrayEquals(expected, stream.toArray());
55+
assertArrayEquals(expected, RangeUtil.range(3, 8).toArray());
56+
}
57+
58+
/**
59+
* Tests descending range where start is greater than end.
60+
*/
61+
@Test
62+
void testRangeStartEndDescending() {
63+
int[] expected = {8, 7, 6, 5, 4};
64+
assertArrayEquals(expected, RangeUtil.range(8, 3).toArray());
5465
}
5566

56-
// Test range(start, end) where start >= end should throw IllegalStateException
67+
/**
68+
* Tests empty stream when start equals end.
69+
*/
5770
@Test
58-
void testRangeStartEndInvalidThrows() {
59-
IllegalStateException exception = assertThrows(IllegalStateException.class,
60-
() -> RangeUtil.range(8, 3));
61-
assertTrue(exception.getMessage().contains("Parameter [start] should less than parameter [end]"));
62-
63-
// Also test equal values
64-
IllegalStateException exceptionEqual = assertThrows(IllegalStateException.class,
65-
() -> RangeUtil.range(5, 5));
66-
assertTrue(exceptionEqual.getMessage().contains("Parameter [start] should less than parameter [end]"));
71+
void testRangeStartEqualsEndReturnsEmpty() {
72+
assertEquals(0, RangeUtil.range(5, 5).count());
6773
}
6874

69-
// Test rangeClosed(start, end) generates inclusive ranges correctly
75+
/**
76+
* Tests that rangeClosed generates inclusive range in ascending order.
77+
*/
7078
@Test
71-
void testRangeClosed() {
72-
IntStream stream = RangeUtil.rangeClosed(3, 8);
79+
void testRangeClosedAscending() {
7380
int[] expected = {3, 4, 5, 6, 7, 8};
74-
assertArrayEquals(expected, stream.toArray());
81+
assertArrayEquals(expected, RangeUtil.rangeClosed(3, 8).toArray());
7582
}
7683

77-
// Test range(start, end, step) with positive step and valid parameters
84+
/**
85+
* Tests range method with positive step generating ascending sequence.
86+
*/
7887
@Test
79-
void testRangeWithStepPositive() {
80-
IntStream stream = RangeUtil.range(3, 10, 2);
81-
int[] expected = {3, 5, 7, 9};
82-
assertArrayEquals(expected, stream.toArray());
88+
void testRangeWithPositiveStep() {
89+
int[] expected = {2, 4, 6, 8};
90+
assertArrayEquals(expected, RangeUtil.range(2, 10, 2).toArray());
8391
}
8492

85-
// Test range(start, end, step) with negative step and valid parameters (descending range)
93+
/**
94+
* Tests range method with negative step generating descending sequence.
95+
*/
8696
@Test
87-
void testRangeWithStepNegative() {
88-
IntStream stream = RangeUtil.range(10, 3, -2);
89-
int[] expected = {10, 8, 6, 4};
90-
assertArrayEquals(expected, stream.toArray());
97+
void testRangeWithNegativeStep() {
98+
int[] expected = {10, 7, 4, 1};
99+
assertArrayEquals(expected, RangeUtil.range(10, 0, -3).toArray());
91100
}
92101

93-
// Test range(start, end, step) throws IllegalArgumentException if step is zero
102+
/**
103+
* Tests that passing zero step throws IllegalArgumentException.
104+
*/
94105
@Test
95-
void testRangeWithStepZeroThrows() {
96-
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
106+
void testRangeStepZeroThrows() {
107+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
97108
() -> RangeUtil.range(0, 10, 0));
98-
assertEquals("Step value must not be zero.", exception.getMessage());
109+
assertEquals("Step value must not be zero.", ex.getMessage());
99110
}
100111

101-
// Test range(start, end, step) throws if parameters inconsistent with step positive
112+
/**
113+
* Tests that range with positive step but invalid start/end throws IllegalArgumentException.
114+
*/
102115
@Test
103-
void testRangeWithStepPositiveInvalidRangeThrows() {
104-
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
116+
void testRangePositiveStepInvalidRangeThrows() {
117+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
105118
() -> RangeUtil.range(10, 5, 1));
106-
assertEquals("Range parameters are inconsistent with the step value.", exception.getMessage());
119+
assertEquals("Range parameters are inconsistent with the step value.", ex.getMessage());
107120
}
108121

109-
// Test range(start, end, step) throws if parameters inconsistent with step negative
122+
/**
123+
* Tests that range with negative step but invalid start/end throws IllegalArgumentException.
124+
*/
110125
@Test
111-
void testRangeWithStepNegativeInvalidRangeThrows() {
112-
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
126+
void testRangeNegativeStepInvalidRangeThrows() {
127+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
113128
() -> RangeUtil.range(5, 10, -1));
114-
assertEquals("Range parameters are inconsistent with the step value.", exception.getMessage());
129+
assertEquals("Range parameters are inconsistent with the step value.", ex.getMessage());
115130
}
116131
}

0 commit comments

Comments
 (0)