-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIntArrayTest.java
More file actions
147 lines (128 loc) · 4.42 KB
/
IntArrayTest.java
File metadata and controls
147 lines (128 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package javasabr.rlib.collections.array;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/**
* @author JavaSaBr
*/
public class IntArrayTest {
@Test
void arrayOfTest() {
// when:
IntArray array1 = IntArray.of(5);
IntArray array2 = IntArray.of(5, 8);
IntArray array3 = IntArray.of(5, 8, 13);
IntArray array4 = IntArray.of(5, 8, 13, 25);
IntArray array5 = IntArray.of(5, 8, 13, 25, 33);
// then:
assertThat(array1.toArray())
.isEqualTo(new int[]{5});
assertThat(array2.toArray())
.isEqualTo(new int[]{5, 8});
assertThat(array3.toArray())
.isEqualTo(new int[]{5, 8, 13});
assertThat(array4.toArray())
.isEqualTo(new int[]{5, 8, 13, 25});
assertThat(array5.toArray())
.isEqualTo(new int[]{5, 8, 13, 25, 33});
}
@Test
void arrayRepeatedTest() {
// when:
IntArray repeated = IntArray.repeated(20, 5);
// then:
assertThat(repeated.toArray())
.isEqualTo(new int[]{20, 20, 20, 20, 20});
}
@ParameterizedTest
@MethodSource("generateArrays")
void shouldCorrectlyTakeValues(IntArray array) {
// then:
Assertions.assertEquals(4, array.size());
Assertions.assertEquals(5, array.get(0));
Assertions.assertEquals(8, array.get(1));
Assertions.assertEquals(13, array.get(2));
Assertions.assertEquals(25, array.get(3));
Assertions.assertEquals(5, array.first());
Assertions.assertEquals(25, array.last());
// then:
Assertions.assertArrayEquals(new int[]{5, 8, 13, 25}, array.stream().toArray());
// then:
Assertions.assertTrue(array.contains(8));
Assertions.assertFalse(array.contains(99));
}
@ParameterizedTest
@MethodSource("generateArraysWithDuplicates")
void shouldFindElementIndex(IntArray array) {
// when/then:
Assertions.assertEquals(0, array.indexOf(5));
Assertions.assertEquals(3, array.indexOf(25));
Assertions.assertEquals(-1, array.indexOf(99));
}
@ParameterizedTest
@MethodSource("generateArraysWithDuplicates")
void shouldFindLastElementIndex(IntArray array) {
// when/then:
Assertions.assertEquals(4, array.lastIndexOf(5));
Assertions.assertEquals(6, array.lastIndexOf(13));
Assertions.assertEquals(-1, array.lastIndexOf(99));
}
@ParameterizedTest
@MethodSource("generateArrays")
void shouldCorrectlyTransformToNativeArray(IntArray array) {
// when/then:
Assertions.assertArrayEquals(new int[]{5, 8, 13, 25}, array.stream().toArray());
}
@ParameterizedTest
@MethodSource("generateArrays")
void shouldCorrectlyCheckOnContains(IntArray array) {
// when/then:
assertThat(array.contains(5)).isTrue();
assertThat(array.contains(25)).isTrue();
assertThat(array.contains(99)).isFalse();
}
@ParameterizedTest
@MethodSource("generateArrays")
void shouldCorrectlyCheckOnContainsAllByArray(IntArray array) {
// given:
IntArray check1 = IntArray.of(5);
IntArray check2 = IntArray.of(8, 13);
IntArray check3 = IntArray.of(8, 13, 99);
// when/then:
assertThat(array.containsAll(check1)).isTrue();
assertThat(array.containsAll(check2)).isTrue();
assertThat(array.containsAll(check3)).isFalse();
}
@ParameterizedTest
@MethodSource("generateArrays")
void shouldCorrectlyCheckOnContainsAllByNativeArray(IntArray array) {
// given:
int[] check1 = new int[]{5};
int[] check2 = new int[]{8, 13};
int[] check3 = new int[]{8, 13, 99};
// when/then:
assertThat(array.containsAll(check1)).isTrue();
assertThat(array.containsAll(check2)).isTrue();
assertThat(array.containsAll(check3)).isFalse();
}
private static Stream<Arguments> generateArrays() {
IntArray array = IntArray.of(5, 8, 13, 25);
MutableIntArray mutableArray = ArrayFactory.mutableIntArray();
mutableArray.addAll(array);
return Stream.of(
Arguments.of(array),
Arguments.of(mutableArray));
}
private static Stream<Arguments> generateArraysWithDuplicates() {
IntArray array = IntArray.of(5, 8, 13, 25, 5, 8, 13);
MutableIntArray mutableArray = ArrayFactory.mutableIntArray();
mutableArray.addAll(array);
return Stream.of(
Arguments.of(array),
Arguments.of(mutableArray));
}
}