Skip to content

Commit ad17e55

Browse files
committed
add tests
1 parent 8c1e23d commit ad17e55

9 files changed

Lines changed: 499 additions & 18 deletions

File tree

rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayIterationFunctions.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
public interface ArrayIterationFunctions<E> {
1111

1212
@Nullable
13-
<T> E findAny(T arg1, BiPredicate<? super E, T> filter);
13+
<A> E findAny(A arg1, BiPredicate<? super E, A> filter);
1414

1515
@Nullable
1616
E findAny(int arg1, ObjIntPredicate<? super E> filter);
1717

18-
<T> ArrayIterationFunctions<E> forEach(T arg1, BiConsumer<? super E, T> consumer);
18+
<A> ArrayIterationFunctions<E> forEach(A arg1, BiConsumer<? super E, A> consumer);
1919

20-
<F, S> ArrayIterationFunctions<E> forEach(F arg1, S arg2, TriConsumer<? super E, F, S> consumer);
20+
<A, B> ArrayIterationFunctions<E> forEach(A arg1, B arg2, TriConsumer<? super E, A, B> consumer);
2121

22-
<F> ArrayIterationFunctions<E> forEach(F arg1, long arg2, ObjObjLongConsumer<? super E, F> consumer);
22+
<A> ArrayIterationFunctions<E> forEach(A arg1, long arg2, ObjObjLongConsumer<? super E, A> consumer);
2323

24-
<T> boolean anyMatch(T arg1, BiPredicate<? super E, T> filter);
24+
<A> boolean anyMatch(A arg1, BiPredicate<? super E, A> filter);
2525
}

