Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>

<groupId>ru.otus.java.basic</groupId>
<artifactId>otus-java-basic</artifactId>
<version>1.0-SNAPSHOT</version>
Expand All @@ -14,4 +21,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Arguments> getArraysForCreateNewArray() {
List<Arguments> 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<Arguments> getArraysForCheck() {
List<Arguments> 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));
}
}