diff --git a/pom.xml b/pom.xml index de103cf..4c34655 100644 --- a/pom.xml +++ b/pom.xml @@ -4,6 +4,13 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + + + central + https://repo.maven.apache.org/maven2 + + + ru.otus.java.basic otus-java-basic 1.0-SNAPSHOT @@ -14,4 +21,18 @@ UTF-8 - \ No newline at end of file + + + org.junit.jupiter + junit-jupiter + 5.10.2 + test + + + org.assertj + assertj-core + 3.27.3 + test + + + diff --git a/src/main/java/ru/otus/java/basic/homeworks/homework22/Application.java b/src/main/java/ru/otus/java/basic/homeworks/homework22/Application.java new file mode 100644 index 0000000..e0cc635 --- /dev/null +++ b/src/main/java/ru/otus/java/basic/homeworks/homework22/Application.java @@ -0,0 +1,52 @@ +package ru.otus.java.basic.homeworks.homework22; + +import java.util.Arrays; + + +public class Application { + + public static void main(String[] args) { + int[] array = new int[]{0, 1, 1, 2, 3, 5, 8}; + + System.out.println(Arrays.toString(array)); + System.out.println(Arrays.toString(createNewArrayAfterLastOne(array))); + + System.out.println(checkArray(new int[]{1, 2})); + System.out.println(checkArray(new int[]{1, 1})); + System.out.println(checkArray(new int[]{1, 3})); + System.out.println(checkArray(new int[]{1, 2, 2, 1})); + } + + public static int[] createNewArrayAfterLastOne(int[] array) throws RuntimeException { + if (array == null) { + return null; + } + + for (int i = array.length - 1; i >= 0; i--) { + if (array[i] == 1) { + return Arrays.copyOfRange(array, ++i, array.length); + } + } + throw new RuntimeException(); + } + + public static boolean checkArray(int[] array) { + if (array == null) { + return false; + } + + boolean existsOne = false; + boolean existsTwo = false; + + for (int element : array) { + if (element == 1) { + existsOne = true; + } else if (element == 2) { + existsTwo = true; + } else { + return false; + } + } + return existsOne && existsTwo; + } +} \ No newline at end of file diff --git a/src/test/java/ru/otus/java/basic/homeworks/homework22/ApplicationTest.java b/src/test/java/ru/otus/java/basic/homeworks/homework22/ApplicationTest.java new file mode 100644 index 0000000..39c5415 --- /dev/null +++ b/src/test/java/ru/otus/java/basic/homeworks/homework22/ApplicationTest.java @@ -0,0 +1,55 @@ +package ru.otus.java.basic.homeworks.homework22; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; +class ApplicationTest { + + public static Stream getArraysForCreateNewArray() { + List out = new ArrayList<>(); + out.add(Arguments.of(new int[] {0,1,1,2,3,5,8}, new int[] {0,2,3,5,8})); + return out.stream(); + } + + public static Stream getArraysForCheck() { + List out = new ArrayList<>(); + out.add(Arguments.of(true, new int[] {1,2})); + out.add(Arguments.of(false, new int[] {1,1})); + out.add(Arguments.of(false, new int[] {1,3})); + out.add(Arguments.of(true, new int[] {1,2,2,1})); + return out.stream(); + } + + @Disabled + @ParameterizedTest + @MethodSource("getArraysForCreateNewArray") + void arrayAfterLastOne(int[] arrayEquals, int[] arrayException) { + Assertions.assertArrayEquals(new int[] {2,3,5,8}, Application.createNewArrayAfterLastOne(arrayEquals)); + Assertions.assertThrowsExactly(RuntimeException.class, ()-> Application.createNewArrayAfterLastOne(arrayException)); + } + + @Test + void arrayAfterLastOne() { + Assertions.assertAll( + ()-> { + Assertions.assertArrayEquals(new int[] {2,3,5,8}, Application.createNewArrayAfterLastOne(new int[] {0,1,1,2,3,5,8})); + }, + ()-> { + Assertions.assertThrowsExactly(RuntimeException.class, ()-> Application.createNewArrayAfterLastOne(new int[] {0,2,3,5,8})); + } + ); + } + + @ParameterizedTest + @MethodSource("getArraysForCheck") + void checkArray(boolean result, int[] array) { + Assertions.assertEquals(result, Application.checkArray(array)); + } +} \ No newline at end of file