migration
This commit is contained in:
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
|
Reference in New Issue
Block a user