Tanoda
MinHeapTest.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using NUnit.Framework;
11
12namespace Leap.Unity.Tests {
13
14 public class MinHeapTest {
15
16 private class HeapElement : IMinHeapNode, IComparable<HeapElement> {
17 public int heapIndex { get; set; }
18 public float value;
19
20 public HeapElement(float value) {
21 this.value = value;
22 }
23
24 public int CompareTo(HeapElement other) {
25 return value.CompareTo(other.value);
26 }
27 }
28
29 private MinHeap<HeapElement> _heap;
30
31 [SetUp]
32 public void Setup() {
33 _heap = new MinHeap<HeapElement>();
34 }
35
36 [TearDown]
37 public void Teardown() {
38 _heap.Clear();
39 _heap = null;
40 }
41
42 [Test]
43 public void HeapTest() {
44 _heap.Insert(new HeapElement(0));
45 _heap.Insert(new HeapElement(2));
46 _heap.Insert(new HeapElement(1));
47 _heap.Insert(new HeapElement(-5));
48 _heap.Insert(new HeapElement(10));
49
50 Assert.That(_heap.Validate(), Is.EqualTo(true));
51 Assert.That(_heap.PeekMin().value, Is.EqualTo(-5));
52
53 _heap.RemoveMin();
54
55 Assert.That(_heap.Validate(), Is.EqualTo(true));
56 Assert.That(_heap.PeekMin().value, Is.EqualTo(0));
57
58 var element4 = new HeapElement(4);
59 _heap.Insert(element4);
60
61 Assert.That(_heap.Validate(), Is.EqualTo(true));
62
63 _heap.Remove(element4);
64
65 Assert.That(_heap.Validate(), Is.EqualTo(true));
66 Assert.That(_heap.Count, Is.EqualTo(4));
67 }
68 }
69}
void Insert(T element)
Definition: MinHeap.cs:35
void Remove(T element)
Definition: MinHeap.cs:53