|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-2024 Team Galacticraft |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package dev.galacticraft.machinelib.test.storage; |
| 24 | + |
| 25 | +import dev.galacticraft.machinelib.api.storage.MachineEnergyStorage; |
| 26 | +import dev.galacticraft.machinelib.impl.storage.MachineEnergyStorageImpl; |
| 27 | +import dev.galacticraft.machinelib.test.MinecraftTest; |
| 28 | +import io.netty.buffer.ByteBuf; |
| 29 | +import io.netty.buffer.Unpooled; |
| 30 | +import net.minecraft.nbt.LongTag; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.*; |
| 35 | + |
| 36 | +public class EnergyStorageTests implements MinecraftTest { |
| 37 | + private static final long CAPACITY = 2000; |
| 38 | + private MachineEnergyStorage storage; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + public void setup() { |
| 42 | + this.storage = new MachineEnergyStorageImpl(CAPACITY, 0, 0, true, true); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void canExtract() { |
| 47 | + this.storage.setEnergy(1000); |
| 48 | + |
| 49 | + assertTrue(this.storage.canExtract(1000)); |
| 50 | + assertTrue(this.storage.canExtract(500)); |
| 51 | + assertFalse(this.storage.canExtract(1001)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void canInsert() { |
| 56 | + this.storage.setEnergy(1000); |
| 57 | + |
| 58 | + assertTrue(this.storage.canInsert(1000)); |
| 59 | + assertTrue(this.storage.canInsert(500)); |
| 60 | + assertFalse(this.storage.canInsert(1001)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void tryExtract() { |
| 65 | + this.storage.setEnergy(1000); |
| 66 | + |
| 67 | + assertEquals(1000, this.storage.tryExtract(1000)); |
| 68 | + assertEquals(500, this.storage.tryExtract(500)); |
| 69 | + assertEquals(1000, this.storage.tryExtract(1001)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void tryInsert() { |
| 74 | + this.storage.setEnergy(1000); |
| 75 | + |
| 76 | + assertEquals(1000, this.storage.tryInsert(1000)); |
| 77 | + assertEquals(500, this.storage.tryInsert(500)); |
| 78 | + assertEquals(1000, this.storage.tryInsert(1001)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void extract() { |
| 83 | + this.storage.setEnergy(1000); |
| 84 | + assertEquals(1000, this.storage.extract(1000)); |
| 85 | + assertEquals(0, this.storage.getAmount()); |
| 86 | + |
| 87 | + this.storage.setEnergy(1000); |
| 88 | + assertEquals(500, this.storage.extract(500)); |
| 89 | + assertEquals(500, this.storage.getAmount()); |
| 90 | + |
| 91 | + this.storage.setEnergy(1000); |
| 92 | + assertEquals(1000, this.storage.extract(1001)); |
| 93 | + assertEquals(0, this.storage.getAmount()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void insert() { |
| 98 | + this.storage.setEnergy(1000); |
| 99 | + assertEquals(1000, this.storage.insert(1000)); |
| 100 | + assertEquals(2000, this.storage.getAmount()); |
| 101 | + |
| 102 | + this.storage.setEnergy(1000); |
| 103 | + assertEquals(500, this.storage.insert(500)); |
| 104 | + assertEquals(1500, this.storage.getAmount()); |
| 105 | + |
| 106 | + this.storage.setEnergy(1000); |
| 107 | + assertEquals(1000, this.storage.insert(1001)); |
| 108 | + assertEquals(2000, this.storage.getAmount()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void extractExact() { |
| 113 | + this.storage.setEnergy(1000); |
| 114 | + assertTrue(this.storage.extractExact(1000)); |
| 115 | + assertEquals(0, this.storage.getAmount()); |
| 116 | + |
| 117 | + this.storage.setEnergy(1000); |
| 118 | + assertTrue(this.storage.extractExact(500)); |
| 119 | + assertEquals(500, this.storage.getAmount()); |
| 120 | + |
| 121 | + this.storage.setEnergy(1000); |
| 122 | + assertFalse(this.storage.extractExact(1001)); |
| 123 | + assertEquals(1000, this.storage.getAmount()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void insertExact() { |
| 128 | + this.storage.setEnergy(1000); |
| 129 | + assertTrue(this.storage.insertExact(1000)); |
| 130 | + assertEquals(2000, this.storage.getAmount()); |
| 131 | + |
| 132 | + this.storage.setEnergy(1000); |
| 133 | + assertTrue(this.storage.insertExact(500)); |
| 134 | + assertEquals(1500, this.storage.getAmount()); |
| 135 | + |
| 136 | + this.storage.setEnergy(1000); |
| 137 | + assertFalse(this.storage.insertExact(1001)); |
| 138 | + assertEquals(1000, this.storage.getAmount()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void getAmount() { |
| 143 | + this.storage.setEnergy(1000); |
| 144 | + assertEquals(1000, this.storage.getAmount()); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void getCapacity() { |
| 149 | + assertEquals(CAPACITY, this.storage.getCapacity()); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void isFull() { |
| 154 | + this.storage.setEnergy(1000); |
| 155 | + assertFalse(this.storage.isFull()); |
| 156 | + |
| 157 | + this.storage.setEnergy(2000); |
| 158 | + assertTrue(this.storage.isFull()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void isEmpty() { |
| 163 | + this.storage.setEnergy(1000); |
| 164 | + assertFalse(this.storage.isEmpty()); |
| 165 | + |
| 166 | + this.storage.setEnergy(0); |
| 167 | + assertTrue(this.storage.isEmpty()); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void nbtSerialization() { |
| 172 | + this.storage.setEnergy(1000); |
| 173 | + |
| 174 | + LongTag tag = this.storage.createTag(); |
| 175 | + this.storage.setEnergy(0); |
| 176 | + this.storage.readTag(tag); |
| 177 | + |
| 178 | + assertEquals(1000, this.storage.getAmount()); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void packetSerialization() { |
| 183 | + this.storage.setEnergy(1000); |
| 184 | + ByteBuf buf = Unpooled.buffer(); |
| 185 | + |
| 186 | + this.storage.writePacket(buf); |
| 187 | + this.storage.setEnergy(0); |
| 188 | + this.storage.readPacket(buf); |
| 189 | + |
| 190 | + assertEquals(1000, this.storage.getAmount()); |
| 191 | + } |
| 192 | +} |
0 commit comments