Search in sources :

Example 31 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class UnitsMutexTest method isUnitFree_unitIsLocked_returnsFalse.

@Test
public void isUnitFree_unitIsLocked_returnsFalse() {
    final Unit unit = UnitBuilder.mockUnit();
    this.target.lockUnit(unit);
    assertThat(this.target.isUnitFree(unit)).isFalse();
}
Also used : Unit(bwapi.Unit) Test(org.junit.Test)

Example 32 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class UnitsMutexTest method unlockUnit_unitLocked_unitUnlocked.

@Test
public void unlockUnit_unitLocked_unitUnlocked() {
    final Unit unit = UnitBuilder.mockUnit();
    assertThat(this.target.isUnitLocked(unit)).as("Sanity test that unit unlocked").isFalse();
    this.target.lockUnit(unit);
    this.target.unlockUnit(unit);
    assertThat(this.target.isUnitLocked(unit)).as("unit unlocked").isFalse();
}
Also used : Unit(bwapi.Unit) Test(org.junit.Test)

Example 33 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class UnitsMutexTest method isUnitLocked_unitIsLocked_returnsTrue.

@Test
public void isUnitLocked_unitIsLocked_returnsTrue() {
    final Unit unit = UnitBuilder.mockUnit();
    this.target.lockUnit(unit);
    assertThat(this.target.isUnitLocked(unit)).isTrue();
}
Also used : Unit(bwapi.Unit) Test(org.junit.Test)

Example 34 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class UnitsMutexTest method acquireLock_multithreaded_onlyAcquiredOnce.

/**
 * Start lots of threads, and all threads try to acquire lock for an object. Verify that
 * only 1 such thread was able to acquire the lock.
 */
@Test
public void acquireLock_multithreaded_onlyAcquiredOnce() throws Exception {
    final int numberOfThreads = 10;
    final AtomicInteger numberOfTimesLockAcquired = new AtomicInteger(0);
    final AtomicInteger numberOfTimesLockFailedAcquired = new AtomicInteger(0);
    final Unit unitToLock = UnitBuilder.mockUnit();
    startMultipleThreadsAndWaitUntilComplete(IntStream.range(0, numberOfThreads).mapToObj(x -> new Runnable() {

        @Override
        public void run() {
            // Try to acquire unit. Only 1 thread should succeeded
            if (UnitsMutexTest.this.target.lockUnit(unitToLock)) {
                System.out.println(x);
                numberOfTimesLockAcquired.incrementAndGet();
            } else {
                numberOfTimesLockFailedAcquired.incrementAndGet();
            }
        }
    }).collect(Collectors.toList()));
    assertThat(numberOfTimesLockAcquired.get()).as("Unit lock only acquired once").isEqualTo(1);
    assertThat(numberOfTimesLockFailedAcquired.get()).as("Unit lock acquisition was blocked " + (numberOfThreads - 1) + " times").isEqualTo(numberOfThreads - 1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Unit(bwapi.Unit) Test(org.junit.Test)

Example 35 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class UnitsMutexTest method lockUnit_unitPreviouslyUnlocked_returnsTrue.

@Test
public void lockUnit_unitPreviouslyUnlocked_returnsTrue() {
    final Unit unit = UnitBuilder.mockUnit();
    assertThat(this.target.isUnitLocked(unit)).as("Sanity test that unit unlocked").isFalse();
    assertThat(this.target.lockUnit(unit)).as("lock acquired").isTrue();
    assertThat(this.target.isUnitLocked(unit)).isTrue();
}
Also used : Unit(bwapi.Unit) Test(org.junit.Test)

Aggregations

Unit (bwapi.Unit)73 GameState (ecgberht.GameState)36 Test (org.junit.Test)21 Position (bwapi.Position)14 TilePosition (bwapi.TilePosition)10 BaseLocation (bwta.BaseLocation)7 Pair (bwapi.Pair)6 UnitType (bwapi.UnitType)6 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Player (bwapi.Player)3 EnemyBuilding (ecgberht.EnemyBuilding)3 RaceUtils.createMockRace (BWJSAL.utils.RaceUtils.createMockRace)2 UnitTypeUtils.mockResourceDepotUnitType (BWJSAL.utils.UnitTypeUtils.mockResourceDepotUnitType)2 Race (bwapi.Race)2 Squad (ecgberht.Squad)2 Game (bwapi.Game)1 UnitCommand (bwapi.UnitCommand)1 Region (bwta.Region)1 Random (java.util.Random)1