Update Print2DArraysTest.java

This commit is contained in:
Neetika23 2023-12-08 12:07:28 +05:30 committed by GitHub
parent 990a4812dc
commit 3210b24038
1 changed files with 24 additions and 10 deletions

View File

@ -5,31 +5,45 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class ArrayPrinterTest {
@SystemStub
private SystemOut systemOut;
@Test
public void testPrint2DNested() {
void whenPrint2D_thenUseNested() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
String expectedOutput = "1 2 3 \n4 5 6 \n7 8 9 \n";
assertEquals(expectedOutput, ArrayPrinter.print2DNested(myArray));
assertThat(systemOut.getLines()).containsExactly(expectedOutput);
}
@Test
public void testPrint2DStream() {
public void whenPrint2D_thenUseStream() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
String expectedOutput = "1 2 3 4 5 6 7 8 9 ";
assertEquals(expectedOutput, ArrayPrinter.print2DStream(myArray));
Arrays.stream(array) .flatMapToInt(Arrays::stream) .forEach(num -> System.out.print(num + " "));
String expectedOutput = "1 2 3 4 5 6 7 8 9";
assertThat(systemOut.getLines()).containsExactly(expectedOutput);
}
@Test
public void testPrint2DDeepToString() {
public void whenPrint2D_thenUseDeepToString() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
String expectedOutput = "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n";
assertEquals(expectedOutput, ArrayPrinter.print2DDeepToString(myArray));
System.out.println(Arrays.deepToString(myArray));
String expectedOutput = "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]";
assertThat(systemOut.getLines()).containsExactly(expectedOutput);
}
@Test
public void testPrintArrayString() {
public void whenPrint2D_thenUseDeepToString() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int[] row : myArray) {
System.out.println(Arrays.toString(row));
}
String expectedOutput = "[1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]\n";
assertEquals(expectedOutput, ArrayPrinter.printArrayString(myArray));
assertThat(systemOut.getLines()).containsExactly(expectedOutput);
}
}