Skip to content

Commit 56dbf1a

Browse files
committed
test: unit test for RangeUtil
1 parent 7363d57 commit 56dbf1a

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (C) 2024-2025 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.devkit.utils;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.stream.IntStream;
23+
24+
import static org.junit.jupiter.api.Assertions.*;
25+
26+
class RangeUtilTest {
27+
28+
// Test range(end) with normal positive end value
29+
@Test
30+
void testRangeEndValid() {
31+
IntStream stream = RangeUtil.range(5);
32+
int[] expected = {0, 1, 2, 3, 4};
33+
assertArrayEquals(expected, stream.toArray());
34+
}
35+
36+
// Test range(end) with end less than or equal to zero should throw IllegalArgumentException
37+
@Test
38+
void testRangeEndInvalidThrows() {
39+
IllegalArgumentException exception1 = assertThrows(IllegalArgumentException.class,
40+
() -> RangeUtil.range(0));
41+
assertTrue(exception1.getMessage().contains("Parameter [end] should not less than 0"));
42+
43+
IllegalArgumentException exception2 = assertThrows(IllegalArgumentException.class,
44+
() -> RangeUtil.range(-5));
45+
assertTrue(exception2.getMessage().contains("Parameter [end] should not less than 0"));
46+
}
47+
48+
// Test range(start, end) with valid input where start < end
49+
@Test
50+
void testRangeStartEndValid() {
51+
IntStream stream = RangeUtil.range(3, 8);
52+
int[] expected = {3, 4, 5, 6, 7};
53+
assertArrayEquals(expected, stream.toArray());
54+
}
55+
56+
// Test range(start, end) where start >= end should throw IllegalStateException
57+
@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]"));
67+
}
68+
69+
// Test rangeClosed(start, end) generates inclusive ranges correctly
70+
@Test
71+
void testRangeClosed() {
72+
IntStream stream = RangeUtil.rangeClosed(3, 8);
73+
int[] expected = {3, 4, 5, 6, 7, 8};
74+
assertArrayEquals(expected, stream.toArray());
75+
}
76+
77+
// Test range(start, end, step) with positive step and valid parameters
78+
@Test
79+
void testRangeWithStepPositive() {
80+
IntStream stream = RangeUtil.range(3, 10, 2);
81+
int[] expected = {3, 5, 7, 9};
82+
assertArrayEquals(expected, stream.toArray());
83+
}
84+
85+
// Test range(start, end, step) with negative step and valid parameters (descending range)
86+
@Test
87+
void testRangeWithStepNegative() {
88+
IntStream stream = RangeUtil.range(10, 3, -2);
89+
int[] expected = {10, 8, 6, 4};
90+
assertArrayEquals(expected, stream.toArray());
91+
}
92+
93+
// Test range(start, end, step) throws IllegalArgumentException if step is zero
94+
@Test
95+
void testRangeWithStepZeroThrows() {
96+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
97+
() -> RangeUtil.range(0, 10, 0));
98+
assertEquals("Step value must not be zero.", exception.getMessage());
99+
}
100+
101+
// Test range(start, end, step) throws if parameters inconsistent with step positive
102+
@Test
103+
void testRangeWithStepPositiveInvalidRangeThrows() {
104+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
105+
() -> RangeUtil.range(10, 5, 1));
106+
assertEquals("Range parameters are inconsistent with the step value.", exception.getMessage());
107+
}
108+
109+
// Test range(start, end, step) throws if parameters inconsistent with step negative
110+
@Test
111+
void testRangeWithStepNegativeInvalidRangeThrows() {
112+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
113+
() -> RangeUtil.range(5, 10, -1));
114+
assertEquals("Range parameters are inconsistent with the step value.", exception.getMessage());
115+
}
116+
}

0 commit comments

Comments
 (0)