Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 72 additions & 8 deletions src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java
Original file line number Diff line number Diff line change
@@ -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;

Check warning on line 6 in src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java

View workflow job for this annotation

GitHub Actions / Coverage with JaCoCo

sun.misc.Unsafe is internal proprietary API and may be removed in a future release

Check warning on line 6 in src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java

View workflow job for this annotation

GitHub Actions / Build with Maven

sun.misc.Unsafe is internal proprietary API and may be removed in a future release

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<String, String> theCaseInsensitiveEnvironment = null;
@SuppressWarnings({"unchecked", "removal", "SameParameterValue"})
private static void evilSetEnv(String key, String value) {
if (theCaseInsensitiveEnvironment == null) {
try {
Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");

Check warning on line 66 in src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java

View workflow job for this annotation

GitHub Actions / Coverage with JaCoCo

sun.misc.Unsafe is internal proprietary API and may be removed in a future release

Check warning on line 66 in src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java

View workflow job for this annotation

GitHub Actions / Build with Maven

sun.misc.Unsafe is internal proprietary API and may be removed in a future release
theUnsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafeField.get(null);

Check warning on line 68 in src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java

View workflow job for this annotation

GitHub Actions / Coverage with JaCoCo

sun.misc.Unsafe is internal proprietary API and may be removed in a future release

Check warning on line 68 in src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java

View workflow job for this annotation

GitHub Actions / Coverage with JaCoCo

sun.misc.Unsafe is internal proprietary API and may be removed in a future release

Check warning on line 68 in src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java

View workflow job for this annotation

GitHub Actions / Build with Maven

sun.misc.Unsafe is internal proprietary API and may be removed in a future release

Check warning on line 68 in src/test/java/hu/bme/mit/spaceship/TorpedoStoreTest.java

View workflow job for this annotation

GitHub Actions / Build with Maven

sun.misc.Unsafe is internal proprietary API and may be removed in a future release

Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field field = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");

Object fieldBase = unsafe.staticFieldBase(field);
long offset = unsafe.staticFieldOffset(field);
theCaseInsensitiveEnvironment = (Map<String, String>) unsafe.getObject(fieldBase, offset);
} catch (Exception ex) {
throw new RuntimeException("Evil trickery is not possible!", ex);
}
}

theCaseInsensitiveEnvironment.put(key, value);
}

}
3 changes: 3 additions & 0 deletions test-data/input-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SINGLE mód: első tüzelés az elsődleges tárból
GT4500,1,0,1,0
TORPEDO,SINGLE
6 changes: 6 additions & 0 deletions test-data/input-2.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test-data/input-3.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test-data/input-4.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test-data/input-5.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test-data/input-6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ALL mód: csak az elsődlegesben van torpedó
GT4500,1,0,0,0
TORPEDO,ALL
4 changes: 4 additions & 0 deletions test-data/input-7.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test-data/input-8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ALL mód: csak a másodikban van torpedó
GT4500,0,0,1,0
TORPEDO,ALL
3 changes: 3 additions & 0 deletions test-data/input-9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ALL mód: mindkét tár üres
GT4500,0,0,0,0
TORPEDO,ALL
2 changes: 2 additions & 0 deletions test-data/output-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUCCESS
SUCCESS
5 changes: 5 additions & 0 deletions test-data/output-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SUCCESS
SUCCESS
SUCCESS
SUCCESS
FAIL
2 changes: 2 additions & 0 deletions test-data/output-3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUCCESS
SUCCESS
2 changes: 2 additions & 0 deletions test-data/output-4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUCCESS
FAIL
3 changes: 3 additions & 0 deletions test-data/output-5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SUCCESS
SUCCESS
FAIL
2 changes: 2 additions & 0 deletions test-data/output-6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUCCESS
SUCCESS
3 changes: 3 additions & 0 deletions test-data/output-7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SUCCESS
SUCCESS
SUCCESS
2 changes: 2 additions & 0 deletions test-data/output-8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUCCESS
SUCCESS
2 changes: 2 additions & 0 deletions test-data/output-9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUCCESS
FAIL
Loading