Skip to content

Commit

Permalink
Extract helper methods for review by Bombe
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneBab committed Jan 30, 2024
1 parent 1bb428e commit 52b2d77
Showing 1 changed file with 157 additions and 145 deletions.
302 changes: 157 additions & 145 deletions test/freenet/client/async/HealingDecisionSupplierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,176 +11,188 @@ public class HealingDecisionSupplierTest {

@Test
public void healingAlwaysTriggersForDarknet() {
for (double i : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.1,
() -> false,
() -> i);
assertThat(
String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.5),
Matchers.equalTo(true));
}
for (double randomValue : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
assertHeals(
getHealingDecisionSupplier(0.1, false, randomValue),
0.5,
randomValue);
}
}

@Test
public void healingAlwaysAcceptsAKeyAtTheNodeLocation() {
for (double i : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.1,
() -> true,
() -> i);
assertThat(
String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.1),
Matchers.equalTo(true));
}
for (double randomValue : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
assertHeals(
getHealingDecisionSupplier(0.1, true, randomValue),
0.1,
randomValue);
}
}

@Test
public void healingAccepts70PercentOfKeysInShortDistance() {
for (double i : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.1,
() -> true,
() -> i);
assertThat(String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.11),
Matchers.equalTo(true));
}
for (double i : Arrays.asList(0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.1,
() -> true,
() -> i);
assertThat(String.format("Healing does not trigger at random value %g", i),
healingDecisionSupplier.shouldHeal(0.11),
Matchers.equalTo(false));
}
for (double randomValue : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4)) {
assertHeals(
getHealingDecisionSupplier(0.1, true, randomValue),
0.11,
randomValue);
}
for (double randomValue : Arrays.asList(0.3, 0.2, 0.1, 0.00000001)) {
assertDoesNotHeal(
getHealingDecisionSupplier(0.1, true, randomValue),
0.11,
randomValue);
}
}

@Test
public void healingAccepts50PercentOfKeysAtTheLimitOfLongDistance() {
for (double i : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.1,
() -> true,
() -> i);
assertThat(String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.1999),
Matchers.equalTo(true));
}
for (double i : Arrays.asList(0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.1,
() -> true,
() -> i);
assertThat(String.format("Healing does not trigger random value %g", i),
healingDecisionSupplier.shouldHeal(0.1999),
Matchers.equalTo(false));
}
for (double randomValue : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6)) {
assertHeals(
getHealingDecisionSupplier(0.1, true, randomValue),
0.1999,
randomValue);
}
for (double randomValue : Arrays.asList(0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
assertDoesNotHeal(
getHealingDecisionSupplier(0.1, true, randomValue),
0.1999,
randomValue);
}
}

@Test
public void healingAccepts50PercentOfKeysAtTheLimitOfLongDistanceAtADifferentNodeLocationToo() {
for (double i : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.6,
() -> true,
() -> i);
assertThat(String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.6999),
Matchers.equalTo(true));
assertThat(String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.5001),
Matchers.equalTo(true));
}
for (double i : Arrays.asList(0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.6,
() -> true,
() -> i);
assertThat(String.format("Healing does not trigger random value %g", i),
healingDecisionSupplier.shouldHeal(0.6999),
Matchers.equalTo(false));
assertThat(String.format("Healing does not trigger random value %g", i),
healingDecisionSupplier.shouldHeal(0.5001),
Matchers.equalTo(false));
}
for (double randomValue : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6)) {
HealingDecisionSupplier healingDecisionSupplier = getHealingDecisionSupplier(
0.6,
true,
randomValue);
assertHeals(
healingDecisionSupplier,
0.6999,
randomValue);
assertHeals(
healingDecisionSupplier,
0.5001,
randomValue);
}
for (double randomValue : Arrays.asList(0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = getHealingDecisionSupplier(
0.6,
true,
randomValue);
assertDoesNotHeal(
healingDecisionSupplier,
0.1999,
randomValue);
assertDoesNotHeal(
healingDecisionSupplier,
0.5001,
randomValue);
}
}

@Test
public void healingAccepts50PercentOfKeysAtTheLimitOfLongDistanceAroundZero() {
for (double i : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.0001,
() -> true,
() -> i);
assertThat(String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.1),
Matchers.equalTo(true));
assertThat(String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.9002),
Matchers.equalTo(true));
} for (double i : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.9999,
() -> true,
() -> i);
assertThat(String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.0998),
Matchers.equalTo(true));
assertThat(String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.9000),
Matchers.equalTo(true));
}
for (double i : Arrays.asList(0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.0001,
() -> true,
() -> i);
assertThat(String.format("Healing does not trigger random value %g", i),
healingDecisionSupplier.shouldHeal(0.1),
Matchers.equalTo(false));
assertThat(String.format("Healing does not trigger random value %g", i),
healingDecisionSupplier.shouldHeal(0.9002),
Matchers.equalTo(false));
}
for (double i : Arrays.asList(0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.9999,
() -> true,
() -> i);
assertThat(String.format("Healing does not trigger random value %g", i),
healingDecisionSupplier.shouldHeal(0.0998),
Matchers.equalTo(false));
assertThat(String.format("Healing does not trigger random value %g", i),
healingDecisionSupplier.shouldHeal(0.9000),
Matchers.equalTo(false));
}
for (double randomValue : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6)) {
HealingDecisionSupplier healingDecisionSupplier = getHealingDecisionSupplier(
0.0001,
true,
randomValue);
assertHeals(
healingDecisionSupplier,
0.1,
randomValue);
assertHeals(
healingDecisionSupplier,
0.9002,
randomValue);
}
for (double randomValue : Arrays.asList(1.0, 0.9, 0.8, 0.7, 0.6)) {
HealingDecisionSupplier healingDecisionSupplier = getHealingDecisionSupplier(
0.9999,
true,
randomValue);
assertHeals(
healingDecisionSupplier,
0.0998,
randomValue);
assertHeals(
healingDecisionSupplier,
0.9000,
randomValue);
}
for (double randomValue : Arrays.asList(0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = getHealingDecisionSupplier(
0.0001,
true,
randomValue);
assertDoesNotHeal(
healingDecisionSupplier,
0.1,
randomValue);
assertDoesNotHeal(
healingDecisionSupplier,
0.9002,
randomValue);
}
for (double randomValue : Arrays.asList(0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = getHealingDecisionSupplier(
0.9999,
true,
randomValue);
assertDoesNotHeal(
healingDecisionSupplier,
0.0998,
randomValue);
assertDoesNotHeal(
healingDecisionSupplier,
0.9000,
randomValue);
}
}

@Test
public void healingAccepts10PercentOfKeysAtLongDistance() {
for (double i : Arrays.asList(1.0, 0.91)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.1,
() -> true,
() -> i);
assertThat(String.format("Healing triggers at random value %g", i),
healingDecisionSupplier.shouldHeal(0.21),
Matchers.equalTo(true));
}
for (double i : Arrays.asList(0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> 0.1,
() -> true,
() -> i);
assertThat(String.format("Healing does not trigger at random value %g", i),
healingDecisionSupplier.shouldHeal(0.21),
Matchers.equalTo(false));
}
for (double randomValue : Arrays.asList(1.0, 0.91)) {
assertHeals(
getHealingDecisionSupplier(0.1, true, randomValue),
0.21,
randomValue);
}
for (double randomValue : Arrays.asList(0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.00000001)) {
assertDoesNotHeal(
getHealingDecisionSupplier(0.1, true, randomValue),
0.21,
randomValue);
}
}

private static HealingDecisionSupplier getHealingDecisionSupplier(double nodeLocation, boolean isOpennet, double randomValue) {
HealingDecisionSupplier healingDecisionSupplier = new HealingDecisionSupplier(
() -> nodeLocation,
() -> isOpennet,
() -> randomValue);
return healingDecisionSupplier;
}

private static void assertHeals(
HealingDecisionSupplier healingDecisionSupplier,
double keyLocation, double randomValue) {
assertThat(
String.format("Healing triggers at random value %g", randomValue),
healingDecisionSupplier.shouldHeal(keyLocation),
Matchers.equalTo(true));
}

private static void assertDoesNotHeal(
HealingDecisionSupplier healingDecisionSupplier,
double keyLocation, double randomValue) {
assertThat(
String.format("Healing does not trigger at random value %g", randomValue),
healingDecisionSupplier.shouldHeal(keyLocation),
Matchers.equalTo(false));
}

}

0 comments on commit 52b2d77

Please sign in to comment.