Last active
August 29, 2015 13:58
-
-
Save cjlucas/9964717 to your computer and use it in GitHub Desktop.
ListTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myList; | |
import myList.List; | |
import org.junit.Before; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
/** | |
* Created by chris on 4/3/14. | |
*/ | |
public class ListTest { | |
private List list; // [0, 1, 2, 3, 4] | |
private List listTwo; // [5, 6, 7, 8, 9] | |
@Before | |
public void setUp() throws Exception { | |
this.list = new List(); | |
this.listTwo = new List(); | |
List.Handle handle1 = this.list.handle(); | |
List.Handle handle2 = this.listTwo.handle(); | |
for (int i = 0; i < 5; i++) { | |
handle1 = handle1.insertAfter(i); | |
handle2 = handle2.insertAfter(i + 5); | |
} | |
} | |
@Test | |
public void testCopy() throws Exception { | |
List listCopy = this.list.copy(); | |
assertEquals(5, this.list.size()); | |
assertEquals(5, listCopy.size()); | |
List.Handle listHandle = this.list.handle(); | |
List.Handle listCopyHandle = listCopy.handle(); | |
for (int i = 0; i < this.list.size(); i++) { | |
listHandle.forward(); | |
listCopyHandle.forward(); | |
assertEquals(listHandle.item(), listCopyHandle.item()); | |
} | |
} | |
@Test | |
public void testPush() throws Exception { | |
this.list.push(-1); | |
this.list.push(-2); | |
assertEquals(7, this.list.size()); | |
// list should now be [-2, -1, 0, 1, 2, 3, 4] | |
List.Handle handle = this.list.handle(); | |
for (int i = -2; i <= 4; i++) { | |
handle.forward(); | |
assertEquals(i, handle.item()); | |
} | |
} | |
@Test | |
public void testPop() throws Exception { | |
// pop is destructive | |
assertEquals(0, this.list.pop()); | |
assertEquals(1, this.list.pop()); | |
assertEquals(2, this.list.pop()); | |
assertEquals(3, this.list.pop()); | |
assertEquals(4, this.list.pop()); | |
assertEquals(0, this.list.size()); | |
} | |
@Test | |
public void testPut() throws Exception { | |
this.list.put(5); | |
this.list.put(6); | |
assertEquals(7, this.list.size()); | |
// list should be [0, 1, 2, 3, 4, 5, 6] | |
List.Handle handle = this.list.handle(); | |
for (int i = 0; i <= 6; i++) { | |
handle.forward(); | |
assertEquals(i, handle.item()); | |
} | |
} | |
@Test | |
public void testGet() throws Exception { | |
// get is destructive | |
assertEquals(0, this.list.get()); | |
assertEquals(1, this.list.get()); | |
assertEquals(2, this.list.get()); | |
assertEquals(3, this.list.get()); | |
assertEquals(4, this.list.get()); | |
assertEquals(0, this.list.size()); | |
} | |
@Test | |
public void testPull() throws Exception { | |
// pull is the destructive version of rear | |
assertEquals(4, this.list.pull()); | |
assertEquals(3, this.list.pull()); | |
assertEquals(2, this.list.pull()); | |
assertEquals(1, this.list.pull()); | |
assertEquals(0, this.list.pull()); | |
assertEquals(0, this.list.size()); | |
} | |
@Test | |
public void testTop() throws Exception { | |
// top is not destructive | |
assertEquals(0, this.list.top()); | |
assertEquals(0, this.list.top()); | |
} | |
@Test | |
public void testFront() throws Exception { | |
// front is not destructive | |
assertEquals(0, this.list.front()); | |
assertEquals(0, this.list.front()); | |
} | |
@Test | |
public void testRear() throws Exception { | |
// rear is not destructive | |
assertEquals(4, this.list.rear()); | |
assertEquals(4, this.list.rear()); | |
} | |
@Test | |
public void testConcatenate() throws Exception { | |
List concatenatedList = this.list.concatenate(this.listTwo); | |
assertEquals(10, this.list.size()); | |
assertTrue(this.listTwo.empty()); // spec says the arg for concatenate should be emptied | |
assertEquals(this.list, concatenatedList); // concatenate should return itself | |
List.Handle handle = this.list.handle(); | |
for (int i = 0; i < 10; i++) { | |
handle.forward(); | |
assertEquals(i, handle.item()); | |
} | |
} | |
@Test | |
public void testSplitBefore01() throws Exception { | |
List.Handle handle = this.list.handle(); | |
// handle is pointed at second element (handle.item() == 1) | |
handle.forward(); | |
handle.forward(); | |
List split = this.list.splitBefore(handle); | |
assertEquals(1, this.list.size()); // splitBefore is destructive | |
assertEquals(4, split.size()); | |
// list should be [0] | |
handle = this.list.handle(); | |
handle.forward(); | |
assertEquals(0, handle.item()); | |
// split should be [1, 2, 3, 4] (splitBefore should include the item at the split point) | |
List.Handle splitHandle = split.handle(); | |
for (int i = 1; i <= 4; i++) { | |
splitHandle.forward(); | |
assertEquals(i, splitHandle.item()); | |
} | |
} | |
@Test | |
public void testSplitBefore02() throws Exception { | |
List.Handle handle = this.list.handle(); | |
// handle is pointed at first element (handle.item() == 0) | |
handle.forward(); | |
List split = this.list.splitBefore(handle); | |
assertEquals(0, this.list.size()); // splitBefore is destructive | |
assertEquals(5, split.size()); | |
// split should be [0, 1, 2, 3, 4] (splitBefore should include the item at the split point) | |
List.Handle splitHandle = split.handle(); | |
for (int i = 0; i <= 4; i++) { | |
splitHandle.forward(); | |
assertEquals(i, splitHandle.item()); | |
} | |
} | |
@Test | |
public void testSplitAfter01() throws Exception { | |
List.Handle handle = this.list.handle(); | |
// handle is pointed at the second element (handle.item() == 1) | |
handle.forward(); | |
handle.forward(); | |
List split = this.list.splitAfter(handle); | |
assertEquals(2, this.list.size()); | |
assertEquals(3, split.size()); | |
// list should be [0, 1] | |
handle = this.list.handle(); | |
for (int i = 0; i <= 1; i++) { | |
handle.forward(); | |
assertEquals(i, handle.item()); | |
} | |
// split should be [2, 3, 4] | |
handle = split.handle(); | |
for (int i = 2; i <= 4; i++) { | |
handle.forward(); | |
assertEquals(i, handle.item()); | |
} | |
} | |
@Test | |
public void testSplitAfter02() throws Exception { | |
List.Handle handle = this.list.handle(); | |
// handle is pointed at the last element (handle.item() == 4) | |
handle.forward(); | |
handle.forward(); | |
handle.forward(); | |
handle.forward(); | |
handle.forward(); | |
List split = this.list.splitAfter(handle); | |
assertEquals(5, this.list.size()); | |
assertEquals(0, split.size()); | |
// list should be [0, 1, 2, 3, 4] | |
handle = this.list.handle(); | |
for (int i = 0; i <= 4; i++) { | |
handle.forward(); | |
assertEquals(i, handle.item()); | |
} | |
} | |
@Test | |
public void testEmpty() throws Exception { | |
assertTrue(new List().empty()); | |
this.list.clear(); | |
assertTrue(this.list.empty()); | |
} | |
@Test | |
public void testSize() throws Exception { | |
assertEquals(5, this.list.size()); | |
} | |
@Test | |
public void testClear() throws Exception { | |
this.list.clear(); | |
assertEquals(0, this.list.size()); | |
} | |
@Test | |
public void testReverse() throws Exception { | |
this.list.reverse(); | |
List.Handle handle = this.list.handle(); | |
handle.forward(); | |
assertEquals(4, handle.item()); | |
handle.forward(); | |
assertEquals(3, handle.item()); | |
handle.forward(); | |
assertEquals(2, handle.item()); | |
handle.forward(); | |
assertEquals(1, handle.item()); | |
handle.forward(); | |
assertEquals(0, handle.item()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment