tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 1 | package org.onlab.graph; |
| 2 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 3 | import com.google.common.collect.Ordering; |
| 4 | import com.google.common.testing.EqualsTester; |
| 5 | import org.junit.Test; |
| 6 | |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.Comparator; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 9 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 10 | import static com.google.common.collect.ImmutableList.of; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 11 | import static org.junit.Assert.*; |
| 12 | |
| 13 | /** |
| 14 | * Heap data structure tests. |
| 15 | */ |
| 16 | public class HeapTest { |
| 17 | |
| 18 | private ArrayList<Integer> data = |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 19 | new ArrayList<>(of(6, 4, 5, 9, 8, 3, 2, 1, 7, 0)); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 20 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 21 | private static final Comparator<Integer> MIN = Ordering.natural().reverse(); |
| 22 | private static final Comparator<Integer> MAX = Ordering.natural(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 23 | |
| 24 | @Test |
| 25 | public void equality() { |
| 26 | new EqualsTester() |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 27 | .addEqualityGroup(new Heap<>(data, MIN), |
| 28 | new Heap<>(data, MIN)) |
| 29 | .addEqualityGroup(new Heap<>(data, MAX)) |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 30 | .testEquals(); |
| 31 | } |
| 32 | |
| 33 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 34 | public void empty() { |
| 35 | Heap<Integer> h = new Heap<>(new ArrayList<Integer>(), MIN); |
| 36 | assertTrue("should be empty", h.isEmpty()); |
| 37 | assertEquals("incorrect size", 0, h.size()); |
| 38 | assertNull("no item expected", h.extreme()); |
| 39 | assertNull("no item expected", h.extractExtreme()); |
| 40 | } |
| 41 | |
| 42 | @Test |
| 43 | public void insert() { |
| 44 | Heap<Integer> h = new Heap<>(data, MIN); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 45 | assertEquals("incorrect size", 10, h.size()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 46 | h.insert(3); |
| 47 | assertEquals("incorrect size", 11, h.size()); |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void minQueue() { |
| 52 | Heap<Integer> h = new Heap<>(data, MIN); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 53 | assertFalse("should not be empty", h.isEmpty()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 54 | assertEquals("incorrect size", 10, h.size()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 55 | assertEquals("incorrect extreme", (Integer) 0, h.extreme()); |
| 56 | |
| 57 | for (int i = 0, n = h.size(); i < n; i++) { |
| 58 | assertEquals("incorrect element", (Integer) i, h.extractExtreme()); |
| 59 | } |
| 60 | assertTrue("should be empty", h.isEmpty()); |
| 61 | } |
| 62 | |
| 63 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 64 | public void maxQueue() { |
| 65 | Heap<Integer> h = new Heap<>(data, MAX); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 66 | assertFalse("should not be empty", h.isEmpty()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 67 | assertEquals("incorrect size", 10, h.size()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 68 | assertEquals("incorrect extreme", (Integer) 9, h.extreme()); |
| 69 | |
| 70 | for (int i = h.size(); i > 0; i--) { |
| 71 | assertEquals("incorrect element", (Integer) (i - 1), h.extractExtreme()); |
| 72 | } |
| 73 | assertTrue("should be empty", h.isEmpty()); |
| 74 | } |
| 75 | |
| 76 | @Test |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 77 | public void iterator() { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 78 | Heap<Integer> h = new Heap<>(data, MIN); |
| 79 | assertTrue("should have next element", h.iterator().hasNext()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | } |