1 package com.github.davidmoten.rtree.internal.util; 2 3 import static com.github.davidmoten.rtree.internal.util.BoundedPriorityQueue.create; 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertTrue; 6 7 import java.util.Comparator; 8 9 import org.junit.Test; 10 11 import com.github.davidmoten.guavamini.Sets; 12 13 public class BoundedPriorityQueueTest { 14 15 private static final Comparator<Integer> comparator = new Comparator<Integer>() { 16 17 @Override 18 public int compare(Integer o1, Integer o2) { 19 return o1.compareTo(o2); 20 } 21 }; 22 23 @Test 24 public void emptyQueueAsListIsEmpty() { 25 BoundedPriorityQueue<Integer> q = create(2, comparator); 26 assertTrue(q.asList().isEmpty()); 27 } 28 29 @Test 30 public void singleItemReturnsSingleItem() { 31 BoundedPriorityQueue<Integer> q = create(2, comparator); 32 q.add(1); 33 assertEquals(Sets.newHashSet(1), Sets.newHashSet(q.asList())); 34 } 35 36 @Test 37 public void twoItemsReturnsSingleItemWhenMaxIsOne() { 38 BoundedPriorityQueue<Integer> q = create(1, comparator); 39 q.add(1); 40 q.add(2); 41 assertEquals(Sets.newHashSet(1), Sets.newHashSet(q.asList())); 42 } 43 44 @Test 45 public void twoItemsReturnsSingleItemWhenMaxIsOneInputOrderFlipped() { 46 BoundedPriorityQueue<Integer> q = create(1, comparator); 47 q.add(2); 48 q.add(1); 49 assertEquals(Sets.newHashSet(1), Sets.newHashSet(q.asList())); 50 } 51 52 @Test 53 public void threeItemsReturnsTwoItemsWhenMaxIsOneInputOrderFlipped() { 54 BoundedPriorityQueue<Integer> q = create(2, comparator); 55 q.add(3); 56 q.add(2); 57 q.add(1); 58 assertEquals(Sets.newHashSet(1, 2), Sets.newHashSet(q.asList())); 59 } 60 61 @Test 62 public void threeItemsReturnsTwoItemsWhenMaxIsOneInputOrderIncreasing() { 63 BoundedPriorityQueue<Integer> q = create(2, comparator); 64 q.add(1); 65 q.add(2); 66 q.add(3); 67 assertEquals(Sets.newHashSet(1, 2), Sets.newHashSet(q.asList())); 68 } 69 70 @Test 71 public void threeItemsReturnsTwoItemsWhenMaxIsOneInputOrderMixed() { 72 BoundedPriorityQueue<Integer> q = create(2, comparator); 73 q.add(3); 74 q.add(1); 75 q.add(2); 76 assertEquals(Sets.newHashSet(1, 2), Sets.newHashSet(q.asList())); 77 } 78 79 @Test 80 public void threeItemsReturnsTwoItemsWhenMaxIsOneInputOrderMixed2() { 81 BoundedPriorityQueue<Integer> q = create(2, comparator); 82 q.add(1); 83 q.add(3); 84 q.add(2); 85 assertEquals(Sets.newHashSet(1, 2), Sets.newHashSet(q.asList())); 86 } 87 88 @Test 89 public void threeItemsReturnsThreeItemsWhenMaxIsOneInputOrderFlipped() { 90 BoundedPriorityQueue<Integer> q = create(10, comparator); 91 q.add(3); 92 q.add(2); 93 q.add(1); 94 assertEquals(Sets.newHashSet(1, 2, 3), Sets.newHashSet(q.asList())); 95 } 96 97 @Test 98 public void threeItemsReturnsThreeItemsWhenMaxIsOneInputOrderIncreasing() { 99 BoundedPriorityQueue<Integer> q = create(10, comparator); 100 q.add(1); 101 q.add(2); 102 q.add(3); 103 assertEquals(Sets.newHashSet(1, 2, 3), Sets.newHashSet(q.asList())); 104 } 105 106 @Test 107 public void threeItemsReturnsThreeItemsWhenMaxIsOneInputOrderMixed() { 108 BoundedPriorityQueue<Integer> q = create(10, comparator); 109 q.add(3); 110 q.add(1); 111 q.add(2); 112 assertEquals(Sets.newHashSet(1, 2, 3), Sets.newHashSet(q.asList())); 113 } 114 115 @Test 116 public void threeItemsReturnsThreeItemsWhenMaxIsOneInputOrderMixed2() { 117 BoundedPriorityQueue<Integer> q = create(10, comparator); 118 q.add(1); 119 q.add(3); 120 q.add(2); 121 assertEquals(Sets.newHashSet(1, 2, 3), Sets.newHashSet(q.asList())); 122 } 123 124 @Test(expected=IllegalArgumentException.class) 125 public void testInstantiateWithNegativeSizeThrowsIAE() { 126 create(-1, comparator); 127 } 128 129 @Test(expected=IllegalArgumentException.class) 130 public void testInstantiateWithZeroSizeThrowsIAE() { 131 create(0, comparator); 132 } 133 134 @Test(expected=NullPointerException.class) 135 public void testAddNullThrowsNPE() { 136 BoundedPriorityQueue<Integer> q = create(10, comparator); 137 q.add(null); 138 } 139 140 }