From 93f36955ca18a934cc6eb072ae823479fd91ceeb Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Sun, 25 Sep 2011 06:03:55 +0200 Subject: [PATCH] Added a test case for BlockData. Fixed some non-breaking inaccuracies to make the test case work. --- .../com/sk89q/worldedit/data/BlockData.java | 36 ++++++++------ .../sk89q/worldedit/data/BlockDataTest.java | 49 +++++++++++++++++++ 2 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 src/test/java/com/sk89q/worldedit/data/BlockDataTest.java diff --git a/src/main/java/com/sk89q/worldedit/data/BlockData.java b/src/main/java/com/sk89q/worldedit/data/BlockData.java index f348978ca..fadbda90e 100644 --- a/src/main/java/com/sk89q/worldedit/data/BlockData.java +++ b/src/main/java/com/sk89q/worldedit/data/BlockData.java @@ -145,13 +145,13 @@ public final class BlockData { break; case BlockID.TRAP_DOOR: - int open = data & 0x4; - int withoutOpen = data & ~0x4; - switch (withoutOpen) { - case 0: return 3 | open; - case 1: return 2 | open; - case 2: return 0 | open; - case 3: return 1 | open; + int withoutOrientation = data & ~0x3; + int orientation = data & 0x3; + switch (orientation) { + case 0: return 3 | withoutOrientation; + case 1: return 2 | withoutOrientation; + case 2: return 0 | withoutOrientation; + case 3: return 1 | withoutOrientation; } break; @@ -302,13 +302,13 @@ public final class BlockData { break; case BlockID.TRAP_DOOR: - int open = data & 0x4; - int withoutOpen = data & ~0x4; - switch (withoutOpen) { - case 3: return 0 | open; - case 2: return 1 | open; - case 0: return 2 | open; - case 1: return 3 | open; + int withoutOrientation = data & ~0x3; + int orientation = data & 0x3; + switch (orientation) { + case 3: return 0 | withoutOrientation; + case 2: return 1 | withoutOrientation; + case 0: return 2 | withoutOrientation; + case 1: return 3 | withoutOrientation; } case BlockID.PISTON_BASE: @@ -380,6 +380,10 @@ public final class BlockData { case BlockID.TORCH: case BlockID.REDSTONE_TORCH_OFF: case BlockID.REDSTONE_TORCH_ON: + if (data > 4) + break; + /* FALL-THROUGH */ + case BlockID.LEVER: case BlockID.STONE_BUTTON: switch (data & ~0x8) { @@ -470,6 +474,10 @@ public final class BlockData { case BlockID.PUMPKIN: case BlockID.JACKOLANTERN: + if (data > 3) + break; + /* FALL-THROUGH */ + case BlockID.REDSTONE_REPEATER_OFF: case BlockID.REDSTONE_REPEATER_ON: switch (data & 0x3) { diff --git a/src/test/java/com/sk89q/worldedit/data/BlockDataTest.java b/src/test/java/com/sk89q/worldedit/data/BlockDataTest.java new file mode 100644 index 000000000..069d080f3 --- /dev/null +++ b/src/test/java/com/sk89q/worldedit/data/BlockDataTest.java @@ -0,0 +1,49 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.worldedit.data; + +import org.junit.*; + +import com.sk89q.worldedit.CuboidClipboard.FlipDirection; + +import static org.junit.Assert.*; + +public class BlockDataTest { + @Test + public void testSlice() { + for (int type = 0; type < 256; ++type) { + for (int data = 0; data < 16; ++data) { + final String message = type+"/"+data; + + assertEquals(message, data, BlockData.rotate90(type, BlockData.rotate90Reverse(type, data))); + assertEquals(message, data, BlockData.rotate90Reverse(type, BlockData.rotate90(type, data))); + + int flipped = BlockData.flip(type, BlockData.flip(type, data, FlipDirection.WEST_EAST), FlipDirection.NORTH_SOUTH); + + assertEquals(message, flipped, BlockData.rotate90(type, BlockData.rotate90(type, data))); + assertEquals(message, flipped, BlockData.rotate90Reverse(type, BlockData.rotate90Reverse(type, data))); + + assertEquals(message, data, BlockData.flip(type, BlockData.flip(type, data, FlipDirection.NORTH_SOUTH), FlipDirection.NORTH_SOUTH)); + assertEquals(message, data, BlockData.flip(type, BlockData.flip(type, data, FlipDirection.WEST_EAST), FlipDirection.WEST_EAST)); + assertEquals(message, data, BlockData.flip(type, BlockData.flip(type, data, FlipDirection.UP_DOWN), FlipDirection.UP_DOWN)); + } + } + } +}