Skip to content

Commit

Permalink
Add tests for workarounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
asofold committed Jan 17, 2016
1 parent 8a70ad9 commit db46209
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 3 deletions.
Expand Up @@ -74,7 +74,8 @@ public IAcceptDenyCounter getAllTimeCounter() {

/**
* Override the parent counters in instance with the ones set in this
* instance, if possible.
* instance, where feasible, if possible. (By default, only the
* allTimeCounter is checked for a parent.)
*
* @param instance
* @return The given instance.
Expand Down
Expand Up @@ -67,7 +67,7 @@ public IAcceptDenyCounter getStageCounter() {
}

@Override
public boolean testUse(boolean isUse) {
public boolean testUse(final boolean isUse) {
if (!isUse) {
return currentCount > 0;
}
Expand Down
Expand Up @@ -48,7 +48,7 @@ public void testAcceptDenyCounter() {
checkCounts(leaf, 73, 65, "leaf");
}

public void checkCounts(IAcceptDenyCounter counter, int acceptCount, int denyCount, String counterName) {
public static void checkCounts(IAcceptDenyCounter counter, int acceptCount, int denyCount, String counterName) {
if (counter.getAcceptCount() != acceptCount) {
fail("Wrong accept count for counter '" + counterName + "': " + counter.getAcceptCount() + " instead of " + acceptCount + ".");
}
Expand All @@ -57,4 +57,27 @@ public void checkCounts(IAcceptDenyCounter counter, int acceptCount, int denyCou
}
}

public static void checkSame(String testName, IAcceptDenyCounter... counters) {
if (counters.length < 2) {
return;
}
IAcceptDenyCounter first = counters[0];
for (int i = 1; i < counters.length; i++) {
if (first.getAcceptCount() != counters[i].getAcceptCount()) {
fail("Accept count differs at index " + i + ": " + testName);
}
if (first.getDenyCount() != counters[i].getDenyCount()) {
fail("Deny count differs at index " + i + ": " + testName);
}
}

}

public static void checkSame(int acceptCount, int denyCount, String testName, IAcceptDenyCounter... counters) {
for (int i = 0; i < counters.length; i++) {
checkCounts(counters[i], acceptCount, denyCount, "counter at index " + i + " / " + testName);
}
checkSame(testName, counters);
}

}
@@ -0,0 +1,115 @@
package fr.neatmonster.nocheatplus.test;

import static org.junit.Assert.*;

import org.junit.Test;

import fr.neatmonster.nocheatplus.utilities.ds.count.acceptdeny.AcceptDenyCounter;
import fr.neatmonster.nocheatplus.utilities.ds.count.acceptdeny.ICounterWithParent;
import fr.neatmonster.nocheatplus.workaround.IWorkaround;
import fr.neatmonster.nocheatplus.workaround.WorkaroundCountDown;
import fr.neatmonster.nocheatplus.workaround.WorkaroundCounter;

public class TestWorkarounds {

/**
* Simple isolated testing for one WorkaroundCounter instance, plus parent
* count.
*/
@Test
public void testWorkaroundCounter() {
WorkaroundCounter wac = new WorkaroundCounter("test.wac");
AcceptDenyCounter pc = new AcceptDenyCounter();
((ICounterWithParent) wac.getAllTimeCounter()).setParentCounter(pc);

for (int i = 0; i < 57; i++) {
checkCanUseAndUse(wac);
}
TestAcceptDenyCounters.checkSame(57, 0, "WorkaroundCounter(c/p)", wac.getAllTimeCounter(), pc);
}

/**
* Simple isolated testing for one WorkaroundCountDown instance, plus parent
* count.
*/
@Test
public void testWorkaroundCountDown() {
WorkaroundCountDown wacd = new WorkaroundCountDown("test.wacd", 1);
AcceptDenyCounter pc = new AcceptDenyCounter();
((ICounterWithParent) wacd.getAllTimeCounter()).setParentCounter(pc);

// Attempt to use a lot of times (all but one get denied).
for (int i = 0; i < 141; i++) {
checkCanUseAndUse(wacd);
}
int accept = 1; // All time count.
int deny = 140; // All time count.
TestAcceptDenyCounters.checkSame(accept, deny, "Just use(s/a/p)", wacd.getStageCounter(), wacd.getAllTimeCounter(), pc);

// Reset.
wacd.resetConditions();
TestAcceptDenyCounters.checkSame(accept, deny, "Just use(s/a/p)", wacd.getAllTimeCounter(), pc);
TestAcceptDenyCounters.checkCounts(wacd.getStageCounter(), 0, 0, "test.wacd.stage");

// Attempt to use a lot of times (all but one get denied).
for (int i = 0; i < 141; i++) {
checkCanUseAndUse(wacd);
}
accept *= 2;
deny *= 2;
TestAcceptDenyCounters.checkSame(accept, deny, "Just use(s/a/p)", wacd.getAllTimeCounter(), pc);
TestAcceptDenyCounters.checkCounts(wacd.getStageCounter(), 1, 140, "test.wacd.stage");

// Set to 5 and use (5xaccept).
wacd.resetConditions();
wacd.setCurrentCount(5);
for (int i = 0; i < 141; i++) {
checkCanUseAndUse(wacd);
}
accept += 5;
deny += 141 - 5;
TestAcceptDenyCounters.checkSame(accept, deny, "Just use(s/a/p)", wacd.getAllTimeCounter(), pc);
TestAcceptDenyCounters.checkCounts(wacd.getStageCounter(), 5, 141 - 5, "test.wacd.stage");

// Set to -1 and use.
wacd.resetConditions();
wacd.setCurrentCount(-1);
for (int i = 0; i < 141; i++) {
checkCanUseAndUse(wacd);
}
deny += 141;
TestAcceptDenyCounters.checkSame(accept, deny, "Just use(s/a/p)", wacd.getAllTimeCounter(), pc);
TestAcceptDenyCounters.checkCounts(wacd.getStageCounter(), 0, 141, "test.wacd.stage");

// Set to 14 and use (14xaccept).
wacd.resetConditions();
wacd.setCurrentCount(14);
for (int i = 0; i < 141; i++) {
checkCanUseAndUse(wacd);
}
accept += 14;
deny += 141 - 14;
TestAcceptDenyCounters.checkSame(accept, deny, "Just use(s/a/p)", wacd.getAllTimeCounter(), pc);
TestAcceptDenyCounters.checkCounts(wacd.getStageCounter(), 14, 141 - 14, "test.wacd.stage");

}

// TODO: Test SimpleWorkaroundRegistry and WorkaroundSet (all sorts of tests. Consistency for workarounds and counters.).

/**
* Check consistency of results of canUse and use called in that order.
*
* @param workaround
* @return Result of use().
*/
public static boolean checkCanUseAndUse(IWorkaround workaround) {
boolean preRes = workaround.canUse();
boolean res = workaround.use();
if (!preRes && res) {
fail("Inconsistency: use() must not return true, if canUse() has returned false.");
}

return res;
}

}

0 comments on commit db46209

Please sign in to comment.