|
19 | 19 |
|
20 | 20 | import org.junit.jupiter.api.Test; |
21 | 21 |
|
22 | | -import java.util.stream.IntStream; |
23 | | - |
24 | 22 | import static org.junit.jupiter.api.Assertions.*; |
25 | 23 |
|
26 | 24 | class RangeUtilTest { |
27 | 25 |
|
28 | | - // Test range(end) with normal positive end value |
| 26 | + /** |
| 27 | + * Tests generating ascending range from 0 up to end (exclusive). |
| 28 | + */ |
29 | 29 | @Test |
30 | 30 | void testRangeEndValid() { |
31 | | - IntStream stream = RangeUtil.range(5); |
32 | 31 | int[] expected = {0, 1, 2, 3, 4}; |
33 | | - assertArrayEquals(expected, stream.toArray()); |
| 32 | + assertArrayEquals(expected, RangeUtil.range(5).toArray()); |
34 | 33 | } |
35 | 34 |
|
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 | + */ |
37 | 38 | @Test |
38 | 39 | void testRangeEndInvalidThrows() { |
39 | | - IllegalArgumentException exception1 = assertThrows(IllegalArgumentException.class, |
| 40 | + IllegalArgumentException ex1 = assertThrows(IllegalArgumentException.class, |
40 | 41 | () -> 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")); |
42 | 43 |
|
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")); |
46 | 47 | } |
47 | 48 |
|
48 | | - // Test range(start, end) with valid input where start < end |
| 49 | + /** |
| 50 | + * Tests ascending range where start is less than end. |
| 51 | + */ |
49 | 52 | @Test |
50 | | - void testRangeStartEndValid() { |
51 | | - IntStream stream = RangeUtil.range(3, 8); |
| 53 | + void testRangeStartEndAscending() { |
52 | 54 | 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()); |
54 | 65 | } |
55 | 66 |
|
56 | | - // Test range(start, end) where start >= end should throw IllegalStateException |
| 67 | + /** |
| 68 | + * Tests empty stream when start equals end. |
| 69 | + */ |
57 | 70 | @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()); |
67 | 73 | } |
68 | 74 |
|
69 | | - // Test rangeClosed(start, end) generates inclusive ranges correctly |
| 75 | + /** |
| 76 | + * Tests that rangeClosed generates inclusive range in ascending order. |
| 77 | + */ |
70 | 78 | @Test |
71 | | - void testRangeClosed() { |
72 | | - IntStream stream = RangeUtil.rangeClosed(3, 8); |
| 79 | + void testRangeClosedAscending() { |
73 | 80 | int[] expected = {3, 4, 5, 6, 7, 8}; |
74 | | - assertArrayEquals(expected, stream.toArray()); |
| 81 | + assertArrayEquals(expected, RangeUtil.rangeClosed(3, 8).toArray()); |
75 | 82 | } |
76 | 83 |
|
77 | | - // Test range(start, end, step) with positive step and valid parameters |
| 84 | + /** |
| 85 | + * Tests range method with positive step generating ascending sequence. |
| 86 | + */ |
78 | 87 | @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()); |
83 | 91 | } |
84 | 92 |
|
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 | + */ |
86 | 96 | @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()); |
91 | 100 | } |
92 | 101 |
|
93 | | - // Test range(start, end, step) throws IllegalArgumentException if step is zero |
| 102 | + /** |
| 103 | + * Tests that passing zero step throws IllegalArgumentException. |
| 104 | + */ |
94 | 105 | @Test |
95 | | - void testRangeWithStepZeroThrows() { |
96 | | - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, |
| 106 | + void testRangeStepZeroThrows() { |
| 107 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
97 | 108 | () -> 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()); |
99 | 110 | } |
100 | 111 |
|
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 | + */ |
102 | 115 | @Test |
103 | | - void testRangeWithStepPositiveInvalidRangeThrows() { |
104 | | - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, |
| 116 | + void testRangePositiveStepInvalidRangeThrows() { |
| 117 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
105 | 118 | () -> 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()); |
107 | 120 | } |
108 | 121 |
|
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 | + */ |
110 | 125 | @Test |
111 | | - void testRangeWithStepNegativeInvalidRangeThrows() { |
112 | | - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, |
| 126 | + void testRangeNegativeStepInvalidRangeThrows() { |
| 127 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
113 | 128 | () -> 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()); |
115 | 130 | } |
116 | 131 | } |
0 commit comments