-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestLinkedList.java
More file actions
75 lines (57 loc) · 2.1 KB
/
TestLinkedList.java
File metadata and controls
75 lines (57 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package interviews;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import org.junit.Assert;
public class TestLinkedList {
@Test
public void testInsertValue() {
// create object of the class under test
LinkedList list = new LinkedList();
// create an object of random class to generate a random integer
Random rn = new Random();
// get a random integer
int expected = rn.nextInt();
// insert that random integer into linked list
list.InsertValue(expected);
// Receive all values present in the Linked List
ArrayList<Integer> tempList = list.covertToArrayList(LinkedList.head);
// Extract last element present in the Linked List (newest element)
int actual = tempList.get(tempList.size()-1);
System.out.println("Expected: " + expected);
System.out.println("Actual: " + actual);
// Make sure that the element we tried to insert has been added to linked list
Assert.assertEquals(expected, actual);
System.out.println("Pass"); // This line executed only if above assert statement is executed successfully.
}
@Test
public void testReverse() {
LinkedList list = new LinkedList();
ArrayList<Integer> myList = new ArrayList<Integer>();
Random rn = new Random();
int number = rn.nextInt(10);
/*
* Generate random integers and insert them to linked list as well as
* our own arrayList
*/
int i=0;
while(i<number) {
int value = rn.nextInt();
myList.add(value);
list.InsertValue(value);
i++;
}
// Reverse our own array list
Collections.reverse(myList);
// Reverse linked list (method under test)
LinkedList.head = list.reverse(LinkedList.head);
// Get the elements of Linked list after they are reversed
ArrayList<Integer> actual = list.covertToArrayList(LinkedList.head);
System.out.println("Expected: " + myList);
System.out.println("Actual: " + actual);
// Check that reversed list matches our own reversed array list
Assert.assertEquals(myList, actual);
System.out.println("Pass");
}
}