mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-23 21:14:26 +00:00
add test for Column
This commit is contained in:
@@ -4,6 +4,8 @@ import com.dfsek.terra.api.util.function.IntIntObjConsumer;
|
|||||||
import com.dfsek.terra.api.util.function.IntObjConsumer;
|
import com.dfsek.terra.api.util.function.IntObjConsumer;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -28,13 +30,18 @@ public interface Column<T> {
|
|||||||
consumer.accept(y, get(y));
|
consumer.accept(y, get(y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
default void forRanges(IntIntObjConsumer<T> consumer) {
|
default void forRanges(IntIntObjConsumer<T> consumer) {
|
||||||
int y = getMinY();
|
int min = getMinY();
|
||||||
int runningMin = y;
|
|
||||||
T runningObj = get(runningMin);
|
int y = min + 1;
|
||||||
while(y < getMaxY()) {
|
|
||||||
y++;
|
T runningObj = get(min);
|
||||||
|
|
||||||
|
|
||||||
|
int runningMin = min;
|
||||||
|
int max = (getMaxY() - 1);
|
||||||
|
|
||||||
|
while(y < max) {
|
||||||
T current = get(y);
|
T current = get(y);
|
||||||
|
|
||||||
if(!current.equals(runningObj)) {
|
if(!current.equals(runningObj)) {
|
||||||
@@ -42,6 +49,7 @@ public interface Column<T> {
|
|||||||
runningMin = y;
|
runningMin = y;
|
||||||
runningObj = current;
|
runningObj = current;
|
||||||
}
|
}
|
||||||
|
y++;
|
||||||
}
|
}
|
||||||
consumer.accept(runningMin, ++y, runningObj);
|
consumer.accept(runningMin, ++y, runningObj);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package util;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.util.Column;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||||
|
import com.dfsek.terra.api.util.mutable.MutableInteger;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class ColumnTest {
|
||||||
|
private final Column<Integer> returnY = new ColumnImpl<>(-10, 10, Function.identity());
|
||||||
|
private final Column<Boolean> returnPositive = new ColumnImpl<>(-10, 10, i -> i >= 0);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testForEach() {
|
||||||
|
returnY.forEach(Assertions::assertEquals);
|
||||||
|
MutableInteger test = new MutableInteger(returnY.getMinY());
|
||||||
|
returnY.forEach(i -> {
|
||||||
|
assertEquals(i, test.get());
|
||||||
|
test.increment();
|
||||||
|
});
|
||||||
|
assertEquals(returnY.getMaxY(), test.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testForRanges() {
|
||||||
|
List<Pair<Pair<Integer, Integer>, Boolean>> list = new ArrayList<>();
|
||||||
|
|
||||||
|
returnPositive.forRanges((min, max, p) -> list.add(Pair.of(Pair.of(min, max), p)));
|
||||||
|
|
||||||
|
assertEquals(List.of(
|
||||||
|
Pair.of(Pair.of(-10, 0), false),
|
||||||
|
Pair.of(Pair.of(0, 10), true)
|
||||||
|
),
|
||||||
|
list);
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ColumnImpl<T> implements Column<T> {
|
||||||
|
private final int min;
|
||||||
|
private final int max;
|
||||||
|
private final Function<Integer, T> p;
|
||||||
|
|
||||||
|
ColumnImpl(int min, int max, Function<Integer, T> p) {
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
|
this.p = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMinY() {
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxY() {
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T get(int y) {
|
||||||
|
return p.apply(y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user