migration
This commit is contained in:
2
semester-4/ОПJа/lb-1/README.md
Normal file
2
semester-4/ОПJа/lb-1/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
Викладач: Колесников Д. О.
|
||||
Оцінка: 100
|
22
semester-4/ОПJа/lb-1/task1/part1.sh
Executable file
22
semester-4/ОПJа/lb-1/task1/part1.sh
Executable file
@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
|
||||
ROOT='src/main/java'
|
||||
|
||||
JAVA_FILE="ua/nure/jfn/task1/Part1.java"
|
||||
CLASS_FILE="ua/nure/jfn/task1/Part1.class"
|
||||
|
||||
CLASS="ua.nure.jfn.task1.Part1"
|
||||
|
||||
if [ ! -r "$ROOT/$JAVA_FILE" ]; then
|
||||
echo "File '$ROOT/$JAVA_FILE' is not found or it is not readable." 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
javac "$ROOT/$JAVA_FILE"
|
||||
|
||||
if [ ! -r "$ROOT/$CLASS_FILE" ]; then
|
||||
echo "Can't find '$ROOT/$CLASS_FILE' after compilation." 1>&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
java -cp "$ROOT" "$CLASS"
|
58
semester-4/ОПJа/lb-1/task1/pom.xml
Normal file
58
semester-4/ОПJа/lb-1/task1/pom.xml
Normal file
@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
|
||||
<groupId>ua.nure.jfn</groupId>
|
||||
<artifactId>task1</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
<junit5.version>5.12.0</junit5.version>
|
||||
<surefire.version>3.5.2</surefire.version>
|
||||
<spoon.version>11.2.0</spoon.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit5.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>${junit5.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>fr.inria.gforge.spoon</groupId>
|
||||
<artifactId>spoon-core</artifactId>
|
||||
<version>${spoon.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>2.0.17</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>task1</finalName>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,26 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
public class Demo {
|
||||
|
||||
private static void printSep(int k) {
|
||||
System.out.printf("%nPart%d =================%n", k);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
printSep(1);
|
||||
Part1.main(new String[] { "A1", "B11 22 C", "D" }); // 34 7
|
||||
|
||||
printSep(2);
|
||||
Part2.main(new String[] { "12", "18", "24" }); // 6 72
|
||||
|
||||
printSep(3);
|
||||
Part3.main(new String[] { "5" }); // 3 5 7 11 13
|
||||
|
||||
printSep(4);
|
||||
Part4.main(new String[] { "360" }); // 360 = 2^3 x 3^2 x 5^1
|
||||
|
||||
printSep(5);
|
||||
Part5.main(new String[] { "1", "5", "3", "0", "5", "2", "1" }); // 5 3 0 5 2 1
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
public class Part1 {
|
||||
public static void main(String[] args) {
|
||||
int numbersSum = 0;
|
||||
int digitsSum = 0;
|
||||
|
||||
for (String arg : args) {
|
||||
int digitPlace = 0;
|
||||
char[] chArray = arg.toCharArray();
|
||||
for (int i = chArray.length - 1; i >= 0; i--) {
|
||||
char ch = chArray[i];
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
digitsSum += ch - '0';
|
||||
numbersSum += (ch - '0') * pow(10, digitPlace++);
|
||||
} else {
|
||||
digitPlace = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.printf("%d %d\n", numbersSum, digitsSum);
|
||||
}
|
||||
|
||||
static int pow(int num, int power) {
|
||||
int result = 1;
|
||||
for (int i = 0; i < power; i++) {
|
||||
result *= num;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
public class Part2 {
|
||||
|
||||
static void main(String[] args) {
|
||||
int num = parseInt(args[0]);
|
||||
|
||||
int overallGcd = num;
|
||||
int overallLcm = num;
|
||||
|
||||
for (int i = 1; i < args.length; i++) {
|
||||
num = parseInt(args[i]);
|
||||
overallGcd = gcd(overallGcd, num);
|
||||
overallLcm = lcm(overallLcm, num);
|
||||
}
|
||||
|
||||
System.out.printf("%d %d", overallGcd, overallLcm);
|
||||
}
|
||||
|
||||
static int pow(int num, int power) {
|
||||
int result = 1;
|
||||
for (int i = 0; i < power; i++) {
|
||||
result *= num;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int parseInt(String str) {
|
||||
int number = 0;
|
||||
int digitPlace = 0;
|
||||
char[] chArray = str.toCharArray();
|
||||
|
||||
for (int i = chArray.length - 1; i >= 0; i--) {
|
||||
char ch = chArray[i];
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
number += (ch - '0') * pow(10, digitPlace++);
|
||||
} else {
|
||||
digitPlace = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
static int gcd(int a, int b) {
|
||||
for (int min = a < b ? a : b; min > 1; min--)
|
||||
if (a % min == 0 && b % min == 0)
|
||||
return min;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int lcm(int a, int b) {
|
||||
return (a * b) / gcd(a, b);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
public class Part3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (int n : getPrimeNumbers(Part2.parseInt(args[0])))
|
||||
System.out.printf("%d ", n);
|
||||
}
|
||||
|
||||
public static int[] getPrimeNumbers(int n) {
|
||||
if (n == 0) return new int[0];
|
||||
|
||||
int[] primes = new int[n];
|
||||
primes[0] = 2;
|
||||
|
||||
for (int i = 3, c = 1; c < n; i += 2)
|
||||
if (isPrime(i))
|
||||
primes[c++] = i;
|
||||
|
||||
return primes;
|
||||
}
|
||||
|
||||
public static boolean isPrime(int n) {
|
||||
if (n <= 1)
|
||||
return false;
|
||||
|
||||
for (int i = 2; i * i <= n; i++) // same as i < sqrt(n)
|
||||
if (n % i == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
public class Part4 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int num = Part2.parseInt(args[0]);
|
||||
|
||||
System.out.printf("%d = ", num);
|
||||
|
||||
int counter;
|
||||
for (int prime = 2; num != 1; prime = nextPrime(prime)) {
|
||||
for (counter = 0; num % prime == 0; counter++)
|
||||
num /= prime;
|
||||
|
||||
if (counter != 0)
|
||||
System.out.printf("%d^%d%s", prime, counter, num != 1 ? " x " : "");
|
||||
}
|
||||
}
|
||||
|
||||
public static int nextPrime(int x) {
|
||||
for (x += 1; !Part3.isPrime(x); x += 1)
|
||||
;
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
public class Part5 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] test = { 1, 2, 3, 2, 1, 1, 4, 3, 2, 2, 1 };
|
||||
for (int item : sequence(test)) {
|
||||
System.out.printf("%d ", item);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static int[] sequence(int[] ar) {
|
||||
if (ar == null)
|
||||
return null;
|
||||
|
||||
int n = ar.length;
|
||||
|
||||
int[] seq = new int[n];
|
||||
int count = 0;
|
||||
boolean inSeq = false;
|
||||
|
||||
for (int i = 1; i < n; i++) {
|
||||
int a = ar[i - 1];
|
||||
int b = ar[i];
|
||||
|
||||
if (a > b) {
|
||||
seq[count++] = a;
|
||||
inSeq = true;
|
||||
} else if (inSeq) {
|
||||
seq[count++] = a;
|
||||
inSeq = false;
|
||||
}
|
||||
|
||||
if (i == n - 1 && inSeq)
|
||||
seq[count++] = b;
|
||||
}
|
||||
|
||||
if (count <= 1)
|
||||
return new int[0];
|
||||
|
||||
int[] result = new int[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
result[i] = seq[i];
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class Base {
|
||||
|
||||
{
|
||||
if (ComplianceTest.MAKE_ALL_TESTS_FAILED) {
|
||||
Assertions.fail("Compliance tests have not been passed", ComplianceTest.CAUSE);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import spoon.Launcher;
|
||||
import spoon.SpoonAPI;
|
||||
import spoon.reflect.declaration.CtType;
|
||||
import spoon.reflect.reference.CtTypeReference;
|
||||
import spoon.reflect.visitor.filter.TypeFilter;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
@Disabled("This test is used as a trigger to fail all the other tests")
|
||||
class ComplianceTest {
|
||||
|
||||
// Assign the following option to false to skip the compliance test
|
||||
// note, during testing at the stand this option will be turned on!!!
|
||||
|
||||
//private static final boolean TURN_TEST_COMPLIANCE_ON = false;
|
||||
private static final boolean TURN_TEST_COMPLIANCE_ON = true;
|
||||
|
||||
public static final boolean MAKE_ALL_TESTS_FAILED;
|
||||
|
||||
public static final Throwable CAUSE;
|
||||
|
||||
private static final Object EOL = System.lineSeparator();
|
||||
|
||||
static {
|
||||
L: {
|
||||
try {
|
||||
if (TURN_TEST_COMPLIANCE_ON) {
|
||||
initSpoon();
|
||||
startCompianceTests();
|
||||
}
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
MAKE_ALL_TESTS_FAILED = true;
|
||||
CAUSE = ex.getCause();
|
||||
break L;
|
||||
}
|
||||
MAKE_ALL_TESTS_FAILED = false;
|
||||
CAUSE = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static SpoonAPI spoon;
|
||||
|
||||
private static void initSpoon() {
|
||||
spoon = new Launcher();
|
||||
spoon.addInputResource("src/main/java/");
|
||||
spoon.buildModel();
|
||||
}
|
||||
|
||||
private static void startCompianceTests() throws ReflectiveOperationException {
|
||||
ComplianceTest cTest = new ComplianceTest();
|
||||
for (Method m : ComplianceTest.class.getDeclaredMethods()) {
|
||||
if (Modifier.isPrivate(m.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
Test[] ar = m.getAnnotationsByType(Test.class);
|
||||
if (ar.length > 0 && m.getAnnotationsByType(Test.class)[0] != null) {
|
||||
m.invoke(cTest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
@Test
|
||||
void appShouldNotUseForbiddenAPI() throws IOException, URISyntaxException {
|
||||
URL url = getClass().getResource("forbidden-api-regex.txt");
|
||||
String regex = Files.readString(Path.of(url.toURI()));
|
||||
Pattern forbiddenAPIRegex = Pattern.compile(regex.toString());
|
||||
StringBuilder errorMessage = new StringBuilder();
|
||||
for (CtType<?> ctType : spoon.getModel().getAllTypes()) {
|
||||
List<String> forbiddenAPI = ctType.getElements(new TypeFilter<>(CtTypeReference.class))
|
||||
.stream()
|
||||
.distinct()
|
||||
.filter(r -> forbiddenAPIRegex.matcher(r.toString()).matches())
|
||||
.map(CtTypeReference::getQualifiedName)
|
||||
.toList();
|
||||
if (!forbiddenAPI.isEmpty()) {
|
||||
errorMessage.append(EOL)
|
||||
.append(ctType.getQualifiedName()).append(": ")
|
||||
.append(forbiddenAPI);
|
||||
}
|
||||
}
|
||||
if (!errorMessage.isEmpty()) {
|
||||
fail(() -> "Using of this API is forbidden: " + errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldBeAppropriateNumberOfPackagesAndClasses() throws IOException, URISyntaxException {
|
||||
URL url = getClass().getResource("list-of-types.txt");
|
||||
String expected = Files.readString(Path.of(url.toURI())).trim();
|
||||
// '\n' character is used for clarity in error message
|
||||
String actual = spoon.getModel().getAllPackages().stream()
|
||||
.filter(p -> p.getTypes().size() != 0)
|
||||
.map(p -> p.getTypes().stream()
|
||||
.map(CtType::getQualifiedName)
|
||||
.sorted()
|
||||
.collect(Collectors.joining("\n")))
|
||||
.collect(Collectors.joining("\n"));
|
||||
assertEquals('\n' + expected, '\n' + actual);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvFileSource;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class Part1Test extends Base {
|
||||
|
||||
private static final PrintStream STD_OUT = System.out;
|
||||
|
||||
private static final String[] EMPTY = {};
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvFileSource(delimiter = ';', resources = "part1.csv")
|
||||
void test(String input, String expected) {
|
||||
String[] args = input == null ? EMPTY : input.split("\\s+");
|
||||
if (expected == null) {
|
||||
expected = "";
|
||||
}
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream ps = new PrintStream(baos);
|
||||
try {
|
||||
System.setOut(ps);
|
||||
Part1.main(args);
|
||||
} finally {
|
||||
System.setOut(STD_OUT);
|
||||
}
|
||||
assertEquals(expected, baos.toString().trim());
|
||||
ps.close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvFileSource;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class Part2Test extends Base {
|
||||
|
||||
private static final PrintStream STD_OUT = System.out;
|
||||
|
||||
private static final String[] EMPTY = {};
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvFileSource(delimiter = ';', resources = "part2.csv")
|
||||
void test(String input, String expected) {
|
||||
String[] args;
|
||||
expected = expected == null ? "" : expected.replaceAll("_", " ");
|
||||
args = input == null ? EMPTY : input.split("\\s+");
|
||||
for (int j = 0; j < args.length; ++j) {
|
||||
args[j] = args[j].replace("_", " ");
|
||||
}
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream ps = new PrintStream(baos);
|
||||
try {
|
||||
System.setOut(ps);
|
||||
Part2.main(args);
|
||||
} finally {
|
||||
System.setOut(STD_OUT);
|
||||
}
|
||||
String actual = baos.toString();
|
||||
assertEquals(expected, actual);
|
||||
ps.close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvFileSource;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class Part3Test extends Base {
|
||||
|
||||
private static final PrintStream STD_OUT = System.out;
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvFileSource(delimiter = ';', resources = "part3.csv")
|
||||
void test(String input, String expected) {
|
||||
if (expected == null) {
|
||||
expected = "";
|
||||
}
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream ps = new PrintStream(baos);
|
||||
try {
|
||||
System.setOut(ps);
|
||||
Part3.main(new String[] { input });
|
||||
} finally {
|
||||
System.setOut(STD_OUT);
|
||||
}
|
||||
assertEquals(expected, baos.toString().trim());
|
||||
ps.close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvFileSource;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class Part4Test extends Base {
|
||||
|
||||
private static final PrintStream STD_OUT = System.out;
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvFileSource(delimiter = ';', resources = "part4.csv")
|
||||
void test(String input, String expected) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream ps = new PrintStream(baos);
|
||||
try {
|
||||
System.setOut(ps);
|
||||
Part4.main(new String[] { input });
|
||||
} finally {
|
||||
System.setOut(STD_OUT);
|
||||
}
|
||||
assertEquals(expected, baos.toString().trim());
|
||||
ps.close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package ua.nure.jfn.task1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvFileSource;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class Part5Test extends Base {
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvFileSource(delimiter = ';', resources = "part5.csv")
|
||||
void test(String input, String expected) {
|
||||
if (expected != null) {
|
||||
if ("_".equals(expected)) {
|
||||
expected = "";
|
||||
} else {
|
||||
expected = expected.replaceAll("\\s+", " ");
|
||||
}
|
||||
}
|
||||
int[] ar = null;
|
||||
if (input != null) {
|
||||
ar = Arrays.stream(input.split("\\s+")).mapToInt(Integer::parseInt).toArray();
|
||||
}
|
||||
int[] result = Part5.sequence(ar);
|
||||
String actual;
|
||||
if (result == null) {
|
||||
actual = null;
|
||||
} else {
|
||||
actual = IntStream.of(result)
|
||||
.mapToObj(String::valueOf)
|
||||
.collect(Collectors.joining(" "));
|
||||
}
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
(?x)
|
||||
^
|
||||
(java\.(util|math)\.\S+)
|
||||
|
|
||||
(java\.lang\.(Byte|Short|Integer|Long|Character|Float|Double))
|
||||
$
|
@ -0,0 +1,6 @@
|
||||
ua.nure.jfn.task1.Demo
|
||||
ua.nure.jfn.task1.Part1
|
||||
ua.nure.jfn.task1.Part2
|
||||
ua.nure.jfn.task1.Part3
|
||||
ua.nure.jfn.task1.Part4
|
||||
ua.nure.jfn.task1.Part5
|
@ -0,0 +1,35 @@
|
||||
P4 R8 40 1 ; 53 17
|
||||
x6vp 45 3 ; 54 18
|
||||
5 wQ8M 9 16 ; 38 29
|
||||
77ksvZg ; 77 14
|
||||
z5189185 ; 5189185 37
|
||||
10X H21b ; 31 4
|
||||
9w6q4U4 P0b ; 23 23
|
||||
43 cy01 ; 44 8
|
||||
Z Q8 C d 5r7n53 ; 73 28
|
||||
428 C1D ; 429 15
|
||||
N 6k81 97 A 204 ; 388 37
|
||||
98aPwh m 3146S13k ; 3257 35
|
||||
3 Ngf ; 3 3
|
||||
W2 9U9pPy ; 20 20
|
||||
761 5u ; 766 19
|
||||
2b Z ; 2 2
|
||||
4r1662 bEfN5Z9BV93 ; 1773 45
|
||||
56 q 48 ; 104 23
|
||||
ph 5 ; 5 5
|
||||
5p 53A81t r ; 139 22
|
||||
5eg E ; 5 5
|
||||
054t716 ; 770 23
|
||||
23yh 0X83 6RT3 x0YV ; 115 25
|
||||
2R0 7m1 17Kz5 qX7 ; 39 30
|
||||
M6 YA6 7 0419f2 ; 440 35
|
||||
xU68M D rq 81 0 42 ; 191 29
|
||||
zSZR2r6M ; 8 8
|
||||
4M2 epBT3 A6 2832 ; 2847 30
|
||||
z38G0 H 565a ; 603 27
|
||||
s 1V 4S 6M z8 ; 19 19
|
||||
7AG 70 3 ; 80 17
|
||||
0 u34 1rcvN 1 2 ; 38 11
|
||||
5 rC86E2H753m4 ; 850 40
|
||||
56 825R ; 881 26
|
||||
FzV 1 2 H ; 3 3
|
|
@ -0,0 +1,35 @@
|
||||
18 9 ; 9 18
|
||||
1 1 14 ; 1 14
|
||||
12 6 ; 6 12
|
||||
12 20 ; 4 60
|
||||
10 16 ; 2 80
|
||||
1 1 25 5 ; 1 25
|
||||
12 24 ; 12 24
|
||||
14 ; 14 14
|
||||
11 ; 11 11
|
||||
21 28 ; 7 84
|
||||
21 14 ; 7 42
|
||||
24 16 ; 8 48
|
||||
12 30 6 ; 6 60
|
||||
10 15 ; 5 30
|
||||
27 9 ; 9 27
|
||||
16 ; 16 16
|
||||
16 8 ; 8 16
|
||||
12 ; 12 12
|
||||
16 24 ; 8 48
|
||||
12 8 4 ; 4 24
|
||||
12 16 ; 4 48
|
||||
10 30 5 25 ; 5 150
|
||||
14 21 ; 7 42
|
||||
13 26 ; 13 26
|
||||
12 3 15 6 ; 3 60
|
||||
9 18 ; 9 18
|
||||
1 1 11 ; 1 11
|
||||
10 5 15 ; 5 30
|
||||
10 16 4 6 ; 2 240
|
||||
10 20 5 ; 5 20
|
||||
8 16 8 ; 8 16
|
||||
20 10 ; 10 20
|
||||
13 ; 13 13
|
||||
8 ; 8 8
|
||||
18 9 27 ; 9 54
|
|
@ -0,0 +1,20 @@
|
||||
8 ; 2 3 5 7 11 13 17 19
|
||||
17 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
|
||||
33 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137
|
||||
16 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53
|
||||
2 ; 2 3
|
||||
24 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
|
||||
26 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101
|
||||
7 ; 2 3 5 7 11 13 17
|
||||
22 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
|
||||
15 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
|
||||
14 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43
|
||||
28 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107
|
||||
13 ; 2 3 5 7 11 13 17 19 23 29 31 37 41
|
||||
10 ; 2 3 5 7 11 13 17 19 23 29
|
||||
19 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
|
||||
38 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163
|
||||
30 ; 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113
|
||||
6 ; 2 3 5 7 11 13
|
||||
12 ; 2 3 5 7 11 13 17 19 23 29 31 37
|
||||
1 ; 2
|
|
@ -0,0 +1,28 @@
|
||||
111 ; 111 = 3^1 x 37^1
|
||||
313 ; 313 = 313^1
|
||||
658 ; 658 = 2^1 x 7^1 x 47^1
|
||||
5 ; 5 = 5^1
|
||||
943 ; 943 = 23^1 x 41^1
|
||||
550 ; 550 = 2^1 x 5^2 x 11^1
|
||||
596 ; 596 = 2^2 x 149^1
|
||||
439 ; 439 = 439^1
|
||||
523 ; 523 = 523^1
|
||||
93 ; 93 = 3^1 x 31^1
|
||||
550 ; 550 = 2^1 x 5^2 x 11^1
|
||||
596 ; 596 = 2^2 x 149^1
|
||||
363 ; 363 = 3^1 x 11^2
|
||||
852 ; 852 = 2^2 x 3^1 x 71^1
|
||||
996 ; 996 = 2^2 x 3^1 x 83^1
|
||||
708 ; 708 = 2^2 x 3^1 x 59^1
|
||||
369 ; 369 = 3^2 x 41^1
|
||||
747 ; 747 = 3^2 x 83^1
|
||||
735 ; 735 = 3^1 x 5^1 x 7^2
|
||||
360 ; 360 = 2^3 x 3^2 x 5^1
|
||||
783 ; 783 = 3^3 x 29^1
|
||||
648 ; 648 = 2^3 x 3^4
|
||||
360 ; 360 = 2^3 x 3^2 x 5^1
|
||||
594 ; 594 = 2^1 x 3^3 x 11^1
|
||||
500 ; 500 = 2^2 x 5^3
|
||||
540 ; 540 = 2^2 x 3^3 x 5^1
|
||||
904 ; 904 = 2^3 x 113^1
|
||||
936 ; 936 = 2^3 x 3^2 x 13^1
|
|
@ -0,0 +1,24 @@
|
||||
1 2 3 2 1 1 4 ; 3 2 1
|
||||
;
|
||||
1 2 3 2 1 1 4 3 2 2 1 ; 3 2 1 4 3 2 2 1
|
||||
1 2 3 4 5 ; _
|
||||
1 2 3 4 5 4 ; 5 4
|
||||
1 2 1 4 1 4 1 ; 2 1 4 1 4 1
|
||||
5 2 1 4 1 4 5 ; 5 2 1 4 1
|
||||
3 2 1 3 2 1 3 2 1 ; 3 2 1 3 2 1 3 2 1
|
||||
1 2 6 2 6 4 7 8 7 8 8 1 6 8 5 8 5 2 ; 6 2 6 4 8 7 8 1 8 5 8 5 2
|
||||
6 4 5 8 2 4 8 2 2 5 ; 6 4 8 2 8 2
|
||||
1 1 1 7 4 7 8 7 3 7 7 4 5 8 8 3 ; 7 4 8 7 3 7 4 8 3
|
||||
4 1 6 2 8 6 2 5 ; 4 1 6 2 8 6 2
|
||||
5 1 6 5 4 5 1 2 2 5 5 8 ; 5 1 6 5 4 5 1
|
||||
1 2 1 6 2 8 ; 2 1 6 2
|
||||
6 1 7 1 2 5 4 4 4 7 7 3 6 6 8 ; 6 1 7 1 5 4 7 3
|
||||
8 6 5 5 8 8 7 3 4 3 5 3 5 ; 8 6 5 8 7 3 4 3 5 3
|
||||
2 7 2 2 3 2 8 2 6 ; 7 2 3 2 8 2
|
||||
1 1 8 8 6 8 1 4 4 7 1 7 1 ; 8 6 8 1 7 1 7 1
|
||||
4 2 4 1 7 6 3 7 1 2 1 1 4 2 3 ; 4 2 4 1 7 6 3 7 1 2 1 4 2
|
||||
8 2 2 8 1 3 8 3 3 5 4 ; 8 2 8 1 8 3 5 4
|
||||
1 6 4 5 7 6 7 3 3 8 4 4 5 1 8 3 8 7 ; 6 4 7 6 7 3 8 4 5 1 8 3 8 7
|
||||
6 6 8 7 1 6 6 2 6 6 ; 8 7 1 6 2
|
||||
5 6 2 3 7 3 2 4 4 2 ; 6 2 7 3 2 4 2
|
||||
5 2 4 8 7 5 2 2 5 7 1 ; 5 2 8 7 5 2 7 1
|
|
BIN
semester-4/ОПJа/lb-1/tasks.pdf
Normal file
BIN
semester-4/ОПJа/lb-1/tasks.pdf
Normal file
Binary file not shown.
2
semester-4/ОПJа/lb-2/README.md
Normal file
2
semester-4/ОПJа/lb-2/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
Викладач: Колесников Д. О.
|
||||
Оцінка: 98
|
58
semester-4/ОПJа/lb-2/task2/pom.xml
Normal file
58
semester-4/ОПJа/lb-2/task2/pom.xml
Normal file
@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
|
||||
<groupId>ua.nure.jfn</groupId>
|
||||
<artifactId>task2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
<junit5.version>5.12.0</junit5.version>
|
||||
<surefire.version>3.5.2</surefire.version>
|
||||
<spoon.version>11.2.0</spoon.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit5.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>${junit5.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>fr.inria.gforge.spoon</groupId>
|
||||
<artifactId>spoon-core</artifactId>
|
||||
<version>${spoon.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>2.0.17</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>task2</finalName>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,19 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
public interface Array<T> extends Container<T> {
|
||||
|
||||
// Returns the element at the specified position.
|
||||
T get(int index);
|
||||
|
||||
// Returns the index of the first occurrence of the specified element,
|
||||
// or -1 if this array does not contain the element.
|
||||
// (use 'equals' method to check an occurrence)
|
||||
int indexOf(T element);
|
||||
|
||||
// Removes the element at the specified position.
|
||||
void remove(int index);
|
||||
|
||||
// Sets the element at the specified position.
|
||||
void set(int index, T element);
|
||||
|
||||
}
|
@ -0,0 +1,183 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ArrayImpl<T> implements Array<T> {
|
||||
|
||||
private Object[] elements;
|
||||
private int size = 0;
|
||||
|
||||
public ArrayImpl() {
|
||||
}
|
||||
|
||||
public ArrayImpl(Container<T> container) {
|
||||
this.elements = new Object[container.size() + 1];
|
||||
|
||||
for (T element : container)
|
||||
this.add(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(T element) {
|
||||
if (elements == null) {
|
||||
elements = new Object[255];
|
||||
} else if (size == elements.length) {
|
||||
Object[] newElements = new Object[elements.length * 2];
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
newElements[i] = elements[i];
|
||||
|
||||
elements = newElements;
|
||||
}
|
||||
|
||||
elements[size++] = element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
for (int i = 0; i < size; i++)
|
||||
elements[i] = null;
|
||||
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T get(int index) {
|
||||
if (index >= size || index < 0)
|
||||
throw new ArrayIndexOutOfBoundsException();
|
||||
|
||||
return (T) elements[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(T element) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element == null) {
|
||||
if (elements[i] == null)
|
||||
return i;
|
||||
} else if (elements[i] != null && elements[i].equals(element))
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(int index) {
|
||||
if (index >= size || index < 0)
|
||||
throw new ArrayIndexOutOfBoundsException();
|
||||
|
||||
for (index += 1; index < size; index++)
|
||||
elements[index - 1] = elements[index];
|
||||
|
||||
size--;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int index, T element) {
|
||||
if (index >= size || index < 0)
|
||||
throw new ArrayIndexOutOfBoundsException();
|
||||
|
||||
elements[index] = element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (elements == null)
|
||||
return "[]";
|
||||
|
||||
String repr = "[";
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (elements[i] == null)
|
||||
repr += "null";
|
||||
else
|
||||
repr += elements[i].toString();
|
||||
|
||||
if (i != size - 1)
|
||||
repr += ", ";
|
||||
}
|
||||
|
||||
repr += "]";
|
||||
|
||||
return repr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new IteratorImpl();
|
||||
}
|
||||
|
||||
private class IteratorImpl implements Iterator<T> {
|
||||
int currentIndex = 0;
|
||||
int lastReturnedIndex = -1;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIndex < size;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T next() {
|
||||
if (!hasNext())
|
||||
throw new java.util.NoSuchElementException();
|
||||
|
||||
lastReturnedIndex = currentIndex;
|
||||
return (T) elements[currentIndex++];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
if (lastReturnedIndex == -1)
|
||||
throw new IllegalStateException(
|
||||
"next() has not been called, or remove() has already been called after the last next()");
|
||||
|
||||
ArrayImpl.this.remove(lastReturnedIndex);
|
||||
currentIndex = lastReturnedIndex;
|
||||
lastReturnedIndex = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<T> stream() {
|
||||
return new StreamImpl<>(this);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Array<Integer> intArray = new ArrayImpl<>();
|
||||
|
||||
intArray.add(10);
|
||||
intArray.add(20);
|
||||
intArray.add(30);
|
||||
intArray.add(40);
|
||||
intArray.add(20);
|
||||
|
||||
System.out.println("Testing get method:");
|
||||
System.out.println("Element at index 0: " + intArray.get(0));
|
||||
System.out.println("Element at index 2: " + intArray.get(2));
|
||||
|
||||
System.out.println("\nTesting indexOf method:");
|
||||
System.out.println("Index of element 20: " + intArray.indexOf(20));
|
||||
System.out.println("Index of element 50: " + intArray.indexOf(50));
|
||||
|
||||
System.out.println("\nTesting set method:");
|
||||
System.out.println("Before setting: " + intArray);
|
||||
intArray.set(1, 25);
|
||||
System.out.println("After setting index 1 to 25: " + intArray);
|
||||
|
||||
System.out.println("\nTesting remove method:");
|
||||
System.out.println("Before removing: " + intArray);
|
||||
intArray.remove(2);
|
||||
System.out.println("After removing element at index 2: " + intArray);
|
||||
|
||||
System.out.println("\nTesting Container interface methods:");
|
||||
System.out.println("Size of array: " + intArray.size());
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public interface Container<T> extends Iterable<T> {
|
||||
|
||||
// Appends the specified element to the end.
|
||||
void add(T element);
|
||||
|
||||
// Removes all of the elements.
|
||||
void clear();
|
||||
|
||||
// Returns the number of elements.
|
||||
int size();
|
||||
|
||||
// Returns a string representation of this container.
|
||||
// See JUnit tests for details.
|
||||
String toString();
|
||||
|
||||
// Returns an iterator over elements.
|
||||
// Iterator must implement the remove method.
|
||||
Iterator<T> iterator();
|
||||
|
||||
// Returns a stream over elements.
|
||||
Stream<T> stream();
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
public class Demo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
public interface List<T> extends Container<T> {
|
||||
|
||||
// Inserts the specified element at the beginning.
|
||||
void addFirst(T element);
|
||||
|
||||
// Removes the first element.
|
||||
void removeFirst();
|
||||
|
||||
// Removes the last element.
|
||||
void removeLast();
|
||||
|
||||
// Returns the first element.
|
||||
T getFirst();
|
||||
|
||||
// Returns the last element.
|
||||
T getLast();
|
||||
|
||||
// Returns the first occurrence of the specified element.
|
||||
// Returns null if no such element.
|
||||
// (use 'equals' method to check an occurrence)
|
||||
T search(T element);
|
||||
|
||||
// Removes the first occurrence of the specified element.
|
||||
// Returns true if this list contained the specified element.
|
||||
// (use 'equals' method to check an occurrence)
|
||||
boolean remove(T element);
|
||||
|
||||
}
|
@ -0,0 +1,300 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ListImpl<T> implements List<T> {
|
||||
|
||||
private Node<T> head;
|
||||
private int size;
|
||||
|
||||
private static class Node<T> {
|
||||
Object data;
|
||||
Node<T> next;
|
||||
|
||||
Node(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
Node(T data, Node<T> next) {
|
||||
this.data = data;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
boolean isLast() {
|
||||
return next == null;
|
||||
}
|
||||
|
||||
public void add(T element) {
|
||||
if (next == null) {
|
||||
next = new Node<>(element);
|
||||
} else {
|
||||
next.add(element);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeLast() {
|
||||
if (next.isLast())
|
||||
next = null;
|
||||
else
|
||||
next.removeLast();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T getLast() {
|
||||
if (next.isLast())
|
||||
return (T) next.data;
|
||||
|
||||
return (T) next.getLast();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T search(T element) {
|
||||
if (next == null)
|
||||
return null;
|
||||
|
||||
if (next.data != null && next.data.equals(element))
|
||||
return (T) next.data;
|
||||
|
||||
return (T) next.search(element);
|
||||
}
|
||||
|
||||
public boolean remove(T element) {
|
||||
if (next == null)
|
||||
return false;
|
||||
|
||||
if (next.data == element || next.data.equals(element)) {
|
||||
next = next.next;
|
||||
return true;
|
||||
}
|
||||
|
||||
return next.remove(element);
|
||||
}
|
||||
|
||||
public boolean removeNode(Node<T> element) {
|
||||
if (next == element) {
|
||||
next = next.next;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (next == null) {
|
||||
return next.removeNode(element);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(T element) {
|
||||
if (head == null)
|
||||
head = new Node<>(element);
|
||||
else
|
||||
head.add(element);
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
head = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFirst(T element) {
|
||||
head = new Node<>(element, head);
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFirst() {
|
||||
if (head == null)
|
||||
throw new java.util.NoSuchElementException();
|
||||
|
||||
head = head.next;
|
||||
size--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeLast() {
|
||||
if (head == null)
|
||||
throw new java.util.NoSuchElementException();
|
||||
|
||||
if (head.isLast())
|
||||
head = null;
|
||||
else
|
||||
head.removeLast();
|
||||
|
||||
size--;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T getFirst() {
|
||||
if (head == null)
|
||||
throw new java.util.NoSuchElementException();
|
||||
|
||||
return (T) head.data;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T getLast() {
|
||||
if (head == null)
|
||||
throw new java.util.NoSuchElementException();
|
||||
|
||||
if (head.isLast())
|
||||
return (T) head.data;
|
||||
else
|
||||
return (T) head.getLast();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T search(T element) {
|
||||
if (head == null)
|
||||
return null;
|
||||
|
||||
if (head.data != null && head.data.equals(element))
|
||||
return (T) head.data;
|
||||
else if (head.next != null)
|
||||
return (T) head.search(element);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(T element) {
|
||||
if (head == null)
|
||||
return false;
|
||||
|
||||
if (head.data == element || head.data.equals(element)) {
|
||||
head = head.next;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (head.next != null) {
|
||||
return head.remove(element);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String result = "[";
|
||||
Node<T> current = head;
|
||||
|
||||
while (current != null) {
|
||||
result += current.data != null ? current.data.toString() : "null";
|
||||
|
||||
if (current.next != null)
|
||||
result += ", ";
|
||||
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
result += "]";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new IteratorImpl();
|
||||
}
|
||||
|
||||
private class IteratorImpl implements Iterator<T> {
|
||||
|
||||
Node<T> curr = head;
|
||||
Node<T> prev = null;
|
||||
boolean nextCalled = false;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return curr != null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T next() {
|
||||
if (!hasNext())
|
||||
throw new java.util.NoSuchElementException();
|
||||
|
||||
prev = curr;
|
||||
curr = curr.next;
|
||||
nextCalled = true;
|
||||
return (T) prev.data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
if (!nextCalled)
|
||||
throw new IllegalStateException(
|
||||
"next() has not been called, or remove() has already been called after the last next()");
|
||||
|
||||
if (head == prev)
|
||||
head = head.next;
|
||||
else
|
||||
head.removeNode(prev);
|
||||
|
||||
prev = null;
|
||||
nextCalled = false;
|
||||
|
||||
size--;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<T> stream() {
|
||||
return new StreamImpl<>(this);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Integer> intList = new ListImpl<>();
|
||||
|
||||
System.out.println("Testing add method (from Container):");
|
||||
intList.add(30);
|
||||
intList.add(40);
|
||||
intList.add(50);
|
||||
System.out.println("List after adding elements: " + intList);
|
||||
|
||||
System.out.println("\nTesting addFirst method:");
|
||||
intList.addFirst(20);
|
||||
intList.addFirst(10);
|
||||
System.out.println("List after adding elements at the beginning: " + intList);
|
||||
|
||||
System.out.println("\nTesting getFirst and getLast methods:");
|
||||
System.out.println("First element: " + intList.getFirst());
|
||||
System.out.println("Last element: " + intList.getLast());
|
||||
|
||||
System.out.println("\nTesting search method:");
|
||||
System.out.println("Search for 30: " + intList.search(30));
|
||||
System.out.println("Search for 60: " + intList.search(60));
|
||||
|
||||
System.out.println("\nTesting remove method (by element):");
|
||||
System.out.println("Before removing 30: " + intList);
|
||||
boolean removed = intList.remove(30);
|
||||
System.out.println("Element 30 removed: " + removed);
|
||||
System.out.println("After removing 30: " + intList);
|
||||
|
||||
removed = intList.remove(100);
|
||||
System.out.println("Element 100 removed: " + removed);
|
||||
|
||||
System.out.println("\nTesting removeFirst method:");
|
||||
System.out.println("Before removeFirst: " + intList);
|
||||
intList.removeFirst();
|
||||
System.out.println("After removeFirst: " + intList);
|
||||
|
||||
System.out.println("\nTesting removeLast method:");
|
||||
System.out.println("Before removeLast: " + intList);
|
||||
intList.removeLast();
|
||||
System.out.println("After removeLast: " + intList);
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
// A sequence of elements.
|
||||
public interface Stream<T> {
|
||||
|
||||
// Represents a function that accepts one argument and produces a result.
|
||||
// X - the type of the input to the function;
|
||||
// Y - the type of the result of the function.
|
||||
interface Function<X, Y> {
|
||||
Y apply(X x);
|
||||
}
|
||||
|
||||
// Represents an operation that accepts a single input argument
|
||||
// and returns no result.
|
||||
// X - the type of the input to the operation.
|
||||
interface Action<X> {
|
||||
void perform(X x);
|
||||
}
|
||||
|
||||
// Intermediate operations
|
||||
|
||||
// Returns a stream consisting of the elements of this stream that match
|
||||
// the given predicate.
|
||||
Stream<T> filter(Function<? super T, Boolean> predicate);
|
||||
|
||||
// Returns a stream consisting of the results of applying the given function
|
||||
// to the elements of this stream.
|
||||
<R> Stream<R> map(Function<? super T, ? extends R> function);
|
||||
|
||||
// terminal operations
|
||||
|
||||
// Returns the count of elements in this stream.
|
||||
int count();
|
||||
|
||||
// Performs an action for each element of this stream.
|
||||
void forEach(Action<? super T> action);
|
||||
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
public class StreamImpl<T> implements Stream<T> {
|
||||
private Container<?> source;
|
||||
private Array<Operation> operations;
|
||||
|
||||
private interface Operation {
|
||||
Container<?> apply(Container<?> input);
|
||||
}
|
||||
|
||||
private static class FilterOperation<T> implements Operation {
|
||||
private Function<? super T, Boolean> predicate;
|
||||
|
||||
public FilterOperation(Function<? super T, Boolean> predicate) {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Container<?> apply(Container<?> input) {
|
||||
Array<T> newContainer = new ArrayImpl<T>();
|
||||
|
||||
for (Object element : input) {
|
||||
if (predicate.apply((T) element))
|
||||
newContainer.add((T) element);
|
||||
}
|
||||
|
||||
return newContainer;
|
||||
}
|
||||
}
|
||||
|
||||
private static class MapOperation<T, R> implements Operation {
|
||||
private Function<? super T, ? extends R> function;
|
||||
|
||||
public MapOperation(Function<? super T, ? extends R> function) {
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Container<?> apply(Container<?> input) {
|
||||
Array<R> newContainer = new ArrayImpl<R>();
|
||||
|
||||
for (Object element : input) {
|
||||
newContainer.add(function.apply((T) element));
|
||||
}
|
||||
|
||||
return newContainer;
|
||||
}
|
||||
}
|
||||
|
||||
public StreamImpl(Container<? extends T> container) {
|
||||
this.source = container;
|
||||
this.operations = new ArrayImpl<>();
|
||||
}
|
||||
|
||||
private StreamImpl(Container<?> source, Array<Operation> operations) {
|
||||
this.source = source;
|
||||
this.operations = operations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<T> filter(Function<? super T, Boolean> predicate) {
|
||||
Array<Operation> newOperations = new ArrayImpl<>(this.operations);
|
||||
newOperations.add(new FilterOperation<>(predicate));
|
||||
return new StreamImpl<T>(source, newOperations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> Stream<R> map(Function<? super T, ? extends R> function) {
|
||||
Array<Operation> newOperations = new ArrayImpl<>(this.operations);
|
||||
newOperations.add(new MapOperation<>(function));
|
||||
return new StreamImpl<R>(source, newOperations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int count() {
|
||||
int count = 0;
|
||||
|
||||
for (Operation operation : operations) {
|
||||
source = operation.apply(source);
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void forEach(Action<? super T> action) {
|
||||
for (Operation operation : operations) {
|
||||
source = operation.apply(source);
|
||||
}
|
||||
|
||||
for (Object element : source) {
|
||||
action.perform((T) element);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,262 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class ArrayImplIntegerTest extends Base {
|
||||
|
||||
private Array<Integer> array = new ArrayImpl<>();
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Container methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[1, 2, 3]",
|
||||
"1 ;[1]",
|
||||
"null 2 null 4 ;[null, 2, null, 4]"
|
||||
})
|
||||
void testAdd(String input, String expected) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
assertEquals(expected, array.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3",
|
||||
"1 2",
|
||||
"null null"
|
||||
})
|
||||
void testClear(String input) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
array.clear();
|
||||
assertEquals("[]", array.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClearEmpty() {
|
||||
array.clear();
|
||||
assertEquals("[]", array.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;1",
|
||||
"1 2 ;2",
|
||||
"1 2 3 null ;4",
|
||||
})
|
||||
void testSize(String input, String expectedStr) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
int expected = Integer.parseInt(expectedStr);
|
||||
assertEquals(expected, array.size());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Array methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;2 ;3",
|
||||
"1 2 3 ;1 ;2",
|
||||
"1 2 3 ;0 ;1",
|
||||
"null null ;0 ;null",
|
||||
"null null ;1 ;null",
|
||||
})
|
||||
void testGet(String input, String indexStr, String expectedStr) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
int index = Integer.parseInt(indexStr);
|
||||
Integer expected = Utils.parseInt(expectedStr);
|
||||
assertEquals(expected, array.get(index));
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 2 ;2 ;1",
|
||||
"1 1 1 ;1 ;0",
|
||||
"1 2 3 ;3 ;2",
|
||||
"null null ;null ;0",
|
||||
"1 null ;null ;1",
|
||||
})
|
||||
void testIndexOf(String input, String xStr, String expectedStr) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
Integer x = Utils.parseInt(xStr);
|
||||
int expected = Integer.parseInt(expectedStr);
|
||||
assertEquals(expected, array.indexOf(x));
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;0 ;[2, 3]",
|
||||
"1 2 3 ;1 ;[1, 3]",
|
||||
"1 2 3 ;2 ;[1, 2]",
|
||||
"null 2 null ;0 ;[2, null]",
|
||||
"1 null ;1 ;[1]",
|
||||
})
|
||||
void testRemove(String input, String indexStr, String expected) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
int index = Utils.parseInt(indexStr);
|
||||
array.remove(index);
|
||||
assertEquals(expected, array.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;0 ;7 ;[7, 2, 3]",
|
||||
"1 2 3 ;1 ;8 ;[1, 8, 3]",
|
||||
"1 2 3 ;2 ;null ;[1, 2, null]",
|
||||
"null 2 null ;2 ;null ;[null, 2, null]",
|
||||
"null 2 null ;1 ;null ;[null, null, null]",
|
||||
})
|
||||
void testSet(String input, String indexStr, String xStr, String expected) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
int index = Integer.parseInt(indexStr);
|
||||
Integer x = Utils.parseInt(xStr);
|
||||
array.set(index, x);
|
||||
assertEquals(expected, array.toString());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Iterator methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;2",
|
||||
"null 3 ;3",
|
||||
"1 null 3 ;null",
|
||||
"null null ;null"
|
||||
})
|
||||
void testIteratorNext(String input, String expectedStr) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
Iterator<Integer> it = array.iterator();
|
||||
Integer expected = Utils.parseInt(expectedStr);
|
||||
it.next();
|
||||
assertEquals(expected, it.next());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;true",
|
||||
"null 3 ;false",
|
||||
"1 null ;false"
|
||||
})
|
||||
void testIteratorHasNext(String input, String expectedStr) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
Boolean expected = Utils.parseBoolean(expectedStr);
|
||||
Iterator<Integer> it = array.iterator();
|
||||
it.next();
|
||||
it.next();
|
||||
assertEquals(expected, it.hasNext());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[3]",
|
||||
"null null null ;[null]",
|
||||
"null null ;[]",
|
||||
})
|
||||
void testIteratorRemove(String input, String expected) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
Iterator<Integer> it = array.iterator();
|
||||
it.next();
|
||||
it.remove();
|
||||
it.next();
|
||||
it.remove();
|
||||
assertEquals(expected, array.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1",
|
||||
"1 2" ,
|
||||
"null null null",
|
||||
})
|
||||
void testIteratorRemoveShouldThrowException(String input) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
Iterator<Integer> it1 = array.iterator();
|
||||
assertThrows(IllegalStateException.class, () -> it1.remove());
|
||||
Iterator<Integer> it2 = array.iterator();
|
||||
it2.next();
|
||||
it2.remove();
|
||||
assertThrows(IllegalStateException.class, () -> it2.remove());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2",
|
||||
"null null",
|
||||
})
|
||||
void testIteratorNextShouldThrowsException(String input) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
Iterator<Integer> it = array.iterator();
|
||||
it.next();
|
||||
it.next();
|
||||
assertThrows(NoSuchElementException.class, () -> it.next());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;1",
|
||||
"1 2 ;12",
|
||||
"1 null 3 ;1null3",
|
||||
})
|
||||
void testIteratorForEach(String input, String expected) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Integer x : array) {
|
||||
sb.append(x);
|
||||
}
|
||||
String actual = sb.toString();
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Stream methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;",
|
||||
"1 2 ;2",
|
||||
"1 2 3 4 ;24",
|
||||
"11 22 33 444 ;22444",
|
||||
})
|
||||
void testStreamFilter(String input, String expected) {
|
||||
Utils.fillInWithIntegers(array, input);
|
||||
if (expected == null) {
|
||||
expected = "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
array.stream()
|
||||
.filter(x -> x % 2 == 0)
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,314 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class ArrayImplStringTest extends Base {
|
||||
|
||||
private Array<String> array = new ArrayImpl<>();
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Container methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[1, 2, 3]",
|
||||
"1 ;[1]",
|
||||
"null 2 null 4 ;[null, 2, null, 4]"
|
||||
})
|
||||
void testAdd(String input, String expected) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
assertEquals(expected, array.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3",
|
||||
"1 2",
|
||||
"null null"
|
||||
})
|
||||
void testClear(String input) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
array.clear();
|
||||
assertEquals("[]", array.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClearEmpty() {
|
||||
array.clear();
|
||||
assertEquals("[]", array.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;1",
|
||||
"1 2 ;2",
|
||||
"1 2 3 null ;4",
|
||||
})
|
||||
void testSize(String input, String expectedStr) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
int expected = Integer.parseInt(expectedStr);
|
||||
assertEquals(expected, array.size());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Array methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;2 ;3",
|
||||
"1 2 3 ;1 ;2",
|
||||
"1 2 3 ;0 ;1",
|
||||
"null null ;0 ;null",
|
||||
"null null ;1 ;null",
|
||||
})
|
||||
void testGet(String input, String indexStr, String expected) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
int index = Integer.parseInt(indexStr);
|
||||
assertEquals(expected, array.get(index));
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 2 ;2 ;1",
|
||||
"1 1 1 ;1 ;0",
|
||||
"1 2 3 ;3 ;2",
|
||||
"null null ;null ;0",
|
||||
"1 null ;null ;1",
|
||||
})
|
||||
void testIndexOf(String input, String xStr, String expectedStr) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
String x = xStr;
|
||||
int expected = Integer.parseInt(expectedStr);
|
||||
assertEquals(expected, array.indexOf(x));
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;0 ;[2, 3]",
|
||||
"1 2 3 ;1 ;[1, 3]",
|
||||
"1 2 3 ;2 ;[1, 2]",
|
||||
"null 2 null ;0 ;[2, null]",
|
||||
"1 null ;1 ;[1]",
|
||||
})
|
||||
void testRemove(String input, String indexStr, String expected) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
int index = Utils.parseInt(indexStr);
|
||||
array.remove(index);
|
||||
assertEquals(expected, array.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;0 ;7 ;[7, 2, 3]",
|
||||
"1 2 3 ;1 ;8 ;[1, 8, 3]",
|
||||
"1 2 3 ;2 ;null ;[1, 2, null]",
|
||||
"null 2 null ;2 ;null ;[null, 2, null]",
|
||||
"null 2 null ;1 ;null ;[null, null, null]",
|
||||
})
|
||||
void testSet(String input, String indexStr, String xStr, String expected) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
int index = Integer.parseInt(indexStr);
|
||||
array.set(index, xStr);
|
||||
assertEquals(expected, array.toString());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Iterator methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;2",
|
||||
"null 3 ;3",
|
||||
"1 null 3 ;null",
|
||||
"null null ;null"
|
||||
})
|
||||
void testIteratorNext(String input, String expected) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
Iterator<String> it = array.iterator();
|
||||
it.next();
|
||||
assertEquals(expected, it.next());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;true",
|
||||
"null 3 ;false",
|
||||
"1 null ;false"
|
||||
})
|
||||
void testIteratorHasNext(String input, String expectedStr) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
Boolean expected = Utils.parseBoolean(expectedStr);
|
||||
Iterator<String> it = array.iterator();
|
||||
it.next();
|
||||
it.next();
|
||||
assertEquals(expected, it.hasNext());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[3]",
|
||||
"null null null ;[null]",
|
||||
"null null ;[]",
|
||||
})
|
||||
void testIteratorRemove(String input, String expected) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
Iterator<String> it = array.iterator();
|
||||
it.next();
|
||||
it.remove();
|
||||
it.next();
|
||||
it.remove();
|
||||
assertEquals(expected, array.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1",
|
||||
"1 2" ,
|
||||
"null null null",
|
||||
})
|
||||
void testIteratorRemoveShouldThrowException(String input) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
Iterator<String> it1 = array.iterator();
|
||||
assertThrows(IllegalStateException.class, () -> it1.remove());
|
||||
Iterator<String> it2 = array.iterator();
|
||||
it2.next();
|
||||
it2.remove();
|
||||
assertThrows(IllegalStateException.class, () -> it2.remove());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2",
|
||||
"null null",
|
||||
})
|
||||
void testIteratorNextShouldThrowsException(String input) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
Iterator<String> it = array.iterator();
|
||||
it.next();
|
||||
it.next();
|
||||
assertThrows(NoSuchElementException.class, () -> it.next());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;1",
|
||||
"1 2 ;12",
|
||||
"1 null 3 ;1null3",
|
||||
})
|
||||
void testIteratorForEach(String input, String expected) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String x : array) {
|
||||
sb.append(x);
|
||||
}
|
||||
String actual = sb.toString();
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Stream methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;",
|
||||
"1 2 ;2",
|
||||
"1 2 3 4 ;24",
|
||||
"11 22 33 444 ;22444",
|
||||
})
|
||||
void testStreamFilter(String input, String expected) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
if (expected == null) {
|
||||
expected = "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
array.stream()
|
||||
.filter(x -> Integer.parseInt(x) % 2 == 0)
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"a as asd asdf ;1234",
|
||||
"a asd asdf ;134",
|
||||
"a ;1",
|
||||
})
|
||||
void testStreamMap(String input, String expected) {
|
||||
Utils.fillInWithStrings(array, input);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
array.stream()
|
||||
.map(String::length)
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"a bc def ghij ;ABD",
|
||||
"a def ;AD",
|
||||
"a ;A",
|
||||
})
|
||||
void testStreamMapFilter(String input, String expected) {
|
||||
Array<String> array = new ArrayImpl<>();
|
||||
Utils.fillInWithStrings(array, input);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
array.stream()
|
||||
.filter(s -> s.length() <= 3)
|
||||
.map(String::toUpperCase)
|
||||
.map(s -> s.charAt(0))
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"a bc def ghij ;ABD",
|
||||
"a def ;AD",
|
||||
"a ;A",
|
||||
})
|
||||
void testStreamFilterWildcard(String input, String expected) {
|
||||
Array<CharSequence> array = new ArrayImpl<>();
|
||||
Utils.fillIn(array, input, s -> (CharSequence)s);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
array.stream()
|
||||
.filter(s -> s.length() <= 3)
|
||||
.map(s -> s.charAt(0))
|
||||
.map(Object::toString)
|
||||
.map(String::toUpperCase)
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class Base {
|
||||
|
||||
{
|
||||
if (ComplianceTest.MAKE_ALL_TESTS_FAILED) {
|
||||
Assertions.fail("Compliance tests have not been passed", ComplianceTest.CAUSE);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import spoon.Launcher;
|
||||
import spoon.SpoonAPI;
|
||||
import spoon.reflect.declaration.CtType;
|
||||
import spoon.reflect.reference.CtTypeReference;
|
||||
import spoon.reflect.visitor.filter.TypeFilter;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
@Disabled("This test is used as a trigger to fail all the other tests")
|
||||
class ComplianceTest {
|
||||
|
||||
// Assign this option to false to skip the compliance test
|
||||
// Note, during testing at the stand this option will be turned on!!!
|
||||
// private static final boolean TURN_TEST_COMPLIANCE_ON = false;
|
||||
private static final boolean TURN_TEST_COMPLIANCE_ON = true;
|
||||
|
||||
public static final boolean MAKE_ALL_TESTS_FAILED;
|
||||
|
||||
public static final Throwable CAUSE;
|
||||
|
||||
private static final Object EOL = System.lineSeparator();
|
||||
|
||||
static {
|
||||
L: {
|
||||
try {
|
||||
if (TURN_TEST_COMPLIANCE_ON) {
|
||||
initSpoon();
|
||||
startCompianceTests();
|
||||
}
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
MAKE_ALL_TESTS_FAILED = true;
|
||||
CAUSE = ex.getCause();
|
||||
break L;
|
||||
}
|
||||
MAKE_ALL_TESTS_FAILED = false;
|
||||
CAUSE = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static SpoonAPI spoon;
|
||||
|
||||
private static void initSpoon() {
|
||||
spoon = new Launcher();
|
||||
spoon.addInputResource("src/main/java/");
|
||||
spoon.buildModel();
|
||||
}
|
||||
|
||||
private static void startCompianceTests() throws ReflectiveOperationException {
|
||||
ComplianceTest cTest = new ComplianceTest();
|
||||
for (Method m : ComplianceTest.class.getDeclaredMethods()) {
|
||||
if (Modifier.isPrivate(m.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
Test[] ar = m.getAnnotationsByType(Test.class);
|
||||
if (ar.length > 0 && m.getAnnotationsByType(Test.class)[0] != null) {
|
||||
m.invoke(cTest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
@Test
|
||||
void appShouldNotUseForbiddenAPI() throws IOException, URISyntaxException {
|
||||
URL url = getClass().getResource("forbidden-api-regex.txt");
|
||||
String regex = Files.readString(Path.of(url.toURI()));
|
||||
Pattern forbiddenAPIRegex = Pattern.compile(regex.toString());
|
||||
StringBuilder errorMessage = new StringBuilder();
|
||||
for (CtType<?> ctType : spoon.getModel().getAllTypes()) {
|
||||
List<String> forbiddenAPI = ctType.getElements(new TypeFilter<>(CtTypeReference.class))
|
||||
.stream()
|
||||
.distinct()
|
||||
.filter(r -> forbiddenAPIRegex.matcher(r.toString()).matches())
|
||||
.map(CtTypeReference::getQualifiedName)
|
||||
.toList();
|
||||
if (!forbiddenAPI.isEmpty()) {
|
||||
errorMessage.append(EOL)
|
||||
.append(ctType.getQualifiedName()).append(": ")
|
||||
.append(forbiddenAPI);
|
||||
}
|
||||
}
|
||||
if (!errorMessage.isEmpty()) {
|
||||
fail(() -> "Using of this API is forbidden: " + errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldBeAppropriateNumberOfPackagesAndClasses() throws IOException, URISyntaxException {
|
||||
URL url = getClass().getResource("list-of-types.txt");
|
||||
String expected = Files.readString(Path.of(url.toURI()));
|
||||
// '\n' character is used for clarity in error message
|
||||
String actual = spoon.getModel().getAllPackages().stream()
|
||||
.filter(p -> p.getTypes().size() != 0)
|
||||
.map(p -> p.getTypes().stream()
|
||||
.map(CtType::getQualifiedName)
|
||||
.sorted()
|
||||
.collect(Collectors.joining("\n")))
|
||||
.collect(Collectors.joining("\n"));
|
||||
assertEquals('\n' + expected.stripTrailing(), '\n' + actual);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,312 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class ListImplIntegerTest extends Base {
|
||||
|
||||
private List<Integer> list = new ListImpl<>();
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Container methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[1, 2, 3]",
|
||||
"1 ;[1]",
|
||||
"null 2 null 4 ;[null, 2, null, 4]"
|
||||
})
|
||||
void testAdd(String input, String expected) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3",
|
||||
"1 2",
|
||||
"null null"
|
||||
})
|
||||
void testClear(String input) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
list.clear();
|
||||
assertEquals("[]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClearEmpty() {
|
||||
list.clear();
|
||||
assertEquals("[]", list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;1",
|
||||
"1 2 ;2",
|
||||
"1 2 3 null ;4",
|
||||
})
|
||||
void testSize(String input, String expectedStr) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
int expected = Integer.parseInt(expectedStr);
|
||||
assertEquals(expected, list.size());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// List methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 2 ;1",
|
||||
"2 1 1 ;2",
|
||||
"null 2 ;null",
|
||||
})
|
||||
void testGetFirst(String input, String expectedStr) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Integer expected = Utils.parseInt(expectedStr);
|
||||
assertEquals(expected, list.getFirst());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 2 ;2",
|
||||
"2 1 1 ;1",
|
||||
"1 null ;null",
|
||||
})
|
||||
void testGetLast(String input, String expectedStr) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Integer expected = Utils.parseInt(expectedStr);
|
||||
assertEquals(expected, list.getLast());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[2, 3]",
|
||||
"null 1 2 ;[1, 2]",
|
||||
"null ;[]",
|
||||
"1 ;[]",
|
||||
})
|
||||
void testRemoveFirst(String input, String expected) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
list.removeFirst();
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[1, 2]",
|
||||
"null 2 3 ;[null, 2]",
|
||||
"null ;[]",
|
||||
"1 ;[]",
|
||||
})
|
||||
void testRemoveLast(String input, String expected) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
list.removeLast();
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;7 ;[7, 1, 2, 3]",
|
||||
"1 2 3 ;null ;[null, 1, 2, 3]",
|
||||
"null 2 null ;null ;[null, null, 2, null]",
|
||||
})
|
||||
void testAddFirst(String input, String xStr, String expected) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Integer x = Utils.parseInt(xStr);
|
||||
list.addFirst(x);
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;7 ;null",
|
||||
"1 2 3 ;3 ;3",
|
||||
"null 2 null ;null ;null",
|
||||
})
|
||||
void testSearch(String input, String xStr, String expectedStr) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Integer x = Utils.parseInt(xStr);
|
||||
Integer expected = Utils.parseInt(expectedStr);
|
||||
Integer actual = list.search(x);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;7 ;[1, 2, 3] ;false",
|
||||
"1 2 3 ;2 ;[1, 3] ;true",
|
||||
"1 2 3 ;3 ;[1, 2] ;true",
|
||||
"null 2 null ;null ;[2, null] ;true",
|
||||
})
|
||||
void testRemove(String input, String xStr, String expected, String isExistStr) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Integer x = Utils.parseInt(xStr);
|
||||
boolean isExistActual = list.remove(x);
|
||||
boolean isExistExpected = Utils.parseBoolean(isExistStr);
|
||||
assertEquals(expected, list.toString());
|
||||
assertEquals(isExistExpected, isExistActual);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Iterator methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;2",
|
||||
"null 3 ;3",
|
||||
"1 null 3 ;null",
|
||||
"null null ;null"
|
||||
})
|
||||
void testIteratorNext(String input, String expectedStr) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Iterator<Integer> it = list.iterator();
|
||||
Integer expected = Utils.parseInt(expectedStr);
|
||||
it.next();
|
||||
assertEquals(expected, it.next());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;true",
|
||||
"null 3 ;false",
|
||||
"1 null ;false"
|
||||
})
|
||||
void testIteratorHasNext(String input, String expectedStr) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Boolean expected = Utils.parseBoolean(expectedStr);
|
||||
Iterator<Integer> it = list.iterator();
|
||||
it.next();
|
||||
it.next();
|
||||
assertEquals(expected, it.hasNext());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[3]",
|
||||
"null null null ;[null]",
|
||||
})
|
||||
void testIteratorRemove(String input, String expected) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Iterator<Integer> it = list.iterator();
|
||||
it.next();
|
||||
it.remove();
|
||||
it.next();
|
||||
it.remove();
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1",
|
||||
"1 2" ,
|
||||
"null null null",
|
||||
})
|
||||
void testIteratorRemoveShouldThrowException(String input) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Iterator<Integer> it1 = list.iterator();
|
||||
assertThrows(IllegalStateException.class, () -> it1.remove());
|
||||
Iterator<Integer> it2 = list.iterator();
|
||||
it2.next();
|
||||
it2.remove();
|
||||
assertThrows(IllegalStateException.class, () -> it2.remove());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2",
|
||||
"null null",
|
||||
})
|
||||
void testIteratorNextShouldThrowsException(String input) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
Iterator<Integer> it = list.iterator();
|
||||
it.next();
|
||||
it.next();
|
||||
assertThrows(NoSuchElementException.class, () -> it.next());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;1",
|
||||
"1 2 ;12",
|
||||
"1 null 3 ;1null3",
|
||||
})
|
||||
void testIteratorForEach(String input, String expected) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Integer x : list) {
|
||||
sb.append(x);
|
||||
}
|
||||
String actual = sb.toString();
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Stream methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;",
|
||||
"1 2 ;2",
|
||||
"1 2 3 4 ;24",
|
||||
})
|
||||
void testStreamFilter(String input, String expected) {
|
||||
Utils.fillInWithIntegers(list, input);
|
||||
if (expected == null) {
|
||||
expected = "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
list.stream()
|
||||
.filter(x -> x % 2 == 0)
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 4 ;++|++++|",
|
||||
"2 3 4 ;++|++++|",
|
||||
"2 ;++|",
|
||||
})
|
||||
void testStreamFilterWildcard(String input, String expected) {
|
||||
Utils.fillIn(list, input, Integer::parseInt);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
list.stream()
|
||||
.filter(x -> x % 2 == 0)
|
||||
.map(x -> "+".repeat(x))
|
||||
.forEach(x -> sb.append(x).append('|'));
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,354 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
class ListImplStringTest extends Base {
|
||||
|
||||
private List<String> list = new ListImpl<>();
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Container methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[1, 2, 3]",
|
||||
"1 ;[1]",
|
||||
"null 2 null 4 ;[null, 2, null, 4]",
|
||||
"a b c ;[a, b, c]",
|
||||
"1 b null ;[1, b, null]",
|
||||
})
|
||||
void testAdd(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3",
|
||||
"1 2",
|
||||
"null null"
|
||||
})
|
||||
void testClear(String input) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
list.clear();
|
||||
assertEquals("[]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClearEmpty() {
|
||||
list.clear();
|
||||
assertEquals("[]", list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;1",
|
||||
"1 2 ;2",
|
||||
"1 2 3 null ;4",
|
||||
})
|
||||
void testSize(String input, String expectedStr) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
int expected = Integer.parseInt(expectedStr);
|
||||
assertEquals(expected, list.size());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// List methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 2 ;1",
|
||||
"2 1 1 ;2",
|
||||
"null 2 ;null",
|
||||
})
|
||||
void testGetFirst(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
assertEquals(expected, list.getFirst());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 2 ;2",
|
||||
"2 1 1 ;1",
|
||||
"1 null ;null",
|
||||
})
|
||||
void testGetLast(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
assertEquals(expected, list.getLast());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[2, 3]",
|
||||
"null 1 2 ;[1, 2]",
|
||||
"null ;[]",
|
||||
"1 ;[]",
|
||||
})
|
||||
void testRemoveFirst(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
list.removeFirst();
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[1, 2]",
|
||||
"null 2 3 ;[null, 2]",
|
||||
"null ;[]",
|
||||
"1 ;[]",
|
||||
})
|
||||
void testRemoveLast(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
list.removeLast();
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;7 ;[7, 1, 2, 3]",
|
||||
"1 2 3 ;null ;[null, 1, 2, 3]",
|
||||
"null 2 null ;null ;[null, null, 2, null]",
|
||||
})
|
||||
void testAddFirst(String input, String xStr, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
list.addFirst(xStr);
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;7 ;null",
|
||||
"1 2 3 ;3 ;3",
|
||||
"null 2 null ;null ;null",
|
||||
})
|
||||
void testSearch(String input, String xStr, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
String x = xStr;
|
||||
if (x != null && "null".equals(x)) {
|
||||
x = null;
|
||||
}
|
||||
if ("null".equals(expected)) {
|
||||
expected = null;
|
||||
}
|
||||
String actual = list.search(x);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;7 ;[1, 2, 3] ;false",
|
||||
"1 2 3 ;2 ;[1, 3] ;true",
|
||||
"1 2 3 ;3 ;[1, 2] ;true",
|
||||
"null 2 null ;null ;[2, null] ;true",
|
||||
})
|
||||
void testRemove(String input, String xStr, String expected, String isExistStr) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
String x = xStr;
|
||||
boolean isExistActual = list.remove(x);
|
||||
boolean isExistExpected = Utils.parseBoolean(isExistStr);
|
||||
assertEquals(expected, list.toString());
|
||||
assertEquals(isExistExpected, isExistActual);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Iterator methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;2",
|
||||
"null 3 ;3",
|
||||
"1 null 3 ;null",
|
||||
"null null ;null"
|
||||
})
|
||||
void testIteratorNext(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
Iterator<String> it = list.iterator();
|
||||
it.next();
|
||||
assertEquals(expected, it.next());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;true",
|
||||
"null 3 ;false",
|
||||
"1 null ;false"
|
||||
})
|
||||
void testIteratorHasNext(String input, String expectedStr) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
Boolean expected = Utils.parseBoolean(expectedStr);
|
||||
Iterator<String> it = list.iterator();
|
||||
it.next();
|
||||
it.next();
|
||||
assertEquals(expected, it.hasNext());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2 3 ;[3]",
|
||||
"null null null ;[null]",
|
||||
})
|
||||
void testIteratorRemove(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
Iterator<String> it = list.iterator();
|
||||
it.next();
|
||||
it.remove();
|
||||
it.next();
|
||||
it.remove();
|
||||
assertEquals(expected, list.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1",
|
||||
"1 2" ,
|
||||
"null null null",
|
||||
})
|
||||
void testIteratorRemoveShouldThrowException(String input) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
Iterator<String> it1 = list.iterator();
|
||||
assertThrows(IllegalStateException.class, () -> it1.remove());
|
||||
Iterator<String> it2 = list.iterator();
|
||||
it2.next();
|
||||
it2.remove();
|
||||
assertThrows(IllegalStateException.class, () -> it2.remove());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 2",
|
||||
"null null",
|
||||
})
|
||||
void testIteratorNextShouldThrowsException(String input) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
Iterator<String> it = list.iterator();
|
||||
it.next();
|
||||
it.next();
|
||||
assertThrows(NoSuchElementException.class, () -> it.next());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;1",
|
||||
"1 2 ;12",
|
||||
"1 null 3 ;1null3",
|
||||
})
|
||||
void testIteratorForEach(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String x : list) {
|
||||
sb.append(x);
|
||||
}
|
||||
String actual = sb.toString();
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Stream methods.
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"1 ;",
|
||||
"1 2 ;2",
|
||||
"1 2 3 4 ;24",
|
||||
})
|
||||
void testStreamFilter(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
if (expected == null) {
|
||||
expected = "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
list.stream()
|
||||
.filter(x -> Utils.parseInt(x) % 2 == 0)
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"a as asd asdf ;1234",
|
||||
"a asd asdf ;134",
|
||||
"a ;1",
|
||||
})
|
||||
|
||||
void testStreamMap(String input, String expected) {
|
||||
Utils.fillInWithStrings(list, input);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
list.stream()
|
||||
.map(String::length)
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"a bc def ghij ;ABD",
|
||||
"a def ;AD",
|
||||
"a ;A",
|
||||
})
|
||||
void testStreamMapFilter(String input, String expected) {
|
||||
Array<String> list = new ArrayImpl<>();
|
||||
Utils.fillInWithStrings(list, input);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
list.stream()
|
||||
.filter(s -> s.length() <= 3)
|
||||
.map(String::toUpperCase)
|
||||
.map(s -> s.charAt(0))
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@CsvSource(delimiter = ';',
|
||||
value = {
|
||||
"a bc def ghij ;ABD",
|
||||
"a def ;AD",
|
||||
"a ;A",
|
||||
})
|
||||
void testStreamFilterWildcard(String input, String expected) {
|
||||
Array<CharSequence> list = new ArrayImpl<>();
|
||||
Utils.fillIn(list, input, s -> (CharSequence)s);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
list.stream()
|
||||
.filter(s -> s.length() <= 3)
|
||||
.map(s -> s.charAt(0))
|
||||
.map(Object::toString)
|
||||
.map(String::toUpperCase)
|
||||
.forEach(sb::append);
|
||||
assertEquals(expected, sb.toString());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package ua.nure.jfn.task2;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author Dmytro Kolesnykov
|
||||
*/
|
||||
public class Utils {
|
||||
|
||||
public static <T extends Container<Integer>> void fillInWithIntegers(T container, String input) {
|
||||
fillIn(container, input, Utils::parseInt);
|
||||
}
|
||||
|
||||
public static <T extends Container<String>> void fillInWithStrings(T container, String input) {
|
||||
fillIn(container, input, Function.identity());
|
||||
}
|
||||
|
||||
public static <E, T extends Container<E>> void fillIn(T container, String input, Function<String, E> func) {
|
||||
Arrays.stream(input.split("\\s+"))
|
||||
.map(func)
|
||||
.forEach(container::add);
|
||||
}
|
||||
|
||||
public static Integer parseInt(String str) {
|
||||
return "null".equals(str) ? null : Integer.valueOf(str);
|
||||
}
|
||||
|
||||
public static Boolean parseBoolean(String str) {
|
||||
return "null".equals(str) ? null : Boolean.valueOf(str);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
(?x)
|
||||
^
|
||||
java\.util\.(?!Arrays|Iterator|NoSuchElementException)\S+
|
||||
$
|
@ -0,0 +1,8 @@
|
||||
ua.nure.jfn.task2.Array
|
||||
ua.nure.jfn.task2.ArrayImpl
|
||||
ua.nure.jfn.task2.Container
|
||||
ua.nure.jfn.task2.Demo
|
||||
ua.nure.jfn.task2.List
|
||||
ua.nure.jfn.task2.ListImpl
|
||||
ua.nure.jfn.task2.Stream
|
||||
ua.nure.jfn.task2.StreamImpl
|
BIN
semester-4/ОПJа/lb-2/tasks.pdf
Normal file
BIN
semester-4/ОПJа/lb-2/tasks.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user