From 40066e1a84fa1ff3e04799aca50dfb4aa46f6da7 Mon Sep 17 00:00:00 2001 From: MkDomain Date: Wed, 28 Oct 2026 00:17:14 +0100 Subject: [PATCH 1/3] Add more unit tests --- test-data/input-1.txt | 3 +++ test-data/input-2.txt | 6 ++++++ test-data/input-3.txt | 3 +++ test-data/input-4.txt | 3 +++ test-data/input-5.txt | 4 ++++ test-data/input-6.txt | 3 +++ test-data/output-1.txt | 2 ++ test-data/output-2.txt | 5 +++++ test-data/output-3.txt | 2 ++ test-data/output-4.txt | 2 ++ test-data/output-5.txt | 3 +++ test-data/output-6.txt | 2 ++ 12 files changed, 38 insertions(+) create mode 100644 test-data/input-1.txt create mode 100644 test-data/input-2.txt create mode 100644 test-data/input-3.txt create mode 100644 test-data/input-4.txt create mode 100644 test-data/input-5.txt create mode 100644 test-data/input-6.txt create mode 100644 test-data/output-1.txt create mode 100644 test-data/output-2.txt create mode 100644 test-data/output-3.txt create mode 100644 test-data/output-4.txt create mode 100644 test-data/output-5.txt create mode 100644 test-data/output-6.txt diff --git a/test-data/input-1.txt b/test-data/input-1.txt new file mode 100644 index 0000000..cc628f9 --- /dev/null +++ b/test-data/input-1.txt @@ -0,0 +1,3 @@ +# SINGLE mód: első tüzelés az elsődleges tárból +GT4500,1,0,1,0 +TORPEDO,SINGLE diff --git a/test-data/input-2.txt b/test-data/input-2.txt new file mode 100644 index 0000000..609a51d --- /dev/null +++ b/test-data/input-2.txt @@ -0,0 +1,6 @@ +# SINGLE mód: váltakozó tüzelés +GT4500,2,0,1,0 +TORPEDO,SINGLE +TORPEDO,SINGLE +TORPEDO,SINGLE +TORPEDO,SINGLE diff --git a/test-data/input-3.txt b/test-data/input-3.txt new file mode 100644 index 0000000..8680729 --- /dev/null +++ b/test-data/input-3.txt @@ -0,0 +1,3 @@ +# SINGLE mód: első tüzelésnél primary üres, secondary-ből tüzel +GT4500,0,0,1,0 +TORPEDO,SINGLE diff --git a/test-data/input-4.txt b/test-data/input-4.txt new file mode 100644 index 0000000..cadbc17 --- /dev/null +++ b/test-data/input-4.txt @@ -0,0 +1,3 @@ +# SINGLE mód: primary hiba esetén nem próbálja a másikat +GT4500,1,1,1,0 +TORPEDO,SINGLE diff --git a/test-data/input-5.txt b/test-data/input-5.txt new file mode 100644 index 0000000..c3e9c50 --- /dev/null +++ b/test-data/input-5.txt @@ -0,0 +1,4 @@ +# ALL mód: mindkét tárból tüzel, ha van torpedó +GT4500,1,0,1,0 +TORPEDO,ALL +TORPEDO,SINGLE diff --git a/test-data/input-6.txt b/test-data/input-6.txt new file mode 100644 index 0000000..b32bd37 --- /dev/null +++ b/test-data/input-6.txt @@ -0,0 +1,3 @@ +# ALL mód: csak az elsődlegesben van torpedó +GT4500,1,0,0,0 +TORPEDO,ALL diff --git a/test-data/output-1.txt b/test-data/output-1.txt new file mode 100644 index 0000000..1db2652 --- /dev/null +++ b/test-data/output-1.txt @@ -0,0 +1,2 @@ +SUCCESS +SUCCESS diff --git a/test-data/output-2.txt b/test-data/output-2.txt new file mode 100644 index 0000000..ef87ad8 --- /dev/null +++ b/test-data/output-2.txt @@ -0,0 +1,5 @@ +SUCCESS +SUCCESS +SUCCESS +SUCCESS +FAIL diff --git a/test-data/output-3.txt b/test-data/output-3.txt new file mode 100644 index 0000000..1db2652 --- /dev/null +++ b/test-data/output-3.txt @@ -0,0 +1,2 @@ +SUCCESS +SUCCESS diff --git a/test-data/output-4.txt b/test-data/output-4.txt new file mode 100644 index 0000000..5ffab45 --- /dev/null +++ b/test-data/output-4.txt @@ -0,0 +1,2 @@ +SUCCESS +FAIL diff --git a/test-data/output-5.txt b/test-data/output-5.txt new file mode 100644 index 0000000..49201b3 --- /dev/null +++ b/test-data/output-5.txt @@ -0,0 +1,3 @@ +SUCCESS +SUCCESS +FAIL diff --git a/test-data/output-6.txt b/test-data/output-6.txt new file mode 100644 index 0000000..1db2652 --- /dev/null +++ b/test-data/output-6.txt @@ -0,0 +1,2 @@ +SUCCESS +SUCCESS From f31a75fbd605f79d26eebdc166db0ed6fe617a31 Mon Sep 17 00:00:00 2001 From: MkDomain Date: Wed, 28 Oct 2026 00:33:23 +0100 Subject: [PATCH 2/3] Add more unit tests to reach coverage --- test-data/input-7.txt | 4 ++++ test-data/input-8.txt | 3 +++ test-data/input-9.txt | 3 +++ test-data/output-7.txt | 3 +++ test-data/output-8.txt | 2 ++ test-data/output-9.txt | 2 ++ 6 files changed, 17 insertions(+) create mode 100644 test-data/input-7.txt create mode 100644 test-data/input-8.txt create mode 100644 test-data/input-9.txt create mode 100644 test-data/output-7.txt create mode 100644 test-data/output-8.txt create mode 100644 test-data/output-9.txt diff --git a/test-data/input-7.txt b/test-data/input-7.txt new file mode 100644 index 0000000..7e53892 --- /dev/null +++ b/test-data/input-7.txt @@ -0,0 +1,4 @@ +# SINGLE mód: primary lőtt először, secondary üres, így újra primary-ből lő +GT4500,2,0,0,0 +TORPEDO,SINGLE +TORPEDO,SINGLE diff --git a/test-data/input-8.txt b/test-data/input-8.txt new file mode 100644 index 0000000..35bd42e --- /dev/null +++ b/test-data/input-8.txt @@ -0,0 +1,3 @@ +# ALL mód: csak a másodikban van torpedó +GT4500,0,0,1,0 +TORPEDO,ALL diff --git a/test-data/input-9.txt b/test-data/input-9.txt new file mode 100644 index 0000000..c91d45d --- /dev/null +++ b/test-data/input-9.txt @@ -0,0 +1,3 @@ +# ALL mód: mindkét tár üres +GT4500,0,0,0,0 +TORPEDO,ALL diff --git a/test-data/output-7.txt b/test-data/output-7.txt new file mode 100644 index 0000000..21667a7 --- /dev/null +++ b/test-data/output-7.txt @@ -0,0 +1,3 @@ +SUCCESS +SUCCESS +SUCCESS diff --git a/test-data/output-8.txt b/test-data/output-8.txt new file mode 100644 index 0000000..1db2652 --- /dev/null +++ b/test-data/output-8.txt @@ -0,0 +1,2 @@ +SUCCESS +SUCCESS diff --git a/test-data/output-9.txt b/test-data/output-9.txt new file mode 100644 index 0000000..5ffab45 --- /dev/null +++ b/test-data/output-9.txt @@ -0,0 +1,2 @@ +SUCCESS +FAIL From ac5d581a3e0d5c29f04b8d1ed3ecfb21e4818274 Mon Sep 17 00:00:00 2001 From: MkDomain Date: Wed, 28 Oct 2026 01:07:54 +0100 Subject: [PATCH 3/3] Add unit tests to TorpedoStore --- .../bme/mit/spaceship/TorpedoStoreTest.java | 80 +++++++++++++++++-- 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java b/src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java index 5df1009..16cec7e 100644 --- a/src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java +++ b/src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java @@ -1,20 +1,84 @@ package hu.bme.mit.spaceship; -import static org.junit.jupiter.api.Assertions.assertEquals; - import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import sun.misc.Unsafe; + +import java.lang.reflect.Field; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; class TorpedoStoreTest { @Test void fire_Success() { - // Arrange - TorpedoStore store = new TorpedoStore(1); + TorpedoStore store = new TorpedoStore(1, 0.0); + assertTrue(store.fire(1)); + } + + @ParameterizedTest + @ValueSource(ints = {0, -1, 2}) + void fire_InvalidNumberOfTorpedos(int numberOfTorpedos) { + TorpedoStore store = new TorpedoStore(1, 0.0); + assertThrows(IllegalArgumentException.class, () -> store.fire(numberOfTorpedos)); + } - // Act - boolean result = store.fire(1); + @Test + void isEmpty() { + TorpedoStore store = new TorpedoStore(0, 0.0); + assertTrue(store.isEmpty()); + } + + @ParameterizedTest + @ValueSource(ints = {5, 10, 15}) + void getTorpedoCount(int count) { + TorpedoStore store = new TorpedoStore(count); + assertEquals(count, store.getTorpedoCount()); + } - // Assert - assertEquals(true, result); + @Test + void constructor_customFailureRate() { + evilSetEnv("IVT_RATE", "5.0"); + TorpedoStore store = new TorpedoStore(1, 0.0); + store.fire(1); } + + @Test + void constructor_invalidCustomFailureRate() { + evilSetEnv("IVT_RATE", "invalid"); + TorpedoStore store = new TorpedoStore(1, 0.0); + assertTrue(store.fire(1)); + } + + @Test + void fireFailure() { + TorpedoStore store = new TorpedoStore(1, 1.0); + assertFalse(store.fire(1)); + } + + private static Map theCaseInsensitiveEnvironment = null; + @SuppressWarnings({"unchecked", "removal", "SameParameterValue"}) + private static void evilSetEnv(String key, String value) { + if (theCaseInsensitiveEnvironment == null) { + try { + Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafeField.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafeField.get(null); + + Class processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); + Field field = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment"); + + Object fieldBase = unsafe.staticFieldBase(field); + long offset = unsafe.staticFieldOffset(field); + theCaseInsensitiveEnvironment = (Map) unsafe.getObject(fieldBase, offset); + } catch (Exception ex) { + throw new RuntimeException("Evil trickery is not possible!", ex); + } + } + + theCaseInsensitiveEnvironment.put(key, value); + } + }