mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-14 13:13:53 +00:00
Switch to Gradle. Use git log --follow for history.
This converts the project into a multi-module Gradle build. By default, Git does not show history past a rename, so use git log --follow to see further history.
This commit is contained in:
@ -0,0 +1,161 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.minecraft.util.commands;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CommandContextTest {
|
||||
|
||||
private static final Logger log = Logger.getLogger(CommandContextTest.class.getCanonicalName());
|
||||
private static final String firstCmdString = "herpderp -opw testers \"mani world\" 'another thing' because something";
|
||||
CommandContext firstCommand;
|
||||
|
||||
@Before
|
||||
public void setUpTest() {
|
||||
try {
|
||||
firstCommand = new CommandContext(firstCmdString, new HashSet<Character>(Arrays.asList('o', 'w')));
|
||||
} catch (CommandException e) {
|
||||
log.log(Level.WARNING, "Error", e);
|
||||
fail("Unexpected exception when creating CommandContext");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = CommandException.class)
|
||||
public void testInvalidFlags() throws CommandException {
|
||||
final String failingCommand = "herpderp -opw testers";
|
||||
new CommandContext(failingCommand, new HashSet<Character>(Arrays.asList('o', 'w')));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicArgs() {
|
||||
String command = firstCommand.getCommand();
|
||||
String argOne = firstCommand.getString(0);
|
||||
String joinedArg = firstCommand.getJoinedStrings(0);
|
||||
assertEquals("herpderp", command);
|
||||
assertEquals("another thing", argOne);
|
||||
assertEquals("'another thing' because something", joinedArg);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlags() {
|
||||
assertTrue(firstCommand.hasFlag('p'));
|
||||
assertTrue(firstCommand.hasFlag('o'));
|
||||
assertTrue(firstCommand.hasFlag('w'));
|
||||
assertEquals("testers", firstCommand.getFlag('o'));
|
||||
assertEquals("mani world", firstCommand.getFlag('w'));
|
||||
assertFalse(firstCommand.hasFlag('u'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnlyQuotedString() {
|
||||
String cmd = "r \"hello goodbye have fun\"";
|
||||
String cmd2 = "r 'hellogeedby' nnnnnee";
|
||||
try {
|
||||
new CommandContext(cmd);
|
||||
new CommandContext(cmd2);
|
||||
} catch (CommandException e) {
|
||||
log.log(Level.WARNING, "Error", e);
|
||||
fail("Error creating CommandContext");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnmatchedQuote() {
|
||||
String cmd = "r \"hello goodbye have fun";
|
||||
try {
|
||||
new CommandContext(cmd);
|
||||
} catch (CommandException e) {
|
||||
log.log(Level.WARNING, "Error", e);
|
||||
fail("Error creating CommandContext");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleSpaces() {
|
||||
String cmd = "r hi self";
|
||||
try {
|
||||
new CommandContext(cmd);
|
||||
} catch (CommandException e) {
|
||||
log.log(Level.WARNING, "Error", e);
|
||||
fail("Error creating CommandContext");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlagsAnywhere() {
|
||||
try {
|
||||
CommandContext context = new CommandContext("r hello -f");
|
||||
assertTrue(context.hasFlag('f'));
|
||||
|
||||
CommandContext context2 = new CommandContext("r hello -f world");
|
||||
assertTrue(context2.hasFlag('f'));
|
||||
} catch (CommandException e) {
|
||||
log.log(Level.WARNING, "Error", e);
|
||||
fail("Error creating CommandContext");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExactJoinedStrings() {
|
||||
try {
|
||||
CommandContext context = new CommandContext("r -f \"hello world\" foo bar");
|
||||
assertTrue(context.hasFlag('f'));
|
||||
assertEquals("\"hello world\" foo bar", context.getJoinedStrings(0));
|
||||
assertEquals("foo bar", context.getJoinedStrings(1));
|
||||
|
||||
CommandContext context2 = new CommandContext("pm name \"hello world\" foo bar");
|
||||
assertEquals("\"hello world\" foo bar", context2.getJoinedStrings(1));
|
||||
} catch (CommandException e) {
|
||||
log.log(Level.WARNING, "Error", e);
|
||||
fail("Error creating CommandContext");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSlice() {
|
||||
try {
|
||||
CommandContext context = new CommandContext("foo bar baz");
|
||||
assertArrayEquals(new String[] { "foo", "bar", "baz" }, context.getSlice(0));
|
||||
|
||||
} catch (CommandException e) {
|
||||
log.log(Level.WARNING, "Error", e);
|
||||
fail("Error creating CommandContext");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyQuote() {
|
||||
try {
|
||||
CommandContext context = new CommandContext("region flag xmas blocked-cmds \"\"");
|
||||
assertEquals(context.argsLength(), 3);
|
||||
} catch (CommandException e) {
|
||||
log.log(Level.WARNING, "Error", e);
|
||||
fail("Error creating CommandContext");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit;
|
||||
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
import com.sk89q.worldedit.blocks.BlockID;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CuboidClipboardTest {
|
||||
@Test
|
||||
public void testFlipCenterPlane() throws Exception {
|
||||
testFlip(0, 1, CuboidClipboard.FlipDirection.UP_DOWN);
|
||||
testFlip(2, 3, CuboidClipboard.FlipDirection.NORTH_SOUTH);
|
||||
testFlip(4, 5, CuboidClipboard.FlipDirection.WEST_EAST);
|
||||
}
|
||||
|
||||
private void testFlip(int data, int expectedDataAfterFlip, CuboidClipboard.FlipDirection flipDirection) {
|
||||
final CuboidClipboard clipboard = new CuboidClipboard(new Vector(1, 1, 1));
|
||||
clipboard.setBlock(Vector.ZERO, new BaseBlock(BlockID.PISTON_BASE, data));
|
||||
clipboard.flip(flipDirection);
|
||||
assertEquals(expectedDataAfterFlip, clipboard.getBlock(Vector.ZERO).getData());
|
||||
}
|
||||
}
|
150
worldedit-core/src/test/java/com/sk89q/worldedit/VectorTest.java
Normal file
150
worldedit-core/src/test/java/com/sk89q/worldedit/VectorTest.java
Normal file
@ -0,0 +1,150 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit;
|
||||
|
||||
import org.junit.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class VectorTest {
|
||||
@Test
|
||||
public void collinearityTest() {
|
||||
assertCollinear(0,0,0, 0,0,0);
|
||||
|
||||
assertCollinear(0,0,0, 1,0,0);
|
||||
assertCollinear(0,0,0, 0,1,0);
|
||||
assertCollinear(0,0,0, 0,0,1);
|
||||
|
||||
assertCollinear(1,0,0, 0,0,0);
|
||||
assertCollinear(0,1,0, 0,0,0);
|
||||
assertCollinear(0,0,1, 0,0,0);
|
||||
|
||||
assertCollinear(1,0,0, 2,0,0);
|
||||
assertNotCollinear(1,0,0, 0,1,0);
|
||||
|
||||
assertNotCollinear(2,2,2, 8,4,4);
|
||||
assertCollinear(8,2,2, 8,2,2);
|
||||
assertNotCollinear(4,2,4, 4,4,4);
|
||||
assertNotCollinear(1,1,2, 4,8,2);
|
||||
assertNotCollinear(4,1,8, 1,4,4);
|
||||
assertCollinear(2,4,2, 1,2,1);
|
||||
assertNotCollinear(2,2,4, 1,2,1);
|
||||
assertNotCollinear(4,4,1, 4,4,4);
|
||||
assertNotCollinear(4,1,4, 1,8,2);
|
||||
assertCollinear(8,8,4, 4,4,2);
|
||||
assertNotCollinear(2,1,8, 1,1,2);
|
||||
assertNotCollinear(8,1,2, 2,1,2);
|
||||
assertNotCollinear(4,4,8, 2,2,8);
|
||||
assertNotCollinear(8,4,8, 1,4,8);
|
||||
assertNotCollinear(2,2,2, 1,4,2);
|
||||
assertNotCollinear(1,1,2, 8,8,2);
|
||||
assertNotCollinear(4,4,8, 8,4,4);
|
||||
assertNotCollinear(1,8,2, 4,4,4);
|
||||
assertNotCollinear(8,4,2, 1,2,2);
|
||||
assertNotCollinear(1,8,2, 8,1,4);
|
||||
assertNotCollinear(4,8,1, 4,8,8);
|
||||
assertNotCollinear(8,1,8, 8,8,8);
|
||||
assertNotCollinear(8,4,1, 4,2,2);
|
||||
assertNotCollinear(4,8,1, 4,2,1);
|
||||
assertNotCollinear(8,8,1, 2,4,2);
|
||||
assertCollinear(8,1,4, 8,1,4);
|
||||
assertNotCollinear(4,1,1, 2,4,8);
|
||||
assertNotCollinear(4,2,8, 1,4,1);
|
||||
assertNotCollinear(1,8,2, 1,8,1);
|
||||
assertNotCollinear(1,1,2, 4,2,2);
|
||||
|
||||
assertCollinear(0,0, 0,0);
|
||||
|
||||
assertCollinear(0,0, 1,0);
|
||||
assertCollinear(0,0, 0,1);
|
||||
assertCollinear(0,0, 0,0);
|
||||
|
||||
assertCollinear(1,0, 0,0);
|
||||
assertCollinear(0,1, 0,0);
|
||||
assertCollinear(0,0, 0,0);
|
||||
|
||||
assertCollinear(1,0, 2,0);
|
||||
assertNotCollinear(1,0, 0,1);
|
||||
|
||||
assertNotCollinear(2,2, 8,4);
|
||||
assertCollinear(8,2, 8,2);
|
||||
assertNotCollinear(4,2, 4,4);
|
||||
assertNotCollinear(1,1, 4,8);
|
||||
assertNotCollinear(4,1, 1,4);
|
||||
assertCollinear(2,4, 1,2);
|
||||
assertNotCollinear(2,2, 1,2);
|
||||
assertCollinear(4,4, 4,4);
|
||||
assertNotCollinear(4,1, 1,8);
|
||||
assertCollinear(8,8, 4,4);
|
||||
assertNotCollinear(2,1, 1,1);
|
||||
assertNotCollinear(8,1, 2,1);
|
||||
assertCollinear(4,4, 2,2);
|
||||
assertNotCollinear(8,4, 1,4);
|
||||
assertNotCollinear(2,2, 1,4);
|
||||
assertCollinear(1,1, 8,8);
|
||||
assertNotCollinear(4,4, 8,4);
|
||||
assertNotCollinear(1,8, 4,4);
|
||||
assertNotCollinear(8,4, 1,2);
|
||||
assertNotCollinear(1,8, 8,1);
|
||||
assertCollinear(4,8, 4,8);
|
||||
assertNotCollinear(8,1, 8,8);
|
||||
assertCollinear(8,4, 4,2);
|
||||
assertNotCollinear(4,8, 4,2);
|
||||
assertNotCollinear(8,8, 2,4);
|
||||
assertCollinear(8,1, 8,1);
|
||||
assertNotCollinear(4,1, 2,4);
|
||||
assertNotCollinear(4,2, 1,4);
|
||||
assertCollinear(1,8, 1,8);
|
||||
assertNotCollinear(1,1, 4,2);
|
||||
}
|
||||
|
||||
private void assertCollinear(double ax, double ay, double az, double bx, double by, double bz) {
|
||||
final Vector a = new Vector(ax,ay,az);
|
||||
final Vector b = new Vector(bx,by,bz);
|
||||
assertTrue(a.isCollinearWith(b));
|
||||
assertTrue(b.isCollinearWith(a));
|
||||
assertTrue(a.multiply(-1.0).isCollinearWith(b));
|
||||
assertTrue(a.isCollinearWith(b.multiply(-1.0)));
|
||||
}
|
||||
private void assertNotCollinear(double ax, double ay, double az, double bx, double by, double bz) {
|
||||
final Vector a = new Vector(ax,ay,az);
|
||||
final Vector b = new Vector(bx,by,bz);
|
||||
assertFalse(a.isCollinearWith(b));
|
||||
assertFalse(b.isCollinearWith(a));
|
||||
assertFalse(a.multiply(-1.0).isCollinearWith(b));
|
||||
assertFalse(a.isCollinearWith(b.multiply(-1.0)));
|
||||
}
|
||||
|
||||
private void assertCollinear(double ax, double az, double bx, double bz) {
|
||||
final Vector2D a = new Vector2D(ax,az);
|
||||
final Vector2D b = new Vector2D(bx,bz);
|
||||
assertTrue(a.isCollinearWith(b));
|
||||
assertTrue(b.isCollinearWith(a));
|
||||
assertTrue(a.multiply(-1.0).isCollinearWith(b));
|
||||
assertTrue(a.isCollinearWith(b.multiply(-1.0)));
|
||||
}
|
||||
private void assertNotCollinear(double ax, double az, double bx, double bz) {
|
||||
final Vector2D a = new Vector2D(ax,az);
|
||||
final Vector2D b = new Vector2D(bx,bz);
|
||||
assertFalse(a.isCollinearWith(b));
|
||||
assertFalse(b.isCollinearWith(a));
|
||||
assertFalse(a.multiply(-1.0).isCollinearWith(b));
|
||||
assertFalse(a.isCollinearWith(b.multiply(-1.0)));
|
||||
}
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.blocks;
|
||||
|
||||
import com.sk89q.worldedit.CuboidClipboard.FlipDirection;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BlockDataTest {
|
||||
|
||||
@Test
|
||||
public void testRotateFlip() {
|
||||
for (int type = 0; type < 256; ++type) {
|
||||
for (int data = 0; data < 16; ++data) {
|
||||
final String message = type + "/" + data;
|
||||
|
||||
//Test r90(r-90(x))==x
|
||||
assertEquals(message, data, BlockData.rotate90(type, BlockData.rotate90Reverse(type, data)));
|
||||
//Test r-90(r90(x))==x
|
||||
assertEquals(message, data, BlockData.rotate90Reverse(type, BlockData.rotate90(type, data)));
|
||||
|
||||
final int flipped = BlockData.flip(type, BlockData.flip(type, data, FlipDirection.WEST_EAST), FlipDirection.NORTH_SOUTH);
|
||||
|
||||
//Test r90(r90(x))==flipNS(flipWE(x))
|
||||
assertEquals(message, flipped, BlockData.rotate90(type, BlockData.rotate90(type, data)));
|
||||
//Test r-90(r-90(x))==flipNS(flipWE(x))
|
||||
assertEquals(message, flipped, BlockData.rotate90Reverse(type, BlockData.rotate90Reverse(type, data)));
|
||||
|
||||
//Test flipNS(flipNS(x))==x
|
||||
assertEquals(message, data, BlockData.flip(type, BlockData.flip(type, data, FlipDirection.NORTH_SOUTH), FlipDirection.NORTH_SOUTH));
|
||||
//Test flipWE(flipWE(x))==x
|
||||
assertEquals(message, data, BlockData.flip(type, BlockData.flip(type, data, FlipDirection.WEST_EAST), FlipDirection.WEST_EAST));
|
||||
//Test flipUD(flipUD(x))==x
|
||||
assertEquals(message, data, BlockData.flip(type, BlockData.flip(type, data, FlipDirection.UP_DOWN), FlipDirection.UP_DOWN));
|
||||
|
||||
//Test r90(r90(r90(r90(x))))==x
|
||||
assertEquals(message, data, BlockData.rotate90(type, BlockData.rotate90(type, BlockData.rotate90(type, BlockData.rotate90(type, data)))));
|
||||
//Test r-90(r-90(r-90(r-90(x))))==x
|
||||
assertEquals(message, data, BlockData.rotate90Reverse(type, BlockData.rotate90Reverse(type, BlockData.rotate90Reverse(type, BlockData.rotate90Reverse(type, data)))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final TreeSet<Integer> datasTemplate = new TreeSet<Integer>();
|
||||
static {
|
||||
for (int data = 0; data < 16; ++data) {
|
||||
datasTemplate.add(data);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCycle() {
|
||||
// Test monotony and continuity
|
||||
for (int type = 0; type < 256; ++type) {
|
||||
// Cloth isn't monotonous, and thus excluded.
|
||||
if (type == BlockID.CLOTH
|
||||
|| type == BlockID.STAINED_CLAY
|
||||
|| type == BlockID.STAINED_GLASS
|
||||
|| type == BlockID.STAINED_GLASS_PANE
|
||||
|| type == BlockID.CARPET) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int data = 0; data < 16; ++data) {
|
||||
final String message = type + "/" + data;
|
||||
|
||||
final int cycled = BlockData.cycle(type, data, 1);
|
||||
|
||||
// If the cycle goes back (including -1), everything is ok.
|
||||
if (cycled <= data) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If there's a gap in the cycle, there's a problem.
|
||||
assertEquals(message, data + 1, cycled);
|
||||
}
|
||||
}
|
||||
|
||||
// Test cyclicity forwards
|
||||
testCycle(1);
|
||||
|
||||
// ...and backwards
|
||||
testCycle(-1);
|
||||
}
|
||||
|
||||
private static void testCycle(final int increment) {
|
||||
// Iterate each block type and data value that wasn't part of a cycle yet.
|
||||
for (int type = 0; type < 256; ++type) {
|
||||
@SuppressWarnings("unchecked")
|
||||
final TreeSet<Integer> datas = (TreeSet<Integer>) datasTemplate.clone();
|
||||
while (!datas.isEmpty()) {
|
||||
final int start = datas.pollFirst();
|
||||
String message = type + "/" + start;
|
||||
int current = start;
|
||||
boolean first = true;
|
||||
while (true) {
|
||||
current = BlockData.cycle(type, current, increment);
|
||||
|
||||
// If the cycle immediately goes to -1, everything is ok.
|
||||
if (first && current == -1) break;
|
||||
|
||||
first = false;
|
||||
message += "->" + current;
|
||||
|
||||
// If the cycle goes off limits (including -1), there's a problem.
|
||||
assertTrue(message, current >= 0);
|
||||
assertTrue(message, current < 16);
|
||||
|
||||
// The cycle completes, everything is ok.
|
||||
if (current == start) break;
|
||||
|
||||
// Mark the current element as walked.
|
||||
assertTrue(message, datas.remove(current));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.extent.transform;
|
||||
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
import com.sk89q.worldedit.blocks.BlockData;
|
||||
import com.sk89q.worldedit.blocks.BlockType;
|
||||
import com.sk89q.worldedit.math.transform.AffineTransform;
|
||||
import com.sk89q.worldedit.math.transform.Transform;
|
||||
import com.sk89q.worldedit.world.registry.BlockRegistry;
|
||||
import com.sk89q.worldedit.world.registry.LegacyBlockRegistry;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class BlockTransformExtentTest {
|
||||
|
||||
private static final Transform ROTATE_90 = new AffineTransform().rotateY(-90);
|
||||
private static final Transform ROTATE_NEG_90 = new AffineTransform().rotateY(90);
|
||||
private final Set<BlockType> ignored = new HashSet<BlockType>();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
ignored.add(BlockType.BED); // Broken in existing rotation code?
|
||||
ignored.add(BlockType.WOODEN_DOOR); // Complicated
|
||||
ignored.add(BlockType.IRON_DOOR); // Complicated
|
||||
ignored.add(BlockType.STONE_BUTTON); // Existing rotation code doesn't handle down/up directions
|
||||
ignored.add(BlockType.WOODEN_BUTTON); // Existing rotation code doesn't handle down/up directions
|
||||
ignored.add(BlockType.END_PORTAL); // Not supported in existing rotation code
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransform() throws Exception {
|
||||
BlockRegistry blockRegistry = new LegacyBlockRegistry();
|
||||
for (BlockType type : BlockType.values()) {
|
||||
if (ignored.contains(type)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
BaseBlock orig = new BaseBlock(type.getID());
|
||||
for (int i = 1; i < 4; i++) {
|
||||
BaseBlock rotated = BlockTransformExtent.transform(new BaseBlock(orig), ROTATE_90, blockRegistry);
|
||||
BaseBlock reference = new BaseBlock(orig.getType(), BlockData.rotate90(orig.getType(), orig.getData()));
|
||||
assertThat(type + "#" + type.getID() + " rotated " + (90 * i) + " degrees did not match BlockData.rotate90()'s expected result", rotated, equalTo(reference));
|
||||
orig = rotated;
|
||||
}
|
||||
|
||||
orig = new BaseBlock(type.getID());
|
||||
for (int i = 0; i < 4; i++) {
|
||||
BaseBlock rotated = BlockTransformExtent.transform(new BaseBlock(orig), ROTATE_NEG_90, blockRegistry);
|
||||
BaseBlock reference = new BaseBlock(orig.getType(), BlockData.rotate90Reverse(orig.getType(), orig.getData()));
|
||||
assertThat(type + "#" + type.getID() + " rotated " + (-90 * i) + " degrees did not match BlockData.rotate90Reverse()'s expected result", rotated, equalTo(reference));
|
||||
orig = rotated;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,208 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.internal.expression;
|
||||
|
||||
import com.sk89q.worldedit.internal.expression.lexer.LexerException;
|
||||
import com.sk89q.worldedit.internal.expression.parser.ParserException;
|
||||
import com.sk89q.worldedit.internal.expression.runtime.EvaluationException;
|
||||
import com.sk89q.worldedit.internal.expression.runtime.ExpressionEnvironment;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.lang.Math.atan2;
|
||||
import static java.lang.Math.sin;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class ExpressionTest {
|
||||
@Test
|
||||
public void testEvaluate() throws ExpressionException {
|
||||
// check
|
||||
assertEquals(1 - 2 + 3, simpleEval("1 - 2 + 3"), 0);
|
||||
|
||||
// check unary ops
|
||||
assertEquals(2 + +4, simpleEval("2 + +4"), 0);
|
||||
assertEquals(2 - -4, simpleEval("2 - -4"), 0);
|
||||
assertEquals(2 * -4, simpleEval("2 * -4"), 0);
|
||||
|
||||
// check functions
|
||||
assertEquals(sin(5), simpleEval("sin(5)"), 0);
|
||||
assertEquals(atan2(3, 4), simpleEval("atan2(3, 4)"), 0);
|
||||
|
||||
// check variables
|
||||
assertEquals(8, compile("foo+bar", "foo", "bar").evaluate(5, 3), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrors() throws ExpressionException {
|
||||
// test lexer errors
|
||||
try {
|
||||
compile("#");
|
||||
fail("Error expected");
|
||||
} catch (LexerException e) {
|
||||
assertEquals("Error position", 0, e.getPosition());
|
||||
}
|
||||
|
||||
// test parser errors
|
||||
try {
|
||||
compile("x");
|
||||
fail("Error expected");
|
||||
} catch (ParserException e) {
|
||||
assertEquals("Error position", 0, e.getPosition());
|
||||
}
|
||||
try {
|
||||
compile("x()");
|
||||
fail("Error expected");
|
||||
} catch (ParserException e) {
|
||||
assertEquals("Error position", 0, e.getPosition());
|
||||
}
|
||||
try {
|
||||
compile("(");
|
||||
fail("Error expected");
|
||||
} catch (ParserException ignored) {}
|
||||
try {
|
||||
compile("x(");
|
||||
fail("Error expected");
|
||||
} catch (ParserException ignored) {}
|
||||
|
||||
// test overloader errors
|
||||
try {
|
||||
compile("atan2(1)");
|
||||
fail("Error expected");
|
||||
} catch (ParserException e) {
|
||||
assertEquals("Error position", 0, e.getPosition());
|
||||
}
|
||||
try {
|
||||
compile("atan2(1, 2, 3)");
|
||||
fail("Error expected");
|
||||
} catch (ParserException e) {
|
||||
assertEquals("Error position", 0, e.getPosition());
|
||||
}
|
||||
try {
|
||||
compile("rotate(1, 2, 3)");
|
||||
fail("Error expected");
|
||||
} catch (ParserException e) {
|
||||
assertEquals("Error position", 0, e.getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssign() throws ExpressionException {
|
||||
Expression foo = compile("{a=x} b=y; c=z", "x", "y", "z", "a", "b", "c");
|
||||
foo.evaluate(2, 3, 5);
|
||||
assertEquals(2, foo.getVariable("a", false).getValue(), 0);
|
||||
assertEquals(3, foo.getVariable("b", false).getValue(), 0);
|
||||
assertEquals(5, foo.getVariable("c", false).getValue(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIf() throws ExpressionException {
|
||||
assertEquals(40, simpleEval("if (1) x=4; else y=5; x*10+y;"), 0);
|
||||
assertEquals(5, simpleEval("if (0) x=4; else y=5; x*10+y;"), 0);
|
||||
|
||||
// test 'dangling else'
|
||||
final Expression expression1 = compile("if (1) if (0) x=4; else y=5;", "x", "y");
|
||||
expression1.evaluate(1, 2);
|
||||
assertEquals(1, expression1.getVariable("x", false).getValue(), 0);
|
||||
assertEquals(5, expression1.getVariable("y", false).getValue(), 0);
|
||||
|
||||
// test if the if construct is correctly recognized as a statement
|
||||
final Expression expression2 = compile("if (0) if (1) x=5; y=4;", "x", "y");
|
||||
expression2.evaluate(1, 2);
|
||||
assertEquals(4, expression2.getVariable("y", false).getValue(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhile() throws ExpressionException {
|
||||
assertEquals(5, simpleEval("c=5; a=0; while (c > 0) { ++a; --c; } a"), 0);
|
||||
assertEquals(5, simpleEval("c=5; a=0; do { ++a; --c; } while (c > 0); a"), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFor() throws ExpressionException {
|
||||
assertEquals(5, simpleEval("a=0; for (i=0; i<5; ++i) { ++a; } a"), 0);
|
||||
assertEquals(12345, simpleEval("y=0; for (i=1,5) { y *= 10; y += i; } y"), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwitch() throws ExpressionException {
|
||||
assertEquals(523, simpleEval("x=1;y=2;z=3;switch (1) { case 1: x=5; break; case 2: y=6; break; default: z=7 } x*100+y*10+z"), 0);
|
||||
assertEquals(163, simpleEval("x=1;y=2;z=3;switch (2) { case 1: x=5; break; case 2: y=6; break; default: z=7 } x*100+y*10+z"), 0);
|
||||
assertEquals(127, simpleEval("x=1;y=2;z=3;switch (3) { case 1: x=5; break; case 2: y=6; break; default: z=7 } x*100+y*10+z"), 0);
|
||||
|
||||
assertEquals(567, simpleEval("x=1;y=2;z=3;switch (1) { case 1: x=5; case 2: y=6; default: z=7 } x*100+y*10+z"), 0);
|
||||
assertEquals(167, simpleEval("x=1;y=2;z=3;switch (2) { case 1: x=5; case 2: y=6; default: z=7 } x*100+y*10+z"), 0);
|
||||
assertEquals(127, simpleEval("x=1;y=2;z=3;switch (3) { case 1: x=5; case 2: y=6; default: z=7 } x*100+y*10+z"), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery() throws Exception {
|
||||
assertEquals(1, simpleEval("a=1;b=2;query(3,4,5,a,b); a==3 && b==4"), 0);
|
||||
assertEquals(1, simpleEval("a=1;b=2;queryAbs(3,4,5,a*1,b*1); a==1 && b==2"), 0);
|
||||
assertEquals(1, simpleEval("a=1;b=2;queryRel(3,4,5,(a),(b)); a==300 && b==400"), 0);
|
||||
assertEquals(1, simpleEval("query(3,4,5,3,4)"), 0);
|
||||
assertEquals(1, simpleEval("!query(3,4,5,3,2)"), 0);
|
||||
assertEquals(1, simpleEval("!queryAbs(3,4,5,10,40)"), 0);
|
||||
assertEquals(1, simpleEval("!queryRel(3,4,5,100,200)"), 0);
|
||||
}
|
||||
|
||||
private double simpleEval(String expressionString) throws ExpressionException {
|
||||
final Expression expression = compile(expressionString);
|
||||
|
||||
expression.setEnvironment(new ExpressionEnvironment() {
|
||||
@Override
|
||||
public int getBlockType(double x, double y, double z) {
|
||||
return (int) x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockData(double x, double y, double z) {
|
||||
return (int) y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockTypeAbs(double x, double y, double z) {
|
||||
return (int) x*10;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockDataAbs(double x, double y, double z) {
|
||||
return (int) y*10;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockTypeRel(double x, double y, double z) {
|
||||
return (int) x*100;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockDataRel(double x, double y, double z) {
|
||||
return (int) y*100;
|
||||
}
|
||||
});
|
||||
|
||||
return expression.evaluate();
|
||||
}
|
||||
|
||||
private Expression compile(String expressionString, String... variableNames) throws ExpressionException, EvaluationException {
|
||||
final Expression expression = Expression.compile(expressionString, variableNames);
|
||||
expression.optimize();
|
||||
return expression;
|
||||
}
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.util;
|
||||
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests {@link Location}.
|
||||
*/
|
||||
public class LocationTest {
|
||||
|
||||
private static final int TEST_VALUE = 10;
|
||||
private static final double EPSILON = 0.0001;
|
||||
|
||||
@Test
|
||||
public void testGetWorld() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location = new Location(world);
|
||||
assertEquals(world, location.getExtent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetWorld() throws Exception {
|
||||
World world1 = mock(World.class);
|
||||
World world2 = mock(World.class);
|
||||
Location location1 = new Location(world1);
|
||||
Location location2 = location1.setExtent(world2);
|
||||
assertEquals(world1, location1.getExtent());
|
||||
assertEquals(world2, location2.getExtent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToVector() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Vector position = new Vector(1, 1, 1);
|
||||
Location location = new Location(world, position);
|
||||
assertEquals(position, location.toVector());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetX() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location = new Location(world, new Vector(TEST_VALUE, 0, 0));
|
||||
assertEquals(TEST_VALUE, location.getX(), EPSILON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBlockX() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location = new Location(world, new Vector(TEST_VALUE, 0, 0));
|
||||
assertEquals(TEST_VALUE, location.getBlockX());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetX() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location1 = new Location(world, new Vector());
|
||||
Location location2 = location1.setX(TEST_VALUE);
|
||||
assertEquals(0, location1.getX(), EPSILON);
|
||||
assertEquals(TEST_VALUE, location2.getX(), EPSILON);
|
||||
assertEquals(0, location2.getY(), EPSILON);
|
||||
assertEquals(0, location2.getZ(), EPSILON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetY() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location = new Location(world, new Vector(0, TEST_VALUE, 0));
|
||||
assertEquals(TEST_VALUE, location.getY(), EPSILON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBlockY() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location = new Location(world, new Vector(0, TEST_VALUE, 0));
|
||||
assertEquals(TEST_VALUE, location.getBlockY());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetY() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location1 = new Location(world, new Vector());
|
||||
Location location2 = location1.setY(TEST_VALUE);
|
||||
assertEquals(0, location1.getY(), EPSILON);
|
||||
assertEquals(0, location2.getX(), EPSILON);
|
||||
assertEquals(TEST_VALUE, location2.getY(), EPSILON);
|
||||
assertEquals(0, location2.getZ(), EPSILON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetZ() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location = new Location(world, new Vector(0, 0, TEST_VALUE));
|
||||
assertEquals(TEST_VALUE, location.getZ(), EPSILON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBlockZ() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location = new Location(world, new Vector(0, 0, TEST_VALUE));
|
||||
assertEquals(TEST_VALUE, location.getBlockZ());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetZ() throws Exception {
|
||||
World world = mock(World.class);
|
||||
Location location1 = new Location(world, new Vector());
|
||||
Location location2 = location1.setZ(TEST_VALUE);
|
||||
assertEquals(0, location1.getZ(), EPSILON);
|
||||
assertEquals(0, location2.getX(), EPSILON);
|
||||
assertEquals(0, location2.getY(), EPSILON);
|
||||
assertEquals(TEST_VALUE, location2.getZ(), EPSILON);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user