Site Search:

SolutionFiveTest.java


import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class SolutionFiveTest {

    @Test
    public void test() {
        MyNode head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6});
        SolutionFive.printResult(head);
        head = SolutionFive.reverse(head);
        SolutionFive.printResult(head);
        MyNode current = head.next;
        assertEquals(6, current.value);
        current = current.next;
        assertEquals(5, current.value);
        current = current.next;
        assertEquals(4, current.value);
        current = current.next;
        assertEquals(3, current.value);
        current = current.next;
        assertEquals(2, current.value);
        current = current.next;
        assertEquals(1, current.value);
        current = current.next;
        assertEquals(null, current);
    }
    
    @Test
    public void test1() {
        MyNode head = createLinkedList(new int[] {1});
        SolutionFive.printResult(head);
        head = SolutionFive.reverse(head);
        SolutionFive.printResult(head);
        MyNode current = head.next;
        assertEquals(1, current.value);
        current = current.next;
        assertEquals(null, current);
    }
    
    @Test
    public void testNull() {
        MyNode head = createLinkedList(new int[] {});
        SolutionFive.printResult(head);
        head = SolutionFive.reverse(head);
        SolutionFive.printResult(head);
        MyNode current = head.next;
        assertEquals(null, current);
        
        SolutionFive.printResult(null);
        head = SolutionFive.reverse(null);
        SolutionFive.printResult(null);
        assertEquals(null, head);
    }
    
    public static MyNode createLinkedList(int[] values) {
        MyNode head = new MyNode(0, null);
        if(values.length == 0) {
            return head;
        }
        head.next = new MyNode(values[0], null);
        
        MyNode current = head.next;
        for(int i = 1; i < values.length; i++) {
            current.next = new MyNode(values[i], null);
            current = current.next;
        }
        return head;
    }

}