Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
1 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
/**
*
*/
package gov.va.med.ewv.util;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.Test;
/**
* @author
DNS
*
*/
public class SetToSortedListTest {
private void show(String label, List<Integer> stuff) {
System.out.print(label + ": [");
if (stuff == null) {
System.out.print(stuff);
} else {
int i = 0;
for (Object it : stuff) {
System.out.print(it);
i++;
if (i < stuff.size()) {
System.out.print(", ");
}
}
}
System.out.println("]");
}
private void show(String label, Set<Integer> stuff) {
System.out.print(label + ": [");
if (stuff == null) {
System.out.print(stuff);
} else {
int i = 0;
for (Object it : stuff) {
System.out.print(it);
i++;
if (i < stuff.size()) {
System.out.print(", ");
}
}
}
System.out.println("]");
}
/**
* Test method for {@link gov.va.med.ewv.util.SetToSortedList#toSortedList(java.util.Set, java.util.Comparator)}.
*/
@Test
public void testToSortedList() {
// public static <T> List<T> toSortedList(Set<T> set, Comparator<T> comparator) {
// if (set == null) return Collections.<T>emptyList();
// return set.stream()
// .sorted(comparator) // What will this match?
// .collect(Collectors.toList());
// }
int[] iArr = {3,1,4,1,5,9,2,6,5,3,5,8,9};
List<Integer> iList = IntStream.of(iArr)
.boxed().collect(Collectors.toList());
List<Integer> iDistSorted = IntStream.of(iArr)
.distinct()
.sorted()
.boxed().collect(Collectors.toList());
Set<Integer> iSet = IntStream.of(iArr)
.boxed().collect(Collectors.toSet());
assertEquals(iList.size(), iArr.length );
assertEquals(iDistSorted.size(), iSet.size());
List<Integer> iSetToList = SetToSortedList.toSortedList(iSet, Integer::compare);
assertEquals(iSetToList.size(), iSet.size());
assertEquals(iSetToList, iDistSorted);
}
/**
* Test method for {@link gov.va.med.ewv.util.SetToSortedList#toNullsFirstList(java.util.Set, java.util.Comparator)}.
*/
@Test
public void testToNullsFirstList() {
// Here, we must also have some nulls to test.
// But you can only have one null in a set!
int[] iArr = {3,1,4,1,5,9,2,6,5,3,5,8,9}; // Very specific, do not change
List<Integer> iDist1 = IntStream.of(iArr)
.distinct() // {3,1,4,5,9,2,6,8}
.sorted() // {1,2,3,4,5,6,8,9}
.boxed() // Integer({1,2,3,4,5,6,8,9})
.map((iv) -> { return iv.equals(1) ? null : iv; }) // convert Integer(1) to null (at beginning)
.collect(Collectors.toList()); // {null, Integer(2), Integer(3), Integer(4), Integer(5), Integer(6), Integer(8), Integer(9)}
List<Integer> iDist9 = IntStream.of(iArr)
.distinct()
.sorted()
.boxed()
.map((iv) -> { return iv.equals(9) ? null : iv; }) // convert Integer(9) to null at end
.collect(Collectors.toList()); // {Integer(1), Integer(2), Integer(3), Integer(4), Integer(5), Integer(6), Integer(8), null}
Set<Integer> iSet1 = IntStream.of(iArr)
.boxed()
.map((iv) -> { return iv.equals(1) ? null : iv; }) // convert Integer(1) to null
.collect(Collectors.toSet()); // You can put a null in a set
Set<Integer> iSet9 = IntStream.of(iArr)
.boxed()
.map((iv) -> { return iv.equals(9) ? null : iv; }) // convert Integer(9) to null
.collect(Collectors.toSet());
// Verify setup
assertEquals(iDist1.size(), iSet1.size());
assertEquals(iDist9.size(), iSet9.size());
List<Integer> iSetList1 = SetToSortedList.toNullsFirstList(iSet1, Integer::compare);
List<Integer> iSetList9 = SetToSortedList.toNullsFirstList(iSet9, Integer::compare);
// Show results
show("iDist1", iDist1);
show("iDist9", iDist9);
show("iSet1", iSet1);
show("iSet9", iSet9);
show("iSetList1", iSetList1);
show("iSetList9", iSetList9);
assertEquals(iDist1.size(), iSetList1.size());
assertEquals(iDist9.size(), iSetList9.size());
assertEquals(iDist1, iSetList1); // Since null is first
assertNotEquals(iDist9, iSetList9); // Since null is last
}
/**
* Test method for {@link gov.va.med.ewv.util.SetToSortedList#toNullsLastList(java.util.Set, java.util.Comparator)}.
*/
@Test
public void testToNullsLastList() {
// Here, we must also have some nulls to test.
// But you can only have one null in a set!
int[] iArr = {3,1,4,1,5,9,2,6,5,3,5,8,9}; // Very specific, do not change
List<Integer> iDist1 = IntStream.of(iArr)
.distinct()
.sorted()
.boxed()
.map((iv) -> { return iv.equals(1) ? null : iv; }) // convert Integer(1) to null (at beginning)
.collect(Collectors.toList());
List<Integer> iDist9 = IntStream.of(iArr)
.distinct()
.sorted()
.boxed()
.map((iv) -> { return iv.equals(9) ? null : iv; }) // convert Integer(9) to null at end
.collect(Collectors.toList());
Set<Integer> iSet1 = IntStream.of(iArr)
.boxed()
.map((iv) -> { return iv.equals(1) ? null : iv; }) // convert Integer(1) to null
.collect(Collectors.toSet());
Set<Integer> iSet9 = IntStream.of(iArr)
.boxed()
.map((iv) -> { return iv.equals(9) ? null : iv; }) // convert Integer(9) to null
.collect(Collectors.toSet());
// Verify setup
assertEquals(iDist1.size(), iSet1.size());
assertEquals(iDist9.size(), iSet9.size());
List<Integer> iSetList1 = SetToSortedList.toNullsLastList(iSet1, Integer::compare);
List<Integer> iSetList9 = SetToSortedList.toNullsLastList(iSet9, Integer::compare);
// Show results
show("iDist1", iDist1);
show("iDist9", iDist9);
show("iSet1", iSet1);
show("iSet9", iSet9);
show("iSetList1", iSetList1);
show("iSetList9", iSetList9);
assertEquals(iDist1.size(), iSetList1.size());
assertEquals(iDist9.size(), iSetList9.size());
assertNotEquals(iDist1, iSetList1); // Since null is first in iDist1, but last in iSetList1
assertEquals(iDist9, iSetList9); // Since null is last in iDist9, and last in iSetList9
}
}