Skip to content

Commit

Permalink
even more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rofidakhaled committed Jan 11, 2025
1 parent cd41e1a commit 38ef4a6
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 32 deletions.
34 changes: 2 additions & 32 deletions entity-component-system/src/main/java/com/iluwatar/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@
*/
package com.iluwatar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The main entry point for the application.
* This class simulates a game loop where entities are created, updated, and their states are modified.
*/
public class App {

// Create a logger instance
public static final Logger logger = LoggerFactory.getLogger(App.class);

/**
* The main method that runs the application.
* It creates entities, adds components, updates them over several frames,
Expand All @@ -44,68 +38,44 @@ public class App {
* @param args Command-line arguments (not used in this application)
*/
public static void main(String[] args) {
System.out.print("Hello, World!");

Entity entity1 = new Entity("Entity1");
Entity entity2 = new Entity("Entity2");

// Set up some transform components (position, rotation, scale)
TransformComponent transform1 = new TransformComponent(new float[]{0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f});
TransformComponent transform2 = new TransformComponent(new float[]{5.0f, 0.0f, 0.0f},
new float[]{0.0f, 45.0f, 0.0f}, new float[]{1.0f, 1.0f, 1.0f});

// Set the transform components for each entity
entity1.addComponent(transform1);
entity2.addComponent(transform2);

// Create a health component for entity1 with initial health of 100
HealthComponent health1 = new HealthComponent(100); // Ensure HealthComponent is implemented
entity1.addComponent(health1);

// Create a velocity component for entity1
VelocityComponent velocity1 = new VelocityComponent(1.0f, 0.0f, 0.0f);
entity1.addComponent(velocity1);

// Set up a system and add entities to the system
GameSystem gameSystem = new GameSystem();
gameSystem.addEntity(entity1);
gameSystem.addEntity(entity2);

// Simulate game update loop (e.g., 60 FPS)
float deltaTime = 1.0f / 60.0f; // 60 FPS
float deltaTime = 1.0f / 60.0f;

// Simulate for a few frames
for (int i = 0; i < 10; i++) {
logger.info("Frame: {}", i + 1);

// Update all entities in the system
gameSystem.update(deltaTime);

// Apply some damage to entity1's health component at frame 6
if (i == 5) {
health1.applyDamage(30);
logger.info("Entity1's health after damage: {}", health1.getCurrentHealth());
}

// Apply some force to entity1's velocity at frame 3
if (i == 3) {
velocity1.applyForce(0.5f, 0.0f, 0.0f);
logger.info("Entity1's velocity after force: ({}, {}, {})", velocity1.getVelocityX(),
velocity1.getVelocityY(), velocity1.getVelocityZ());
}

// Render the system (optional rendering logic can be added here)
gameSystem.renderSystem();
}

// After the simulation, check final entity states
logger.info("\nFinal Entity States:");
logger.info("Entity1 position: {}, {}, {}",
entity1.getTransformComponent().getPosition()[0],
entity1.getTransformComponent().getPosition()[1],
entity1.getTransformComponent().getPosition()[2]);
logger.info("Entity1 velocity: {}, {}, {}",
velocity1.getVelocityX(), velocity1.getVelocityY(), velocity1.getVelocityZ());
logger.info("Entity1 health: {}", health1.getCurrentHealth());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,13 @@ public boolean isEnabled() {
* @param isEnabled true to enable the entity, false to disable it
*/
public void setIsEnabled(boolean isEnabled) {

this.isEnabled = isEnabled;

for (Entity child : children) {
child.setIsEnabled(isEnabled);

}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class VelocityComponent extends Component {
private float velocityY; // The velocity in the Y direction
private float velocityZ; // The velocity in the Z direction

private final float initialVelocityX;
private final float initialVelocityY;
private final float initialVelocityZ;

/**
* Constructs a VelocityComponent with the given velocity values in the X, Y, and Z directions.
*
Expand All @@ -49,8 +53,24 @@ public VelocityComponent(float velocityX, float velocityY, float velocityZ) {
this.velocityX = velocityX;
this.velocityY = velocityY;
this.velocityZ = velocityZ;

this.initialVelocityX = velocityX;
this.initialVelocityY = velocityY;
this.initialVelocityZ = velocityZ;
}

/**
* update the velocity of the entity.
* This method resets the velocity
*
*/
public void resetVelocity() {
this.velocityX = this.initialVelocityX;
this.velocityY = this.initialVelocityY;
this.velocityZ = this.initialVelocityZ;
}


/**
* Updates the velocity of the entity based on delta time.
* This method multiplies the current velocity by the delta time to simulate movement.
Expand Down
93 changes: 93 additions & 0 deletions entity-component-system/src/test/java/com/iluwatar/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class AppTest {

Expand All @@ -53,7 +55,16 @@ public void setUp() {
velocity1 = new VelocityComponent(1.0f, 0.0f, 0.0f);
entity1.addComponent(velocity1);
}
@Test
public void testMain_shouldPrintHelloWorld() {

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));

App.main(new String[]{});

assertEquals("Hello, World!", outputStream.toString(), "The output should be 'Hello, World!'");
}
@Test
public void testHealthComponentApplyDamage() {

Expand Down Expand Up @@ -121,4 +132,86 @@ public void testFinalEntityStateAfterSimulation() {
assertNotNull(entity1.getTransformComponent(), "Entity1 should have a transform component");

}

@Test
public void testAddTransformComponent() {
entity1.addComponent(transform1);
assertTrue(entity1.getComponents().contains(transform1), "Entity1 should contain the added TransformComponent.");
}

@Test
public void testGameLoopUpdatesEntityState() {
GameSystem gameSystem = new GameSystem();
gameSystem.addEntity(entity1);

for (int i = 0; i < 5; i++) {
gameSystem.update(1.0f / 60.0f);
}

// Check the updated health and velocity
assertEquals(100, health1.getCurrentHealth(), "Health should not be affected yet.");
assertEquals(1.0f, velocity1.getVelocityX(), "Velocity X should remain the same as no force is applied.");
}

@Test
public void testHealthReductionOverMultipleDamages() {
health1.applyDamage(20);
health1.applyDamage(30);

assertEquals(50, health1.getCurrentHealth(), "Health should be reduced by 50 after two damage applications.");
}

@Test
public void testEntityRemovalFromGameSystem() {
GameSystem gameSystem = new GameSystem();
gameSystem.addEntity(entity1);

gameSystem.removeEntity(entity1);

// Assert that entity1 is no longer in the system
assertFalse(gameSystem.getEntities().contains(entity1), "Entity1 should no longer be in the GameSystem after removal.");
}

@Test
public void testEntityWithoutComponents() {
Entity entity = new Entity("EmptyEntity");
entity.removeComponent(entity.getTransformComponent());
assertTrue(entity.getComponents().isEmpty(), "Entity should have no components.");
assertEquals("EmptyEntity", entity.getName(), "Entity should have the correct name.");
}

@Test
public void testSetParentAndChildren() {
Entity parentEntity = new Entity("ParentEntity");
Entity childEntity = new Entity("ChildEntity");

parentEntity.addChild(childEntity);

assertTrue(parentEntity.getChildren().contains(childEntity), "Parent entity should contain the child entity.");
assertEquals(parentEntity, childEntity.getParent(), "Child entity should have the parent entity set.");
}

@Test
public void testVelocityComponentReset() {
velocity1.applyForce(1.0f, 0.0f, 0.0f);
float newVelocityX = velocity1.getVelocityX();

velocity1.resetVelocity();

assertEquals(1.0f, velocity1.getVelocityX(), "Velocity should be reset to its initial value.");
assertEquals(0.0f, velocity1.getVelocityY(), "Velocity Y should remain reset.");
assertEquals(0.0f, velocity1.getVelocityZ(), "Velocity Z should remain reset.");
}

@Test
public void testHealthAndForceAppliedTogether() {
health1.applyDamage(20);
velocity1.applyForce(0.5f, 0.0f, 0.0f);

// Update entity state after applying damage and force
entity1.update(1.0f / 60.0f);

assertEquals(80, health1.getCurrentHealth(), "Health should be reduced by 20 after damage.");
assertEquals(1.5f, velocity1.getVelocityX(), "Velocity X should increase by 0.5 after applying force.");
}
}
48 changes: 48 additions & 0 deletions entity-component-system/src/test/java/com/iluwatar/EntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,52 @@ public void testGetAndSetGameSystem() {

assertEquals(gameSystem, entity.getGameSystem(), "The game system should match the one set.");
}

@Test
public void testUpdate_whenEntityDisabled_shouldReturnImmediately() {


Entity parent = new Entity("parent");
Entity child = new Entity("child");

child.setParent(parent);

parent.setEnabled(false);
parent.addComponent(transform1);

parent.update(1.0f);

assertFalse(transform1.getEnabled(), "Component should not be enabled.");
assertDoesNotThrow(() -> transform1.update(1.0f), "Component update should not throw an exception when entity is disabled.");

assertDoesNotThrow(() -> child.update(1.0f), "Child entity should not throw an exception if the parent is disabled.");
}

@Test
public void testUpdate_shouldUpdateEnabledComponents() {
entity1.setEnabled(true);
entity2.setEnabled(false);
entity1.addComponent(transform1);
entity2.addComponent(transform2);
entity1.update(1.0f);
entity2.update(1.0f);

assertTrue(transform1.getEnabled(), "Transform1 should be enabled after update when the parent entity is enabled.");
assertDoesNotThrow(() -> transform1.update(1.0f), "Transform1 update should not throw an exception when the parent entity is enabled.");

assertFalse(transform2.getEnabled(), "Transform2 should remain disabled after update when it is added but disabled.");
assertDoesNotThrow(() -> transform2.update(1.0f), "Transform2 update should not throw an exception even though it's disabled.");
}

@Test
public void testUpdate_shouldUpdateChildEntities() {
Entity child = new Entity("child");
entity1.setEnabled(true);
entity1.addChild(child);
child.setParent(entity1);
entity1.update(1.0f);
assertDoesNotThrow(() -> child.update(1.0f));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ public void testApplyFriction() {
assertEquals(0.9f, velocity.getVelocityX(), "VelocityX should be reduced by the friction coefficient.");
}

@Test
public void testReset_shouldResetVelocityToZero() {

velocity.setVelocityX( 5.0f);
velocity.setVelocityY( 10.0f);
velocity.setVelocityZ( 15.0f);
velocity.resetVelocity();

assertEquals(1.0f, velocity.getVelocityX(), "Velocity X should be reset to 1.0f");
assertEquals(0.0f, velocity.getVelocityY(), "Velocity Y should be reset to 0");
assertEquals(0.0f, velocity.getVelocityZ(), "Velocity Z should be reset to 0");
}

@Test
public void testUpdateVelocity() {
float deltaTime = 1.0f / 60.0f;
Expand Down

0 comments on commit 38ef4a6

Please sign in to comment.