Skip to content

Commit a492669

Browse files
committed
add more tests
1 parent 33f64ed commit a492669

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

rlib-collections/src/test/java/javasabr/rlib/collections/array/MutableArrayTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,61 @@ void shouldClearArray(MutableArray<String> mutableArray) {
208208
Assertions.assertArrayEquals(new String[0], mutableArray.toArray());
209209
}
210210

211+
@ParameterizedTest
212+
@MethodSource("generateArraysWithTrimToSize")
213+
@DisplayName("should trim to size wrapped array")
214+
void shouldTrimToSizeWrappedArray(MutableArray<String> mutableArray) {
215+
// given:
216+
UnsafeMutableArray<String> unsafe = mutableArray.asUnsafe();
217+
218+
// when:
219+
for(int i = 0; i < 100; i++) {
220+
mutableArray.add("value_" + i);
221+
}
222+
223+
// then:
224+
Assertions.assertEquals(100, mutableArray.size());
225+
226+
// when:
227+
for(int i = 0; i < 30; i++) {
228+
mutableArray.remove(i);
229+
}
230+
231+
// then:
232+
Assertions.assertEquals(70, mutableArray.size());
233+
Assertions.assertEquals(109, unsafe.wrapped().length);
234+
235+
// when:
236+
unsafe.trimToSize();
237+
238+
// then:
239+
Assertions.assertEquals(70, unsafe.wrapped().length);
240+
}
241+
242+
@ParameterizedTest
243+
@MethodSource("generateMutableArrays")
244+
@DisplayName("should render to string correctly")
245+
void shouldRenderToStringCorrectly(MutableArray<String> mutableArray) {
246+
// when:
247+
for(int i = 0; i < 10; i++) {
248+
mutableArray.add("val_" + i);
249+
}
250+
// then:
251+
Assertions.assertEquals(
252+
"[val_0, val_1, val_2, val_3, val_4, val_5, val_6, val_7, val_8, val_9]",
253+
mutableArray.toString());
254+
}
255+
211256
private static Stream<Arguments> generateMutableArrays() {
212257
return Stream.of(
213258
Arguments.of(ArrayFactory.mutableArray(String.class)),
214259
Arguments.of(ArrayFactory.copyOnModifyArray(String.class)),
215260
Arguments.of(ArrayFactory.stampedLockBasedArray(String.class)));
216261
}
262+
263+
private static Stream<Arguments> generateArraysWithTrimToSize() {
264+
return Stream.of(
265+
Arguments.of(ArrayFactory.mutableArray(String.class)),
266+
Arguments.of(ArrayFactory.stampedLockBasedArray(String.class)));
267+
}
217268
}

rlib-collections/src/test/java/javasabr/rlib/collections/array/MutableIntArrayTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ void shouldCorrectlyRemoveBatchElements(MutableIntArray mutableArray) {
162162
Assertions.assertEquals(120, mutableArray.get(20));
163163
Assertions.assertEquals(41, mutableArray.get(21));
164164
Assertions.assertEquals(46, mutableArray.get(26));
165+
166+
// when:
167+
mutableArray.removeAll(anotherNativeArray);
168+
169+
// then:
170+
Assertions.assertEquals(21, mutableArray.size());
165171
}
166172

167173
@ParameterizedTest
@@ -186,6 +192,49 @@ void shouldClearArray(MutableIntArray mutableArray) {
186192
Assertions.assertArrayEquals(new int[0], mutableArray.toArray());
187193
}
188194

195+
@ParameterizedTest
196+
@MethodSource("generateMutableArrays")
197+
@DisplayName("should trim to size wrapped array")
198+
void shouldTrimToSizeWrappedArray(MutableIntArray mutableArray) {
199+
// given:
200+
UnsafeMutableIntArray unsafe = mutableArray.asUnsafe();
201+
202+
// when:
203+
for(int i = 0; i < 100; i++) {
204+
mutableArray.add(100 + i);
205+
}
206+
207+
// then:
208+
Assertions.assertEquals(100, mutableArray.size());
209+
210+
// when:
211+
for(int i = 0; i < 30; i++) {
212+
mutableArray.removeByIndex(i);
213+
}
214+
215+
// then:
216+
Assertions.assertEquals(70, mutableArray.size());
217+
Assertions.assertEquals(109, unsafe.wrapped().length);
218+
219+
// when:
220+
unsafe.trimToSize();
221+
222+
// then:
223+
Assertions.assertEquals(70, unsafe.wrapped().length);
224+
}
225+
226+
@ParameterizedTest
227+
@MethodSource("generateMutableArrays")
228+
@DisplayName("should render to string correctly")
229+
void shouldRenderToStringCorrectly(MutableIntArray mutableArray) {
230+
// when:
231+
for(int i = 0; i < 10; i++) {
232+
mutableArray.add(20 + i);
233+
}
234+
// then:
235+
Assertions.assertEquals("[20,21,22,23,24,25,26,27,28,29]", mutableArray.toString());
236+
}
237+
189238
private static Stream<Arguments> generateMutableArrays() {
190239
return Stream.of(
191240
Arguments.of(ArrayFactory.mutableIntArray()));

rlib-collections/src/test/java/javasabr/rlib/collections/array/MutableLongArrayTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ void shouldCorrectlyRemoveBatchElements(MutableLongArray mutableArray) {
162162
Assertions.assertEquals(120, mutableArray.get(20));
163163
Assertions.assertEquals(41, mutableArray.get(21));
164164
Assertions.assertEquals(46, mutableArray.get(26));
165+
166+
// when:
167+
mutableArray.removeAll(anotherNativeArray);
168+
169+
// then:
170+
Assertions.assertEquals(21, mutableArray.size());
165171
}
166172

167173
@ParameterizedTest
@@ -186,6 +192,49 @@ void shouldClearArray(MutableLongArray mutableArray) {
186192
Assertions.assertArrayEquals(new long[0], mutableArray.toArray());
187193
}
188194

195+
@ParameterizedTest
196+
@MethodSource("generateMutableArrays")
197+
@DisplayName("should trim to size wrapped array")
198+
void shouldTrimToSizeWrappedArray(MutableLongArray mutableArray) {
199+
// given:
200+
UnsafeMutableLongArray unsafe = mutableArray.asUnsafe();
201+
202+
// when:
203+
for(int i = 0; i < 100; i++) {
204+
mutableArray.add(100 + i);
205+
}
206+
207+
// then:
208+
Assertions.assertEquals(100, mutableArray.size());
209+
210+
// when:
211+
for(int i = 0; i < 30; i++) {
212+
mutableArray.removeByIndex(i);
213+
}
214+
215+
// then:
216+
Assertions.assertEquals(70, mutableArray.size());
217+
Assertions.assertEquals(109, unsafe.wrapped().length);
218+
219+
// when:
220+
unsafe.trimToSize();
221+
222+
// then:
223+
Assertions.assertEquals(70, unsafe.wrapped().length);
224+
}
225+
226+
@ParameterizedTest
227+
@MethodSource("generateMutableArrays")
228+
@DisplayName("should render to string correctly")
229+
void shouldRenderToStringCorrectly(MutableLongArray mutableArray) {
230+
// when:
231+
for(int i = 0; i < 10; i++) {
232+
mutableArray.add(20 + i);
233+
}
234+
// then:
235+
Assertions.assertEquals("[20,21,22,23,24,25,26,27,28,29]", mutableArray.toString());
236+
}
237+
189238
private static Stream<Arguments> generateMutableArrays() {
190239
return Stream.of(
191240
Arguments.of(ArrayFactory.mutableLongArray()));

0 commit comments

Comments
 (0)