use of javaslang.Tuple2 in project javaslang by javaslang.
the class VectorPropertyTest method shouldBehaveLikeArray.
@Test
public void shouldBehaveLikeArray() {
final int seed = ThreadLocalRandom.current().nextInt();
System.out.println("using seed " + seed);
final Random random = new Random(seed);
for (int i = 1; i < 10; i++) {
Seq<Object> expected = Array.empty();
Vector<Object> actual = Vector.empty();
for (int j = 0; j < 20_000; j++) {
Seq<Tuple2<Seq<Object>, Vector<Object>>> history = Array.empty();
if (percent(random) < 20) {
expected = Array.ofAll(Vector.ofAll(randomValues(random, 100)).filter(v -> v instanceof Integer));
actual = (percent(random) < 30) ? Vector.narrow(Vector.ofAll(ArrayType.<int[]>asPrimitives(int.class, expected))) : Vector.ofAll(expected);
assertAreEqual(expected, actual);
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 50) {
final Object value = randomValue(random);
expected = expected.append(value);
actual = assertAreEqual(actual, value, Vector::append, expected);
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 10) {
Iterable<Object> values = randomValues(random, random.nextInt(2 * BRANCHING_FACTOR));
expected = expected.appendAll(values);
values = (percent(random) < 50) ? Iterator.ofAll(values.iterator()) : values;
/* not traversable again */
actual = assertAreEqual(actual, values, Vector::appendAll, expected);
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 50) {
final Object value = randomValue(random);
expected = expected.prepend(value);
actual = assertAreEqual(actual, value, Vector::prepend, expected);
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 10) {
Iterable<Object> values = randomValues(random, random.nextInt(2 * BRANCHING_FACTOR));
expected = expected.prependAll(values);
values = (percent(random) < 50) ? Iterator.ofAll(values) : values;
/* not traversable again */
actual = assertAreEqual(actual, values, Vector::prependAll, expected);
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 30) {
final int n = random.nextInt(expected.size() + 1);
expected = expected.drop(n);
actual = assertAreEqual(actual, n, Vector::drop, expected);
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 10) {
final int index = random.nextInt(expected.size() + 1);
Iterable<Object> values = randomValues(random, random.nextInt(2 * BRANCHING_FACTOR));
expected = expected.insertAll(index, values);
values = (percent(random) < 50) ? Iterator.ofAll(values) : values;
/* not traversable again */
actual = assertAreEqual(actual, values, (a, p) -> a.insertAll(index, p), expected);
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 30) {
final int n = random.nextInt(expected.size() + 1);
expected = expected.take(n);
actual = assertAreEqual(actual, n, Vector::take, expected);
history = history.append(Tuple(expected, actual));
}
if (!expected.isEmpty()) {
assertThat(actual.head()).isEqualTo(expected.head());
assertThat(actual.tail().toJavaList()).isEqualTo(expected.tail().toJavaList());
history = history.append(Tuple(expected, actual));
}
if (!expected.isEmpty()) {
final int index = random.nextInt(expected.size());
assertThat(actual.get(index)).isEqualTo(expected.get(index));
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 50) {
if (!expected.isEmpty()) {
final int index = random.nextInt(expected.size());
final Object value = randomValue(random);
expected = expected.update(index, value);
actual = assertAreEqual(actual, null, (a, p) -> a.update(index, value), expected);
history = history.append(Tuple(expected, actual));
}
}
if (percent(random) < 20) {
final Function<Object, Object> mapper = val -> (val instanceof Integer) ? ((Integer) val + 1) : val;
expected = expected.map(mapper);
actual = assertAreEqual(actual, null, (a, p) -> a.map(mapper), expected);
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 30) {
final Predicate<Object> filter = val -> (String.valueOf(val).length() % 10) == 0;
expected = expected.filter(filter);
actual = assertAreEqual(actual, null, (a, p) -> a.filter(filter), expected);
history = history.append(Tuple(expected, actual));
}
if (percent(random) < 30) {
for (int k = 0; k < 2; k++) {
if (!expected.isEmpty()) {
final int to = random.nextInt(expected.size());
final int from = random.nextInt(to + 1);
expected = expected.slice(from, to);
actual = assertAreEqual(actual, null, (a, p) -> a.slice(from, to), expected);
history = history.append(Tuple(expected, actual));
}
}
}
// test that the modifications are persistent
history.forEach(t -> assertAreEqual(t._1, t._2));
}
}
}
Aggregations