import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class LoopEntryPointSolutionOneTest {
private LNode createLinkedList(int numOfNode, int headNodePos) {
LNode head = new LNode(0, null);
LNode current = new LNode(1, null);
LNode loopHead = null;
if(headNodePos == 1) loopHead = current;
head.next = current;
for(int i = 2; i <= numOfNode; i++) {
current.next = new LNode(i, null);
current = current.next;
if(i == headNodePos) {
loopHead = current;
}
}
current.next = loopHead;
return head;
}
private LNode getHeadNode(LNode head, int headNodePos) {
LNode current = head.next;
for(int i = 1; i < headNodePos; i++) {
current = current.next;
}
return current;
}
@Test
public void testLoop() {
int numOfNode = 5;
int headNodePos = 2;
LNode head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head).value == headNodePos);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head).value == headNodePos);
numOfNode = 5;
headNodePos = 5;
head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head).value == headNodePos);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head).value == headNodePos);
numOfNode = 5;
headNodePos = 1;
head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head).value == headNodePos);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head).value == headNodePos);
numOfNode = 1;
headNodePos = 1;
head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head).value == headNodePos);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head).value == headNodePos);
numOfNode = 7;
headNodePos = 5;
head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head).value == headNodePos);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head).value == headNodePos);
numOfNode = 800;
headNodePos = 3;
head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head).value == headNodePos);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head).value == headNodePos);
}
@Test
public void testNoLoop() {
int numOfNode = 5;
int headNodePos = 0;
LNode head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head) == null);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head) == null);
numOfNode = 1;
headNodePos = 0;
head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head) == null);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head) == null);
numOfNode = 7;
headNodePos = 0;
head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head) == null);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head) == null);
numOfNode = 800;
headNodePos = 0;
head = createLinkedList(numOfNode, headNodePos);
LoopEntryPointSolutionOne.printResult(head, getHeadNode(head, headNodePos));
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head) == null);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head) == null);
}
@Test
public void testNull() {
LNode head = null;
LoopEntryPointSolutionOne.printResult(head, null);
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head) == null);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head) == null);
head = new LNode(0, new LNode(1, null));
LoopEntryPointSolutionOne.printResult(head, null);
assertTrue(LoopEntryPointSolutionOne.hasLoopBrutalForce(head) == null);
assertTrue(LoopEntryPointSolutionOne.hasLoopSlowFastPointer(head) == null);
}
}