rlib-collections/src/main/java/javasabr/rlib/collections/array/ReversedArrayIterationFunctions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
public interface ReversedArrayIterationFunctions<E> {
99

1010
@Nullable
11-
<T> E findAny(T arg1, BiPredicate<T, ? super E> filter);
11+
<A> E findAny(A arg1, BiPredicate<A, ? super E> filter);
1212

13-
<T> ReversedArrayIterationFunctions<E> forEach(T arg1, BiConsumer<T, ? super E> consumer);
13+
<A> ReversedArrayIterationFunctions<E> forEach(A arg1, BiConsumer<A, ? super E> consumer);
1414

15-
<F, S> ReversedArrayIterationFunctions<E> forEach(F arg1, S arg2, TriConsumer<F, S, ? super E> consumer);
15+
<A, B> ReversedArrayIterationFunctions<E> forEach(A arg1, B arg2, TriConsumer<A, B, ? super E> consumer);
1616

17-
<T> boolean anyMatch(T arg1, BiPredicate<T, ? super E> filter);
17+
<A> boolean anyMatch(A arg1, BiPredicate<A, ? super E> filter);
1818
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package javasabr.rlib.collections.array;
2+
3+
import java.util.Objects;
4+
import java.util.stream.Stream;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
class ArrayIterationsTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("generateArrays")
14+
void shouldFindAnyCorrectly(Array<String> array) {
15+
// when/then:
16+
Assertions.assertNull(array
17+
.iterations()
18+
.findAny("notexist", Objects::equals));
19+
Assertions.assertNotNull(array
20+
.iterations()
21+
.findAny("Second", Objects::equals));
22+
}
23+
24+
@ParameterizedTest
25+
@MethodSource("generateArrays")
26+
void shouldFindAny2Correctly(Array<String> array) {
27+
// when/then:
28+
Assertions.assertNull(array
29+
.iterations()
30+
.findAny(10, (element, intArg) -> String.valueOf(intArg).equals(element)));
31+
Assertions.assertNotNull(array
32+
.iterations()
33+
.findAny(5, (element, intArg) -> String.valueOf(intArg).equals(element)));
34+
}
35+
36+
@ParameterizedTest
37+
@MethodSource("generateArrays")
38+
void shouldDoForEachCorrectly(Array<String> array) {
39+
// given:
40+
var result = MutableArray.ofType(String.class);
41+
var expected = Array.typed(String.class,
42+
"First_postfix",
43+
"Second_postfix",
44+
"Third_postfix",
45+
" _postfix",
46+
"5_postfix");
47+
48+
// when:
49+
array.iterations()
50+
.forEach("_postfix", (element, arg1) -> result.add(element + arg1));
51+
52+
// then:
53+
Assertions.assertEquals(expected, result);
54+
}
55+
56+
@ParameterizedTest
57+
@MethodSource("generateArrays")
58+
void shouldDoForEach2Correctly(Array<String> array) {
59+
// given:
60+
var result = MutableArray.ofType(String.class);
61+
var expected = Array.typed(String.class,
62+
"prefix_First_postfix",
63+
"prefix_Second_postfix",
64+
"prefix_Third_postfix",
65+
"prefix_ _postfix",
66+
"prefix_5_postfix");
67+
68+
// when:
69+
array.iterations()
70+
.forEach("prefix_", "_postfix", (element, arg1, arg2) -> result.add(arg1 + element + arg2));
71+
72+
// then:
73+
Assertions.assertEquals(expected, result);
74+
}
75+
76+
@ParameterizedTest
77+
@MethodSource("generateArrays")
78+
void shouldDoForEach3Correctly(Array<String> array) {
79+
// given:
80+
var result = MutableArray.ofType(String.class);
81+
var expected = Array.typed(String.class,
82+
"prefix_First55",
83+
"prefix_Second55",
84+
"prefix_Third55",
85+
"prefix_ 55",
86+
"prefix_555");
87+
88+
// when:
89+
array.iterations()
90+
.forEach("prefix_", 55L, (element, arg1, arg2) -> result.add(arg1 + element + arg2));
91+
92+
// then:
93+
Assertions.assertEquals(expected, result);
94+
}
95+
96+
@ParameterizedTest
97+
@MethodSource("generateArrays")
98+
void shouldAnyMatchCorrectly(Array<String> array) {
99+
// when/then:
100+
Assertions.assertFalse(array
101+
.iterations()
102+
.anyMatch(10, (element, intArg) -> String.valueOf(intArg).equals(element)));
103+
Assertions.assertTrue(array
104+
.iterations()
105+
.anyMatch(5, (element, intArg) -> String.valueOf(intArg).equals(element)));
106+
}
107+
108+
private static Stream<Arguments> generateArrays() {
109+
Array<String> array = Array.typed(String.class, "First", "Second", "Third", " ", "5");
110+
MutableArray<String> mutableArray = ArrayFactory.mutableArray(String.class);
111+
mutableArray.addAll(array);
112+
MutableArray<String> copyOnModifyArray = ArrayFactory.copyOnModifyArray(String.class);
113+
copyOnModifyArray.addAll(array);
114+
LockableArray<String> stampedLockBasedArray = ArrayFactory.stampedLockBasedArray(String.class);
115+
stampedLockBasedArray.addAll(array);
116+
return Stream.of(
117+
Arguments.of(array),
118+
Arguments.of(mutableArray),
119+
Arguments.of(copyOnModifyArray),
120+
Arguments.of(stampedLockBasedArray));
121+
}
122+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ void shouldCorrectlyTakeValues(Array<String> array) {
6868
Assertions.assertFalse(array.contains("test"));
6969
}
7070

71+
@ParameterizedTest
72+
@MethodSource("generateArrays")
73+
void shouldFindElementIndex(Array<String> array) {
74+
// when/then:
75+
Assertions.assertEquals(0, array.indexOf("First"));
76+
Assertions.assertEquals(3, array.indexOf(" "));
77+
Assertions.assertEquals(-1, array.indexOf("notexist"));
78+
}
79+
80+
@ParameterizedTest
81+
@MethodSource("generateArrays")
82+
void shouldFindElementIndexWithFunction(Array<String> array) {
83+
// when/then:
84+
Assertions.assertEquals(0, array.indexOf(s -> s + s, "FirstFirst"));
85+
Assertions.assertEquals(3, array.indexOf(s -> s + s, " "));
86+
Assertions.assertEquals(-1, array.indexOf("notexist"));
87+
}
88+
7189
@ParameterizedTest
7290
@MethodSource("generateArrays")
7391
void shouldCorrectlyTransformToNativeArray(Array<String> array) {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package javasabr.rlib.collections.array;
2+
3+
import java.util.Objects;
4+
import java.util.stream.Stream;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
class ReversedArrayIterationsTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("generateArrays")
14+
void shouldFindAnyCorrectly(Array<String> array) {
15+
// when/then:
16+
Assertions.assertNull(array
17+
.reversedIterations()
18+
.findAny("notexist", Objects::equals));
19+
Assertions.assertNotNull(array
20+
.iterations()
21+
.findAny("Second", Objects::equals));
22+
}
23+
24+
@ParameterizedTest
25+
@MethodSource("generateArrays")
26+
void shouldDoForEachCorrectly(Array<String> array) {
27+
// given:
28+
var result = MutableArray.ofType(String.class);
29+
var expected = Array.typed(String.class,
30+
"prefix_First",
31+
"prefix_Second",
32+
"prefix_Third",
33+
"prefix_ ",
34+
"prefix_5");
35+
36+
// when:
37+
array.reversedIterations()
38+
.forEach("prefix_", (arg1, element) -> result.add(arg1 + element));
39+
40+
// then:
41+
Assertions.assertEquals(expected, result);
42+
}
43+
44+
@ParameterizedTest
45+
@MethodSource("generateArrays")
46+
void shouldDoForEach2Correctly(Array<String> array) {
47+
// given:
48+
var result = MutableArray.ofType(String.class);
49+
var expected = Array.typed(String.class,
50+
"prefix__middle_First",
51+
"prefix__middle_Second",
52+
"prefix__middle_Third",
53+
"prefix__middle_ ",
54+
"prefix__middle_5");
55+
56+
// when:
57+
array.reversedIterations()
58+
.forEach("prefix_", "_middle_", (arg1, arg2, element) -> result.add(arg1 + arg2 + element));
59+
60+
// then:
61+
Assertions.assertEquals(expected, result);
62+
}
63+
64+
@ParameterizedTest
65+
@MethodSource("generateArrays")
66+
void shouldAnyMatchCorrectly(Array<String> array) {
67+
// when/then:
68+
Assertions.assertFalse(array
69+
.reversedIterations()
70+
.anyMatch("10", String::equals));
71+
Assertions.assertTrue(array
72+
.reversedIterations()
73+
.anyMatch("5", String::equals));
74+
}
75+
76+
private static Stream<Arguments> generateArrays() {
77+
Array<String> array = Array.typed(String.class, "First", "Second", "Third", " ", "5");
78+
MutableArray<String> mutableArray = ArrayFactory.mutableArray(String.class);
79+
mutableArray.addAll(array);
80+
MutableArray<String> copyOnModifyArray = ArrayFactory.copyOnModifyArray(String.class);
81+
copyOnModifyArray.addAll(array);
82+
LockableArray<String> stampedLockBasedArray = ArrayFactory.stampedLockBasedArray(String.class);
83+
stampedLockBasedArray.addAll(array);
84+
return Stream.of(
85+
Arguments.of(array),
86+
Arguments.of(mutableArray),
87+
Arguments.of(copyOnModifyArray),
88+
Arguments.of(stampedLockBasedArray));
89+
}
90+
}

rlib-collections/src/test/java/javasabr/rlib/collections/array/DequeTest.java renamed to rlib-collections/src/test/java/javasabr/rlib/collections/deque/DequeTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package javasabr.rlib.collections.array;
1+
package javasabr.rlib.collections.deque;
22

33
import static javasabr.rlib.common.util.ArrayUtils.array;
44
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
@@ -12,7 +12,7 @@
1212
import java.util.List;
1313
import java.util.NoSuchElementException;
1414
import java.util.stream.Stream;
15-
import javasabr.rlib.collections.deque.DequeFactory;
15+
import javasabr.rlib.collections.array.MutableArray;
1616
import javasabr.rlib.common.util.ReflectionUtils;
1717
import org.junit.jupiter.api.Assertions;
1818
import org.junit.jupiter.api.Test;

rlib-collections/src/test/java/javasabr/rlib/collections/array/MutableRefToRefDictionaryTest.java renamed to rlib-collections/src/test/java/javasabr/rlib/collections/dictionary/MutableRefToRefDictionaryTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
package javasabr.rlib.collections.array;
1+
package javasabr.rlib.collections.dictionary;
22

33
import static javasabr.rlib.collections.dictionary.RefToRefDictionary.entry;
44

55
import java.util.stream.Stream;
6-
import javasabr.rlib.collections.dictionary.DictionaryFactory;
7-
import javasabr.rlib.collections.dictionary.MutableRefToRefDictionary;
8-
import javasabr.rlib.collections.dictionary.RefToRefDictionary;
96
import org.junit.jupiter.api.Assertions;
107
import org.junit.jupiter.params.ParameterizedTest;
118
import org.junit.jupiter.params.provider.Arguments;

rlib-collections/src/test/java/javasabr/rlib/collections/array/RefToRefDictionaryTest.java renamed to rlib-collections/src/test/java/javasabr/rlib/collections/dictionary/RefToRefDictionaryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package javasabr.rlib.collections.array;
1+
package javasabr.rlib.collections.dictionary;
22

33
import static javasabr.rlib.collections.dictionary.RefToRefDictionary.entry;
44

55
import java.util.HashSet;
66
import java.util.Set;
77
import java.util.stream.Stream;
8-
import javasabr.rlib.collections.dictionary.DictionaryFactory;
9-
import javasabr.rlib.collections.dictionary.RefToRefDictionary;
8+
import javasabr.rlib.collections.array.Array;
9+
import javasabr.rlib.collections.array.MutableArray;
1010
import javasabr.rlib.common.tuple.Tuple;
1111
import org.junit.jupiter.api.Assertions;
1212
import org.junit.jupiter.params.ParameterizedTest;

0 commit comments

Comments
 (0)