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