diff options
Diffstat (limited to 'src/functionalTest/java')
6 files changed, 879 insertions, 0 deletions
diff --git a/src/functionalTest/java/gregtech/test/GT5TestMod.java b/src/functionalTest/java/gregtech/test/GT5TestMod.java new file mode 100644 index 0000000000..88029f641d --- /dev/null +++ b/src/functionalTest/java/gregtech/test/GT5TestMod.java @@ -0,0 +1,86 @@ +package gregtech.test; + +import java.io.File; +import java.io.PrintWriter; +import java.nio.file.FileSystems; +import java.nio.file.Path; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.ChatComponentText; + +import org.apache.commons.io.output.CloseShieldOutputStream; +import org.junit.platform.engine.discovery.DiscoverySelectors; +import org.junit.platform.launcher.Launcher; +import org.junit.platform.launcher.LauncherDiscoveryRequest; +import org.junit.platform.launcher.LauncherSession; +import org.junit.platform.launcher.TestPlan; +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; +import org.junit.platform.launcher.core.LauncherFactory; +import org.junit.platform.launcher.listeners.SummaryGeneratingListener; +import org.junit.platform.launcher.listeners.TestExecutionSummary; +import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.Mod; +import cpw.mods.fml.common.event.FMLServerStartedEvent; + +@Mod(modid = "gt5-tests", name = "GT5 Dev Tests", version = "1.0", dependencies = "required-after:gregtech") +public class GT5TestMod { + + @Mod.EventHandler + public void onServerStarted(FMLServerStartedEvent event) { + MinecraftServer.getServer() + .addChatMessage(new ChatComponentText("Running GT5 unit tests...")); + runTests(); + MinecraftServer.getServer() + .addChatMessage(new ChatComponentText("Running GT5 unit tests finished")); + } + + private void runTests() { + // https://junit.org/junit5/docs/current/user-guide/#launcher-api + System.setProperty("junit.platform.reporting.open.xml.enabled", "false"); + final Path testsXmlOutDir = FileSystems.getDefault() + .getPath("./junit-out/") + .toAbsolutePath(); + final File testsXmlOutDirFile = testsXmlOutDir.toFile(); + testsXmlOutDirFile.mkdirs(); + { + File[] fileList = testsXmlOutDirFile.listFiles(); + if (fileList != null) { + for (File child : fileList) { + if (child.isFile() && child.getName() + .endsWith(".xml")) { + child.delete(); + } + } + } + } + final LauncherDiscoveryRequest discovery = LauncherDiscoveryRequestBuilder.request() + .selectors(DiscoverySelectors.selectPackage("gregtech.test")) + .build(); + final SummaryGeneratingListener summaryGenerator = new SummaryGeneratingListener(); + final TestExecutionSummary summary; + try (PrintWriter stderrWriter = new PrintWriter(new CloseShieldOutputStream(System.err), true)) { + final LegacyXmlReportGeneratingListener xmlGenerator = new LegacyXmlReportGeneratingListener( + testsXmlOutDir, + stderrWriter); + try (LauncherSession session = LauncherFactory.openSession()) { + final Launcher launcher = session.getLauncher(); + final TestPlan plan = launcher.discover(discovery); + launcher.registerTestExecutionListeners(summaryGenerator, xmlGenerator); + launcher.execute(plan); + } + summary = summaryGenerator.getSummary(); + + summary.printFailuresTo(stderrWriter, 32); + summary.printTo(stderrWriter); + stderrWriter.flush(); + } + // Throw an exception if running via `runServer` + if (summary.getTotalFailureCount() > 0 && FMLCommonHandler.instance() + .getSide() + .isServer()) { + throw new RuntimeException("Some of the unit tests failed to execute, check the log for details"); + } + } +} diff --git a/src/functionalTest/java/gregtech/test/GTParallelHelperTest.java b/src/functionalTest/java/gregtech/test/GTParallelHelperTest.java new file mode 100644 index 0000000000..67abe8a5bd --- /dev/null +++ b/src/functionalTest/java/gregtech/test/GTParallelHelperTest.java @@ -0,0 +1,122 @@ +package gregtech.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import gregtech.api.enums.Materials; +import gregtech.api.enums.TierEU; +import gregtech.api.util.GT_ParallelHelper; +import gregtech.api.util.GT_Recipe; +import gregtech.api.util.GT_Utility; +import gregtech.test.mock.MockIVoidableMachine; + +public class GTParallelHelperTest { + + static GT_Recipe rubberRecipe; + static ItemStack[] inputItems; + static MockIVoidableMachine machine; + + @BeforeAll + static void setup() { + machine = new MockIVoidableMachine(); + ItemStack rubberDust = Materials.RawRubber.getDust(1); + ItemStack sulfurDust = Materials.Sulfur.getDust(1); + rubberRecipe = new GT_Recipe( + new ItemStack[] { rubberDust.copy(), sulfurDust.copy() }, + new ItemStack[] { Materials.Rubber.getDust(1), Materials.Rubber.getDustTiny(1) }, + null, + new int[] { 10000, 6667 }, + null, + new FluidStack[] { Materials.Rubber.getMolten(1000) }, + 1, + 1, + 0); + + inputItems = new ItemStack[] { GT_Utility.copyAmountUnsafe(Integer.MAX_VALUE, rubberDust), + GT_Utility.copyAmountUnsafe(Integer.MAX_VALUE, rubberDust), + GT_Utility.copyAmountUnsafe(Integer.MAX_VALUE, sulfurDust), + GT_Utility.copyAmountUnsafe(Integer.MAX_VALUE, sulfurDust) }; + } + + @Test + void OutputsIntegerOverflow() { + GT_ParallelHelper helper = new GT_ParallelHelper().setRecipe(rubberRecipe) + .setMachine(machine, false, false) + .setItemInputs(inputItems) + .setMaxParallel(4_000_000) + .setAvailableEUt(4_000_000) + .setOutputCalculation(true) + .setConsumption(false); + helper.build(); + FluidStack[] fluidStacks = helper.getFluidOutputs(); + + assertEquals(2, fluidStacks.length); + assertEquals(Integer.MAX_VALUE, fluidStacks[0].amount); + assertEquals(4_000_000L * 1000 - Integer.MAX_VALUE, fluidStacks[1].amount); + } + + @Test + void parallelIntegerOverflow() { + // Without batch mode + GT_ParallelHelper helperWithoutBatchMode = new GT_ParallelHelper().setRecipe(rubberRecipe) + .setMachine(machine, false, false) + .setItemInputs(inputItems) + .setMaxParallel(Integer.MAX_VALUE) + .setAvailableEUt(TierEU.MAX * 16) + .setConsumption(false) + .build(); + assertEquals(Integer.MAX_VALUE, helperWithoutBatchMode.getCurrentParallel()); + + // With batch mode + GT_ParallelHelper helperWithBatchMode = new GT_ParallelHelper().setRecipe(rubberRecipe) + .setMachine(machine, false, false) + .setItemInputs(inputItems) + .setMaxParallel(Integer.MAX_VALUE / 50) + .setAvailableEUt(TierEU.MAX * 16) + .enableBatchMode(128) + .setConsumption(false) + .build(); + assertEquals(Integer.MAX_VALUE, helperWithBatchMode.getCurrentParallel()); + } + + @Test + void chanceMultiplier() { + GT_ParallelHelper helper = new GT_ParallelHelper().setRecipe(rubberRecipe) + .setMachine(machine, false, false) + .setItemInputs(inputItems) + .setMaxParallel(10) + .setAvailableEUt(10) + .setConsumption(false) + .setOutputCalculation(true) + .setChanceMultiplier(10) + .build(); + + int rubberDustAmount = helper.getItemOutputs()[0].stackSize; + int rubberDustTinyAmount = helper.getItemOutputs()[1].stackSize; + + assertEquals(100, rubberDustAmount); + assertTrue(rubberDustTinyAmount >= 60 && rubberDustTinyAmount <= 70); + } + + @Test + void outputMultiplier() { + GT_ParallelHelper helper = new GT_ParallelHelper().setRecipe(rubberRecipe) + .setMachine(machine, false, false) + .setItemInputs(inputItems) + .setMaxParallel(1) + .setAvailableEUt(1) + .setConsumption(false) + .setOutputCalculation(true) + .setOutputMultiplier(2) + .build(); + + assertEquals(2000, helper.getFluidOutputs()[0].amount); + assertEquals(2, helper.getItemOutputs()[0].stackSize); + } +} diff --git a/src/functionalTest/java/gregtech/test/GTRecipeTest.java b/src/functionalTest/java/gregtech/test/GTRecipeTest.java new file mode 100644 index 0000000000..104228992e --- /dev/null +++ b/src/functionalTest/java/gregtech/test/GTRecipeTest.java @@ -0,0 +1,238 @@ +package gregtech.test; + +import static gregtech.api.GregTech_API.sBlockOres1; +import static gregtech.api.enums.GT_Values.RA; +import static gregtech.api.enums.ItemList.Circuit_Parts_Crystal_Chip_Master; +import static gregtech.api.enums.ItemList.IC2_LapotronCrystal; +import static gregtech.api.enums.OrePrefixes.circuit; +import static gregtech.api.enums.OrePrefixes.lens; +import static gregtech.api.util.GT_OreDictUnificator.get; +import static gregtech.api.util.GT_Utility.copyAmount; +import static net.minecraft.init.Blocks.chest; +import static net.minecraft.init.Blocks.iron_ore; +import static net.minecraft.init.Blocks.lapis_block; +import static net.minecraft.init.Blocks.log; +import static net.minecraft.init.Blocks.planks; +import static net.minecraft.init.Blocks.stone; +import static net.minecraft.init.Blocks.stone_slab; +import static net.minecraft.init.Items.glass_bottle; +import static net.minecraft.init.Items.iron_ingot; +import static net.minecraftforge.oredict.OreDictionary.WILDCARD_VALUE; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import gregtech.api.enums.ItemList; +import gregtech.api.enums.Materials; +import gregtech.api.recipe.RecipeMap; +import gregtech.api.recipe.RecipeMapBuilder; +import gregtech.api.util.GT_Recipe; + +class GTRecipeTest { + + static RecipeMap<?> recipeMap; + static GT_Recipe lapotronChipRecipe; + + @BeforeAll + static void setup() { + recipeMap = RecipeMapBuilder.of("__test__") + .maxIO(9, 1, 1, 0) + .build(); + + RA.stdBuilder() + .itemInputs(new ItemStack(log, 2, WILDCARD_VALUE), new ItemStack(planks, 2, WILDCARD_VALUE)) + .itemOutputs(new ItemStack(chest, 1)) + .duration(0) + .eut(0) + .addTo(recipeMap); + + RA.stdBuilder() + .itemInputs(new ItemStack(lapis_block, 1), get(circuit, Materials.HV, 1)) + .itemOutputs(IC2_LapotronCrystal.get(1)) + .duration(0) + .eut(0) + .addTo(recipeMap); + + lapotronChipRecipe = RA.stdBuilder() + .itemInputs(IC2_LapotronCrystal.getWildcard(1), copyAmount(0, get(lens, Materials.BlueTopaz, 1))) + .itemOutputs(Circuit_Parts_Crystal_Chip_Master.get(3)) + .duration(0) + .eut(0) + .addTo(recipeMap) + .toArray(new GT_Recipe[0])[0]; + + RA.stdBuilder() + .itemInputs(new ItemStack(sBlockOres1, 1, 32)) + .itemOutputs(new ItemStack(iron_ingot, 1)) + .duration(0) + .eut(0) + .addTo(recipeMap); + + RA.stdBuilder() + .itemInputs(new ItemStack(stone_slab, 64), new ItemStack(stone_slab, 64)) + .itemOutputs(new ItemStack(stone, 2)) + .duration(0) + .eut(0) + .addTo(recipeMap); + + ItemStack dataStick = ItemList.Tool_DataStick.get(0); + NBTTagCompound dataStickTag = new NBTTagCompound(); + dataStickTag.setInteger("integer", 123456); + dataStick.setTagCompound(dataStickTag); + RA.stdBuilder() + .itemInputs(dataStick) + .itemOutputs(new ItemStack(chest, 1)) + .duration(0) + .eut(0) + .addTo(recipeMap); + + ItemStack glass = new ItemStack(glass_bottle, 2); + NBTTagCompound glassTag = new NBTTagCompound(); + glassTag.setInteger("integer", 123456); + glass.setTagCompound(glassTag); + RA.stdBuilder() + .itemInputs(glass) + .itemOutputs(new ItemStack(chest, 1)) + .duration(0) + .eut(0) + .nbtSensitive() + .addTo(recipeMap); + } + + @Test + void ensureRecipesAdded() { + assertEquals( + recipeMap.getAllRecipes() + .size(), + 7); + } + + @Test + void findWithExactSameInputs() { + GT_Recipe recipe = recipeMap.findRecipeQuery() + .items(new ItemStack(lapis_block, 1), get(circuit, Materials.HV, 1)) + .find(); + assertNotNull(recipe); + + GT_Recipe stoneRecipe = recipeMap.findRecipeQuery() + .items(new ItemStack(stone_slab, 128)) + .find(); + assertNotNull(stoneRecipe); + } + + @Test + void findWildcardWithExactSameInputs() { + GT_Recipe chestRecipe = recipeMap.findRecipeQuery() + .items(new ItemStack(log, 2, WILDCARD_VALUE), new ItemStack(planks, 2, WILDCARD_VALUE)) + .find(); + assertNotNull(chestRecipe); + + GT_Recipe lapotronChipRecipe = recipeMap.findRecipeQuery() + .items(IC2_LapotronCrystal.getWildcard(1), copyAmount(0, get(lens, Materials.BlueTopaz, 1))) + .find(); + assertNotNull(lapotronChipRecipe); + } + + @Test + void findWildcardWithDifferentMeta() { + // https://github.com/GTNewHorizons/GT5-Unofficial/pull/2364/commits/e7112fce5f24431f3a4ad19288d662b93cbb91f2 + GT_Recipe recipe = recipeMap.findRecipeQuery() + .items(new ItemStack(log, 2, 0), new ItemStack(planks, 2, 1)) + .find(); + assertNotNull(recipe); + } + + @Test + void findWithNBT() { + // https://github.com/GTNewHorizons/GT5-Unofficial/pull/2364/commits/844a38662b05494b42a4439bbc0e6d4d7df1a683 + ItemStack lapisBlock = new ItemStack(lapis_block, 1); + NBTTagCompound tag = new NBTTagCompound(); + tag.setFloat("charge", 123456); + lapisBlock.stackTagCompound = tag; + GT_Recipe recipe = recipeMap.findRecipeQuery() + .items(lapisBlock, get(circuit, Materials.HV, 1)) + .find(); + assertNotNull(recipe); + + // For NBT sensitive recipes + ItemStack glass = new ItemStack(glass_bottle, 2); + NBTTagCompound glassTag = new NBTTagCompound(); + glassTag.setInteger("integer", 123456); + glass.setTagCompound(glassTag); + GT_Recipe nbtSensitiveRecipe = recipeMap.findRecipeQuery() + .items(glass) + .find(); + assertNotNull(nbtSensitiveRecipe); + + // For items that need to check NBT, e.g. data sticks + ItemStack dataStick = ItemList.Tool_DataStick.get(0); + NBTTagCompound dataStickTag = new NBTTagCompound(); + dataStickTag.setInteger("integer", 123456); + dataStick.setTagCompound(dataStickTag); + GT_Recipe checkNBTRecipe = recipeMap.findRecipeQuery() + .items(dataStick) + .find(); + assertNotNull(checkNBTRecipe); + } + + @Test + void rejectWithInsufficientAmount() { + GT_Recipe recipe = recipeMap.findRecipeQuery() + .items(new ItemStack(log, 1, 0), new ItemStack(planks, 1, 0)) + .find(); + assertNull(recipe); + + GT_Recipe stoneRecipe = recipeMap.findRecipeQuery() + .items(new ItemStack(stone_slab, 127)) + .find(); + assertNull(stoneRecipe); + } + + @Test + void rejectWithoutNonConsumable() { + // https://github.com/GTNewHorizons/GT5-Unofficial/pull/2364/commits/bfc93bff7ed34616021e8c5b6dbdc50dd7096af5 + GT_Recipe recipe = recipeMap.findRecipeQuery() + .items(IC2_LapotronCrystal.get(1)) + .cachedRecipe(lapotronChipRecipe) + .find(); + assertNull(recipe); + } + + @Test + void rejectWithoutCorrectNBT() { + // For NBT sensitive recipes + GT_Recipe nbtSensitiveRecipe = recipeMap.findRecipeQuery() + .items(new ItemStack(glass_bottle, 2)) + .find(); + assertNull(nbtSensitiveRecipe); + + // For items that need to check NBT, e.g. data sticks + GT_Recipe checkNBTRecipe = recipeMap.findRecipeQuery() + .items(ItemList.Tool_DataStick.get(0)) + .find(); + assertNull(checkNBTRecipe); + } + + @Test + void findWithSpecificOreDictionary() { + // https://github.com/GTNewHorizons/GT5-Unofficial/pull/2379 + // We cannot use circuit assembling recipe like the issue mentioned above, + // as mUnificationTarget is not set for circuits in GT5. + // But it works in the same way; specific circuit -> GT ore block, unificated circuit -> vanilla ore block + GT_Recipe recipeCorrectOre = recipeMap.findRecipeQuery() + .items(new ItemStack(sBlockOres1, 1, 32)) + .find(); + assertNotNull(recipeCorrectOre); + + GT_Recipe recipeWrongOre = recipeMap.findRecipeQuery() + .items(new ItemStack(iron_ore, 1)) + .find(); + assertNull(recipeWrongOre); + } +} diff --git a/src/functionalTest/java/gregtech/test/mock/MockIVoidableMachine.java b/src/functionalTest/java/gregtech/test/mock/MockIVoidableMachine.java new file mode 100644 index 0000000000..4796d1d3ee --- /dev/null +++ b/src/functionalTest/java/gregtech/test/mock/MockIVoidableMachine.java @@ -0,0 +1,55 @@ +package gregtech.test.mock; + +import java.util.List; + +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; + +import gregtech.api.enums.VoidingMode; +import gregtech.api.interfaces.fluid.IFluidStore; +import gregtech.api.interfaces.tileentity.IVoidable; + +public class MockIVoidableMachine implements IVoidable { + + protected VoidingMode voidingMode = getDefaultVoidingMode(); + + @Override + public boolean supportsVoidProtection() { + return true; + } + + @Override + public VoidingMode getVoidingMode() { + return voidingMode; + } + + @Override + public void setVoidingMode(VoidingMode mode) { + voidingMode = mode; + } + + @Override + public List<ItemStack> getItemOutputSlots(ItemStack[] toOutput) { + return null; + } + + @Override + public List<? extends IFluidStore> getFluidOutputSlots(FluidStack[] toOutput) { + return null; + } + + @Override + public boolean canDumpItemToME() { + return false; + } + + @Override + public boolean canDumpFluidToME() { + return false; + } + + @Override + public VoidingMode getDefaultVoidingMode() { + return VoidingMode.VOID_ALL; + } +} diff --git a/src/functionalTest/java/kubatech/test/EIGTests.java b/src/functionalTest/java/kubatech/test/EIGTests.java new file mode 100644 index 0000000000..7cdfa8dae4 --- /dev/null +++ b/src/functionalTest/java/kubatech/test/EIGTests.java @@ -0,0 +1,277 @@ +/* + * spotless:off + * KubaTech - Gregtech Addon + * Copyright (C) 2022 - 2024 kuba6000 + * + * This library 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 library 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 library. If not, see <https://www.gnu.org/licenses/>. + * spotless:on + */ + +package kubatech.test; + +import static gregtech.api.util.GT_RecipeBuilder.HOURS; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.util.Map; + +import net.minecraft.block.Block; +import net.minecraft.init.Blocks; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.MinecraftException; +import net.minecraft.world.World; +import net.minecraft.world.WorldProvider; +import net.minecraft.world.WorldProviderSurface; +import net.minecraft.world.WorldServer; +import net.minecraft.world.WorldSettings; +import net.minecraft.world.WorldType; +import net.minecraft.world.biome.BiomeGenBase; +import net.minecraft.world.chunk.storage.IChunkLoader; +import net.minecraft.world.storage.IPlayerFileData; +import net.minecraft.world.storage.ISaveHandler; +import net.minecraft.world.storage.WorldInfo; +import net.minecraftforge.common.DimensionManager; + +import org.junit.jupiter.api.Test; + +import gregtech.api.GregTech_API; +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import gregtech.common.blocks.GT_Item_Machines; +import ic2.api.crops.CropCard; +import ic2.api.crops.Crops; +import ic2.core.Ic2Items; +import ic2.core.crop.TileEntityCrop; +import ic2.core.item.ItemCropSeed; +import kubatech.api.eig.EIGDropTable; +import kubatech.tileentity.gregtech.multiblock.GT_MetaTileEntity_ExtremeIndustrialGreenhouse; +import kubatech.tileentity.gregtech.multiblock.eigbuckets.EIGIC2Bucket; + +public class EIGTests { + + private static final int EIG_CONTROLLER_METADATA = 12_792; + private static final int EIG_SIMULATION_TIME = 24 * HOURS; + private static final int NUMBER_OF_TESTS_TO_DO = 1000; + + static World myWorld; + + public EIGTests() { + if (!DimensionManager.isDimensionRegistered(256)) { + DimensionManager.registerProviderType(256, WorldProviderSurface.class, false); + DimensionManager.registerDimension(256, 256); + } + if (myWorld == null) { + myWorld = new WorldServer(MinecraftServer.getServer(), new ISaveHandler() { + + @Override + public WorldInfo loadWorldInfo() { + return null; + } + + @Override + public void checkSessionLock() throws MinecraftException { + + } + + @Override + public IChunkLoader getChunkLoader(WorldProvider p_75763_1_) { + return null; + } + + @Override + public void saveWorldInfoWithPlayer(WorldInfo p_75755_1_, NBTTagCompound p_75755_2_) { + + } + + @Override + public void saveWorldInfo(WorldInfo p_75761_1_) { + + } + + @Override + public IPlayerFileData getSaveHandler() { + return null; + } + + @Override + public void flush() { + + } + + @Override + public File getWorldDirectory() { + return null; + } + + @Override + public File getMapFileFromName(String p_75758_1_) { + return null; + } + + @Override + public String getWorldDirectoryName() { + return "dummy"; + } + }, + "DummyTestWorld", + 256, + new WorldSettings(256, WorldSettings.GameType.SURVIVAL, false, false, WorldType.DEFAULT), + MinecraftServer.getServer().theProfiler) { + + @Override + public File getChunkSaveLocation() { + return new File("ignoreme"); + } + + @Override + public int getBlockLightValue(int p_72957_1_, int p_72957_2_, int p_72957_3_) { + return 4; + } + + @Override + public BiomeGenBase getBiomeGenForCoords(int x, int z) { + // give the environment a fighting chance of being bearable for crops + return BiomeGenBase.jungle; + } + }; + } + } + + EIGDropTable getRealDrops(TileEntityCrop cropTile, CropCard cc, int growth, int gain, int resistance) { + cropTile.setCrop(cc); + cropTile.setGrowth((byte) growth); + cropTile.setGain((byte) gain); + cropTile.setResistance((byte) resistance); + EIGDropTable expected = new EIGDropTable(); + byte startingHumidity = cropTile.humidity; + byte startingNutrients = cropTile.nutrients; + byte startingAirQuality = cropTile.airQuality; + int startingNutrientStorage = cropTile.nutrientStorage; + int startingWaterStorage = cropTile.waterStorage; + // reset the crop to it's stage after harvest + cropTile.setSize((byte) cc.maxSize()); + cropTile.setSize(cc.getSizeAfterHarvest(cropTile)); + for (int timeElapsed = 0; timeElapsed < EIG_SIMULATION_TIME; timeElapsed += TileEntityCrop.tickRate) { + // force reset the stats to max because the eig shouldn't make them change. + // some crops check water storage in the can grow and we are ticking which consumes water. + cropTile.humidity = startingHumidity; + cropTile.nutrients = startingNutrients; + cropTile.airQuality = startingAirQuality; + cropTile.nutrientStorage = startingNutrientStorage; + cropTile.waterStorage = startingWaterStorage; + // if fully grown harvest the crop + if (cropTile.getSize() >= cc.maxSize()) { + ItemStack[] stacks = cropTile.harvest_automated(false); + for (ItemStack stack : stacks) { + expected.addDrop(stack, stack.stackSize); + } + } + cropTile.tick(); + } + // ensure it leaves the same way it came in + cropTile.humidity = startingHumidity; + cropTile.nutrients = startingNutrients; + cropTile.airQuality = startingAirQuality; + cropTile.nutrientStorage = startingNutrientStorage; + cropTile.waterStorage = startingWaterStorage; + return expected; + } + + EIGDropTable getEIGDrops(GT_MetaTileEntity_ExtremeIndustrialGreenhouse EIG, ItemStack stack) { + EIGDropTable generated = new EIGDropTable(); + EIGIC2Bucket bucket = new EIGIC2Bucket(stack, stack.stackSize, null, false); + bucket.revalidate(EIG); + bucket.addProgress(EIG_SIMULATION_TIME, generated); + return generated; + } + + @Test + void EIGDrops() { + myWorld.setBlock(10, 80, 0, Blocks.farmland, 0, 0); + myWorld.setBlock(10, 81, 0, Block.getBlockFromItem(Ic2Items.crop.getItem()), 0, 0); + // using stickreed since it has a random stage after harvest. + // it's also more preferable to test using faster growing crops since they can be harvested more often. + CropCard cc = Crops.instance.getCropCard("IC2", "stickreed"); + TileEntityCrop cropTile = (TileEntityCrop) myWorld.getTileEntity(10, 81, 0); + ItemStack ccStack = ItemCropSeed.generateItemStackFromValues(cc, (byte) 10, (byte) 10, (byte) 10, (byte) 1); + + GT_Item_Machines itemMachines = (GT_Item_Machines) Item.getItemFromBlock(GregTech_API.sBlockMachines); + itemMachines.placeBlockAt( + new ItemStack(itemMachines, 1, EIG_CONTROLLER_METADATA), + null, + myWorld, + 0, + 81, + 0, + 2, + 0, + 0, + 0, + EIG_CONTROLLER_METADATA); + IGregTechTileEntity te = (IGregTechTileEntity) myWorld.getTileEntity(0, 81, 0); + GT_MetaTileEntity_ExtremeIndustrialGreenhouse EIG = (GT_MetaTileEntity_ExtremeIndustrialGreenhouse) te + .getMetaTileEntity(); + + // update stats of crop TE to those provided by the EIG + cropTile.humidity = EIGIC2Bucket.getHumidity(EIG, false); + cropTile.nutrients = EIGIC2Bucket.getNutrients(EIG); + cropTile.airQuality = EIGIC2Bucket.getAirQuality(EIG); + + int[] abc = new int[] { 0, -2, 3 }; + int[] xyz = new int[] { 0, 0, 0 }; + EIG.getExtendedFacing() + .getWorldOffset(abc, xyz); + xyz[0] += te.getXCoord(); + xyz[1] += te.getYCoord(); + xyz[2] += te.getZCoord(); + + myWorld.setBlock(xyz[0], xyz[1] - 2, xyz[2], GregTech_API.sBlockCasings4, 1, 0); + myWorld.setBlock(xyz[0], xyz[1] - 1, xyz[2], Blocks.farmland, 0, 0); + + ItemStack stackToTest = null; + + double realAvg = 0, eigAvg = 0; + + for (int i = 0; i < NUMBER_OF_TESTS_TO_DO; i++) { + EIGDropTable expected = getRealDrops(cropTile, cc, 10, 10, 10); + EIGDropTable generated = getEIGDrops(EIG, ccStack); + + // we are only comparing one item from drops + if (stackToTest == null) { + stackToTest = expected.entrySet() + .stream() + .max(Map.Entry.comparingByValue()) + .get() + .getKey(); + } + + realAvg += expected.getItemAmount(stackToTest); + // EIG with ic2 crops doesn't actually have variance, it uses very precise approximations that create + // accurate growth rate and drop quality approximations. + eigAvg += generated.getItemAmount(stackToTest); + } + realAvg /= NUMBER_OF_TESTS_TO_DO; + eigAvg /= NUMBER_OF_TESTS_TO_DO; + double accuracy = Math.min(realAvg / eigAvg, eigAvg / realAvg); + + String debugInfo = String.format("realAvg: %.5f | eigAvg : %.5f | accuracy = %.5f", realAvg, eigAvg, accuracy); + System.out.println(debugInfo); + + // We aim for about 99% accuracy over here. + assertTrue(accuracy >= 0.99d); + } + +} diff --git a/src/functionalTest/java/kubatech/test/kubatechTestMod.java b/src/functionalTest/java/kubatech/test/kubatechTestMod.java new file mode 100644 index 0000000000..bc68d3f203 --- /dev/null +++ b/src/functionalTest/java/kubatech/test/kubatechTestMod.java @@ -0,0 +1,101 @@ +package kubatech.test; + +import java.io.File; +import java.io.PrintWriter; +import java.nio.file.FileSystems; +import java.nio.file.Path; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.ChatComponentText; + +import org.apache.commons.io.output.CloseShieldOutputStream; +import org.junit.platform.engine.discovery.DiscoverySelectors; +import org.junit.platform.launcher.Launcher; +import org.junit.platform.launcher.LauncherDiscoveryRequest; +import org.junit.platform.launcher.LauncherSession; +import org.junit.platform.launcher.TestPlan; +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; +import org.junit.platform.launcher.core.LauncherFactory; +import org.junit.platform.launcher.listeners.SummaryGeneratingListener; +import org.junit.platform.launcher.listeners.TestExecutionSummary; +import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.Mod; +import cpw.mods.fml.common.Mod.EventHandler; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.event.FMLServerStartedEvent; +import gregtech.GT_Mod; + +@Mod( + modid = "kubatech-tests", + name = "KubaTech Dev Tests", + version = "1.0", + dependencies = "required-after:kubatech;required-after:gregtech;after:berriespp;") +public class kubatechTestMod { + + @EventHandler + public void preInit(FMLPreInitializationEvent ev) { + // Disable GT5u messing with vanilla recipes for unit tests + GT_Mod.gregtechproxy.mNerfedWoodPlank = false; + GT_Mod.gregtechproxy.mNerfedVanillaTools = false; + } + + @EventHandler + public void onServerStarted(FMLServerStartedEvent startedEv) { + MinecraftServer.getServer() + .addChatMessage(new ChatComponentText("Running KT unit tests...")); + runTests(); + MinecraftServer.getServer() + .addChatMessage(new ChatComponentText("Running KT unit tests finished")); + } + + public void runTests() { + // https://junit.org/junit5/docs/current/user-guide/#launcher-api + System.setProperty("junit.platform.reporting.open.xml.enabled", "false"); + final Path testsXmlOutDir = FileSystems.getDefault() + .getPath("./junit-out/") + .toAbsolutePath(); + final File testsXmlOutDirFile = testsXmlOutDir.toFile(); + testsXmlOutDirFile.mkdirs(); + { + File[] fileList = testsXmlOutDirFile.listFiles(); + if (fileList != null) { + for (File child : fileList) { + if (child.isFile() && child.getName() + .endsWith(".xml")) { + child.delete(); + } + } + } + } + final LauncherDiscoveryRequest discovery = LauncherDiscoveryRequestBuilder.request() + .selectors(DiscoverySelectors.selectPackage("kubatech.test")) + .build(); + final SummaryGeneratingListener summaryGenerator = new SummaryGeneratingListener(); + final TestExecutionSummary summary; + try (PrintWriter stderrWriter = new PrintWriter(new CloseShieldOutputStream(System.err), true)) { + final LegacyXmlReportGeneratingListener xmlGenerator = new LegacyXmlReportGeneratingListener( + testsXmlOutDir, + stderrWriter); + try (LauncherSession session = LauncherFactory.openSession()) { + final Launcher launcher = session.getLauncher(); + final TestPlan plan = launcher.discover(discovery); + launcher.registerTestExecutionListeners(summaryGenerator, xmlGenerator); + launcher.execute(plan); + } + summary = summaryGenerator.getSummary(); + + summary.printFailuresTo(stderrWriter, 32); + summary.printTo(stderrWriter); + stderrWriter.flush(); + } + // Throw an exception if running via `runServer` + if (summary.getTotalFailureCount() > 0 && FMLCommonHandler.instance() + .getSide() + .isServer()) { + throw new RuntimeException("Some of the unit tests failed to execute, check the log for details"); + } + } + +} |