fix: make BlockArea.iterator() use the same order as noise and facet data (#4797)

* fix: make BlockArea.iterator() use the same order as noise and facet data

* add javadoc explaining BlockArea iterator order

* add test for BlockArea iterator
develop
Sam Dirkswager 2021-06-30 08:40:27 -05:00 committed by GitHub
parent fbeed19316
commit 0524dead18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View File

@ -4,11 +4,13 @@
package org.terasology.engine.world.block;
import org.joml.Vector2i;
import org.joml.Vector2ic;
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;
import java.util.Iterator;
import java.util.Optional;
import java.util.stream.Stream;
@ -19,6 +21,24 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class BlockAreaTest {
@Test
public void testIterator() {
BlockArea a = new BlockArea(0, 0, 2, 2);
// Make sure the iterator visits positions in the correct order, and that it works without calling hasNext() in between
Iterator<Vector2ic> i = a.iterator();
assertEquals(i.next(), new Vector2i(0, 0));
assertEquals(i.next(), new Vector2i(1, 0));
assertEquals(i.next(), new Vector2i(2, 0));
assertEquals(i.next(), new Vector2i(0, 1));
assertEquals(i.next(), new Vector2i(1, 1));
assertEquals(i.next(), new Vector2i(2, 1));
assertEquals(i.next(), new Vector2i(0, 2));
assertEquals(i.next(), new Vector2i(1, 2));
assertEquals(i.next(), new Vector2i(2, 2));
assertFalse(i.hasNext());
}
@Test
public void containsInvalid() {
BlockArea a = new BlockArea(BlockArea.INVALID);

View File

@ -73,6 +73,10 @@ public class BlockArea implements BlockAreac {
// -- ITERABLE ---------------------------------------------------------------------------------------------------//
/**
* Iterates over each position in the BlockArea, in x-first order.
* For example: (0, 0), (1, 0), (2, 0), (0, 1), (1, 1), ...
*/
@Override
public Iterator<Vector2ic> iterator() {
return new Iterator<Vector2ic>() {
@ -81,10 +85,10 @@ public class BlockArea implements BlockAreac {
public boolean findNext() {
if (current.equals(next)) {
next.y++;
if (next.y > maxY) {
next.y = minY;
next.x++;
next.x++;
if (next.x > maxX) {
next.x = minX;
next.y++;
}
return contains(next);
@ -116,6 +120,7 @@ public class BlockArea implements BlockAreac {
if (current.equals(next)) {
if (findNext()) {
current.set(next);
return next;
}
return null;