blob: 678222d8a2144596dd0d53858e743b0052ff10eb [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
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 */
tome3489412014-08-29 02:30:38 -070019package org.onlab.graph;
20
tome3489412014-08-29 02:30:38 -070021import com.google.common.collect.Ordering;
22import com.google.common.testing.EqualsTester;
23import org.junit.Test;
24
25import java.util.ArrayList;
26import java.util.Comparator;
tome3489412014-08-29 02:30:38 -070027
tom144de692014-08-29 11:38:44 -070028import static com.google.common.collect.ImmutableList.of;
tome3489412014-08-29 02:30:38 -070029import static org.junit.Assert.*;
30
31/**
32 * Heap data structure tests.
33 */
34public class HeapTest {
35
36 private ArrayList<Integer> data =
tom144de692014-08-29 11:38:44 -070037 new ArrayList<>(of(6, 4, 5, 9, 8, 3, 2, 1, 7, 0));
tome3489412014-08-29 02:30:38 -070038
tom144de692014-08-29 11:38:44 -070039 private static final Comparator<Integer> MIN = Ordering.natural().reverse();
40 private static final Comparator<Integer> MAX = Ordering.natural();
tome3489412014-08-29 02:30:38 -070041
42 @Test
43 public void equality() {
44 new EqualsTester()
tom144de692014-08-29 11:38:44 -070045 .addEqualityGroup(new Heap<>(data, MIN),
46 new Heap<>(data, MIN))
47 .addEqualityGroup(new Heap<>(data, MAX))
tome3489412014-08-29 02:30:38 -070048 .testEquals();
49 }
50
51 @Test
tom144de692014-08-29 11:38:44 -070052 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);
tome3489412014-08-29 02:30:38 -070063 assertEquals("incorrect size", 10, h.size());
tom144de692014-08-29 11:38:44 -070064 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);
tome3489412014-08-29 02:30:38 -070071 assertFalse("should not be empty", h.isEmpty());
tom144de692014-08-29 11:38:44 -070072 assertEquals("incorrect size", 10, h.size());
tome3489412014-08-29 02:30:38 -070073 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
tom144de692014-08-29 11:38:44 -070082 public void maxQueue() {
83 Heap<Integer> h = new Heap<>(data, MAX);
tome3489412014-08-29 02:30:38 -070084 assertFalse("should not be empty", h.isEmpty());
tom144de692014-08-29 11:38:44 -070085 assertEquals("incorrect size", 10, h.size());
tome3489412014-08-29 02:30:38 -070086 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
tome3489412014-08-29 02:30:38 -070095 public void iterator() {
tom144de692014-08-29 11:38:44 -070096 Heap<Integer> h = new Heap<>(data, MIN);
97 assertTrue("should have next element", h.iterator().hasNext());
tome3489412014-08-29 02:30:38 -070098 }
99
100}