import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
class Node {
int value;
Node next;
Node(int value) {
this.value = value;
this.next = null;
}
}
class CustomLinkedList {
Node head;
CustomLinkedList(){
head = null;
}
public void add(int value) {
if (head == null) {
head = new Node(value);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(value);
}
public boolean remove(int value) {
if (head == null) return false;
if (head.value == value) {
head = head.next;
return true;
}
Node current = head;
while (current.next != null) {
if (current.next.value == value) {
current.next = current.next.next;
return true;
}
current = current.next;
}
return false;
}
public boolean find(int value) {
Node current = head;
while (current != null) {
if (current.value == value) {
return true;
}
current = current.next;
}
return false;
}
}
class CustomLinkedListTest {
@Test
void testAdd() {
CustomLinkedList list = new CustomLinkedList();
list.add(1);
assertTrue(list.find(1));
list.add(2);
assertTrue(list.find(2));
list.add(3);
assertTrue(list.find(3));
}
@Test
void testRemoveExisting() {
CustomLinkedList list = new CustomLinkedList();
list.add(1);
list.add(2);
list.add(3);
assertTrue(list.remove(2));
assertFalse(list.find(2));
assertTrue(list.remove(1));
assertFalse(list.find(1));
assertTrue(list.remove(3));
assertFalse(list.find(3));
}
@Test
void testRemoveNonExisting() {
CustomLinkedList list = new CustomLinkedList();
list.add(1);
list.add(2);
list.add(3);
assertFalse(list.remove(4));
assertTrue(list.find(1));
assertTrue(list.find(2));
assertTrue(list.find(3));
}
@Test
void testFind() {
CustomLinkedList list = new CustomLinkedList();
list.add(1);
list.add(2);
list.add(3);
assertTrue(list.find(1));
assertTrue(list.find(2));
assertTrue(list.find(3));
assertFalse(list.find(4));
}
@Test
void testEmptyList() {
CustomLinkedList list = new CustomLinkedList();
assertFalse(list.find(1));
assertFalse(list.remove(1));
}
}
class TestRunner {
public static void main
(String[] args
) { Result result = JUnitCore.runClasses(CustomLinkedListTest.class);
for (Failure failure : result.getFailures()) {
System.
out.
println(failure.
toString()); }
System.
out.
println(result.
wasSuccessful()); }
}