/
javapractice
/
JavaPractice
Обзор
Документация
Войти
/
javapractice
/
JavaPractice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
2
CI/CD
Аналитика
develop
CodeRun/src/test/java/quickStart/QuickStartExercise09Test.java
152 строки
4 KB
Кузьма Даждъбогин
TSKJVPRCTC5-15
15 янв 2026, 15:10
15 янв 2026, 15:10
e546175
Код
Авторство
О чём код?
package quickStart; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.PrintStream; import java.nio.charset.StandardCharsets; import static org.junit.jupiter.api.Assertions.assertEquals; class QuickStartExercise09Test { private final InputStream systemIn = System.in; private final PrintStream systemOut = System.out; private ByteArrayInputStream testIn; private ByteArrayOutputStream testOut; @BeforeEach void setUp() { testOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(testOut, true, StandardCharsets.UTF_8)); } @AfterEach void tearDown() { System.setIn(systemIn); System.setOut(systemOut); } void provideInput(String data) { testIn = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)); System.setIn(testIn); } String getOutput() { return testOut.toString(StandardCharsets.UTF_8).trim(); } @Test void method_shouldMultiply2x3And3x2Matrices() throws Exception { // Given: A (2x3), B (3x2) String input = "2 3 2\n" + "1 2 3\n" + "4 5 6\n" + "7 8\n" + "9 10\n" + "11 12"; String expectedOutput = "58 139\n" + "64 154"; provideInput(input); // When QuickStartExercise09 q = new QuickStartExercise09(); q.method(); // Then assertEquals(expectedOutput, getOutput()); } @Test void method_shouldMultiply1x1Matrices() throws Exception { // Given String input = "1 1 1\n" + "5\n" + "3"; String expectedOutput = "15"; provideInput(input); // When QuickStartExercise09 q = new QuickStartExercise09(); q.method(); // Then assertEquals(expectedOutput, getOutput()); } @Test void method_shouldMultiply2x2MatricesAndOutputByColumns() throws Exception { // Given: A = [[1,0],[0,1]], B = [[2,3],[4,5]] → C = [[2,3],[4,5]] // Output by columns: "2 4\n3 5" String input = "2 2 2\n" + "1 0\n" + "0 1\n" + "2 3\n" + "4 5"; String expectedOutput = "2 4\n" + "3 5"; provideInput(input); // When QuickStartExercise09 q = new QuickStartExercise09(); q.method(); // Then assertEquals(expectedOutput, getOutput()); } @Test void method_shouldHandleIdentityMultiplication() throws Exception { // Given: A = [[2,3],[4,5]], B = [[1,0],[0,1]] → C = A String input = "2 2 2\n" + "2 3\n" + "4 5\n" + "1 0\n" + "0 1"; String expectedOutput = "2 4\n" + "3 5"; provideInput(input); // When QuickStartExercise09 q = new QuickStartExercise09(); q.method(); // Then assertEquals(expectedOutput, getOutput()); } @Test void method_shouldHandleZeroMatrixMultiplication() throws Exception { // Given: A filled with 0 String input = "2 2 2\n" + "0 0\n" + "0 0\n" + "1 2\n" + "3 4"; String expectedOutput = "0 0\n" + "0 0"; provideInput(input); // When QuickStartExercise09 q = new QuickStartExercise09(); q.method(); // Then assertEquals(expectedOutput, getOutput()); } }