This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cadc077
commit fa07958
Showing
3 changed files
with
124 additions
and
16 deletions.
There are no files selected for viewing
28 changes: 27 additions & 1 deletion
28
culminating-mastermind/app/src/test/java/mastermind/CodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters