This commit is contained in:
Salomão
2021-03-31 18:14:16 -03:00
parent c3b1dea7c5
commit ca7ce33f34
17 changed files with 1402 additions and 593 deletions
@@ -0,0 +1,91 @@
package appeng.util;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.EnumSet;
import org.junit.jupiter.api.Test;
class EnumCyclerTest {
@Test
void testRotateEnumForwardWithOnlySomeValidOptions() {
EnumSet<RotateTestEnum> validOptions = EnumSet.of(RotateTestEnum.A, RotateTestEnum.C, RotateTestEnum.E);
assertThat(EnumCycler.rotateEnum(RotateTestEnum.A, false, validOptions)).isEqualTo(RotateTestEnum.C);
assertThat(EnumCycler.rotateEnum(RotateTestEnum.C, false, validOptions)).isEqualTo(RotateTestEnum.E);
assertThat(EnumCycler.rotateEnum(RotateTestEnum.E, false, validOptions)).isEqualTo(RotateTestEnum.A);
}
@Test
void testRotateEnumBackwardsWithOnlySomeValidOptions() {
EnumSet<RotateTestEnum> validOptions = EnumSet.of(RotateTestEnum.A, RotateTestEnum.C, RotateTestEnum.E);
assertThat(EnumCycler.rotateEnum(RotateTestEnum.A, true, validOptions)).isEqualTo(RotateTestEnum.E);
assertThat(EnumCycler.rotateEnum(RotateTestEnum.C, true, validOptions)).isEqualTo(RotateTestEnum.A);
assertThat(EnumCycler.rotateEnum(RotateTestEnum.E, true, validOptions)).isEqualTo(RotateTestEnum.C);
}
/**
* When there are no valid options, the function should reject the arguments.
*/
@Test
void testRotateEnumNoValidOptions() {
assertThrows(IllegalArgumentException.class, () -> {
EnumCycler.rotateEnum(TestEnum.A, false, EnumSet.noneOf(TestEnum.class));
});
}
/**
* When the current enum literal is not part of the valid options, it should just skip to the next valid option
* instead.
*/
@Test
void testRotateEnumCurrentIsNotAValidOption() {
assertThat(EnumCycler.rotateEnum(TestEnum.B, false, EnumSet.of(TestEnum.A))).isEqualTo(TestEnum.A);
assertThat(EnumCycler.rotateEnum(TestEnum.B, true, EnumSet.of(TestEnum.A))).isEqualTo(TestEnum.A);
}
/**
* When there's only one valid option, it should just rotate back to it.
*/
@Test
void testRotateEnumOnlyOneValidOption() {
assertThat(EnumCycler.rotateEnum(TestEnum.A, false, EnumSet.of(TestEnum.A))).isEqualTo(TestEnum.A);
assertThat(EnumCycler.rotateEnum(TestEnum.A, true, EnumSet.of(TestEnum.A))).isEqualTo(TestEnum.A);
}
@Test
void testNext() {
assertThat(EnumCycler.next(TestEnum.A)).isEqualTo(TestEnum.B);
assertThat(EnumCycler.next(TestEnum.B)).isEqualTo(TestEnum.C);
assertThat(EnumCycler.next(TestEnum.C)).isEqualTo(TestEnum.A);
assertThat(EnumCycler.next(SingleLiteralEnum.A)).isEqualTo(SingleLiteralEnum.A);
}
@Test
void testPrev() {
assertThat(EnumCycler.prev(TestEnum.A)).isEqualTo(TestEnum.C);
assertThat(EnumCycler.prev(TestEnum.B)).isEqualTo(TestEnum.A);
assertThat(EnumCycler.prev(TestEnum.C)).isEqualTo(TestEnum.B);
assertThat(EnumCycler.prev(SingleLiteralEnum.A)).isEqualTo(SingleLiteralEnum.A);
}
enum TestEnum {
A,
B,
C
}
enum RotateTestEnum {
A,
B,
C,
D,
E
}
enum SingleLiteralEnum {
A
}
}
@@ -1,61 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Tests for {@link UUIDMatcher}
*/
public final class UUIDMatcherTest
{
private static final String IS_UUID = "03ba29a1-d6bd-32ba-90b2-375e4d65abc9";
private static final String NO_UUID = "no";
private static final String INVALID_UUID = "g3ba29a1-d6bd-32ba-90b2-375e4d65abc9";
private final UUIDMatcher matcher;
public UUIDMatcherTest()
{
this.matcher = new UUIDMatcher();
}
@Test
public void testUUID_shouldPass()
{
assertTrue( this.matcher.isUUID( IS_UUID ) );
}
@Test
public void testNoUUD_shouldPass()
{
assertFalse( this.matcher.isUUID( NO_UUID ) );
}
@Test
public void testInvalidUUID_shouldPass()
{
assertFalse( this.matcher.isUUID( INVALID_UUID ) );
}
}
@@ -21,7 +21,7 @@ package appeng.util;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
@@ -22,7 +22,7 @@ package appeng.util.helpers;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import appeng.api.util.AEColor;
@@ -0,0 +1,78 @@
package appeng.util.item;
import java.util.IdentityHashMap;
import java.util.Map;
import com.google.common.testing.EqualsTester;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.registry.Bootstrap;
import net.minecraft.util.text.StringTextComponent;
class AESharedItemStackTest {
@BeforeAll
static void bootstrap() {
Bootstrap.register();
}
// Test stack -> Name for debugging the tests
final Map<AESharedItemStack, String> stacks = new IdentityHashMap<>();
AESharedItemStackTest() {
TestItemWithCaps TEST_ITEM = new TestItemWithCaps();
ItemStack nameTag1 = new ItemStack(TEST_ITEM);
stacks.put(new AESharedItemStack(nameTag1), "no-nbt");
// NBT
ItemStack nameTag2 = new ItemStack(TEST_ITEM);
nameTag2.setDisplayName(new StringTextComponent("Hello World"));
stacks.put(new AESharedItemStack(nameTag2), "nbt1");
// Different NBT
ItemStack nameTag3 = new ItemStack(TEST_ITEM);
nameTag3.setDisplayName(new StringTextComponent("ABCDEFGH"));
stacks.put(new AESharedItemStack(nameTag3), "nbt2");
// NBT + Cap
CompoundNBT capNbt = new CompoundNBT();
capNbt.putInt("Parent", 1);
ItemStack nameTag4 = new ItemStack(TEST_ITEM, 1, capNbt);
nameTag4.setDisplayName(new StringTextComponent("Hello World"));
stacks.put(new AESharedItemStack(nameTag4), "nbt1+cap1");
// NBT + Different Cap
CompoundNBT capNbt2 = new CompoundNBT();
capNbt2.putInt("Parent", 123);
ItemStack nameTag5 = new ItemStack(TEST_ITEM, 1, capNbt2);
nameTag5.setDisplayName(new StringTextComponent("Hello World"));
stacks.put(new AESharedItemStack(nameTag5), "nbt1+cap2");
}
/**
* Tests equality between shared item stacks.
*/
@Test
void testEquals() {
EqualsTester tester = new EqualsTester();
for (AESharedItemStack stack : stacks.keySet()) {
// Add the stack, and a pristine copy of the stack
tester.addEqualityGroup(stack, new AESharedItemStack(stack.getDefinition().copy()));
}
// Test that using the same item stack instance makes two separate shared stacks equal
ItemStack itemStack = new ItemStack(Items.CRAFTING_TABLE);
tester.addEqualityGroup(
new AESharedItemStack(itemStack),
new AESharedItemStack(itemStack));
tester.testEquals();
}
}
@@ -0,0 +1,157 @@
package appeng.util.item;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import appeng.api.config.FuzzyMode;
public class FuzzyItemVariantListTest {
@Test
void testOrderForDamagedItems() {
// Diamond Sword @ 100% durability
ItemStack undamagedSword = new ItemStack(Items.DIAMOND_SWORD);
AESharedItemStack undamagedStack = new AESharedItemStack(undamagedSword);
// Unenchanted Diamond Sword @ 0% durability
ItemStack damagedSword = new ItemStack(Items.DIAMOND_SWORD);
damagedSword.setDamage(damagedSword.getMaxDamage());
AESharedItemStack damagedStack = new AESharedItemStack(damagedSword);
// Create a list of stacks and sort by their natural order
AESharedItemStack[] stacks = new AESharedItemStack[] {
damagedStack, undamagedStack
};
Arrays.sort(stacks, FuzzyItemVariantList.COMPARATOR);
assertThat(stacks).containsExactly(damagedStack, undamagedStack);
}
@Nested
class Bounds {
final ItemStack stack = new ItemStack(Items.DIAMOND_SWORD);
final ItemStack damagedStack;
{
damagedStack = stack.copy();
damagedStack.setDamage(damagedStack.getMaxDamage());
}
@Test
void testIgnoreAll() {
DamageBounds bounds = new DamageBounds(stack, FuzzyMode.IGNORE_ALL);
assertEquals(stack.getMaxDamage(), bounds.lower.itemDamage);
assertEquals(-1, bounds.upper.itemDamage);
}
/**
* PERCENT_99 with an undamaged item should select only undamaged items, which translates to a damage range of
* [0, -1).
*/
@Test
void test99PercentDurabilityWithUndamagedItem() {
DamageBounds bounds = new DamageBounds(stack, FuzzyMode.PERCENT_99);
assertEquals(0, bounds.lower.itemDamage);
assertEquals(-1, bounds.upper.itemDamage);
}
/**
* PERCENT_99 with a damaged item should select only damaged items, which translates to a damage range of
* [maxDmg, 0).
*/
@Test
void test99PercentDurabilityWithDamagedItem() {
DamageBounds bounds = new DamageBounds(damagedStack, FuzzyMode.PERCENT_99);
assertEquals(stack.getMaxDamage(), bounds.lower.itemDamage);
assertEquals(0, bounds.upper.itemDamage);
}
/**
* PERCENT_75 with an undamaged item should select items that have 75% or more durability, which should
* translate to a damage range of [0.25*maxDmg, -1).
*/
@Test
void test75PercentWithUndamagedItem() {
DamageBounds bounds = new DamageBounds(stack, FuzzyMode.PERCENT_75);
assertEquals((int) (0.25 * stack.getMaxDamage()), bounds.lower.itemDamage);
assertEquals(-1, bounds.upper.itemDamage);
}
/**
* PERCENT_75 with a damaged item should select items that have less than 75% durability, which should translate
* to a damage range of [maxDmg, 0.25*maxDmg).
*/
@Test
void test75PercentWithDamagedItem() {
DamageBounds bounds = new DamageBounds(damagedStack, FuzzyMode.PERCENT_75);
assertEquals(stack.getMaxDamage(), bounds.lower.itemDamage);
assertEquals((int) (0.25 * stack.getMaxDamage()), bounds.upper.itemDamage);
}
/**
* PERCENT_50 with an undamaged item should select items that have 50% or more durability, which should
* translate to a damage range of [0.50*maxDmg, -1).
*/
@Test
void test50PercentWithUndamagedItem() {
DamageBounds bounds = new DamageBounds(stack, FuzzyMode.PERCENT_50);
assertEquals((int) (0.50 * stack.getMaxDamage()), bounds.lower.itemDamage);
assertEquals(-1, bounds.upper.itemDamage);
}
/**
* PERCENT_50 with a damaged item should select items that have less than 50% durability, which should translate
* to a damage range of [maxDmg, 0.50*maxDmg).
*/
@Test
void test50PercentWithDamagedItem() {
DamageBounds bounds = new DamageBounds(damagedStack, FuzzyMode.PERCENT_50);
assertEquals(stack.getMaxDamage(), bounds.lower.itemDamage);
assertEquals((int) (0.50 * stack.getMaxDamage()), bounds.upper.itemDamage);
}
/**
* PERCENT_25 with an undamaged item should select items that have 25% or more durability, which should
* translate to a damage range of [0.75*maxDmg, -1).
*/
@Test
void test25PercentWithUndamagedItem() {
DamageBounds bounds = new DamageBounds(stack, FuzzyMode.PERCENT_25);
assertEquals((int) (0.75 * stack.getMaxDamage()), bounds.lower.itemDamage);
assertEquals(-1, bounds.upper.itemDamage);
}
/**
* PERCENT_25 with a damaged item should select items that have less than 25% durability, which should translate
* to a damage range of [maxDmg, 0.75*maxDmg).
*/
@Test
void test25PercentWithDamagedItem() {
DamageBounds bounds = new DamageBounds(damagedStack, FuzzyMode.PERCENT_25);
assertEquals(stack.getMaxDamage(), bounds.lower.itemDamage);
assertEquals((int) (0.75 * stack.getMaxDamage()), bounds.upper.itemDamage);
}
}
private static class DamageBounds {
final FuzzyItemVariantList.ItemDamageBound lower;
final FuzzyItemVariantList.ItemDamageBound upper;
public DamageBounds(ItemStack stack, FuzzyMode mode) {
lower = FuzzyItemVariantList.makeLowerBound(stack, mode);
upper = FuzzyItemVariantList.makeUpperBound(stack, mode);
// This may be counter intuitive, but the map is sorted in descending order of item damage
assertThat(lower.itemDamage).isGreaterThan(upper.itemDamage);
}
}
}
@@ -0,0 +1,452 @@
package appeng.util.item;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.text.StringTextComponent;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEItemStack;
public class ItemListTest {
ItemList itemList = new ItemList();
/**
* add should merge item stacks by adding stored/requestable counts, and setting craftable if it wasn't set before.
*/
@Test
public void testAddMergesAllStackProperties() {
itemList.add(diamondSword(100, 1, 0, false));
assertPreciseStackProperties(diamondSwordFilter(100), 1, 0, false);
itemList.add(diamondSword(100, 0, 1, false));
assertPreciseStackProperties(diamondSwordFilter(100), 1, 1, false);
itemList.add(diamondSword(100, 0, 0, true));
assertPreciseStackProperties(diamondSwordFilter(100), 1, 1, true);
}
/**
* addStorage only considers {@link IAEItemStack#getStackSize()} and ignores other properties when merging stacks,
* but inherits all properties when it's adding a new item.
*/
@Test
public void testAddStorageForNewItem() {
// TODO: This might actually be incorrect, given how addrequestable et al behave
itemList.addStorage(diamondSword(100, 1, 1, true));
assertPreciseStackProperties(diamondSwordFilter(100), 1, 1, true);
}
@Test
public void testAddStorageForExistingItem() {
itemList.addStorage(diamondSword(100, 1, 0, false));
itemList.addStorage(diamondSword(100, 1, 2, true));
assertPreciseStackProperties(diamondSwordFilter(100), 2, 0, false);
}
/**
* addRequestable only considers {@link IAEItemStack#getCountRequestable()} and sets the stored amount to 0 and
* craftable to false when adding an item.
*/
@Test
public void testAddRequestableForNewItem() {
itemList.addRequestable(diamondSword(100, 1, 2, true));
assertPreciseStackProperties(diamondSwordFilter(100), 0, 2, false);
}
/**
* addRequestable only considers {@link IAEItemStack#getCountRequestable()} when merging into an existing item.
*/
@Test
public void testAddRequestableForExistingItem() {
itemList.addRequestable(diamondSword(100, 0, 1, false));
itemList.addRequestable(diamondSword(100, 2, 1, true));
assertPreciseStackProperties(diamondSwordFilter(100), 0, 2, false);
}
/**
* addCraftable only considers {@link IAEItemStack#isCraftable()} and sets the stored and requestable amounts to 0
* when adding an item.
*/
@Test
public void testAddCraftingForNewItem() {
itemList.addCrafting(diamondSword(100, 1, 2, true));
// TODO: I think it is unintended that the requestable amount is used
assertPreciseStackProperties(diamondSwordFilter(100), 0, 2, true);
}
/**
* addRequestable only considers {@link IAEItemStack#getCountRequestable()} when merging into an existing item.
*/
@Test
public void testAddCraftingForExistingItem() {
itemList.addCrafting(diamondSword(100, 0, 0, false));
itemList.addCrafting(diamondSword(100, 1, 2, true));
assertPreciseStackProperties(diamondSwordFilter(100), 0, 0, true);
}
/**
* an empty craftable stack still creates an entry in the list
*/
@Test
public void testAddEmptyStackThatIsCraftable() {
itemList.add(diamondSword(100, 0, 0, true));
assertPreciseStackProperties(diamondSwordFilter(100), 0, 0, true);
}
/**
* check that craftable isn't accidentally reset to false when merging stacks
*/
@Test
public void testAddDoesNotResetCraftableBackToFalse() {
itemList.add(diamondSword(100, 0, 0, true));
itemList.add(diamondSword(100, 1, 0, false));
assertPreciseStackProperties(diamondSwordFilter(100), 1, 0, true);
}
/**
* stacks for the same item, but different damage values should not be merged
*/
@Test
public void testAddDoesNotMergeAcrossDamageValues() {
itemList.add(diamondSword(100, 1, 0, false));
itemList.add(diamondSword(99, 1, 0, false));
assertPreciseStackProperties(diamondSwordFilter(100), 1, 0, false);
assertPreciseStackProperties(diamondSwordFilter(99), 1, 0, false);
}
private void assertPreciseStackProperties(IAEItemStack stack, long stored, long requestable, boolean craftable) {
IAEItemStack storedStack = itemList.findPrecise(stack);
assertEquals(stored, storedStack.getStackSize(), "stored amount");
assertEquals(requestable, storedStack.getCountRequestable(), "requestable amount");
assertEquals(craftable, storedStack.isCraftable(), "craftable");
}
/**
* Even if the stack has no stored or requestable amounts, it should be returned by the item list if it is
* craftable.
*/
@Test
public void testSizeAndIterateForEmptyButCraftableStack() {
itemList.add(diamondSword(100, 0, 0, true));
assertListContent(diamondSword(100, 0, 0, true));
}
/**
* If the stack is not craftable, an empty stack is actually ignored.
*/
@Test
public void testEmptyStackIsIgnored() {
itemList.add(diamondSword(100, 0, 0, false));
assertListContent();
}
@Test
public void testResetStatus() {
itemList.add(diamondSword(100, 1, 0, false));
itemList.add(nameTag(1, 0, false));
assertEquals(2, itemList.size());
itemList.resetStatus();
assertListContent(); // The list should now be empty
}
/**
* Tests that iteration across multiple items and variations of those items works.
*/
@Test
public void testIterateAcrossMultipleItems() {
// Add damaged variants of the same item, including NBT variants
AEItemStack sword1 = diamondSword(100, 1, 0, false);
itemList.add(sword1);
AEItemStack sword2 = diamondSword(50, 1, 0, false);
itemList.add(sword2);
AEItemStack sword3 = diamondSword(25, 1, 0, false);
itemList.add(sword3);
AEItemStack sword4 = diamondSword(100, "master sword", 1, 0, false);
itemList.add(sword4);
// And a non-damagable item with different NBT
AEItemStack nameTag1 = nameTag(1, 0, false);
itemList.add(nameTag1);
AEItemStack nameTag2 = nameTag("bob", 1, 0, false);
itemList.add(nameTag2);
assertListContent(sword1, sword2, sword3, sword4, nameTag1, nameTag2);
}
@Test
void testConcurrentModificationByAddingItemType() {
AEItemStack sword = diamondSword(100, 1, 0, false);
itemList.add(sword);
AEItemStack nameTag = nameTag(1, 0, false);
itemList.add(nameTag);
AEItemStack craftingTable = AEItemStack.fromItemStack(new ItemStack(Items.CRAFTING_TABLE));
assertThrows(ConcurrentModificationException.class, () -> {
Iterator<IAEItemStack> it = itemList.iterator();
itemList.add(craftingTable);
assertThat(Iterators.toArray(it, IAEItemStack.class)).containsOnly(sword, nameTag);
});
}
/**
* Regression test for broken iterators that mutated state in {@see Iterator#hasNext}. This was the case for both
* the top-level and sub-iterator.
*/
@Test
public void testIteratorHasNextDoesNotSkipItems() {
itemList.add(diamondSword(100, 1, 0, false));
itemList.add(diamondSword(50, 1, 0, false));
Iterator<IAEItemStack> it = itemList.iterator();
assertTrue(it.hasNext());
assertTrue(it.hasNext());
assertTrue(it.hasNext());
}
@Test
public void testGetFirstItemForEmptyList() {
assertNull(itemList.getFirstItem());
}
@Test
public void testGetFirstItem() {
AEItemStack itemStack = diamondSword(100, 1, 0, false);
itemList.add(itemStack);
// The order is no longer well defined w.r.t. the hashmap
assertEquals(itemStack, itemList.getFirstItem());
}
@Nested
class FindFuzzyDamageableItems {
// Swords to cover all durability values
AEItemStack swordAbove100 = diamondSword(101, 1, 0, false);
AEItemStack[] swords = new AEItemStack[101];
// Filters for inverting the filter as needed
AEItemStack undamagedFilter = diamondSwordFilter(100);
AEItemStack damagedFilter = diamondSwordFilter(0);
@BeforeEach
void addItems() {
itemList.add(swordAbove100);
for (int i = 0; i <= 100; i++) {
swords[i] = diamondSword(i, 1, 0, false);
assertEquals(i, getDurabilityPercent(swords[i]));
itemList.add(swords[i]);
}
}
@Test
public void testIgnoreAllWithUndamagedFilter() {
assertReturnedDurabilities(undamagedFilter, FuzzyMode.IGNORE_ALL, 0, 100, false);
}
@Test
public void testIgnoreAllWithDamagedFilter() {
assertReturnedDurabilities(damagedFilter, FuzzyMode.IGNORE_ALL, 0, 100, false);
}
@Test
public void testPercent99WithUndamagedFilter() {
assertReturnedDurabilities(undamagedFilter, FuzzyMode.PERCENT_99, 100, 100, false);
}
@Test
public void testPercent99WithDamagedFilter() {
assertReturnedDurabilities(damagedFilter, FuzzyMode.PERCENT_99, 0, 99, false);
}
@Test
public void testPercent75WithUndamagedFilter() {
assertReturnedDurabilities(undamagedFilter, FuzzyMode.PERCENT_75, 75, 100, false);
}
@Test
public void testPercent75WithDamagedFilter() {
assertReturnedDurabilities(damagedFilter, FuzzyMode.PERCENT_75, 0, 74, false);
}
@Test
public void testPercent50WithUndamagedFilter() {
assertReturnedDurabilities(undamagedFilter, FuzzyMode.PERCENT_50, 50, 100, false);
}
@Test
public void testPercent50WithDamagedFilter() {
assertReturnedDurabilities(damagedFilter, FuzzyMode.PERCENT_50, 0, 49, false);
}
@Test
public void testPercent25WithUndamagedFilter() {
assertReturnedDurabilities(undamagedFilter, FuzzyMode.PERCENT_25, 25, 100, false);
}
@Test
public void testPercent25WithDamagedFilter() {
assertReturnedDurabilities(damagedFilter, FuzzyMode.PERCENT_25, 0, 24, false);
}
private void assertReturnedDurabilities(IAEItemStack filter, FuzzyMode fuzzyMode, int minDurabilityInclusive,
int maxDurabilityInclusive, boolean above100) {
Collection<IAEItemStack> items = itemList.findFuzzy(filter, fuzzyMode);
// Build a list of the durabilities that got returned
List<Integer> durabilities = items.stream().map(this::getDurabilityPercent)
.sorted()
.collect(Collectors.toList());
// Build a sorted list of the durabilities we expect
List<Integer> expectedDurabilities = new ArrayList<>();
for (int i = minDurabilityInclusive; i <= maxDurabilityInclusive; i++) {
expectedDurabilities.add(getDurabilityPercent(swords[i]));
}
if (above100) {
expectedDurabilities.add(getDurabilityPercent(swordAbove100));
}
expectedDurabilities.sort(Integer::compare);
assertEquals(expectedDurabilities, durabilities);
}
private int getDurabilityPercent(IAEItemStack stack) {
if (stack == swordAbove100) {
return 101;
}
return (int) ((1.0f - (stack.getItemDamage() / (float) stack.getDefinition().getMaxDamage())) * 100);
}
}
@Test
void testFindFuzzyForNormalItems() {
AEItemStack item1 = nameTag(null, 1, 0, false);
itemList.add(item1);
AEItemStack item2 = nameTag("name1", 1, 0, false);
itemList.add(item2);
AEItemStack item3 = nameTag("name2", 1, 0, false);
itemList.add(item3);
// Add another item to ensure this is not returned
itemList.add(AEItemStack.fromItemStack(new ItemStack(Items.CRAFTING_TABLE)));
for (FuzzyMode fuzzyMode : FuzzyMode.values()) {
Collection<IAEItemStack> result = itemList.findFuzzy(nameTag(null, 0, 0, false), fuzzyMode);
assertThat(result).containsOnly(item1, item2, item3);
}
}
/**
* Tests how ItemList behaves w.r.t. null arguments, given that {@link AEItemStack#fromItemStack(ItemStack)} can
* return null for an empty stack, this sometimes leaks into method parameters. As such, methods should behave as if
* an empty item stack was passed.
*/
@Nested
class NullArguments {
@BeforeEach
void addItem() {
itemList.add(diamondSword(100, 1, 0, false));
}
@Test
void testFindFuzzy() {
assertThat(itemList.findFuzzy(null, FuzzyMode.PERCENT_99)).isEmpty();
}
@Test
void testFindPrecise() {
assertThat(itemList.findPrecise(null)).isNull();
}
@Test
void testAdd() {
assertThat(itemList.size()).isEqualTo(1);
itemList.add(null);
assertThat(itemList.size()).isEqualTo(1);
}
@Test
void testAddStorage() {
assertThat(itemList.size()).isEqualTo(1);
itemList.addStorage(null);
assertThat(itemList.size()).isEqualTo(1);
}
@Test
void testAddRequestable() {
assertThat(itemList.size()).isEqualTo(1);
itemList.addRequestable(null);
assertThat(itemList.size()).isEqualTo(1);
}
@Test
void testAddCrafting() {
assertThat(itemList.size()).isEqualTo(1);
itemList.addCrafting(null);
assertThat(itemList.size()).isEqualTo(1);
}
}
private void assertListContent(AEItemStack... stacks) {
assertEquals(stacks.length == 0, itemList.isEmpty(), "isEmpty");
assertEquals(stacks.length, itemList.size());
assertEquals(ImmutableSet.copyOf(stacks), ImmutableSet.copyOf(itemList));
}
private AEItemStack diamondSwordFilter(int durabilityPercent) {
return diamondSword(durabilityPercent, 0, 0, false);
}
private AEItemStack diamondSword(int durabilityPercent, long stored, long requestable, boolean craftable) {
return diamondSword(durabilityPercent, null, stored, requestable, craftable);
}
private AEItemStack diamondSword(int durabilityPercent, String customName, long stored, long requestable,
boolean craftable) {
ItemStack is = new ItemStack(Items.DIAMOND_SWORD);
if (customName != null) {
is.setDisplayName(new StringTextComponent(customName));
}
int damage = (int) ((100 - durabilityPercent) / 100.0f * is.getMaxDamage());
is.setDamage(damage);
AEItemStack ais = AEItemStack.fromItemStack(is);
ais.setStackSize(stored);
ais.setCountRequestable(requestable);
ais.setCraftable(craftable);
return ais;
}
// customName can be used to create items that differ in NBT
private AEItemStack nameTag(long stored, long requestable, boolean craftable) {
return nameTag(null, stored, requestable, craftable);
}
private AEItemStack nameTag(String customName, long stored, long requestable, boolean craftable) {
ItemStack is = new ItemStack(Items.NAME_TAG);
if (customName != null) {
is.setDisplayName(new StringTextComponent(customName));
}
AEItemStack ais = AEItemStack.fromItemStack(is);
ais.setStackSize(stored);
ais.setCountRequestable(requestable);
ais.setCraftable(craftable);
return ais;
}
}
@@ -0,0 +1,60 @@
package appeng.util.item;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.IntNBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;
public class TestItemWithCaps extends Item {
public TestItemWithCaps() {
super(new Properties());
setRegistryName("appliedenergistics2:test_item");
}
@Nullable
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
if (nbt == null) {
return null;
} else {
return new CapabilityProvider();
}
}
/**
* Simple capability provider that just has a single counter value to produce different NBT.
*/
public static class CapabilityProvider implements ICapabilityProvider, INBTSerializable<IntNBT> {
private int counter;
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
return LazyOptional.empty();
}
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap) {
return LazyOptional.empty();
}
@Override
public IntNBT serializeNBT() {
return IntNBT.valueOf(counter);
}
@Override
public void deserializeNBT(IntNBT nbt) {
counter = nbt.getInt();
}
}
}