Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Ordering; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Comparator; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 24 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 25 | import static com.google.common.collect.ImmutableList.of; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.*; |
| 27 | |
| 28 | /** |
| 29 | * Heap data structure tests. |
| 30 | */ |
| 31 | public class HeapTest { |
| 32 | |
| 33 | private ArrayList<Integer> data = |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 34 | new ArrayList<>(of(6, 4, 5, 9, 8, 3, 2, 1, 7, 0)); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 35 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 36 | private static final Comparator<Integer> MIN = Ordering.natural().reverse(); |
| 37 | private static final Comparator<Integer> MAX = Ordering.natural(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 38 | |
| 39 | @Test |
| 40 | public void equality() { |
| 41 | new EqualsTester() |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 42 | .addEqualityGroup(new Heap<>(data, MIN), |
| 43 | new Heap<>(data, MIN)) |
| 44 | .addEqualityGroup(new Heap<>(data, MAX)) |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 45 | .testEquals(); |
| 46 | } |
| 47 | |
| 48 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 49 | public void empty() { |
| 50 | Heap<Integer> h = new Heap<>(new ArrayList<Integer>(), MIN); |
| 51 | assertTrue("should be empty", h.isEmpty()); |
| 52 | assertEquals("incorrect size", 0, h.size()); |
| 53 | assertNull("no item expected", h.extreme()); |
| 54 | assertNull("no item expected", h.extractExtreme()); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void insert() { |
| 59 | Heap<Integer> h = new Heap<>(data, MIN); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 60 | assertEquals("incorrect size", 10, h.size()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 61 | h.insert(3); |
| 62 | assertEquals("incorrect size", 11, h.size()); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void minQueue() { |
| 67 | Heap<Integer> h = new Heap<>(data, MIN); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 68 | assertFalse("should not be empty", h.isEmpty()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 69 | assertEquals("incorrect size", 10, h.size()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 70 | assertEquals("incorrect extreme", (Integer) 0, h.extreme()); |
| 71 | |
| 72 | for (int i = 0, n = h.size(); i < n; i++) { |
| 73 | assertEquals("incorrect element", (Integer) i, h.extractExtreme()); |
| 74 | } |
| 75 | assertTrue("should be empty", h.isEmpty()); |
| 76 | } |
| 77 | |
| 78 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 79 | public void maxQueue() { |
| 80 | Heap<Integer> h = new Heap<>(data, MAX); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 81 | assertFalse("should not be empty", h.isEmpty()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 82 | assertEquals("incorrect size", 10, h.size()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 83 | assertEquals("incorrect extreme", (Integer) 9, h.extreme()); |
| 84 | |
| 85 | for (int i = h.size(); i > 0; i--) { |
| 86 | assertEquals("incorrect element", (Integer) (i - 1), h.extractExtreme()); |
| 87 | } |
| 88 | assertTrue("should be empty", h.isEmpty()); |
| 89 | } |
| 90 | |
| 91 | @Test |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 92 | public void iterator() { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 93 | Heap<Integer> h = new Heap<>(data, MIN); |
| 94 | assertTrue("should have next element", h.iterator().hasNext()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } |