Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
comment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethchen416 committed Jan 15, 2025
1 parent cadc077 commit fa07958
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,63 +1,89 @@
package mastermind;

// Import necessary classes from the mastermind.core package
import mastermind.core.Code;
import mastermind.core.CodeFactory;
// Import the Test annotation from JUnit for writing test methods
import org.junit.jupiter.api.Test;

// Import necessary classes from the Java standard library
import java.util.HashMap;
import java.util.List;

// Import static methods from JUnit for assertions and assumptions
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

class CodeTest {
// Test method to verify the generation of a random code
@Test
void testGenerateRandomCode() {
// Generate a random code using the CodeFactory
final Code randomCode = CodeFactory.getRandom();
// Define the expected length of the code based on the Mastermind game settings
final int expectedCodeLength = Mastermind.CODE_LENGTH;
// Get the list of colors from the generated random code
final var colorArray = randomCode.getColors();

// Assert that the length of the color array matches the expected code length
assertEquals(expectedCodeLength, colorArray.size());
}

// Test method to verify the creation of a code from a list of color indices
@Test
void testCodeFromIndices() {
// Create a code using the CodeFactory from a list of color indices
final Code code = CodeFactory.fromColorIndices(List.of(0, 1, 2, 3));
// Define the expected list of colors corresponding to the indices
final List<Code.Color> expectedColors = List.of(Code.Color.Green, Code.Color.Red, Code.Color.Blue,
Code.Color.Yellow);

// Assert that the colors of the created code match the expected colors
assertEquals(expectedColors, code.getColors());
}

// Test method to verify the retrieval of individual colors from a code
@Test
void testGetColor() {
// Create a code using the CodeFactory from a list of color indices
final Code code = CodeFactory.fromColorIndices(List.of(0, 1, 2, 3));
// Define the expected list of colors corresponding to the indices
final List<Code.Color> expectedColors = List.of(Code.Color.Green, Code.Color.Red, Code.Color.Blue,
Code.Color.Yellow);

// Loop through each index in the code
for (int i = 0; i < Mastermind.CODE_LENGTH; ++i) {
// Assert that the color at each index matches the expected color
assertEquals(expectedColors.get(i), code.getColor(i));
}
}

// Test method to verify the retrieval of the entire list of colors from a code
@Test
void testGetColors() {
// Create a code using the CodeFactory from a list of color indices
final Code code = CodeFactory.fromColorIndices(List.of(0, 1, 2, 3));
// Define the expected list of colors corresponding to the indices
final List<Code.Color> expectedColors = List.of(Code.Color.Green, Code.Color.Red, Code.Color.Blue,
Code.Color.Yellow);

// Assert that the colors of the created code match the expected colors
assertEquals(expectedColors, code.getColors());
}

// Test method to verify the occurrence count of each color in a code
@Test
void testGetOccurrences() {
// Create a code using the CodeFactory from a list of color indices
final Code code = CodeFactory.fromColorIndices(List.of(0, 1, 2, 3));
// Define the expected occurrences of each color in the code
HashMap<Code.Color, Integer> expectedOccurrences = new HashMap<>(4);
expectedOccurrences.put(Code.Color.Green, 1);
expectedOccurrences.put(Code.Color.Red, 1);
expectedOccurrences.put(Code.Color.Blue, 1);
expectedOccurrences.put(Code.Color.Yellow, 1);

// Assume that the occurrences of colors in the code match the expected occurrences
assumeTrue(code.getOccurrences().equals(expectedOccurrences));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mastermind;

// Import necessary classes from the mastermind.core package
import mastermind.core.Code;
import mastermind.core.CodeFactory;
import mastermind.core.Response;
Expand All @@ -12,115 +13,187 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class ResponseTest {
// Test method to verify the response when the guess is completely correct
@Test
void guessAllCorrect() {
// Create a code with all colors being the same
final Code code = CodeFactory.fromColorIndices(List.of(0, 0, 0, 0));
// Create a guess that matches the code exactly
final Code guess = CodeFactory.fromColorIndices(List.of(0, 0, 0, 0));
// Generate a response based on the code and the guess
final Response response = new Response(code, guess);
// Define the expected number of correct positions
final int expectedCorrectCount = 4;
// Define the expected number of misplaced colors
final int expectedMisplacementCount = 0;

// Assert that the number of correct positions matches the expected count
assertEquals(expectedCorrectCount, response.getResponse().first());
// Assert that the number of misplaced colors matches the expected count
assertEquals(expectedMisplacementCount, response.getResponse().second());
}

// Test method to verify the response when the guess is partially correct with no misplacements
@Test
void guessPartialCorrectNoMisplacement() {
// Create a code with distinct colors
final Code code = CodeFactory.fromColorIndices(List.of(0, 1, 2, 3));
// Create a guess that partially matches the code
final Code guess = CodeFactory.fromColorIndices(List.of(0, 1, 4, 5));
// Generate a response based on the code and the guess
final Response response = new Response(code, guess);
// Define the expected number of correct positions
final int expectedCorrectCount = 2;
// Define the expected number of misplaced colors
final int expectedMisplacementCount = 0;

// Assert that the number of correct positions matches the expected count
assertEquals(expectedCorrectCount, response.getResponse().first());
// Assert that the number of misplaced colors matches the expected count
assertEquals(expectedMisplacementCount, response.getResponse().second());
}

// Test method to verify the response when the guess has no correct or misplaced colors
@Test
void guessNoCorrectNoMisplacement() {
// Create a code with all colors being the same
final Code code = CodeFactory.fromColorIndices(List.of(0, 0, 0, 0));
// Create a guess with completely different colors
final Code guess = CodeFactory.fromColorIndices(List.of(1, 1, 1, 1));
// Generate a response based on the code and the guess
final Response response = new Response(code, guess);
// Define the expected number of correct positions
final int expectedCorrectCount = 0;
// Define the expected number of misplaced colors
final int expectedMisplacementCount = 0;

// Assert that the number of correct positions matches the expected count
assertEquals(expectedCorrectCount, response.getResponse().first());
// Assert that the number of misplaced colors matches the expected count
assertEquals(expectedMisplacementCount, response.getResponse().second());
}

// Test method to verify the response when the guess is partially correct with some misplacements
@Test
void guessPartialCorrectWithMisplacement() {
// Create a code with distinct colors
final Code code = CodeFactory.fromColorIndices(List.of(0, 1, 2, 3));
// Create a guess that partially matches the code with some misplacements
final Code guess = CodeFactory.fromColorIndices(List.of(0, 1, 3, 2));
// Generate a response based on the code and the guess
final Response response = new Response(code, guess);
// Define the expected number of correct positions
final int expectedCorrectCount = 2;
// Define the expected number of misplaced colors
final int expectedMisplacementCount = 2;

// Assert that the number of correct positions matches the expected count
assertEquals(expectedCorrectCount, response.getResponse().first());
// Assert that the number of misplaced colors matches the expected count
assertEquals(expectedMisplacementCount, response.getResponse().second());
}

// Test method to verify the response when all guessed colors are misplaced
@Test
void guessAllMisplacement() {
// Create a code with distinct colors
final Code code = CodeFactory.fromColorIndices(List.of(0, 1, 2, 3));
// Create a guess with all colors misplaced
final Code guess = CodeFactory.fromColorIndices(List.of(3, 2, 1, 0));
// Generate a response based on the code and the guess
final Response response = new Response(code, guess);
// Define the expected number of correct positions
final int expectedCorrectCount = 0;
// Define the expected number of misplaced colors
final int expectedMisplacementCount = 4;

// Assert that the number of correct positions matches the expected count
assertEquals(expectedCorrectCount, response.getResponse().first());
// Assert that the number of misplaced colors matches the expected count
assertEquals(expectedMisplacementCount, response.getResponse().second());
}

// Test method to verify the response when there are misplacements with duplicate colors
@Test
void guessAllMisplacementWithDuplicates() {
// Create a code with distinct colors
final Code code = CodeFactory.fromColorIndices(List.of(0, 1, 2, 3));
// Create a guess with duplicate colors and some misplacements
final Code guess = CodeFactory.fromColorIndices(List.of(3, 3, 3, 4));
// Generate a response based on the code and the guess
final Response response = new Response(code, guess);
// Define the expected number of correct positions
final int expectedCorrectCount = 0;
// Define the expected number of misplaced colors
final int expectedMisplacementCount = 1;

// Assert that the number of correct positions matches the expected count
assertEquals(expectedCorrectCount, response.getResponse().first());
// Assert that the number of misplaced colors matches the expected count
assertEquals(expectedMisplacementCount, response.getResponse().second());
}

// Test method to verify the response when the guess has one correct color and no displacements
@Test
void guessMisplacementIsCorrect() {
// Create a code with distinct colors
final Code code = CodeFactory.fromColorIndices(List.of(0, 1, 2, 3));
// Create a guess with one correct color and the rest incorrect
final Code guess = CodeFactory.fromColorIndices(List.of(0, 0, 0, 0));
// Generate a response based on the code and the guess
final Response response = new Response(code, guess);
// Define the expected number of correct positions
final int expectedCorrectCount = 1;
// Define the expected number of misplaced colors
final int expectedMisplacementCount = 0;

// Assert that the number of correct positions matches the expected count
assertEquals(expectedCorrectCount, response.getResponse().first());
// Assert that the number of misplaced colors matches the expected count
assertEquals(expectedMisplacementCount, response.getResponse().second());
}

// Test method to verify the creation of a response from a tuple
@Test
void responseFromTuple() {
// Create a tuple with counts of correct and misplaced colors
final Tuple2<Integer, Integer> counts = new Tuple2<>(1, 2);
// Generate a response based on the tuple
final Response response = new Response(counts);

// Assert that the response matches the tuple
assertEquals(counts, response.getResponse());
}

// Test method to verify the equality of response objects
@Test
void equals() {
// Create response objects with different tuples
final Response response1 = new Response(new Tuple2<>(1, 2));
final Response response2 = new Response(new Tuple2<>(1, 2));
final Response response3 = new Response(new Tuple2<>(2, 1));

// Assert that response1 equals response2 and vice versa
assertEquals(response1, response2);
assertEquals(response2, response1);
// Assert that response1 equals itself
assertEquals(response1, response1);
// Assert that response2 equals itself
assertEquals(response2, response2);
// Assert that the hash codes of response1 and response2 are equal
assertEquals(response1.hashCode(), response2.hashCode());

// Assert that response1 does not equal response3
assertNotEquals(response1, response3);
// Assert that response2 does not equal response3
assertNotEquals(response2, response3);
// Assert that response3 does not equal response1
assertNotEquals(response3, response1);
// Assert that response3 does not equal response2
assertNotEquals(response3, response2);
// Assert that the hash codes of response1 and response3 are not equal
assertNotEquals(response1.hashCode(), response3.hashCode());
// Assert that the hash codes of response2 and response3 are not equal
assertNotEquals(response2.hashCode(), response3.hashCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,60 @@
class DonaldKnuthAlgorithmTest {
@Test
void testFirstGuess() {
// Create an instance of the DonaldKnuthAlgorithm solver
final DonaldKnuthAlgorithm solver = new DonaldKnuthAlgorithm();

// Get the first guess from the solver
final Code firstGuess = solver.guess();

// Define the expected first guess based on the algorithm's strategy
final Code expectedFirstGuess = CodeFactory.fromColorIndices(List.of(0, 0, 1, 1));

// Assert that the first guess matches the expected first guess
assertEquals(expectedFirstGuess.getColors(), firstGuess.getColors());
}

@Test
void testAllGuesses() {
final int possibilities = (int) Math.pow(
Mastermind.TOTAL_COLORS, Mastermind.CODE_LENGTH);
// Calculate the total number of possible codes based on the number of colors and code length
final int possibilities = (int) Math.pow(Mastermind.TOTAL_COLORS, Mastermind.CODE_LENGTH);

// Iterate over all possible codes
for (int i = 0; i < possibilities; ++i) {
final List<Integer> colorIndices = MathUtil.digitsFromBase(
i, Mastermind.TOTAL_COLORS, Mastermind.CODE_LENGTH);
// Convert the current index to a list of color indices representing a code
final List<Integer> colorIndices = MathUtil.digitsFromBase(i, Mastermind.TOTAL_COLORS, Mastermind.CODE_LENGTH);

// Create a Code object from the list of color indices
final Code secretCode = CodeFactory.fromColorIndices(colorIndices);

// Test the solver's ability to guess the secret code
testGuess(secretCode);
}
}

void testGuess(final Code secretCode) {
// Create an instance of the DonaldKnuthAlgorithm solver
final DonaldKnuthAlgorithm solver = new DonaldKnuthAlgorithm();

Tuple2<Status, Code> result =
new Tuple2<>(Status.Continue, solver.guess());
// Initialize the result with a status of Continue and the first guess from the solver
Tuple2<Status, Code> result = new Tuple2<>(Status.Continue, solver.guess());

// Loop until the solver either solves the code or fails
while (result.first() == Status.Continue) {
// Generate a response (hints) based on the secret code and the solver's guess
final Response hints = new Response(secretCode, result.second());

// Get the next guess from the solver based on the hints
result = solver.guess(hints);
}

// If the solver fails to solve the code, fail the test
if (result.first() == Status.Lose) {
fail("Donald Knuth algorithm failed to solve secret code: " + secretCode);
}

// Donald Knuth algorithm relies on a minimax technique, where the
// score of each potential guess is calculated, and the most number
// of guesses it would make to solve the code is 5.
// However, on the event where there's a tie in the scores, the
// algorithm may make up to 6 guesses.
// On top of that, solver.getAttempts() is incremented after each
// guess. So if the algorithm makes 6 guesses, after performing the
// last one, solver.getAttempts() would return 7.
// Assert that the solver solved the code within the allowed number of attempts
// The algorithm should solve the code in at most 6 guesses, but due to the way attempts are counted, it may return 7
assertTrue(solver.getAttempts() <= 7);
}
}

0 comments on commit fa07958

Please sign in to comment.