Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UctNode and MoveMaker Tests #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

Rette66
Copy link

@Rette66 Rette66 commented Nov 13, 2023

Description

Fixed the flaky test testConstructionOfNodeWithNoChildren inside the UctNodeTest class.
Fixed the flaky test testCrazyP1Combo testFinalPlayer1MoveInHomeBin testFinalPlayer1MoveOnPlayersSide testFinalPlayer2MoveInHomeBin testFinalPlayer2MoveOnPlayersSide testPlayer1CapturingMove testPlayer2CapturingMove inside the MoveMakerTest class.

Root Cause

UctNodeTest
The test testConstructionOfNodeWithNoChildren has been reported as flaky when run with the NonDex tool. The test failed because it tries to compare two strings, but the method uctNode.getAttributes() returns a NodeAttribute Object.

assertEquals("Unexpected attrs", "{wins=0.0, visits=0, winRate=0.50122}", uctNode.getAttributes().toString());

public NodeAttributes getAttributes() {
NodeAttributes attributes = new NodeAttributes();
attributes.put("visits", Integer.toString(numVisits));
attributes.put("wins", Float.toString(numWins));
attributes.put("winRate", FormatUtil.formatNumber(getWinRate()));
return attributes;
}

However, the NodeAttribute class extends from Java Hashmap.

public class NodeAttributes extends HashMap<String, String> {

Java Hashmap does not guarantee the order of elements in it. Therefore, it fails when applying toString() and is compared with a hardcoded string.

MoveMakerTest
All tests I mentioned at the beginning has been reported as flaky when run with the NonDex tool. These tests failed because they tried to compare two strings. They all have the same root cause, so I will use testFinalPlayer1MoveInHomeBin for instance.

assertEquals("Unexpected captures.",
"{(row=2, column=2)=3, (row=2, column=3)=3, (row=2, column=4)=3, (row=2, column=5)=3, " +
"(row=2, column=6)=3, (row=2, column=7)=3}",
move.getCaptures().toString());

public Captures getCaptures() {
return captures;
}

private Captures captures = new Captures();

Method move.getCaptures() will return a Capture Object as inferred by the codes above. However, Capture Object is a Class that extends from Java HashMap.

public class Captures extends HashMap<Location, Byte> {

Java Hashmap does not guarantee the order of elements in it. Therefore, the test fails when applying toString() to a Capture object and compares it with a hardcoded string.

Fix

UctNodeTest
Test in this class is fixed by adding a temporary NodeAttribute tempAttr and putting information in the hard-coded string into this tempAttr. Then compare tempAttr with uctNode.getAttributes(). Since these two are all NodeAttribute types, assertEquals can apply to them.

MoveMakerTest
Tests in this class are fixed by adding a temporary Capture tempCap and putting information in the hard-coded string into this tempCap. Then compare tempCap with move.getCaptures(). Since these two are all Capture types, assertEquals can apply to them.

How has this been tested?

  1. Regular test - Successful
    Command used -
./gradlew --info test --tests com.barrybecker4.game.twoplayer.common.search.strategy.UctNodeTest.testConstructionOfNodeWithNoChildren

for UctNodetTest class

Command used -

./gradlew --info test --tests com.barrybecker4.game.twoplayer.mancala.move.MoveMakerTest.Specific_Test_Name_Here

for MoveMakerTest class
Note: replace Specific_Test_Name_Here with the test names mentioned in the beginning.

  1. NonDex test - Failed
    Command used -
./gradlew --info nondexTest --tests=com.barrybecker4.game.twoplayer.common.search.strategy.UctNodeTest.testConstructionOfNodeWithNoChildren --nondexRuns=X

for UctNodetTest class

Command used -

./gradlew --info nondexTest --tests=com.barrybecker4.game.twoplayer.mancala.move.MoveMakerTest.Specific_Test_Name_Here --nondexRuns=X

for UctNodetTest class

Note: replace Specific_Test_Name_Here with the test names mentioned in the beginning.
Replace X with an int number.
The NonDex test passed after the fix.

@Rette66
Copy link
Author

Rette66 commented Nov 30, 2023

Hi, @barrybecker4 could you please review this Pull Request? I found the tests mentioned in this PR are flaky and I found a way to fix them.

Please let me know if you need additional details. Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant