Skip to content

_02_04_ make tests more general #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions src/test/java/linkedlist/_02_04_PartitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertEquals;

public class _02_04_PartitionTest {
Expand All @@ -15,27 +20,59 @@ public void withEmptyList() {

@Test
public void withSortedList() {
assertEquals(LinkedListNode.of(1, 2, 3), s.partition(LinkedListNode.of(1, 2, 3), 2));
assertPartition(LinkedListNode.of(1, 2, 3), s.partition(LinkedListNode.of(1, 2, 3), 2), 2);
}

@Test
public void withSortedList_AndOutOfListX() {
assertEquals(LinkedListNode.of(3, 2, 1), s.partition(LinkedListNode.of(1, 2, 3), 4));
assertPartition(LinkedListNode.of(3, 2, 1), s.partition(LinkedListNode.of(1, 2, 3), 4), 4);
}

@Test
public void withSortedList_AndOutOfListX_Smaller() {
assertEquals(LinkedListNode.of(1, 2, 3), s.partition(LinkedListNode.of(1, 2, 3), 0));
assertPartition(LinkedListNode.of(1, 2, 3), s.partition(LinkedListNode.of(1, 2, 3), 0), 0);
}

@Test
public void withUnSortedList() {
assertEquals(LinkedListNode.of(1, 2, 4, 3, 5), s.partition(LinkedListNode.of(4, 3, 2, 5, 1), 3));
assertPartition(LinkedListNode.of(1, 2, 4, 3, 5), s.partition(LinkedListNode.of(4, 3, 2, 5, 1), 3), 3);
}

@Test
public void withUnSortedList_AndOutOfScopeX() {
assertEquals(LinkedListNode.of(1, 2, 4, 3, 6), s.partition(LinkedListNode.of(3, 4, 2, 6, 1), 5));
assertPartition(LinkedListNode.of(1, 2, 4, 3, 6), s.partition(LinkedListNode.of(3, 4, 2, 6, 1), 5), 5);
}

private void assertPartition(LinkedListNode input, LinkedListNode actual, int x) {
List<Integer> original = toList(input);
List<Integer> result = toList(actual);

// Check that elements are the same (ignoring order)
List<Integer> originalSorted = new ArrayList<>(original);
List<Integer> resultSorted = new ArrayList<>(result);
Collections.sort(originalSorted);
Collections.sort(resultSorted);
assertEquals("Partitioned list must contain the same elements", originalSorted, resultSorted);

// Check partition order
boolean seenRightPartition = false;
for (int value : result) {
if (value < x) {
if (seenRightPartition) {
fail("Elements less than x appeared after elements greater than or equal to x");
}
} else {
seenRightPartition = true;
}
}
}

}
private List<Integer> toList(LinkedListNode node) {
List<Integer> list = new ArrayList<>();
while (node != null) {
list.add(node.val);
node = node.next;
}
return list;
}
}